You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentHelper.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ooxml.util;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.StringWriter;
  19. import java.lang.reflect.Method;
  20. import java.util.concurrent.TimeUnit;
  21. import javax.xml.XMLConstants;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. import javax.xml.stream.events.Namespace;
  26. import javax.xml.transform.OutputKeys;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerFactory;
  29. import javax.xml.transform.dom.DOMSource;
  30. import javax.xml.transform.stream.StreamResult;
  31. import org.apache.poi.util.POILogFactory;
  32. import org.apache.poi.util.POILogger;
  33. import org.w3c.dom.*;
  34. import org.xml.sax.ErrorHandler;
  35. import org.xml.sax.InputSource;
  36. import org.xml.sax.SAXException;
  37. import org.xml.sax.SAXParseException;
  38. public final class DocumentHelper {
  39. private static POILogger logger = POILogFactory.getLogger(DocumentHelper.class);
  40. private static long lastLog;
  41. private DocumentHelper() {}
  42. private static class DocHelperErrorHandler implements ErrorHandler {
  43. public void warning(SAXParseException exception) {
  44. printError(POILogger.WARN, exception);
  45. }
  46. public void error(SAXParseException exception) {
  47. printError(POILogger.ERROR, exception);
  48. }
  49. public void fatalError(SAXParseException exception) throws SAXException {
  50. printError(POILogger.FATAL, exception);
  51. throw exception;
  52. }
  53. /** Prints the error message. */
  54. private void printError(int type, SAXParseException ex) {
  55. StringBuilder sb = new StringBuilder();
  56. String systemId = ex.getSystemId();
  57. if (systemId != null) {
  58. int index = systemId.lastIndexOf('/');
  59. if (index != -1)
  60. systemId = systemId.substring(index + 1);
  61. sb.append(systemId);
  62. }
  63. sb.append(':');
  64. sb.append(ex.getLineNumber());
  65. sb.append(':');
  66. sb.append(ex.getColumnNumber());
  67. sb.append(": ");
  68. sb.append(ex.getMessage());
  69. logger.log(type, sb.toString(), ex);
  70. }
  71. }
  72. /**
  73. * Creates a new document builder, with sensible defaults
  74. *
  75. * @throws IllegalStateException If creating the DocumentBuilder fails, e.g.
  76. * due to {@link ParserConfigurationException}.
  77. */
  78. public static DocumentBuilder newDocumentBuilder() {
  79. try {
  80. DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  81. documentBuilder.setEntityResolver(SAXHelper.IGNORING_ENTITY_RESOLVER);
  82. documentBuilder.setErrorHandler(new DocHelperErrorHandler());
  83. return documentBuilder;
  84. } catch (ParserConfigurationException e) {
  85. throw new IllegalStateException("cannot create a DocumentBuilder", e);
  86. }
  87. }
  88. public static String domToString(Node node) throws Exception {
  89. TransformerFactory tf = TransformerFactory.newInstance();
  90. Transformer t = tf.newTransformer();
  91. t.setOutputProperty(OutputKeys.INDENT, "yes");
  92. StringWriter sw = new StringWriter();
  93. t.transform(new DOMSource(node), new StreamResult(sw));
  94. return sw.toString();
  95. }
  96. public static Attr findIdAttr(Element e, String name) throws Exception {
  97. Attr att = e.getAttributeNode("Id");
  98. if(att != null && name.equals(att.getValue())) {
  99. return att;
  100. }
  101. NodeList nl = e.getChildNodes();
  102. for (int i = 0; i < nl.getLength(); i++) {
  103. Node child = nl.item(i);
  104. if (child instanceof Element) {
  105. Attr x = findIdAttr((Element)child, name);
  106. if (x != null) {
  107. return x;
  108. }
  109. }
  110. }
  111. return null;
  112. }
  113. static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  114. static {
  115. documentBuilderFactory.setNamespaceAware(true);
  116. documentBuilderFactory.setValidating(false);
  117. //this doesn't appear to work, and we still need to limit
  118. //entity expansions to 1 in trySetXercesSecurityManager
  119. documentBuilderFactory.setExpandEntityReferences(false);
  120. trySetFeature(documentBuilderFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
  121. trySetFeature(documentBuilderFactory, POIXMLConstants.FEATURE_DISALLOW_DOCTYPE_DECL, true);
  122. trySetFeature(documentBuilderFactory, POIXMLConstants.FEATURE_LOAD_DTD_GRAMMAR, false);
  123. trySetFeature(documentBuilderFactory, POIXMLConstants.FEATURE_LOAD_EXTERNAL_DTD, false);
  124. trySetXercesSecurityManager(documentBuilderFactory);
  125. }
  126. private static void trySetFeature(@SuppressWarnings("SameParameterValue") DocumentBuilderFactory dbf, String feature, boolean enabled) {
  127. try {
  128. dbf.setFeature(feature, enabled);
  129. } catch (Exception e) {
  130. logger.log(POILogger.WARN, "DocumentBuilderFactory Feature unsupported", feature, e);
  131. } catch (AbstractMethodError ame) {
  132. logger.log(POILogger.WARN, "Cannot set DocumentBuilderFactory feature because outdated XML parser in classpath", feature, ame);
  133. }
  134. }
  135. private static void trySetXercesSecurityManager(@SuppressWarnings("SameParameterValue") DocumentBuilderFactory dbf) {
  136. // Try built-in JVM one first, standalone if not
  137. for (String securityManagerClassName : new String[]{
  138. //"com.sun.org.apache.xerces.internal.util.SecurityManager",
  139. "org.apache.xerces.util.SecurityManager"
  140. }) {
  141. try {
  142. Object mgr = Class.forName(securityManagerClassName).newInstance();
  143. Method setLimit = mgr.getClass().getMethod("setEntityExpansionLimit", Integer.TYPE);
  144. setLimit.invoke(mgr, 1);
  145. dbf.setAttribute(POIXMLConstants.PROPERTY_SECURITY_MANAGER, mgr);
  146. // Stop once one can be setup without error
  147. return;
  148. } catch (ClassNotFoundException e) {
  149. // continue without log, this is expected in some setups
  150. } catch (Throwable e) { // NOSONAR - also catch things like NoClassDefError here
  151. if(System.currentTimeMillis() > lastLog + TimeUnit.MINUTES.toMillis(5)) {
  152. logger.log(POILogger.WARN, "DocumentBuilderFactory Security Manager could not be setup [log suppressed for 5 minutes]", e);
  153. lastLog = System.currentTimeMillis();
  154. }
  155. }
  156. }
  157. // separate old version of Xerces not found => use the builtin way of setting the property
  158. // Note: when entity_expansion_limit==0, there is no limit!
  159. try {
  160. dbf.setAttribute(POIXMLConstants.PROPERTY_ENTITY_EXPANSION_LIMIT, 1);
  161. } catch (Throwable e) {
  162. if(System.currentTimeMillis() > lastLog + TimeUnit.MINUTES.toMillis(5)) {
  163. logger.log(POILogger.WARN, "DocumentBuilderFactory Entity Expansion Limit could not be setup [log suppressed for 5 minutes]", e);
  164. lastLog = System.currentTimeMillis();
  165. }
  166. }
  167. }
  168. /**
  169. * Parses the given stream via the default (sensible)
  170. * DocumentBuilder
  171. * @param inp Stream to read the XML data from
  172. * @return the parsed Document
  173. */
  174. public static Document readDocument(InputStream inp) throws IOException, SAXException {
  175. return newDocumentBuilder().parse(inp);
  176. }
  177. /**
  178. * Parses the given stream via the default (sensible)
  179. * DocumentBuilder
  180. * @param inp sax source to read the XML data from
  181. * @return the parsed Document
  182. */
  183. public static Document readDocument(InputSource inp) throws IOException, SAXException {
  184. return newDocumentBuilder().parse(inp);
  185. }
  186. // must only be used to create empty documents, do not use it for parsing!
  187. private static final DocumentBuilder documentBuilderSingleton = newDocumentBuilder();
  188. /**
  189. * Creates a new DOM Document
  190. */
  191. public static Document createDocument() {
  192. return documentBuilderSingleton.newDocument();
  193. }
  194. /**
  195. * Adds a namespace declaration attribute to the given element.
  196. */
  197. public static void addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI) {
  198. element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
  199. XMLConstants.XMLNS_ATTRIBUTE + ':' + namespacePrefix,
  200. namespaceURI);
  201. }
  202. /**
  203. * Adds a namespace declaration attribute to the given element.
  204. */
  205. public static void addNamespaceDeclaration(Element element, Namespace namespace) {
  206. addNamespaceDeclaration(element, namespace.getPrefix(), namespace.getNamespaceURI());
  207. }
  208. }