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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.StringReader;
  18. import java.lang.reflect.Method;
  19. import javax.xml.XMLConstants;
  20. import javax.xml.parsers.ParserConfigurationException;
  21. import javax.xml.parsers.SAXParserFactory;
  22. import org.xml.sax.EntityResolver;
  23. import org.xml.sax.InputSource;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.XMLReader;
  26. /**
  27. * Provides handy methods for working with SAX parsers and readers
  28. */
  29. public final class SAXHelper {
  30. private static POILogger logger = POILogFactory.getLogger(SAXHelper.class);
  31. private SAXHelper() {}
  32. /**
  33. * Creates a new SAX XMLReader, with sensible defaults
  34. */
  35. public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
  36. XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
  37. xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER);
  38. trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING, true);
  39. trySetXercesSecurityManager(xmlReader);
  40. return xmlReader;
  41. }
  42. static final EntityResolver IGNORING_ENTITY_RESOLVER = new EntityResolver() {
  43. @Override
  44. public InputSource resolveEntity(String publicId, String systemId)
  45. throws SAXException, IOException {
  46. return new InputSource(new StringReader(""));
  47. }
  48. };
  49. private static final SAXParserFactory saxFactory;
  50. static {
  51. saxFactory = SAXParserFactory.newInstance();
  52. saxFactory.setValidating(false);
  53. saxFactory.setNamespaceAware(true);
  54. }
  55. private static void trySetSAXFeature(XMLReader xmlReader, String feature, boolean enabled) {
  56. try {
  57. xmlReader.setFeature(feature, enabled);
  58. } catch (Exception e) {
  59. logger.log(POILogger.WARN, "SAX Feature unsupported", feature, e);
  60. } catch (AbstractMethodError ame) {
  61. logger.log(POILogger.WARN, "Cannot set SAX feature because outdated XML parser in classpath", feature, ame);
  62. }
  63. }
  64. private static void trySetXercesSecurityManager(XMLReader xmlReader) {
  65. // Try built-in JVM one first, standalone if not
  66. for (String securityManagerClassName : new String[] {
  67. "com.sun.org.apache.xerces.internal.util.SecurityManager",
  68. "org.apache.xerces.util.SecurityManager"
  69. }) {
  70. try {
  71. Object mgr = Class.forName(securityManagerClassName).newInstance();
  72. Method setLimit = mgr.getClass().getMethod("setEntityExpansionLimit", Integer.TYPE);
  73. setLimit.invoke(mgr, 4096);
  74. xmlReader.setProperty("http://apache.org/xml/properties/security-manager", mgr);
  75. // Stop once one can be setup without error
  76. return;
  77. } catch (Exception e) {
  78. logger.log(POILogger.WARN, "SAX Security Manager could not be setup", e);
  79. }
  80. }
  81. }
  82. }