aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/util
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2014-09-17 21:12:36 +0000
committerAndreas Beeker <kiwiwings@apache.org>2014-09-17 21:12:36 +0000
commit56051c3e44486c2b54cc53cf2c91a1f94555f14f (patch)
tree6b9254561bd856791084d32a6432cfd00fdd1cc6 /src/ooxml/java/org/apache/poi/util
parentd1bc3ab1fd1ac2f7a410fd4ecc372970e5150556 (diff)
downloadpoi-56051c3e44486c2b54cc53cf2c91a1f94555f14f.tar.gz
poi-56051c3e44486c2b54cc53cf2c91a1f94555f14f.zip
merge with trunk
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/xml_signature@1625828 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi/util')
-rw-r--r--src/ooxml/java/org/apache/poi/util/DocumentHelper.java75
-rw-r--r--src/ooxml/java/org/apache/poi/util/SAXHelper.java72
2 files changed, 101 insertions, 46 deletions
diff --git a/src/ooxml/java/org/apache/poi/util/DocumentHelper.java b/src/ooxml/java/org/apache/poi/util/DocumentHelper.java
index 22bdd4e0fd..0c18b5d009 100644
--- a/src/ooxml/java/org/apache/poi/util/DocumentHelper.java
+++ b/src/ooxml/java/org/apache/poi/util/DocumentHelper.java
@@ -17,6 +17,10 @@
package org.apache.poi.util;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
+
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -25,20 +29,81 @@ import javax.xml.stream.events.Namespace;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
-public class DocumentHelper {
+public final class DocumentHelper {
+ private static POILogger logger = POILogFactory.getLogger(DocumentHelper.class);
+
+ private DocumentHelper() {}
- private static final DocumentBuilder newDocumentBuilder;
- static {
+ /**
+ * Creates a new document builder, with sensible defaults
+ */
+ public static synchronized DocumentBuilder newDocumentBuilder() {
try {
- newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
+ documentBuilder.setEntityResolver(SAXHelper.IGNORING_ENTITY_RESOLVER);
+ return documentBuilder;
} catch (ParserConfigurationException e) {
throw new IllegalStateException("cannot create a DocumentBuilder", e);
}
}
+ private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+ static {
+ documentBuilderFactory.setNamespaceAware(true);
+ documentBuilderFactory.setValidating(false);
+ trySetSAXFeature(documentBuilderFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ trySetXercesSecurityManager(documentBuilderFactory);
+ }
+
+ private static void trySetSAXFeature(DocumentBuilderFactory documentBuilderFactory, String feature, boolean enabled) {
+ try {
+ documentBuilderFactory.setFeature(feature, enabled);
+ } catch (Exception e) {
+ logger.log(POILogger.WARN, "SAX Feature unsupported", feature, e);
+ } catch (AbstractMethodError ame) {
+ logger.log(POILogger.WARN, "Cannot set SAX feature because outdated XML parser in classpath", feature, ame);
+ }
+ }
+
+ private static void trySetXercesSecurityManager(DocumentBuilderFactory documentBuilderFactory) {
+ // Try built-in JVM one first, standalone if not
+ for (String securityManagerClassName : new String[] {
+ "com.sun.org.apache.xerces.internal.util.SecurityManager",
+ "org.apache.xerces.util.SecurityManager"
+ }) {
+ try {
+ Object mgr = Class.forName(securityManagerClassName).newInstance();
+ Method setLimit = mgr.getClass().getMethod("setEntityExpansionLimit", Integer.TYPE);
+ setLimit.invoke(mgr, 4096);
+ documentBuilderFactory.setAttribute("http://apache.org/xml/properties/security-manager", mgr);
+ // Stop once one can be setup without error
+ return;
+ } catch (Exception e) {
+ logger.log(POILogger.WARN, "SAX Security Manager could not be setup", e);
+ }
+ }
+ }
+
+ /**
+ * Parses the given stream via the default (sensible)
+ * DocumentBuilder
+ * @param inp Stream to read the XML data from
+ * @return the parsed Document
+ */
+ public static Document readDocument(InputStream inp) throws IOException, SAXException {
+ return newDocumentBuilder().parse(inp);
+ }
+
+ // must only be used to create empty documents, do not use it for parsing!
+ private static final DocumentBuilder documentBuilderSingleton = newDocumentBuilder();
+
+ /**
+ * Creates a new DOM Document
+ */
public static synchronized Document createDocument() {
- return newDocumentBuilder.newDocument();
+ return documentBuilderSingleton.newDocument();
}
/**
diff --git a/src/ooxml/java/org/apache/poi/util/SAXHelper.java b/src/ooxml/java/org/apache/poi/util/SAXHelper.java
index 81049a9a2e..d4d016cb3b 100644
--- a/src/ooxml/java/org/apache/poi/util/SAXHelper.java
+++ b/src/ooxml/java/org/apache/poi/util/SAXHelper.java
@@ -18,19 +18,17 @@
package org.apache.poi.util;
import java.io.IOException;
-import java.io.InputStream;
import java.io.StringReader;
import java.lang.reflect.Method;
import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
-import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
/**
@@ -39,43 +37,45 @@ import org.xml.sax.SAXException;
public final class SAXHelper {
private static POILogger logger = POILogFactory.getLogger(SAXHelper.class);
- private static final EntityResolver IGNORING_ENTITY_RESOLVER = new EntityResolver() {
+ private SAXHelper() {}
+
+ /**
+ * Creates a new SAX XMLReader, with sensible defaults
+ */
+ public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
+ XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
+ xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER);
+ trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ trySetXercesSecurityManager(xmlReader);
+ return xmlReader;
+ }
+
+ static final EntityResolver IGNORING_ENTITY_RESOLVER = new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
};
-
- private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+
+ private static final SAXParserFactory saxFactory;
static {
- documentBuilderFactory.setNamespaceAware(true);
- documentBuilderFactory.setValidating(false);
- trySetSAXFeature(documentBuilderFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
- trySetXercesSecurityManager(documentBuilderFactory);
- }
-
- /**
- * Creates a new document builder, with sensible defaults
- */
- public static synchronized DocumentBuilder getDocumentBuilder() {
- try {
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- documentBuilder.setEntityResolver(IGNORING_ENTITY_RESOLVER);
- return documentBuilder;
- } catch (ParserConfigurationException e) {
- throw new IllegalStateException("cannot create a DocumentBuilder", e);
- }
+ saxFactory = SAXParserFactory.newInstance();
+ saxFactory.setValidating(false);
+ saxFactory.setNamespaceAware(true);
}
-
- private static void trySetSAXFeature(DocumentBuilderFactory documentBuilderFactory, String feature, boolean enabled) {
+
+ private static void trySetSAXFeature(XMLReader xmlReader, String feature, boolean enabled) {
try {
- documentBuilderFactory.setFeature(feature, enabled);
+ xmlReader.setFeature(feature, enabled);
} catch (Exception e) {
- logger.log(POILogger.INFO, "SAX Feature unsupported", feature, e);
+ logger.log(POILogger.WARN, "SAX Feature unsupported", feature, e);
+ } catch (AbstractMethodError ame) {
+ logger.log(POILogger.WARN, "Cannot set SAX feature because outdated XML parser in classpath", feature, ame);
}
}
- private static void trySetXercesSecurityManager(DocumentBuilderFactory documentBuilderFactory) {
+
+ private static void trySetXercesSecurityManager(XMLReader xmlReader) {
// Try built-in JVM one first, standalone if not
for (String securityManagerClassName : new String[] {
"com.sun.org.apache.xerces.internal.util.SecurityManager",
@@ -85,22 +85,12 @@ public final class SAXHelper {
Object mgr = Class.forName(securityManagerClassName).newInstance();
Method setLimit = mgr.getClass().getMethod("setEntityExpansionLimit", Integer.TYPE);
setLimit.invoke(mgr, 4096);
- documentBuilderFactory.setAttribute("http://apache.org/xml/properties/security-manager", mgr);
+ xmlReader.setProperty("http://apache.org/xml/properties/security-manager", mgr);
// Stop once one can be setup without error
return;
} catch (Exception e) {
- logger.log(POILogger.INFO, "SAX Security Manager could not be setup", e);
+ logger.log(POILogger.WARN, "SAX Security Manager could not be setup", e);
}
}
}
-
- /**
- * Parses the given stream via the default (sensible)
- * SAX Reader
- * @param inp Stream to read the XML data from
- * @return the SAX processed Document
- */
- public static Document readSAXDocument(InputStream inp) throws IOException, SAXException {
- return getDocumentBuilder().parse(inp);
- }
}