You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2006/09/28 21:19:37 UTC

svn commit: r450975 - in /xerces/java/branches/stax-dev/src/org/apache: xerces/impl/XMLEntityManager.java xml/serialize/DOMSerializerImpl.java

Author: mrglavas
Date: Thu Sep 28 12:19:36 2006
New Revision: 450975

URL: http://svn.apache.org/viewvc?view=rev&rev=450975
Log:
Moving code which creates an OutputStream from a URI to XMLEntityManager.

Modified:
    xerces/java/branches/stax-dev/src/org/apache/xerces/impl/XMLEntityManager.java
    xerces/java/branches/stax-dev/src/org/apache/xml/serialize/DOMSerializerImpl.java

Modified: xerces/java/branches/stax-dev/src/org/apache/xerces/impl/XMLEntityManager.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/impl/XMLEntityManager.java?view=diff&rev=450975&r1=450974&r2=450975
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xerces/impl/XMLEntityManager.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xerces/impl/XMLEntityManager.java Thu Sep 28 12:19:36 2006
@@ -16,9 +16,11 @@
 
 package org.apache.xerces.impl;
 
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
 import java.io.Reader;
 import java.io.StringReader;
 import java.lang.reflect.Method;
@@ -30,6 +32,7 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Stack;
+import java.util.StringTokenizer;
 
 import org.apache.xerces.impl.io.ASCIIReader;
 import org.apache.xerces.impl.io.Latin1Reader;
@@ -1833,6 +1836,54 @@
         }
         // setInstanceFollowRedirects doesn't exist.
         catch (Exception exc) {}
+    }
+    
+    public static OutputStream createOutputStream(String uri) throws IOException {
+        // URI was specified. Handle relative URIs.
+        String expanded = XMLEntityManager.expandSystemId(uri, null, true);
+        URL url = new URL(expanded != null ? expanded : uri);
+        OutputStream out = null;
+        String protocol = url.getProtocol();
+        String host = url.getHost();
+        // Use FileOutputStream if this URI is for a local file.
+        if (protocol.equals("file") 
+                && (host == null || host.length() == 0 || host.equals("localhost"))) {
+            out = new FileOutputStream(getPathWithoutEscapes(url.getFile()));
+        }
+        // Try to write to some other kind of URI. Some protocols
+        // won't support this, though HTTP should work.
+        else {
+            URLConnection urlCon = url.openConnection();
+            urlCon.setDoInput(false);
+            urlCon.setDoOutput(true);
+            urlCon.setUseCaches(false); // Enable tunneling.
+            if (urlCon instanceof HttpURLConnection) {
+                // The DOM L3 REC says if we are writing to an HTTP URI
+                // it is to be done with an HTTP PUT.
+                HttpURLConnection httpCon = (HttpURLConnection) urlCon;
+                httpCon.setRequestMethod("PUT");
+            }
+            out = urlCon.getOutputStream();
+        }
+        return out;
+    }
+    
+    private static String getPathWithoutEscapes(String origPath) {
+        if (origPath != null && origPath.length() != 0 && origPath.indexOf('%') != -1) {
+            // Locate the escape characters
+            StringTokenizer tokenizer = new StringTokenizer(origPath, "%");
+            StringBuffer result = new StringBuffer(origPath.length());
+            int size = tokenizer.countTokens();
+            result.append(tokenizer.nextToken());
+            for(int i = 1; i < size; ++i) {
+                String token = tokenizer.nextToken();
+                // Decode the 2 digit hexadecimal number following % in '%nn'
+                result.append((char)Integer.valueOf(token.substring(0, 2), 16).intValue());
+                result.append(token.substring(2));
+            }
+            return result.toString();
+        }
+        return origPath;
     }
 
     //

Modified: xerces/java/branches/stax-dev/src/org/apache/xml/serialize/DOMSerializerImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xml/serialize/DOMSerializerImpl.java?view=diff&rev=450975&r1=450974&r2=450975
==============================================================================
--- xerces/java/branches/stax-dev/src/org/apache/xml/serialize/DOMSerializerImpl.java (original)
+++ xerces/java/branches/stax-dev/src/org/apache/xml/serialize/DOMSerializerImpl.java Thu Sep 28 12:19:36 2006
@@ -16,17 +16,12 @@
 
 package org.apache.xml.serialize;
 
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.lang.reflect.Method;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.StringTokenizer;
 import java.util.Vector;
 
 import org.apache.xerces.dom.CoreDocumentImpl;
@@ -35,10 +30,6 @@
 import org.apache.xerces.dom.DOMMessageFormatter;
 import org.apache.xerces.dom.DOMNormalizer;
 import org.apache.xerces.dom.DOMStringListImpl;
-import org.w3c.dom.DOMConfiguration;
-import org.w3c.dom.DOMError;
-import org.w3c.dom.DOMErrorHandler;
-import org.w3c.dom.DOMStringList;
 import org.apache.xerces.impl.Constants;
 import org.apache.xerces.impl.XMLEntityManager;
 import org.apache.xerces.util.DOMUtil;
@@ -48,7 +39,11 @@
 import org.apache.xerces.util.XMLChar;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Comment;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMError;
+import org.w3c.dom.DOMErrorHandler;
 import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMStringList;
 import org.w3c.dom.Document;
 import org.w3c.dom.DocumentFragment;
 import org.w3c.dom.Element;
@@ -60,7 +55,6 @@
 import org.w3c.dom.ls.LSSerializer;
 import org.w3c.dom.ls.LSSerializerFilter;
 
-
 /**
  * EXPERIMENTAL: Implemenatation of DOM Level 3 org.w3c.ls.LSSerializer  by delegating serialization
  * calls to <CODE>XMLSerializer</CODE>.
@@ -713,33 +707,7 @@
                         throw new LSException(LSException.SERIALIZE_ERR, msg);
                     }
                     else {
-                        // URI was specified. Handle relative URIs.
-                        String expanded = XMLEntityManager.expandSystemId(uri, null, true);
-                        URL url = new URL(expanded != null ? expanded : uri);
-                        OutputStream out = null;
-                        String protocol = url.getProtocol();
-                        String host = url.getHost();
-                        // Use FileOutputStream if this URI is for a local file.
-                        if (protocol.equals("file") 
-                            && (host == null || host.length() == 0 || host.equals("localhost"))) {
-                            out = new FileOutputStream(getPathWithoutEscapes(url.getFile()));
-                        }
-                        // Try to write to some other kind of URI. Some protocols
-                        // won't support this, though HTTP should work.
-                        else {
-                            URLConnection urlCon = url.openConnection();
-                            urlCon.setDoInput(false);
-                            urlCon.setDoOutput(true);
-                            urlCon.setUseCaches(false); // Enable tunneling.
-                            if (urlCon instanceof HttpURLConnection) {
-                                // The DOM L3 LS CR says if we are writing to an HTTP URI
-                                // it is to be done with an HTTP PUT. 
-                                HttpURLConnection httpCon = (HttpURLConnection) urlCon;
-                                httpCon.setRequestMethod("PUT");
-                            }
-                            out = urlCon.getOutputStream();
-                        }
-                        ser.setOutputByteStream(out);
+                        ser.setOutputByteStream(XMLEntityManager.createOutputStream(uri));
                     }
                 }
                 else {
@@ -862,34 +830,7 @@
         try {
             prepareForSerialization(ser, node);
             ser._format.setEncoding(encoding);
-            
-            // URI was specified. Handle relative URIs.
-            String expanded = XMLEntityManager.expandSystemId(URI, null, true);
-            URL url = new URL(expanded != null ? expanded : URI);
-            OutputStream out = null;
-            String protocol = url.getProtocol();
-            String host = url.getHost();
-            // Use FileOutputStream if this URI is for a local file.
-            if (protocol.equals("file") 
-                && (host == null || host.length() == 0 || host.equals("localhost"))) {
-                out = new FileOutputStream(getPathWithoutEscapes(url.getFile()));            
-            }
-            // Try to write to some other kind of URI. Some protocols
-            // won't support this, though HTTP should work.
-            else {
-                URLConnection urlCon = url.openConnection();
-                urlCon.setDoInput(false);
-                urlCon.setDoOutput(true);
-                urlCon.setUseCaches(false); // Enable tunneling.
-                if (urlCon instanceof HttpURLConnection) {
-                    // The DOM L3 LS CR says if we are writing to an HTTP URI
-                    // it is to be done with an HTTP PUT. 
-                    HttpURLConnection httpCon = (HttpURLConnection) urlCon;
-                    httpCon.setRequestMethod("PUT");
-                }
-                out = urlCon.getOutputStream();
-            }
-            ser.setOutputByteStream(out);
+            ser.setOutputByteStream(XMLEntityManager.createOutputStream(URI));
 
             if (node.getNodeType() == Node.DOCUMENT_NODE)
                 ser.serialize((Document) node);
@@ -1104,24 +1045,6 @@
         }        
         }
                
-    }
-    
-    private String getPathWithoutEscapes(String origPath) {
-        if (origPath != null && origPath.length() != 0 && origPath.indexOf('%') != -1) {
-            // Locate the escape characters
-            StringTokenizer tokenizer = new StringTokenizer(origPath, "%");
-            StringBuffer result = new StringBuffer(origPath.length());
-            int size = tokenizer.countTokens();
-            result.append(tokenizer.nextToken());
-            for(int i = 1; i < size; ++i) {
-                String token = tokenizer.nextToken();
-                // Decode the 2 digit hexadecimal number following % in '%nn'
-                result.append((char)Integer.valueOf(token.substring(0, 2), 16).intValue());
-                result.append(token.substring(2));
-            }
-            return result.toString();
-        }
-        return origPath;
     }
     
     private String _getXmlVersion(Node node) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org