]> source.dussan.org Git - poi.git/commitdiff
#59268 - remove duplicated typeloader in POI and use fixed XmlBeans implementation...
authorAndreas Beeker <kiwiwings@apache.org>
Sat, 23 Jun 2018 22:56:34 +0000 (22:56 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sat, 23 Jun 2018 22:56:34 +0000 (22:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1834229 13f79535-47bb-0310-9956-ffa450edef68

build.xml
src/ooxml/java/org/apache/poi/ooxml/POIXMLTypeLoader.java
src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java

index 0465da74a89bfaf933f675d1643e3e9fa9c5c52a..e7e5e5e2e1fcbc832619d2e227655ab8fd64778f 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -871,9 +871,16 @@ under the License.
                 </classpath>
             </xmlbean>
 
+            <replaceregexp byline="true"
+                match="(\s*)public static ([^ ]+) newInstance\(\) \{"
+                replace="\1private static org.apache.xmlbeans.SchemaTypeLoader getTypeLoader() { return org.apache.xmlbeans.XmlBeans.typeLoaderForClassLoader(\2.class.getClassLoader()); }${line.separator}${line.separator}\1public static \2 newInstance\(\) \{"
+            >
+                <fileset dir="${xmlbean.sources.dir}" includes="**/*.java" excludes="**/impl/**"/>
+            </replaceregexp>
+
             <replace dir="${xmlbean.sources.dir}" includes="**/*.java" excludes="**/impl/**">
-                <replacetoken>org.apache.xmlbeans.XmlBeans.getContextTypeLoader()</replacetoken>
-                <replacevalue>org.apache.poi.ooxml.POIXMLTypeLoader</replacevalue>
+                <replacetoken>org.apache.xmlbeans.XmlBeans.getContextTypeLoader</replacetoken>
+                <replacevalue>getTypeLoader</replacevalue>
             </replace>
 
             <!-- remove deprecated warnings, as we prefer the array methods - see #56854 -->
@@ -882,22 +889,6 @@ under the License.
 ]]></replacetoken>
             </replace>
 
-            <copy todir="${xmlbean.sources.dir}">
-                <fileset dir="${ooxml.src}">
-                    <include name="org/apache/poi/ooxml/POIXMLTypeLoader.java"/>
-                    <include name="org/apache/poi/ooxml/util/DocumentHelper.java"/>
-                    <include name="org/apache/poi/ooxml/util/SAXHelper.java"/>
-                    <include name="org/apache/poi/openxml4j/opc/PackageNamespaces.java"/>
-                </fileset>
-                <fileset dir="${main.src}">
-                    <include name="org/apache/poi/util/POILogFactory.java"/>
-                    <include name="org/apache/poi/util/POILogger.java"/>
-                    <include name="org/apache/poi/util/NullLogger.java"/>
-                    <include name="org/apache/poi/util/Internal.java"/>
-                       <include name="org/apache/poi/util/Removal.java"/>
-                </fileset>
-            </copy>
-
             <echo>Forking javac with max heap size ${ooxml.memory}</echo>
 
             <javac target="${jdk.version.class}"
index 123c0b5786778808335d1c1cefaf0b70e4a4c317..c795cd0d62881c8a9ca6f7620c81b283911eec32 100644 (file)
 
 package org.apache.poi.ooxml;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.io.StringReader;
-import java.net.URL;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.xml.stream.XMLStreamReader;
-
 import org.apache.poi.openxml4j.opc.PackageNamespaces;
-import org.apache.poi.ooxml.util.DocumentHelper;
-import org.apache.xmlbeans.SchemaType;
-import org.apache.xmlbeans.SchemaTypeLoader;
-import org.apache.xmlbeans.XmlBeans;
-import org.apache.xmlbeans.XmlException;
-import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
-import org.apache.xmlbeans.xml.stream.XMLInputStream;
-import org.apache.xmlbeans.xml.stream.XMLStreamException;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
 
-@SuppressWarnings("deprecation")
 public class POIXMLTypeLoader {
 
-    private static ThreadLocal<SchemaTypeLoader> 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));
-    }
 }
index d3218099388ec890a2f2e9769fcf3f9027396313..c718b2295dcea573e647456c812268e7a3d6c989 100644 (file)
@@ -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);
index 4bb6acc59aeb23ce8355a1142057fdb91aa38c40..482654e8f96a06e4b3f4a6eb6ebed48768094813 100644 (file)
@@ -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();
index 3e747b3a363210bdfac13dd9df54ad9a8ac841a5..a92906b7651091fadcf06bce90a03cdc5359e2ad 100644 (file)
@@ -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");
        }