From d72bd78c19dfb7b57395a66ae8d9269d59a87bd2 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 19 Feb 2014 23:54:46 +0000 Subject: [PATCH] Fix bug #56164 - Tidy up the OPC SAX setup code with a new common Helper git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1569991 13f79535-47bb-0310-9956-ffa450edef68 --- .../opc/PackageRelationshipCollection.java | 6 +- .../opc/internal/ContentTypeManager.java | 7 +-- .../PackagePropertiesUnmarshaller.java | 17 +++--- .../java/org/apache/poi/util/SAXHelper.java | 59 +++++++++++++++++++ .../apache/poi/openxml4j/opc/TestPackage.java | 24 +++++--- 5 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/util/SAXHelper.java diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java index 7a3d79141..6adc73792 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java @@ -26,10 +26,10 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.util.SAXHelper; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; -import org.dom4j.io.SAXReader; /** * Represents a collection of PackageRelationship elements that are owned by a @@ -309,10 +309,8 @@ public final class PackageRelationshipCollection implements private void parseRelationshipsPart(PackagePart relPart) throws InvalidFormatException { try { - SAXReader reader = new SAXReader(); logger.log(POILogger.DEBUG, "Parsing relationship: " + relPart.getPartName()); - Document xmlRelationshipsDoc = reader - .read(relPart.getInputStream()); + Document xmlRelationshipsDoc = SAXHelper.readSAXDocument(relPart.getInputStream()); // Browse default types Element root = xmlRelationshipsDoc.getRootElement(); diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java index b8c30df93..2c3b97a7f 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java @@ -23,8 +23,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Iterator; import java.util.List; -import java.util.TreeMap; import java.util.Map.Entry; +import java.util.TreeMap; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException; @@ -33,13 +33,13 @@ import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagingURIHelper; +import org.apache.poi.util.SAXHelper; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; -import org.dom4j.io.SAXReader; /** * Manage package content types ([Content_Types].xml part). @@ -373,8 +373,7 @@ public abstract class ContentTypeManager { private void parseContentTypesFile(InputStream in) throws InvalidFormatException { try { - SAXReader xmlReader = new SAXReader(); - Document xmlContentTypetDoc = xmlReader.read(in); + Document xmlContentTypetDoc = SAXHelper.readSAXDocument(in); // Default content types List defaultTypes = xmlContentTypetDoc.getRootElement().elements( diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/unmarshallers/PackagePropertiesUnmarshaller.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/unmarshallers/PackagePropertiesUnmarshaller.java index 36719d347..6f1d62b8a 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/unmarshallers/PackagePropertiesUnmarshaller.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/unmarshallers/PackagePropertiesUnmarshaller.java @@ -23,13 +23,6 @@ import java.util.Iterator; import java.util.List; import java.util.zip.ZipEntry; -import org.dom4j.Attribute; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.Namespace; -import org.dom4j.QName; -import org.dom4j.io.SAXReader; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.PackageNamespaces; import org.apache.poi.openxml4j.opc.PackagePart; @@ -38,6 +31,13 @@ import org.apache.poi.openxml4j.opc.ZipPackage; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PartUnmarshaller; import org.apache.poi.openxml4j.opc.internal.ZipHelper; +import org.apache.poi.util.SAXHelper; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Namespace; +import org.dom4j.QName; /** * Package properties unmarshaller. @@ -118,10 +118,9 @@ public final class PackagePropertiesUnmarshaller implements PartUnmarshaller { "Error while trying to get the part input stream."); } - SAXReader xmlReader = new SAXReader(); Document xmlDoc; try { - xmlDoc = xmlReader.read(in); + xmlDoc = SAXHelper.readSAXDocument(in); /* Check OPC compliance */ diff --git a/src/ooxml/java/org/apache/poi/util/SAXHelper.java b/src/ooxml/java/org/apache/poi/util/SAXHelper.java new file mode 100644 index 000000000..6a43292a5 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/util/SAXHelper.java @@ -0,0 +1,59 @@ +/* ==================================================================== + 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 java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + + +/** + * Provides handy methods for working with SAX parsers and readers + */ +public final class SAXHelper { + /** + * Creates a new SAX Reader, with sensible defaults + */ + public static SAXReader getSAXReader() { + SAXReader xmlReader = new SAXReader(); + xmlReader.setEntityResolver(new EntityResolver() { + public InputSource resolveEntity(String publicId, String systemId) + throws SAXException, IOException { + return new InputSource(new StringReader("")); + } + }); + return xmlReader; + } + + /** + * Parses the given stream via the default (sensible) + * SAX Reader + * @param inp Stream to read the XML data from + * @return the SAX processed Document + */ + public static Document readSAXDocument(InputStream inp) throws DocumentException { + return getSAXReader().read(inp); + } +} diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index b4ac8bd7c..01116abcf 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -17,10 +17,19 @@ package org.apache.poi.openxml4j.opc; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.lang.reflect.Field; import java.net.URI; -import java.util.*; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.TreeMap; import java.util.regex.Pattern; import junit.framework.TestCase; @@ -31,15 +40,15 @@ import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; import org.apache.poi.openxml4j.opc.internal.FileHelper; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; -import org.apache.poi.util.TempFile; -import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; +import org.apache.poi.util.SAXHelper; +import org.apache.poi.util.TempFile; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; -import org.dom4j.io.SAXReader; public final class TestPackage extends TestCase { private static final POILogger logger = POILogFactory.getLogger(TestPackage.class); @@ -211,9 +220,8 @@ public final class TestPackage extends TestCase { private void assertMSCompatibility(OPCPackage pkg) throws Exception { PackagePartName relName = PackagingURIHelper.createPartName(PackageRelationship.getContainerPartRelationship()); PackagePart relPart = pkg.getPart(relName); - SAXReader reader = new SAXReader(); - Document xmlRelationshipsDoc = reader - .read(relPart.getInputStream()); + + Document xmlRelationshipsDoc = SAXHelper.readSAXDocument(relPart.getInputStream()); Element root = xmlRelationshipsDoc.getRootElement(); for (Iterator i = root