From 59e16f9e61a832befb856aee1058a10375fac450 Mon Sep 17 00:00:00 2001 From: Tom Robinson Date: Mon, 22 Sep 2008 00:11:12 -0700 Subject: [PATCH] Prevent Rhino's XML parsing from downloading plist DTDs all the time --- Objective-J/plist.js | 2 +- Tools/Utilities/bridge.js | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Objective-J/plist.js b/Objective-J/plist.js index d03ede527..59f0f8515 100644 --- a/Objective-J/plist.js +++ b/Objective-J/plist.js @@ -340,7 +340,7 @@ function CPPropertyListCreateFromXMLData(XMLNodeOrData) if (XMLNode.string) { #if RHINO - XMLNode = DOCUMENT_ELEMENT(Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( + XMLNode = DOCUMENT_ELEMENT(_documentBuilder.parse( new Packages.org.xml.sax.InputSource(new Packages.java.io.StringReader(XMLNode.string)))); #else if (window.ActiveXObject) diff --git a/Tools/Utilities/bridge.js b/Tools/Utilities/bridge.js index bf55fb76f..96cc8e29c 100644 --- a/Tools/Utilities/bridge.js +++ b/Tools/Utilities/bridge.js @@ -283,9 +283,25 @@ if (typeof Packages != "undefined") { } }; - var xhr_builder = Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); - xhr_builder.setErrorHandler(function(exception, methodName) { - //print("xml error!"); + var _documentBuilderFactory = Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance(); + // setValidating to false doesn't seem to prevent it from downloading the DTD, but lets do it anyway + _documentBuilderFactory.setValidating(false); + + _documentBuilder = _documentBuilderFactory.newDocumentBuilder(); + // prevent the Java XML parser from downloading the plist DTD from Apple every time we parse a plist + _documentBuilder.setEntityResolver(new Packages.org.xml.sax.EntityResolver({ + resolveEntity: function(publicId, systemId) { + //Packages.java.lang.System.out.println("publicId=" + publicId + " systemId=" + systemId); + + // TODO: return a local copy of the DTD? + if (String(systemId) == "http://www.apple.com/DTDs/PropertyList-1.0.dtd") + return new Packages.org.xml.sax.InputSource(new Packages.java.io.StringReader("")); + + return null; + } + })); + // throw an exception on error + _documentBuilder.setErrorHandler(function(exception, methodName) { throw exception; }); @@ -372,7 +388,7 @@ if (typeof Packages != "undefined") { } parseXMLString = function(string) { - return (Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( + return (_documentBuilder.parse( new Packages.org.xml.sax.InputSource( new Packages.java.io.StringReader(string))).getDocumentElement()); } @@ -444,7 +460,7 @@ XMLHttpRequest.prototype.send = function(body) { if (this.responseText.length > 0) { try { - this.responseXML = xhr_builder.parse(new Packages.org.xml.sax.InputSource(new Packages.java.io.StringReader(this.responseText))); + this.responseXML = _documentBuilder.parse(new Packages.org.xml.sax.InputSource(new Packages.java.io.StringReader(this.responseText))); } catch (e) { this.responseXML = null; }