#59268 - remove duplicated typeloader in POI and use fixed XmlBeans implementation instead

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1834229 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2018-06-23 22:56:34 +00:00
parent c88e551e05
commit eb03fc42be
5 changed files with 16 additions and 118 deletions

View File

@ -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}"

View File

@ -17,39 +17,15 @@
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));
}
}

View 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);

View 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();

View 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");
}