Apache Nutch is an extensible and scalable web crawler
NUTCH-1234 Upgrade to Tika 1.1 (jnioche, markus)
Julien Nioche7f44cdc99811
diff --git a/CHANGES.txt b/CHANGES.txtindex c7d8b9256..f60bba37c 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -1,6 +1,6 @@Nutch Change Log-* NUTCH-1234 Upgrade to Tika 1.1 (markus)+* NUTCH-1234 Upgrade to Tika 1.1 (jnioche, markus)* NUTCH-809 Parse-metatags plugin (jnioche)diff --git a/src/plugin/parse-tika/ivy.xml b/src/plugin/parse-tika/ivy.xmlindex bd34f2cf8..0c8f20b47 100644--- a/src/plugin/parse-tika/ivy.xml+++ b/src/plugin/parse-tika/ivy.xml@@ -36,8 +36,7 @@</publications><dependencies>- <dependency org="org.apache.tika" name="tika-parsers" rev="0.10" conf="*->default">- <exclude org="org.apache.tika" name="tika-core" />+ <dependency org="org.apache.tika" name="tika-parsers" rev="1.1" conf="*->default"></dependency></dependencies>diff --git a/src/plugin/parse-tika/plugin.xml b/src/plugin/parse-tika/plugin.xmlindex 730dddd85..aec9d988f 100644--- a/src/plugin/parse-tika/plugin.xml+++ b/src/plugin/parse-tika/plugin.xml@@ -32,26 +32,30 @@<library name="bcmail-jdk15-1.45.jar"/><library name="bcprov-jdk15-1.45.jar"/><library name="boilerpipe-1.1.0.jar"/>- <library name="commons-codec-1.4.jar"/>- <library name="commons-compress-1.1.jar"/>- <library name="commons-httpclient-3.1.jar"/>+ <library name="commons-codec-1.5.jar"/>+ <library name="commons-compress-1.3.jar"/><library name="commons-logging-1.1.1.jar"/><library name="dom4j-1.6.1.jar"/><library name="fontbox-1.6.0.jar"/><library name="geronimo-stax-api_1.0_spec-1.0.1.jar"/>+ <library name="isoparser-1.0-beta-5.jar"/>+ <library name="javassist-3.6.0.GA.jar"/><library name="jdom-1.0.jar"/><library name="jempbox-1.6.0.jar"/><library name="metadata-extractor-2.4.0-beta-1.jar"/><library name="netcdf-4.2-min.jar"/><library name="pdfbox-1.6.0.jar"/>- <library name="poi-3.8-beta4.jar"/>+ <library name="poi-3.8-beta5.jar"/><library name="poi-ooxml-3.8-beta4.jar"/>- <library name="poi-ooxml-schemas-3.8-beta4.jar"/>- <library name="poi-scratchpad-3.8-beta4.jar"/>+ <library name="poi-ooxml-schemas-3.8-beta5.jar"/>+ <library name="poi-scratchpad-3.8-beta5.jar"/><library name="rome-0.9.jar"/>+ <library name="scannotation-1.0.2.jar"/><library name="slf4j-api-1.5.6.jar"/><library name="tagsoup-1.2.1.jar"/>- <library name="tika-parsers-0.10.jar"/>+ <library name="tika-parsers-1.1.jar"/>+ <library name="vorbis-java-core-0.1.jar"/>+ <library name="vorbis-java-tika-0.1.jar"/><library name="xmlbeans-2.3.0.jar"/></runtime>diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaConfig.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaConfig.javadeleted file mode 100644index 5b3eaf0ab..000000000--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaConfig.java+++ /dev/null@@ -1,251 +0,0 @@-/*- * 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.nutch.parse.tika;--import java.io.File;-import java.io.IOException;-import java.io.InputStream;-import java.net.URL;-import java.util.HashMap;-import java.util.Iterator;-import java.util.Map;--import javax.imageio.spi.ServiceRegistry;-import javax.xml.parsers.DocumentBuilder;-import javax.xml.parsers.DocumentBuilderFactory;-import javax.xml.parsers.ParserConfigurationException;--import org.apache.tika.exception.TikaException;-import org.apache.tika.mime.MediaType;-import org.apache.tika.mime.MimeTypeException;-import org.apache.tika.mime.MimeTypes;-import org.apache.tika.mime.MimeTypesFactory;-import org.apache.tika.parser.ParseContext;-import org.apache.tika.parser.Parser;-import org.mortbay.log.Log;-import org.w3c.dom.Document;-import org.w3c.dom.Element;-import org.w3c.dom.Node;-import org.w3c.dom.NodeList;-import org.xml.sax.SAXException;--/**- * Parse xml config file.- */-public class TikaConfig {-- private final Map<String, Parser> parsers = new HashMap<String, Parser>();-- private final MimeTypes mimeTypes;-- public TikaConfig(String file) throws TikaException, IOException,- SAXException {- this(new File(file));- }-- public TikaConfig(File file) throws TikaException, IOException,- SAXException {- this(getBuilder().parse(file));- }-- public TikaConfig(URL url) throws TikaException, IOException, SAXException {- this(getBuilder().parse(url.toString()));- }-- public TikaConfig(InputStream stream) throws TikaException, IOException,- SAXException {- this(getBuilder().parse(stream));- }-- /**- * @deprecated This method will be removed in Apache Tika 1.0- * @see <a- * href="https://issues.apache.org/jira/browse/TIKA-275">TIKA-275</a>- */- public TikaConfig(InputStream stream, Parser delegate)- throws TikaException, IOException, SAXException {- this(stream);- }-- public TikaConfig(Document document) throws TikaException, IOException {- this(document.getDocumentElement());- }-- /**- * @deprecated This method will be removed in Apache Tika 1.0- * @see <a- * href="https://issues.apache.org/jira/browse/TIKA-275">TIKA-275</a>- */- public TikaConfig(Document document, Parser delegate) throws TikaException,- IOException {- this(document);- }-- public TikaConfig(Element element) throws TikaException, IOException {- Element mtr = getChild(element, "mimeTypeRepository");- if (mtr != null && mtr.hasAttribute("resource")) {- mimeTypes = MimeTypesFactory.create(mtr.getAttribute("resource"));- } else {- mimeTypes = MimeTypesFactory.create("tika-mimetypes.xml");- }-- NodeList nodes = element.getElementsByTagName("parser");- for (int i = 0; i < nodes.getLength(); i++) {- Element node = (Element) nodes.item(i);- String name = node.getAttribute("class");-- try {- Class<?> parserClass = Class.forName(name);- Object instance = parserClass.newInstance();- if (!(instance instanceof Parser)) {- throw new TikaException(- "Configured class is not a Tika Parser: " + name);- }- Parser parser = (Parser) instance;-- NodeList mimes = node.getElementsByTagName("mime");- if (mimes.getLength() > 0) {- for (int j = 0; j < mimes.getLength(); j++) {- parsers.put(getText(mimes.item(j)).trim(), parser);- }- } else {- ParseContext context = new ParseContext();- for (MediaType type : parser.getSupportedTypes(context)) {- parsers.put(type.toString(), parser);- }- }- } catch (ClassNotFoundException e) {- throw new TikaException("Configured parser class not found: "- + name, e);- } catch (IllegalAccessException e) {- throw new TikaException("Unable to access a parser class: "- + name, e);- } catch (InstantiationException e) {- throw new TikaException(- "Unable to instantiate a parser class: " + name, e);- }- }- }-- public TikaConfig() throws MimeTypeException, IOException {- ParseContext context = new ParseContext();- Iterator<Parser> iterator = ServiceRegistry.lookupProviders(- Parser.class, this.getClass().getClassLoader());- while (iterator.hasNext()) {- Parser parser = iterator.next();- for (MediaType type : parser.getSupportedTypes(context)) {- parsers.put(type.toString(), parser);- }- }- mimeTypes = MimeTypesFactory.create("tika-mimetypes.xml");- }-- /**- * @deprecated This method will be removed in Apache Tika 1.0- * @see <a- * href="https://issues.apache.org/jira/browse/TIKA-275">TIKA-275</a>- */- public TikaConfig(Element element, Parser delegate) throws TikaException,- IOException {- this(element);- }-- private String getText(Node node) {- if (node.getNodeType() == Node.TEXT_NODE) {- return node.getNodeValue();- } else if (node.getNodeType() == Node.ELEMENT_NODE) {- StringBuilder builder = new StringBuilder();- NodeList list = node.getChildNodes();- for (int i = 0; i < list.getLength(); i++) {- builder.append(getText(list.item(i)));- }- return builder.toString();- } else {- return "";- }- }-- /**- * Returns the parser instance configured for the given MIME type. Returns- * <code>null</code> if the given MIME type is unknown.- *- * @param mimeType- * MIME type- * @return configured Parser instance, or <code>null</code>- */- public Parser getParser(String mimeType) {- return parsers.get(mimeType);- }-- public Map<String, Parser> getParsers() {- return parsers;- }-- public MimeTypes getMimeRepository() {- return mimeTypes;- }-- /**- * Provides a default configuration (TikaConfig). Currently creates a new- * instance each time it's called; we may be able to have it return a shared- * instance once it is completely immutable.- *- * @return default configuration- */- public static TikaConfig getDefaultConfig() {- try {- return new TikaConfig();- } catch (IOException e) {- throw new RuntimeException("Unable to read default configuration",- e);- } catch (TikaException e) {- throw new RuntimeException(- "Unable to access default configuration", e);- }- }-- /**- * @deprecated This method will be removed in Apache Tika 1.0- * @see <a- * href="https://issues.apache.org/jira/browse/TIKA-275">TIKA-275</a>- */- public static TikaConfig getDefaultConfig(Parser delegate)- throws TikaException {- return getDefaultConfig();- }-- private static DocumentBuilder getBuilder() throws TikaException {- try {- return DocumentBuilderFactory.newInstance().newDocumentBuilder();- } catch (ParserConfigurationException e) {- throw new TikaException("XML parser not available", e);- }- }-- private static Element getChild(Element element, String name) {- Node child = element.getFirstChild();- while (child != null) {- if (child.getNodeType() == Node.ELEMENT_NODE- && name.equals(child.getNodeName())) {- return (Element) child;- }- child = child.getNextSibling();- }- return null;- }--}\ No newline at end of filediff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.javaindex 7db0fc474..372f846eb 100644--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.java+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/TikaParser.java@@ -20,11 +20,8 @@ import java.io.ByteArrayInputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;-import java.util.HashMap;import java.util.Map;-import org.slf4j.Logger;-import org.slf4j.LoggerFactory;import org.apache.hadoop.conf.Configuration;import org.apache.html.dom.HTMLDocumentImpl;import org.apache.nutch.metadata.Nutch;@@ -38,9 +35,13 @@ import org.apache.nutch.parse.ParseImpl;import org.apache.nutch.parse.ParseResult;import org.apache.nutch.parse.ParseStatus;import org.apache.nutch.protocol.Content;+import org.apache.tika.config.TikaConfig;import org.apache.tika.metadata.Metadata;+import org.apache.tika.mime.MediaType;import org.apache.tika.parser.ParseContext;import org.apache.tika.parser.Parser;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;import org.w3c.dom.DocumentFragment;/**@@ -70,7 +71,7 @@ public class TikaParser implements org.apache.nutch.parse.Parser {}// get the right parser using the mime type as a clue- Parser parser = tikaConfig.getParser(mimeType);+ Parser parser = tikaConfig.getParser(MediaType.parse(mimeType));byte[] raw = content.getContent();if (parser == null) {@@ -202,7 +203,7 @@ public class TikaParser implements org.apache.nutch.parse.Parser {}} else {try {- tikaConfig = TikaConfig.getDefaultConfig();+ tikaConfig = new TikaConfig(this.getClass().getClassLoader());} catch (Exception e2) {String message = "Problem loading default Tika configuration";LOG.error(message, e2);