diff --git a/build.xml b/build.xml index 0465da74a..e7e5e5e2e 100644 --- a/build.xml +++ b/build.xml @@ -871,9 +871,16 @@ under the License. + + + + - org.apache.xmlbeans.XmlBeans.getContextTypeLoader() - org.apache.poi.ooxml.POIXMLTypeLoader + org.apache.xmlbeans.XmlBeans.getContextTypeLoader + getTypeLoader @@ -882,22 +889,6 @@ under the License. ]]> - - - - - - - - - - - - - - - - Forking javac with max heap size ${ooxml.memory} typeLoader = new ThreadLocal<>(); - // TODO: Do these have a good home like o.a.p.openxml4j.opc.PackageNamespaces and PackageRelationshipTypes? // These constants should be common to all of POI and easy to use by other applications such as Tika private static final String MS_OFFICE_URN = "urn:schemas-microsoft-com:office:office"; @@ -91,76 +67,4 @@ public class POIXMLTypeLoader { map.put(MS_VML_URN, "v"); DEFAULT_XML_OPTIONS.setSaveSuggestedPrefixes(Collections.unmodifiableMap(map)); } - - private static XmlOptions getXmlOptions(XmlOptions options) { - return options == null ? DEFAULT_XML_OPTIONS : options; - } - - private static SchemaTypeLoader getTypeLoader(SchemaType type) { - SchemaTypeLoader tl = typeLoader.get(); - if (tl == null) { - ClassLoader cl = type.getClass().getClassLoader(); - tl = XmlBeans.typeLoaderForClassLoader(cl); - typeLoader.set(tl); - } - return tl; - } - - public static XmlObject newInstance(SchemaType type, XmlOptions options) { - return getTypeLoader(type).newInstance(type, getXmlOptions(options)); - } - - public static XmlObject parse(String xmlText, SchemaType type, XmlOptions options) throws XmlException { - try { - return parse(new StringReader(xmlText), type, options); - } catch (IOException e) { - throw new XmlException("Unable to parse xml bean", e); - } - } - - public static XmlObject parse(File file, SchemaType type, XmlOptions options) throws XmlException, IOException { - try (InputStream is = new FileInputStream(file)) { - return parse(is, type, options); - } - } - - public static XmlObject parse(URL file, SchemaType type, XmlOptions options) throws XmlException, IOException { - try (InputStream is = file.openStream()) { - return parse(is, type, options); - } - } - - public static XmlObject parse(InputStream jiois, SchemaType type, XmlOptions options) throws XmlException, IOException { - try { - Document doc = DocumentHelper.readDocument(jiois); - return getTypeLoader(type).parse(doc.getDocumentElement(), type, getXmlOptions(options)); - } catch (SAXException e) { - throw new IOException("Unable to parse xml bean", e); - } - } - - public static XmlObject parse(XMLStreamReader xsr, SchemaType type, XmlOptions options) throws XmlException { - return getTypeLoader(type).parse(xsr, type, getXmlOptions(options)); - } - - public static XmlObject parse(Reader jior, SchemaType type, XmlOptions options) throws XmlException, IOException { - try { - Document doc = DocumentHelper.readDocument(new InputSource(jior)); - return getTypeLoader(type).parse(doc.getDocumentElement(), type, getXmlOptions(options)); - } catch (SAXException e) { - throw new XmlException("Unable to parse xml bean", e); - } - } - - public static XmlObject parse(Node node, SchemaType type, XmlOptions options) throws XmlException { - return getTypeLoader(type).parse(node, type, getXmlOptions(options)); - } - - public static XmlObject parse(XMLInputStream xis, SchemaType type, XmlOptions options) throws XmlException, XMLStreamException { - return getTypeLoader(type).parse(xis, type, getXmlOptions(options)); - } - - public static XMLInputStream newValidatingXMLInputStream ( XMLInputStream xis, SchemaType type, XmlOptions options ) throws XmlException, XMLStreamException { - return getTypeLoader(type).newValidatingXMLInputStream(xis, type, getXmlOptions(options)); - } } diff --git a/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java index d32180993..c718b2295 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java @@ -141,7 +141,10 @@ public class ExtractorFactory { } catch (OfficeXmlFileException e) { // ensure file-handle release IOUtils.closeQuietly(fs); - return (T)createExtractor(OPCPackage.open(f.toString(), PackageAccess.READ)); + OPCPackage pkg = OPCPackage.open(f.toString(), PackageAccess.READ); + T t = (T)createExtractor(pkg); + t.setFilesystem(pkg); + return t; } catch (NotOLE2FileException ne) { // ensure file-handle release IOUtils.closeQuietly(fs); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java index 4bb6acc59..482654e8f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java @@ -81,7 +81,7 @@ public class XSSFWorkbookFactory extends WorkbookFactory { public static XSSFWorkbook createWorkbook(OPCPackage pkg) throws IOException { try { return new XSSFWorkbook(pkg); - } catch (IllegalArgumentException ioe) { + } catch (RuntimeException ioe) { // ensure that file handles are closed (use revert() to not re-write the file) pkg.revert(); //pkg.close(); 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 3e747b3a3..a92906b76 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -859,7 +859,7 @@ public final class TestPackage { @Test public void testZipEntityExpansionExceedsMemory() throws IOException, OpenXML4JException, XmlException { expectedEx.expect(POIXMLException.class); - expectedEx.expectMessage("Unable to parse xml bean"); + expectedEx.expectMessage("unable to parse shared strings table"); expectedEx.expectCause(getCauseMatcher(SAXParseException.class, "The parser has encountered more than")); openXmlBombFile("poc-xmlbomb.xlsx"); } @@ -867,7 +867,7 @@ public final class TestPackage { @Test public void testZipEntityExpansionExceedsMemory2() throws IOException, OpenXML4JException, XmlException { expectedEx.expect(POIXMLException.class); - expectedEx.expectMessage("Unable to parse xml bean"); + expectedEx.expectMessage("unable to parse shared strings table"); expectedEx.expectCause(getCauseMatcher(SAXParseException.class, "The parser has encountered more than")); openXmlBombFile("poc-xmlbomb-empty.xlsx"); }