]> source.dussan.org Git - poi.git/commitdiff
Rather than having lots of classes all create their own XML DocumentBuilderFactory...
authorNick Burch <nick@apache.org>
Tue, 15 Apr 2014 21:59:52 +0000 (21:59 +0000)
committerNick Burch <nick@apache.org>
Tue, 15 Apr 2014 21:59:52 +0000 (21:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1587739 13f79535-47bb-0310-9956-ffa450edef68

14 files changed:
src/java/org/apache/poi/dev/RecordGenerator.java
src/java/org/apache/poi/util/XMLHelper.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java
src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java
src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java
src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java
src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java
src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java

index 56198b6b5c661541dcc2e8fbbe5aad2eaa9ff14c..9aa074de25f4a38b0297bd681c9c6ac59d6c7ff9 100644 (file)
@@ -34,6 +34,7 @@ import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -76,8 +77,7 @@ public class RecordGenerator {
                     )
                     ) {
                 // Get record name and package
-                DocumentBuilderFactory factory =
-                        DocumentBuilderFactory.newInstance();
+                DocumentBuilderFactory factory = XMLHelper.getDocumentBuilderFactory();
                 DocumentBuilder builder = factory.newDocumentBuilder();
                 Document document = builder.parse(file);
                 Element record = document.getDocumentElement();
diff --git a/src/java/org/apache/poi/util/XMLHelper.java b/src/java/org/apache/poi/util/XMLHelper.java
new file mode 100644 (file)
index 0000000..f2da607
--- /dev/null
@@ -0,0 +1,48 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+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.
+ * @see SAXHelper
+ */
+public final class XMLHelper
+{
+    /**
+     * Creates a new DocumentBuilderFactory, with sensible defaults
+     */
+    public static DocumentBuilderFactory getDocumentBuilderFactory() {
+        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);
+        }
+    }
+}
index 910741f5117a85780c8090ddfac017de81519141..edd0665b51da6f53461c00e2dd358c8be85b43b3 100644 (file)
@@ -45,6 +45,7 @@ import javax.xml.validation.Validator;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.util.XMLHelper;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFMap;
 import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -107,7 +108,7 @@ public class XSSFExportToXml implements Comparator<String>{
 
     private Document getEmptyDocument() throws ParserConfigurationException{
 
-        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
+        DocumentBuilderFactory dbfac = XMLHelper.getDocumentBuilderFactory();
         DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
         Document doc = docBuilder.newDocument();
 
@@ -532,4 +533,4 @@ public class XSSFExportToXml implements Comparator<String>{
         }
         return complexTypeNode;
     }
-}
\ No newline at end of file
+}
index e87a0333b372b2438a55f6d12aecd227e43afe99..945f1677ca8df8542f4c9f0e1653ffe668635283 100644 (file)
@@ -33,10 +33,11 @@ import javax.xml.xpath.XPathFactory;
 
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
-import org.apache.poi.xssf.usermodel.XSSFTable;
+import org.apache.poi.util.XMLHelper;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFMap;
 import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFTable;
 import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
 import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
 import org.w3c.dom.Document;
@@ -77,7 +78,7 @@ public class XSSFImportFromXML {
      */
     public void importFromXML(String xmlInputString) throws SAXException, XPathExpressionException, ParserConfigurationException, IOException {
 
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilderFactory factory = XMLHelper.getDocumentBuilderFactory();
         factory.setNamespaceAware(true);
         DocumentBuilder builder = factory.newDocumentBuilder();
 
index 7726fc37fbfe0546794894c3c15ac1676b51d062..3e279426c30e339ab7a75ba8a30928caba99ab4b 100644 (file)
@@ -34,6 +34,7 @@ import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.util.XMLHelper;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.MapInfo;
 import org.apache.poi.xssf.usermodel.XSSFCell;
@@ -472,7 +473,7 @@ public final class TestXSSFExportToXML extends TestCase {
    }
    
    private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException {
-       DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+       DocumentBuilderFactory docBuilderFactory = XMLHelper.getDocumentBuilderFactory();
        docBuilderFactory.setNamespaceAware(true);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
index 3533757b658f29c27fc8d19e40f2f93ed896c210..ac2f7316ba880a240820a7259dcb8c92a336c92f 100644 (file)
@@ -21,7 +21,6 @@ import java.io.FileWriter;
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -45,6 +44,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Text;
@@ -122,7 +122,7 @@ public class ExcelToFoConverter extends AbstractExcelConverter
     {
         final HSSFWorkbook workbook = ExcelToFoUtils.loadXls( xlsFile );
         ExcelToFoConverter excelToHtmlConverter = new ExcelToFoConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
                         .newDocument() );
         excelToHtmlConverter.processWorkbook( workbook );
         return excelToHtmlConverter.getDocument();
index ee421d481019a62b0f3823d513efd9476fe134f9..2eefe6703bbfc53b54ef956dc930e72b8eb4be21 100644 (file)
@@ -23,7 +23,6 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -45,6 +44,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Text;
@@ -115,7 +115,7 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
     {
         final HSSFWorkbook workbook = ExcelToHtmlUtils.loadXls( xlsFile );
         ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
                         .newDocument() );
         excelToHtmlConverter.processWorkbook( workbook );
         return excelToHtmlConverter.getDocument();
index f2bdcc97a4e2468894c330cdc98f908d70b79d2c..6da6d141a062859dabaa5bbd0444d3ff25ed3a23 100644 (file)
@@ -24,7 +24,6 @@ import java.util.List;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -48,6 +47,7 @@ import org.apache.poi.hwpf.usermodel.TableRow;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -109,7 +109,7 @@ public class WordToFoConverter extends AbstractWordConverter
     {
         final HWPFDocumentCore hwpfDocument = WordToFoUtils.loadDoc( docFile );
         WordToFoConverter wordToFoConverter = new WordToFoConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
                         .newDocument() );
         wordToFoConverter.processDocument( hwpfDocument );
         return wordToFoConverter.getDocument();
index d5314c9e7be9bdaa3450b9da4f255a7d937c0a7e..520d22004e3eb22ee317c8d0264e0a11f3fec745 100644 (file)
 ==================================================================== */
 package org.apache.poi.hwpf.converter;
 
+import static org.apache.poi.hwpf.converter.AbstractWordUtils.TWIPS_PER_INCH;
+
 import java.io.File;
 import java.io.FileWriter;
 import java.util.List;
 import java.util.Stack;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -45,12 +46,11 @@ import org.apache.poi.hwpf.usermodel.TableRow;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Text;
 
-import static org.apache.poi.hwpf.converter.AbstractWordUtils.TWIPS_PER_INCH;
-
 /**
  * Converts Word files (95-2007) into HTML files.
  * <p>
@@ -157,7 +157,7 @@ public class WordToHtmlConverter extends AbstractWordConverter
     {
         final HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc( docFile );
         WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
                         .newDocument() );
         wordToHtmlConverter.processDocument( wordDocument );
         return wordToHtmlConverter.getDocument();
index f77dc0286cd869b825d67fcae47ec6c194345e6b..3549e363bffe2b22ce0cf0315ac5e2dd0d896e4f 100644 (file)
@@ -50,6 +50,7 @@ import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -76,8 +77,7 @@ public class WordToTextConverter extends AbstractWordConverter
             throws Exception
     {
         WordToTextConverter wordToTextConverter = new WordToTextConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         wordToTextConverter.processDocument( wordDocument );
         return wordToTextConverter.getText();
     }
@@ -130,8 +130,7 @@ public class WordToTextConverter extends AbstractWordConverter
         final HWPFDocumentCore wordDocument = AbstractWordUtils
                 .loadDoc( docFile );
         WordToTextConverter wordToTextConverter = new WordToTextConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         wordToTextConverter.processDocument( wordDocument );
         return wordToTextConverter.getDocument();
     }
index 2eaee22e1f4a7efacf500374e418f9c96d18dfc2..895267fd3c6c04c4db5a1ccb2b9d031db5f1b653 100644 (file)
@@ -22,7 +22,6 @@ import java.io.StringWriter;
 import java.util.Arrays;
 import java.util.List;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -35,6 +34,7 @@ import junit.framework.TestSuite;
 
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.util.XMLHelper;
 
 public class TestExcelConverterSuite
 {
@@ -96,8 +96,7 @@ public class TestExcelConverterSuite
         }
 
         ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         excelToHtmlConverter.processWorkbook( workbook );
 
         StringWriter stringWriter = new StringWriter();
@@ -126,8 +125,7 @@ public class TestExcelConverterSuite
         }
 
         ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         excelToHtmlConverter.processWorkbook( workbook );
 
         StringWriter stringWriter = new StringWriter();
index 52473c824a00128e2de5bea06e98133317aef5f4..77a75a19373063c22bbe8662fc8d2dfc6a185b23 100644 (file)
@@ -22,7 +22,6 @@ import java.io.StringWriter;
 import java.util.Arrays;
 import java.util.List;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -32,8 +31,10 @@ import javax.xml.transform.stream.StreamResult;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
+
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hwpf.HWPFDocumentCore;
+import org.apache.poi.util.XMLHelper;
 
 public class TestWordToConverterSuite
 {
@@ -100,8 +101,7 @@ public class TestWordToConverterSuite
         }
 
         WordToFoConverter wordToFoConverter = new WordToFoConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         wordToFoConverter.processDocument( hwpfDocument );
 
         StringWriter stringWriter = new StringWriter();
@@ -130,8 +130,7 @@ public class TestWordToConverterSuite
         }
 
         WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         wordToHtmlConverter.processDocument( hwpfDocument );
 
         StringWriter stringWriter = new StringWriter();
@@ -161,8 +160,7 @@ public class TestWordToConverterSuite
         }
 
         WordToTextConverter wordToTextConverter = new WordToTextConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         wordToTextConverter.processDocument( wordDocument );
 
         StringWriter stringWriter = new StringWriter();
index 4bfff5981522a7e8fb07c7d54c95f867226a5aba..4b04bdffb28e6b1892e5e482b4867967534947fe 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.poi.hwpf.converter;
 
 import java.io.StringWriter;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -30,6 +29,7 @@ import javax.xml.transform.stream.StreamResult;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.POITestCase;
 import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.util.XMLHelper;
 
 /**
  * Test cases for {@link WordToFoConverter}
@@ -43,8 +43,7 @@ public class TestWordToFoConverter extends POITestCase
                 .getDocumentInstance().openResourceAsStream( sampleFileName ) );
 
         WordToFoConverter wordToFoConverter = new WordToFoConverter(
-                DocumentBuilderFactory.newInstance().newDocumentBuilder()
-                        .newDocument() );
+                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
         wordToFoConverter.processDocument( hwpfDocument );
 
         StringWriter stringWriter = new StringWriter();
index 845c7e9c97d6b3b6f34ca1d1fb36ee9b83a89d6c..27768953360b4a7e1a96b153635a2ef579c15e84 100644 (file)
@@ -18,19 +18,17 @@ package org.apache.poi.hwpf.converter;
 
 import java.io.StringWriter;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
-import junit.framework.TestCase;
-
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.POITestCase;
 import org.apache.poi.hwpf.HWPFDocument;
 import org.apache.poi.hwpf.usermodel.PictureType;
+import org.apache.poi.util.XMLHelper;
 import org.w3c.dom.Document;
 
 /**
@@ -50,8 +48,7 @@ public class TestWordToHtmlConverter extends POITestCase
         HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples
                 .getDocumentInstance().openResourceAsStream( sampleFileName ) );
 
-        Document newDocument = DocumentBuilderFactory.newInstance()
-                .newDocumentBuilder().newDocument();
+        Document newDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument();
         WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                 newDocument );