Skip to content

Commit

Permalink
Fix bug #56164 - Tidy up the OPC SAX setup code with a new common Helper
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1569991 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Gagravarr committed Feb 19, 2014
1 parent 8afdb7a commit d72bd78
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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
Expand Down Expand Up @@ -309,10 +309,8 @@ public int size() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand All @@ -33,13 +33,13 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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).
Expand Down Expand Up @@ -373,8 +373,7 @@ public void clearOverrideContentTypes() {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand All @@ -38,6 +31,13 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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.
Expand Down Expand Up @@ -118,10 +118,9 @@ public PackagePart unmarshall(UnmarshallContext context, InputStream in)
"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 */

Expand Down
59 changes: 59 additions & 0 deletions src/ooxml/java/org/apache/poi/util/SAXHelper.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 16 additions & 8 deletions src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand All @@ -31,15 +40,15 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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);
Expand Down Expand Up @@ -211,9 +220,8 @@ public void testCreatePackageWithCoreDocument() throws Exception {
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
Expand Down

0 comments on commit d72bd78

Please sign in to comment.