Patch: FYI: import 2 XML fixes

Tom Tromey tromey@redhat.com
Wed Feb 7 18:19:00 GMT 2007


I'm checking this in on the trunk and the RH 4.1 branch.

This pulls in a couple XML fixes from Chris Burdess.

One is needed in order to drop xerces into the endorsed directory.
The other fixes a bug seen with Eclipse Mylar.

Tom

Index: classpath/ChangeLog
from  Chris Burdess  <dog@gnu.org>

	Fixes PR 30718.
	* gnu/xml/dom/ls/SAXEventSink.java: Add public accessor/mutators.
	* gnu/xml/transform/XSLURIResolver.java: Add support for custom
	SAXSources without a backing URL or stream.

	Fixes PR 27710.
	* gnu/xml/dom/DomDocumentBuilderFactory.java: Fall back to synchronous
	LSParser if implementation does not support asynchronous.
	* gnu/xml/stream/XMLParser.java,
	gnu/xml/stream/XIncludeFilter.java: Use custom code instead of
	java.net.URL to resolve to an an absolute URI, to avoid nonexistent
	protocol handler problems.

Index: classpath/gnu/xml/dom/ls/SAXEventSink.java
===================================================================
--- classpath/gnu/xml/dom/ls/SAXEventSink.java	(revision 121691)
+++ classpath/gnu/xml/dom/ls/SAXEventSink.java	(working copy)
@@ -111,11 +111,16 @@
     interrupted = true;
   }
 
-  protected Document getDocument()
+  public Document getDocument()
   {
     return doc;
   }
 
+  public void setReader(XMLReader reader)
+  {
+    this.reader = reader;
+  }
+
   // -- ContentHandler2 --
   
   public void setDocumentLocator(Locator locator)
Index: classpath/gnu/xml/dom/DomDocumentBuilderFactory.java
===================================================================
--- classpath/gnu/xml/dom/DomDocumentBuilderFactory.java	(revision 121691)
+++ classpath/gnu/xml/dom/DomDocumentBuilderFactory.java	(working copy)
@@ -43,6 +43,7 @@
 import javax.xml.parsers.FactoryConfigurationError;
 import javax.xml.parsers.ParserConfigurationException;
 import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
 import org.w3c.dom.DOMImplementation;
 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
 import org.w3c.dom.ls.DOMImplementationLS;
@@ -84,8 +85,38 @@
   public DocumentBuilder newDocumentBuilder()
     throws ParserConfigurationException
   {
-    LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS,
-                                        "http://www.w3.org/TR/REC-xml");
+    LSParser parser = null;
+    try
+      {
+        parser = ls.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS,
+                                   "http://www.w3.org/TR/REC-xml");
+      }
+    catch (DOMException e)
+      {
+        if (e.code == DOMException.NOT_SUPPORTED_ERR)
+          {
+            // Fall back to synchronous parser
+            try
+              {
+                parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS,
+                                           "http://www.w3.org/TR/REC-xml");
+              }
+            catch (DOMException e2)
+              {
+                ParserConfigurationException pce =
+                    new ParserConfigurationException();
+                pce.initCause(e2);
+                throw pce;
+              }
+          }
+        else
+          {
+            ParserConfigurationException pce =
+                new ParserConfigurationException();
+            pce.initCause(e);
+            throw pce;
+          }
+      }
     DOMConfiguration config = parser.getDomConfig();
     setParameter(config, "namespaces",
                  isNamespaceAware() ? Boolean.TRUE : Boolean.FALSE);
Index: classpath/gnu/xml/stream/XMLParser.java
===================================================================
--- classpath/gnu/xml/stream/XMLParser.java	(revision 121691)
+++ classpath/gnu/xml/stream/XMLParser.java	(working copy)
@@ -1592,7 +1592,6 @@
    * @param href the (absolute or relative) URL to resolve
    */
   public static String absolutize(String base, String href)
-    throws MalformedURLException
   {
     if (href == null)
       return null;
@@ -1622,7 +1621,60 @@
         if (!base.endsWith("/"))
           base += "/";
       }
-    return new URL(new URL(base), href).toString();
+    // We can't use java.net.URL here to do the parsing, as it searches for
+    // a protocol handler. A protocol handler may not be registered for the
+    // URL scheme here. Do it manually.
+    // 
+    // Set aside scheme and host portion of base URL
+    String basePrefix = null;
+    ci = base.indexOf(':');
+    if (ci > 1 && isURLScheme(base.substring(0, ci)))
+      {
+          if (base.length() > (ci + 3)  &&
+              base.charAt(ci + 1) == '/' &&
+              base.charAt(ci + 2) == '/')
+            {
+              int si = base.indexOf('/', ci + 3);
+              if (si == -1)
+                base = null;
+              else
+                {
+                  basePrefix = base.substring(0, si);
+                  base = base.substring(si);
+                }
+            }
+          else
+            base = null;
+      }
+    if (base == null) // unknown or malformed base URL, use href
+      return href;
+    if (href.startsWith("/")) // absolute href pathname
+      return (basePrefix == null) ? href : basePrefix + href;
+    // relative href pathname
+    if (!base.endsWith("/"))
+      {
+        int lsi = base.lastIndexOf('/');
+        if (lsi == -1)
+          base = "/";
+        else
+          base = base.substring(0, lsi + 1);
+      }
+    while (href.startsWith("../") || href.startsWith("./"))
+      {
+        if (href.startsWith("../"))
+          {
+            // strip last path component from base
+            int lsi = base.lastIndexOf('/', base.length() - 2);
+            if (lsi > -1)
+              base = base.substring(0, lsi + 1);
+            href = href.substring(3); // strip ../ prefix
+          }
+        else
+          {
+            href = href.substring(2); // strip ./ prefix
+          }
+      }
+    return (basePrefix == null) ? base + href : basePrefix + base + href;
   }
 
   /**
Index: classpath/gnu/xml/stream/XIncludeFilter.java
===================================================================
--- classpath/gnu/xml/stream/XIncludeFilter.java	(revision 121691)
+++ classpath/gnu/xml/stream/XIncludeFilter.java	(working copy)
@@ -42,7 +42,6 @@
 import java.io.IOException;
 import java.io.Reader;
 import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.HashSet;
@@ -122,17 +121,7 @@
                  boolean expandERefs)
   {
     super(reader);
-    try
-      {
-        this.systemId = XMLParser.absolutize(null, systemId);
-      }
-    catch (MalformedURLException e)
-      {
-        RuntimeException e2 = new RuntimeException("unsupported URL: " +
-                                                   systemId);
-        e2.initCause(e);
-        throw e2;
-      }
+    this.systemId = XMLParser.absolutize(null, systemId);
     this.namespaceAware = namespaceAware;
     this.validating = validating;
     this.expandERefs = expandERefs;
Index: classpath/gnu/xml/transform/XSLURIResolver.java
===================================================================
--- classpath/gnu/xml/transform/XSLURIResolver.java	(revision 121691)
+++ classpath/gnu/xml/transform/XSLURIResolver.java	(working copy)
@@ -55,9 +55,13 @@
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamSource;
+import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.ls.SAXEventSink;
 import gnu.xml.dom.ls.ReaderInputStream;
 
 /**
@@ -137,17 +141,14 @@
         else if (source != null && source instanceof SAXSource)
           {
             SAXSource ss = (SAXSource) source;
-            if (ss.getInputSource() != null)
+            InputSource input = ss.getInputSource();
+            if (input != null)
               {
-                in =  ss.getInputSource().getByteStream();
-                if (in == null)
-                  {
-                    Reader reader = ss.getInputSource().getCharacterStream();
-                    if (reader != null)
-                      {
-                        in = new ReaderInputStream(reader);
-                      }
-                  }
+                if (systemId == null)
+                  systemId = input.getSystemId();
+                XMLReader reader = ss.getXMLReader();
+                if (reader != null)
+                  return parse(input, reader);
               }
           }
         if (in == null)
@@ -294,6 +295,27 @@
         throw new TransformerException(e);
       }
   }
+
+  DOMSource parse(InputSource source, XMLReader reader)
+    throws SAXException, IOException
+  {
+    SAXEventSink eventSink = new SAXEventSink();
+    eventSink.setReader(reader);
+    reader.setContentHandler(eventSink);
+    reader.setDTDHandler(eventSink);
+    reader.setProperty("http://xml.org/sax/properties/lexical-handler",
+            eventSink);
+    reader.setProperty("http://xml.org/sax/properties/declaration-handler",
+            eventSink);
+    // XXX entityResolver
+    // XXX errorHandler
+    reader.parse(source);
+    Document doc = eventSink.getDocument();
+    String systemId = source.getSystemId();
+    if (systemId != null && doc instanceof DomDocument)
+      ((DomDocument) doc).setDocumentURI(systemId);
+    return new DOMSource(doc, systemId);
+  }
   
 }
 



More information about the Java-patches mailing list