From: Nick Burch Date: Tue, 15 Apr 2014 21:59:52 +0000 (+0000) Subject: Rather than having lots of classes all create their own XML DocumentBuilderFactory... X-Git-Tag: REL_3_11_BETA1~191 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3c3e164a50695490826dc161faf3005b9927a4a3;p=poi.git Rather than having lots of classes all create their own XML DocumentBuilderFactory instance, push that logic to a helper which sets all the right defaults git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1587739 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/dev/RecordGenerator.java b/src/java/org/apache/poi/dev/RecordGenerator.java index 56198b6b5c..9aa074de25 100644 --- a/src/java/org/apache/poi/dev/RecordGenerator.java +++ b/src/java/org/apache/poi/dev/RecordGenerator.java @@ -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 index 0000000000..f2da607762 --- /dev/null +++ b/src/java/org/apache/poi/util/XMLHelper.java @@ -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); + } + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java index 910741f511..edd0665b51 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java @@ -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{ 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{ } return complexTypeNode; } -} \ No newline at end of file +} diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java index e87a0333b3..945f1677ca 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java @@ -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(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java index 7726fc37fb..3e279426c3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java @@ -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(); diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java index 3533757b65..ac2f7316ba 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java @@ -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(); diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java index ee421d4810..2eefe6703b 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java @@ -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(); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java index f2bdcc97a4..6da6d141a0 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -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(); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java index d5314c9e7b..520d22004e 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java @@ -16,12 +16,13 @@ ==================================================================== */ 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. *

@@ -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(); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java index f77dc0286c..3549e363bf 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java @@ -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(); } diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java b/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java index 2eaee22e1f..895267fd3c 100644 --- a/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java +++ b/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java @@ -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(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java index 52473c824a..77a75a1937 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java @@ -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(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java index 4bfff59815..4b04bdffb2 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java @@ -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(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java index 845c7e9c97..2776895336 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java @@ -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 );