mirror of https://github.com/apache/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
This commit is contained in:
parent
9cc498fa5c
commit
3c3e164a50
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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.
|
||||
* <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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 );
|
||||
|
||||
|
|
Loading…
Reference in New Issue