Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6549 - Apache Tika can not parse Microsoft Docx format in native mode #7198

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@
<jzlib.version>1.1.1</jzlib.version>
<checker-qual.version>2.5.2</checker-qual.version>
<error-prone-annotations.version>2.2.0</error-prone-annotations.version>
<ooxml-schemas.version>1.4</ooxml-schemas.version>
<xmlbeans.version>3.0.2</xmlbeans.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -1126,6 +1128,10 @@
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
<version>${tika.version}</version>
</dependency>
Expand Down Expand Up @@ -2616,6 +2622,12 @@
<groupId>org.kie.kogito</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${kogito.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
Expand Down Expand Up @@ -2762,6 +2774,25 @@
<artifactId>quarkus-banner</artifactId>
<version>${project.version}</version>
</dependency>

<!-- ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>${ooxml-schemas.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>${xmlbeans.version}</version>
</dependency>

</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
package io.quarkus.deployment.util;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.UndeclaredThrowableException;
import java.lang.reflect.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use star imports, make sure to use the IDE config file as described in https://github.com/quarkusio/quarkus/blob/master/CONTRIBUTING.md#ide-config-and-code-style

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right @gastaldi -the IDE config file wasn't included. Now it is, however the wildcard import is still there :(

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down
11 changes: 11 additions & 0 deletions extensions/arc/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
<groupId>org.eclipse.microprofile.context-propagation</groupId>
<artifactId>microprofile-context-propagation-api</artifactId>
</dependency>
<dependency>
<groupId>xalan</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ARC needs to depend on Xalan?

Copy link
Author

@tpenakov tpenakov Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @gastaldi ,
If I put it on the other places the tika-integration-tests are failed.
I've added a few more notes it he PR description.

<artifactId>xalan</artifactId>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
<version>2.7.2</version>
</dependency>
</dependencies>

<build>
Expand Down
22 changes: 22 additions & 0 deletions extensions/tika/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<artifactId>quarkus-tika-deployment</artifactId>
<name>Quarkus - Apache Tika - Deployment</name>

<properties>
<reflections.version>0.9.12</reflections.version>
</properties>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -25,6 +29,24 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-tika</artifactId>
</dependency>
<!-- reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>

<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package io.quarkus.tika.deployment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.TransformerFactory;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xslf.usermodel.XSLFTheme;
import org.apache.poi.xwpf.usermodel.XWPFSettings;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.apache.tika.detect.Detector;
import org.apache.tika.detect.EncodingDetector;
import org.apache.tika.parser.Parser;
import org.apache.xerces.parsers.XIncludeAwareParserConfiguration;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.impl.ThemeDocumentImpl;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import org.openxmlformats.schemas.presentationml.x2006.main.impl.PresentationDocumentImpl;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CalcChainDocumentImpl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.DocumentDocumentImpl;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
Expand All @@ -26,9 +37,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.CapabilityBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.builditem.nativeimage.*;
import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.tika.TikaParseException;
import io.quarkus.tika.runtime.TikaConfiguration;
Expand All @@ -46,9 +55,13 @@ public class TikaProcessor {
"org.apache.tika.parser.geo.topic.GeoParser"
}).collect(Collectors.toSet());

public static final String PDF_PARSER_NAME = "pdf";
public static final String OOXML_PARSER_NAME = "ooxml";

private static final Map<String, String> PARSER_ABBREVIATIONS = Arrays.stream(new String[][] {
{ "pdf", "org.apache.tika.parser.pdf.PDFParser" },
{ "odf", "org.apache.tika.parser.odf.OpenDocumentParser" }
{ "odf", "org.apache.tika.parser.odf.OpenDocumentParser" },
{ OOXML_PARSER_NAME, "org.apache.tika.parser.microsoft.ooxml.OOXMLParser" },
}).collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));

@BuildStep
Expand All @@ -66,34 +79,148 @@ FeatureBuildItem feature() {
return new FeatureBuildItem(FeatureBuildItem.TIKA);
}

@BuildStep
public void registerRuntimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> resource) {
//org.apache.tika.parser.pdf.PDFParser (https://issues.apache.org/jira/browse/PDFBOX-4548)
resource.produce(new RuntimeInitializedClassBuildItem("org.apache.pdfbox.pdmodel.font.PDType1Font"));
}

@BuildStep
public void registerTikaCoreResources(BuildProducer<NativeImageResourceBuildItem> resource) {
resource.produce(new NativeImageResourceBuildItem("org/apache/tika/mime/tika-mimetypes.xml"));
resource.produce(new NativeImageResourceBuildItem("org/apache/tika/parser/external/tika-external-parsers.xml"));
}

@BuildStep
public void registerTikaParsersResources(BuildProducer<NativeImageResourceBuildItem> resource) {
resource.produce(new NativeImageResourceBuildItem("org/apache/tika/parser/pdf/PDFParser.properties"));
public void registerPdfParser(TikaConfiguration configuration,
BuildProducer<RuntimeInitializedClassBuildItem> runtimeProducer,
BuildProducer<NativeImageResourceBuildItem> nativeImageResourceProducer) {
if (doNotIncludeParser(configuration, PDF_PARSER_NAME)) {
return;
}

//org.apache.tika.parser.pdf.PDFParser (https://issues.apache.org/jira/browse/PDFBOX-4548)
runtimeProducer.produce(new RuntimeInitializedClassBuildItem("org.apache.pdfbox.pdmodel.font.PDType1Font"));

nativeImageResourceProducer
.produce(new NativeImageResourceBuildItem("org/apache/tika/parser/pdf/PDFParser.properties"));

nativeImageResourceProducer
.produce(new NativeImageResourceBuildItem("org/apache/pdfbox/resources/glyphlist/additional.txt"));
nativeImageResourceProducer
.produce(new NativeImageResourceBuildItem("org/apache/pdfbox/resources/glyphlist/glyphlist.txt"));
nativeImageResourceProducer
.produce(new NativeImageResourceBuildItem("org/apache/pdfbox/resources/glyphlist/zapfdingbats.txt"));
}

@BuildStep
public void registerPdfBoxResources(BuildProducer<NativeImageResourceBuildItem> resource) {
resource.produce(new NativeImageResourceBuildItem("org/apache/pdfbox/resources/glyphlist/additional.txt"));
resource.produce(new NativeImageResourceBuildItem("org/apache/pdfbox/resources/glyphlist/glyphlist.txt"));
resource.produce(new NativeImageResourceBuildItem("org/apache/pdfbox/resources/glyphlist/zapfdingbats.txt"));
public void registerOOXMLParser(TikaConfiguration configuration,
BuildProducer<ReflectiveClassBuildItem> resource,
BuildProducer<ServiceProviderBuildItem> serviceProvider,
BuildProducer<NativeImageResourceBundleBuildItem> resourceBundle,
BuildProducer<NativeImageResourceDirectoryBuildItem> resourceDirectory,
BuildProducer<NativeImageResourceBuildItem> resourceBuildItem) throws Exception {
//https://github.com/quarkusio/quarkus/issues/6549

if (doNotIncludeParser(configuration, OOXML_PARSER_NAME)) {
return;
}

Map<String, Map<String, String>> fileFormatSupport = configuration.parserFileFormatSupport;

boolean docxSupport = getOOXMLFileFormatSupport(fileFormatSupport, "docx", true);
boolean pptxSupport = getOOXMLFileFormatSupport(fileFormatSupport, "pptx", false);
boolean xlsxSupport = getOOXMLFileFormatSupport(fileFormatSupport, "xlsx", false);

resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xerces.impl.dv.dtd.DTDDVFactoryImpl"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xerces.impl.msg.XMLMessageFormatter"));

resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.poi.POIXMLTextExtractor"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.poi.openxml4j.opc.ZipPackagePart"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.poi.openxml4j.opc.PackagePart"));

if (docxSupport) {
resource.produce(new ReflectiveClassBuildItem(true, true, true, XWPFSettings.class.getName()));
resource.produce(new ReflectiveClassBuildItem(true, true, true, XWPFStyles.class.getName()));
}

if (pptxSupport) {
resource.produce(new ReflectiveClassBuildItem(true, true, true, STPlaceholderType.Enum.class.getName()));
getAllClassesFromPackage(XSLFTheme.class.getPackage().getName(), POIXMLDocumentPart.class)
.forEach(aClass -> resource.produce(
new ReflectiveClassBuildItem(true, true, true, aClass)));
}

resource.produce(new ReflectiveClassBuildItem(true, true, true, POIXMLDocumentPart.class.getName()));

resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.values.XmlComplexContentImpl"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.schema.SchemaTypeImpl"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.store.Cursor"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.store.Xobj"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.store.Xobj.AttrXobj"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.store.Xobj.ElementXobj"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.store.Xobj.DocumentXobj"));
resource.produce(new ReflectiveClassBuildItem(true, true, "org.apache.xmlbeans.impl.store.Locale"));

resource.produce(new ReflectiveClassBuildItem(true, true,
"schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder"));

if (pptxSupport) {
getAllClassesFromPackage(ThemeDocumentImpl.class.getPackage().getName(), XmlObject.class)
.forEach(aClass -> resource.produce(
new ReflectiveClassBuildItem(true, true, true, aClass)));
getAllClassesFromPackage(
PresentationDocumentImpl.class.getPackage().getName(),
XmlObject.class)
.forEach(aClass -> resource.produce(
new ReflectiveClassBuildItem(true, true, true, aClass)));
}

if (xlsxSupport) {
getAllClassesFromPackage(CalcChainDocumentImpl.class.getPackage().getName(), XmlObject.class)
.forEach(aClass -> resource.produce(
new ReflectiveClassBuildItem(true, true, true, aClass)));
}

if (docxSupport) {
getAllClassesFromPackage(DocumentDocumentImpl.class.getPackage().getName(), XmlObject.class)
.forEach(aClass -> resource.produce(
new ReflectiveClassBuildItem(true, true, true, aClass)));
}

serviceProvider.produce(
new ServiceProviderBuildItem(XMLParserConfiguration.class.getName(),
Arrays.asList(XIncludeAwareParserConfiguration.class.getName())));
serviceProvider.produce(
new ServiceProviderBuildItem(SAXParserFactory.class.getName(),
getProviderNames(SAXParserFactory.class.getName())));
serviceProvider.produce(
new ServiceProviderBuildItem(TransformerFactory.class.getName(),
getProviderNames(TransformerFactory.class.getName())));

resourceBundle.produce(new NativeImageResourceBundleBuildItem("org.apache.xerces.impl.msg.SAXMessages"));

resourceDirectory.produce(new NativeImageResourceDirectoryBuildItem(
"schemaorg_apache_xmlbeans/system/sD023D6490046BA0250A839A9AD24C443"));
resourceBuildItem.produce(new NativeImageResourceBuildItem("org/apache/xalan/res/XSLTInfo.properties"));
resourceBuildItem.produce(new NativeImageResourceBuildItem("org/apache/xalan/internal/res/XSLTInfo.properties"));
}

private Boolean getOOXMLFileFormatSupport(Map<String, Map<String, String>> fileFormatSupport, String fileFormat,
boolean defaultValue) {
return Optional.ofNullable(fileFormatSupport.get(OOXML_PARSER_NAME))
.filter(map -> map.containsKey(fileFormat))
.map(map -> map.get(fileFormat))
.map(Boolean::parseBoolean)
.orElse(defaultValue);
}

private boolean doNotIncludeParser(TikaConfiguration configuration, String parserName) {
return configuration.parsers.isPresent()
&& !getParserAbbreviations(configuration.parsers).contains(parserName);
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void initializeTikaParser(BeanContainerBuildItem beanContainer, TikaRecorder recorder,
BuildProducer<ServiceProviderBuildItem> serviceProvider, TikaConfiguration configuration)
BuildProducer<ServiceProviderBuildItem> serviceProvider,
TikaConfiguration configuration)
throws Exception {
Map<String, List<TikaParserParameter>> parsers = getSupportedParserConfig(configuration.tikaConfigPath,
configuration.parsers,
Expand Down Expand Up @@ -124,8 +251,7 @@ public static Map<String, List<TikaParserParameter>> getSupportedParserConfig(Op
return providerNames.stream().filter(pred).collect(Collectors.toMap(Function.identity(),
p -> Collections.<TikaParserParameter> emptyList()));
} else {
List<String> abbreviations = Arrays.stream(requiredParsers.get().split(",")).map(s -> s.trim())
.collect(Collectors.toList());
List<String> abbreviations = getParserAbbreviations(requiredParsers);
Map<String, String> fullNamesAndAbbreviations = abbreviations.stream()
.collect(Collectors.toMap(p -> getParserNameFromConfig(p, parserAbbreviations), Function.identity()));
return providerNames.stream().filter(pred).filter(p -> fullNamesAndAbbreviations.containsKey(p))
Expand All @@ -134,6 +260,24 @@ public static Map<String, List<TikaParserParameter>> getSupportedParserConfig(Op
}
}

public static Stream<Class> getAllClassesFromPackage(String packageName, Class... baseClasses) {
if (StringUtils.isBlank(packageName) || ArrayUtils.isEmpty(baseClasses)) {
return new ArrayList<Class>().stream();
}

org.reflections.Reflections reflections = new org.reflections.Reflections(packageName);

return (Stream<Class>) Arrays.stream(baseClasses)
.flatMap(aClass -> reflections.getSubTypesOf(aClass).stream())
.filter(aClass -> !((Class) aClass).isInterface())
.filter(aClass -> ((Class) aClass).getPackage().getName().equals(packageName));
}

private static List<String> getParserAbbreviations(Optional<String> requiredParsers) {
return Arrays.stream(requiredParsers.get().split(",")).map(s -> s.trim())
.collect(Collectors.toList());
}

private static String generateTikaXmlConfiguration(Map<String, List<TikaParserParameter>> parserConfig) {
StringBuilder tikaXmlConfigurationBuilder = new StringBuilder();
tikaXmlConfigurationBuilder.append("<properties>");
Expand Down
Loading