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;
)
) {
// 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();
--- /dev/null
+/* ====================================================================
+ 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);
+ }
+ }
+}
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;
private Document getEmptyDocument() throws ParserConfigurationException{
- DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
+ DocumentBuilderFactory dbfac = XMLHelper.getDocumentBuilderFactory();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
}
return complexTypeNode;
}
-}
\ No newline at end of file
+}
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;
*/
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();
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;
}
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();
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;
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;
{
final HSSFWorkbook workbook = ExcelToFoUtils.loadXls( xlsFile );
ExcelToFoConverter excelToHtmlConverter = new ExcelToFoConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() );
excelToHtmlConverter.processWorkbook( workbook );
return excelToHtmlConverter.getDocument();
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;
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;
{
final HSSFWorkbook workbook = ExcelToHtmlUtils.loadXls( xlsFile );
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() );
excelToHtmlConverter.processWorkbook( workbook );
return excelToHtmlConverter.getDocument();
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;
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;
{
final HWPFDocumentCore hwpfDocument = WordToFoUtils.loadDoc( docFile );
WordToFoConverter wordToFoConverter = new WordToFoConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() );
wordToFoConverter.processDocument( hwpfDocument );
return wordToFoConverter.getDocument();
==================================================================== */
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;
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>
{
final HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc( docFile );
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() );
wordToHtmlConverter.processDocument( wordDocument );
return wordToHtmlConverter.getDocument();
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;
throws Exception
{
WordToTextConverter wordToTextConverter = new WordToTextConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
wordToTextConverter.processDocument( wordDocument );
return wordToTextConverter.getText();
}
final HWPFDocumentCore wordDocument = AbstractWordUtils
.loadDoc( docFile );
WordToTextConverter wordToTextConverter = new WordToTextConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
wordToTextConverter.processDocument( wordDocument );
return wordToTextConverter.getDocument();
}
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;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.util.XMLHelper;
public class TestExcelConverterSuite
{
}
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
excelToHtmlConverter.processWorkbook( workbook );
StringWriter stringWriter = new StringWriter();
}
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
excelToHtmlConverter.processWorkbook( workbook );
StringWriter stringWriter = new 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;
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
{
}
WordToFoConverter wordToFoConverter = new WordToFoConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
wordToFoConverter.processDocument( hwpfDocument );
StringWriter stringWriter = new StringWriter();
}
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
wordToHtmlConverter.processDocument( hwpfDocument );
StringWriter stringWriter = new StringWriter();
}
WordToTextConverter wordToTextConverter = new WordToTextConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
wordToTextConverter.processDocument( wordDocument );
StringWriter stringWriter = new StringWriter();
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 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}
.getDocumentInstance().openResourceAsStream( sampleFileName ) );
WordToFoConverter wordToFoConverter = new WordToFoConverter(
- DocumentBuilderFactory.newInstance().newDocumentBuilder()
- .newDocument() );
+ XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() );
wordToFoConverter.processDocument( hwpfDocument );
StringWriter stringWriter = new StringWriter();
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;
/**
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 );