]> source.dussan.org Git - poi.git/commitdiff
Add Dominik's suggestion to DocumentBuilderFactories. I also removed the setXIncludeA...
authorUwe Schindler <uschindler@apache.org>
Mon, 18 Aug 2014 16:01:35 +0000 (16:01 +0000)
committerUwe Schindler <uschindler@apache.org>
Mon, 18 Aug 2014 16:01:35 +0000 (16:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1618644 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/util/XMLHelper.java
src/ooxml/java/org/apache/poi/util/DocumentHelper.java
src/ooxml/java/org/apache/poi/util/SAXHelper.java

index f2da6077620c4925a6fd9e1d3d999821adfe9e41..3e97cee0b31be0b9402681b1a9a375f7fc2d5621 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.poi.util;
 
 import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 
 /**
  * Helper methods for working with javax.xml classes.
@@ -27,22 +26,31 @@ import javax.xml.parsers.ParserConfigurationException;
  */
 public final class XMLHelper
 {
+    private static POILogger logger = POILogFactory.getLogger(XMLHelper.class);
+    
     /**
      * Creates a new DocumentBuilderFactory, with sensible defaults
      */
     public static DocumentBuilderFactory getDocumentBuilderFactory() {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setExpandEntityReferences(false);
+        trySetSAXFeature(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        trySetSAXFeature(factory, "http://xml.org/sax/features/external-general-entities", false);
+        trySetSAXFeature(factory, "http://xml.org/sax/features/external-parameter-entities", false);
+        trySetSAXFeature(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+        trySetSAXFeature(factory, "http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
+        return factory;
+    }
+    
+    private static void trySetSAXFeature(DocumentBuilderFactory documentBuilderFactory, String feature, boolean enabled) {
         try {
-            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            factory.setXIncludeAware(false);
-            factory.setExpandEntityReferences(false);
-            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
-            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
-            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
-            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
-            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
-            return factory;
-        } catch (ParserConfigurationException e) {
-            throw new RuntimeException("Broken XML Setup", e);
+            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);
         }
     }
+    
+
 }
index 9ff6e7fb02ff63eb8826e38c892f561505cb0c8d..0c18b5d0091a347c2e67dd8db340beb1d634e959 100644 (file)
@@ -61,9 +61,12 @@ public final class DocumentHelper {
         try {
             documentBuilderFactory.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) {
         // Try built-in JVM one first, standalone if not
         for (String securityManagerClassName : new String[] {
@@ -78,7 +81,7 @@ public final class DocumentHelper {
                 // 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);
             }
         }
     }
index bbc58e51300d718f567d013710c3f99044889f9f..d4d016cb3b05b01b80f788f7f7989dccd37e1032 100644 (file)
@@ -69,7 +69,9 @@ public final class SAXHelper {
         try {
             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);
         }
     }
     
@@ -87,7 +89,7 @@ public final class SAXHelper {
                 // 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);
             }
         }
     }