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.

SAXHelper.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.util;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.StringReader;
  19. import java.lang.reflect.Method;
  20. import javax.xml.XMLConstants;
  21. import org.dom4j.Document;
  22. import org.dom4j.DocumentException;
  23. import org.dom4j.io.SAXReader;
  24. import org.xml.sax.EntityResolver;
  25. import org.xml.sax.InputSource;
  26. import org.xml.sax.SAXException;
  27. /**
  28. * Provides handy methods for working with SAX parsers and readers
  29. */
  30. public final class SAXHelper {
  31. private static POILogger logger = POILogFactory.getLogger(SAXHelper.class);
  32. /**
  33. * Creates a new SAX Reader, with sensible defaults
  34. */
  35. public static SAXReader getSAXReader() {
  36. SAXReader xmlReader = new SAXReader();
  37. xmlReader.setValidation(false);
  38. xmlReader.setEntityResolver(new EntityResolver() {
  39. public InputSource resolveEntity(String publicId, String systemId)
  40. throws SAXException, IOException {
  41. return new InputSource(new StringReader(""));
  42. }
  43. });
  44. trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING, true);
  45. trySetXercesSecurityManager(xmlReader);
  46. return xmlReader;
  47. }
  48. private static void trySetSAXFeature(SAXReader xmlReader, String feature, boolean enabled) {
  49. try {
  50. xmlReader.setFeature(feature, enabled);
  51. } catch (Exception e) {
  52. logger.log(POILogger.INFO, "SAX Feature unsupported", feature, e);
  53. }
  54. }
  55. private static void trySetXercesSecurityManager(SAXReader xmlReader) {
  56. // Try built-in JVM one first, standalone if not
  57. for (String securityManagerClassName : new String[] {
  58. "com.sun.org.apache.xerces.internal.util.SecurityManager",
  59. "org.apache.xerces.util.SecurityManager"
  60. }) {
  61. try {
  62. Object mgr = Class.forName(securityManagerClassName).newInstance();
  63. Method setLimit = mgr.getClass().getMethod("setEntityExpansionLimit", Integer.TYPE);
  64. setLimit.invoke(mgr, 4096);
  65. xmlReader.setProperty("http://apache.org/xml/properties/security-manager", mgr);
  66. // Stop once one can be setup without error
  67. return;
  68. } catch (Exception e) {
  69. logger.log(POILogger.INFO, "SAX Security Manager could not be setup", e);
  70. }
  71. }
  72. }
  73. /**
  74. * Parses the given stream via the default (sensible)
  75. * SAX Reader
  76. * @param inp Stream to read the XML data from
  77. * @return the SAX processed Document
  78. */
  79. public static Document readSAXDocument(InputStream inp) throws DocumentException {
  80. return getSAXReader().read(inp);
  81. }
  82. }