re PR classpath/30718 (TransformerException in XSLURIResolver)

2007-02-07  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.

From-SVN: r121694
This commit is contained in:
Chris Burdess 2007-02-07 18:22:26 +00:00 committed by Tom Tromey
parent 74372bdfc6
commit 08452f4553
24 changed files with 155 additions and 29 deletions

View File

@ -1,3 +1,18 @@
2007-02-07 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.
2007-02-06 Tom Tromey <tromey@redhat.com>
PR libgcj/30707:

View File

@ -43,6 +43,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
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 class DomDocumentBuilderFactory
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);

View File

@ -111,11 +111,16 @@ public class SAXEventSink
interrupted = true;
}
protected Document getDocument()
public Document getDocument()
{
return doc;
}
public void setReader(XMLReader reader)
{
this.reader = reader;
}
// -- ContentHandler2 --
public void setDocumentLocator(Locator locator)

View File

@ -42,7 +42,6 @@ import java.io.InputStreamReader;
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 @@ class XIncludeFilter
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;

View File

@ -1592,7 +1592,6 @@ public class XMLParser
* @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 @@ public class XMLParser
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;
}
/**

View File

@ -55,9 +55,13 @@ import javax.xml.transform.URIResolver;
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 @@ class XSLURIResolver
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 @@ class XSLURIResolver
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);
}
}

View File

@ -57,9 +57,9 @@ public:
SAXEventSink();
public: // actually package-private
virtual void interrupt();
public: // actually protected
virtual ::org::w3c::dom::Document * getDocument();
public:
virtual ::org::w3c::dom::Document * getDocument();
virtual void setReader(::org::xml::sax::XMLReader *);
virtual void setDocumentLocator(::org::xml::sax::Locator *);
virtual void startDocument();
virtual void endDocument();

View File

@ -46,6 +46,17 @@ extern "Java"
}
}
}
namespace org
{
namespace xml
{
namespace sax
{
class InputSource;
class XMLReader;
}
}
}
}
class gnu::xml::transform::XSLURIResolver : public ::java::lang::Object
@ -62,6 +73,7 @@ public: // actually package-private
virtual ::javax::xml::transform::dom::DOMSource * resolveDOM(::javax::xml::transform::Source *, ::java::lang::String *, ::java::lang::String *);
virtual ::java::net::URL * resolveURL(::java::lang::String *, ::java::lang::String *, ::java::lang::String *);
virtual ::javax::xml::parsers::DocumentBuilder * getDocumentBuilder();
virtual ::javax::xml::transform::dom::DOMSource * parse(::org::xml::sax::InputSource *, ::org::xml::sax::XMLReader *);
::java::util::Map * __attribute__((aligned(__alignof__( ::java::lang::Object)))) lastModifiedCache;
::java::util::Map * nodeCache;
::javax::xml::parsers::DocumentBuilder * builder;