Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2584 Upgrade parse-tika to use Tika 1.18 - fix failing unit tests - use Tika parser to get DOM tree of test documents - fix HTMLMetaProcessor to extract no-cache and base-href attributes on DOM tree modified by Tika - ignore links from FORM and SOURCE elements which are not extracted by Tika parser

Sebastian Nagelf5e3a3015316
diff --git a/src/plugin/parse-tika/build.xml b/src/plugin/parse-tika/build.xml
index 4ecb3f878..bda9e893b 100644
--- a/src/plugin/parse-tika/build.xml
+++ b/src/plugin/parse-tika/build.xml
@@ -19,23 +19,10 @@
<import file="../build-plugin.xml"/>
- <!-- Build compilation dependencies -->
- <target name="deps-jar">
- <ant target="jar" inheritall="false" dir="../lib-nekohtml" />
- </target>
-
- <!-- Add compilation dependencies to classpath -->
- <path id="plugin.deps">
- <fileset dir="${nutch.root}/build">
- <include name="**/lib-nekohtml/*.jar" />
- </fileset>
- </path>
-
- <!-- Deploy Unit test dependencies -->
+ <!-- Deploy Unit test dependencies -->
<target name="deps-test">
<ant target="deploy" inheritall="false" dir="../nutch-extensionpoints"/>
<ant target="deploy" inheritall="false" dir="../protocol-file"/>
- <ant target="deploy" inheritall="false" dir="../lib-nekohtml" />
</target>
<!-- for junit test -->
diff --git a/src/plugin/parse-tika/plugin.xml b/src/plugin/parse-tika/plugin.xml
index 0e9bd483c..398c0e423 100644
--- a/src/plugin/parse-tika/plugin.xml
+++ b/src/plugin/parse-tika/plugin.xml
@@ -130,7 +130,6 @@
<requires>
<import plugin="nutch-extensionpoints"/>
- <import plugin="lib-nekohtml"/>
</requires>
<extension point="org.apache.nutch.parse.Parser"
diff --git a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java
index 8d5baec24..e5be8855d 100644
--- a/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java
+++ b/src/plugin/parse-tika/src/java/org/apache/nutch/parse/tika/HTMLMetaProcessor.java
@@ -17,6 +17,7 @@
package org.apache.nutch.parse.tika;
+import java.net.MalformedURLException;
import java.net.URL;
import org.apache.nutch.parse.HTMLMetaTags;
@@ -113,6 +114,26 @@ public class HTMLMetaProcessor {
}
} // end if (name == robots)
+ // meta names added/transformed by Tika
+ else if (name.equals("pragma")) {
+ String content = contentNode.getNodeValue().toLowerCase();
+ if (content.contains("no-cache")) {
+ metaTags.setNoCache();
+ }
+ } else if (name.equals("content-location")) {
+ String urlString = contentNode.getNodeValue();
+ URL url = null;
+ try {
+ if (currURL == null) {
+ url = new URL(urlString);
+ } else {
+ url = new URL(currURL, urlString);
+ }
+ metaTags.setBaseHref(url);
+ } catch (MalformedURLException e) {
+ // ignore, base-href not set
+ }
+ }
}
}
diff --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.java
index 8c867d8be..40d82bcaf 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
@@ -57,8 +57,7 @@ import org.xml.sax.ContentHandler;
/**
* Wrapper for Tika parsers. Mimics the HTMLParser but using the XHTML
* representation returned by Tika as SAX events
- ***/
-
+ */
public class TikaParser implements org.apache.nutch.parse.Parser {
private static final Logger LOG = LoggerFactory
@@ -72,8 +71,17 @@ public class TikaParser implements org.apache.nutch.parse.Parser {
private HtmlMapper HTMLMapper;
private boolean upperCaseElementNames = true;
- @SuppressWarnings("deprecation")
public ParseResult getParse(Content content) {
+ HTMLDocumentImpl doc = new HTMLDocumentImpl();
+ doc.setErrorChecking(false);
+ DocumentFragment root = doc.createDocumentFragment();
+
+ return getParse(content, doc, root);
+ }
+
+ @SuppressWarnings("deprecation")
+ ParseResult getParse(Content content, HTMLDocumentImpl doc,
+ DocumentFragment root) {
String mimeType = content.getContentType();
boolean useBoilerpipe = getConf().get("tika.extractor", "none").equals("boilerpipe");
@@ -103,10 +111,6 @@ public class TikaParser implements org.apache.nutch.parse.Parser {
Metadata tikamd = new Metadata();
- HTMLDocumentImpl doc = new HTMLDocumentImpl();
- doc.setErrorChecking(false);
- DocumentFragment root = doc.createDocumentFragment();
-
ContentHandler domHandler;
// Check whether to use Tika's BoilerplateContentHandler
@@ -266,7 +270,7 @@ public class TikaParser implements org.apache.nutch.parse.Parser {
String htmlmapperClassName = conf.get("tika.htmlmapper.classname");
if (StringUtils.isNotBlank(htmlmapperClassName)) {
try {
- Class HTMLMapperClass = Class.forName(htmlmapperClassName);
+ Class<?> HTMLMapperClass = Class.forName(htmlmapperClassName);
boolean interfaceOK = HtmlMapper.class
.isAssignableFrom(HTMLMapperClass);
if (!interfaceOK) {
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestDOMContentUtils.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestDOMContentUtils.java
similarity index 89%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestDOMContentUtils.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestDOMContentUtils.java
index 2159b9d5a..06828cf7e 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestDOMContentUtils.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestDOMContentUtils.java
@@ -15,25 +15,23 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
-import org.apache.nutch.parse.Outlink;
-import org.apache.nutch.parse.tika.DOMContentUtils;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.nutch.util.NutchConfiguration;
-
-import java.io.ByteArrayInputStream;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.StringTokenizer;
-import org.xml.sax.*;
-import org.w3c.dom.*;
-import org.apache.html.dom.*;
-import org.cyberneko.html.parsers.DOMFragmentParser;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.html.dom.HTMLDocumentImpl;
+import org.apache.nutch.metadata.Metadata;
+import org.apache.nutch.parse.Outlink;
+import org.apache.nutch.protocol.Content;
+import org.apache.nutch.util.NutchConfiguration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.w3c.dom.DocumentFragment;
/**
* Unit tests for DOMContentUtils.
@@ -55,7 +53,7 @@ public class TestDOMContentUtils {
+ "<a href=\"/\"> separate this " + "<a href=\"ok\"> from this"
+ "</a></a>" + "</body></html>"),
- // this one relies on certain neko fixup behavior, possibly
+ // this one relies on certain fixup behavior, possibly
// distributing the anchors into the LI's-but not the other
// anchors (outside of them, instead)! So you get a tree that
// looks like:
@@ -112,7 +110,7 @@ public class TestDOMContentUtils {
+ "<a href=\"http://www.nutch.org\" rel=\"nofollow\"> ignore </a>"
+ "<a rel=\"nofollow\" href=\"http://www.nutch.org\"> ignore </a>"
+ "</body></html>"),
- // test that POST form actions are skipped
+ // test that all form actions are skipped
new String("<html><head></head><body>"
+ "<form method='POST' action='/search.jsp'><input type=text>"
+ "<input type=submit><p>test1</p></form>"
@@ -150,20 +148,17 @@ public class TestDOMContentUtils {
private static URL[] testBaseHrefURLs = new URL[testPages.length];
- private static final String[] answerText = {
- "title body anchor",
- "title body home bots",
- "separate this from this",
- "my title body home 1 2",
- "my title",
- "my title the bottom",
- "my title Whitespace test whitespace test "
- + "This is a whitespace test . Newlines should appear as space too. "
- + "Tabs are spaces too. This is a break -> and the line after break . "
- + "one two three space here space there no space "
- + "one two two three three four put some text here and there. "
- + "End this madness ! . . . .", "ignore ignore", "test1 test2",
- "test1 test2", "title anchor1 anchor2 anchor3",
+ private static final String[] answerText = { "title body anchor",
+ "title body home bots", "separate this from this",
+ "my title body home 1 2", "my title", "my title the bottom",
+ "my title\n" + "Whitespace test\n" + "whitespace test\n"
+ + "This is a whitespace test. Newlines should appear as space too.\n"
+ + "Tabs are spaces too. This is a break -> and the line after break.\n"
+ + "one\n" + "two\n" + "three\n" + "space here\n" + "space there\n"
+ + "no space\n" + "one two\n" + "two three\n" + "three four\n"
+ + "put some text here and there. End this madness ! . . . .",
+ "ignore ignore", "test1 test2", "test1 test2",
+ "title anchor1 anchor2 anchor3",
"title anchor1 anchor2 anchor3 anchor4 anchor5", "" };
private static final String[] answerTitle = { "title", "title", "",
@@ -179,23 +174,26 @@ public class TestDOMContentUtils {
@Before
public void setup() throws Exception {
conf = NutchConfiguration.create();
- conf.setBoolean("parser.html.form.use_action", true);
utils = new DOMContentUtils(conf);
- DOMFragmentParser parser = new DOMFragmentParser();
- parser.setFeature(
- "http://cyberneko.org/html/features/scanner/allow-selfclosing-iframe",
- true);
+ conf.set("plugin.includes", "parse-tika");
+ TikaParser parser = new TikaParser();
+ parser.setConf(conf);
+
for (int i = 0; i < testPages.length; i++) {
- DocumentFragment node = new HTMLDocumentImpl().createDocumentFragment();
try {
- parser.parse(
- new InputSource(new ByteArrayInputStream(testPages[i].getBytes())),
- node);
- testBaseHrefURLs[i] = new URL(testBaseHrefs[i]);
+ String url = testBaseHrefs[i];
+ testBaseHrefURLs[i] = new URL(url);
+ Content content = new Content(url, url,
+ testPages[i].getBytes(StandardCharsets.UTF_8), "text/html",
+ new Metadata(), conf);
+ HTMLDocumentImpl doc = new HTMLDocumentImpl();
+ doc.setErrorChecking(false);
+ DocumentFragment root = doc.createDocumentFragment();
+ parser.getParse(content, doc, root);
+ testDOMs[i] = root;
} catch (Exception e) {
Assert.assertTrue("caught exception: " + e, false);
}
- testDOMs[i] = node;
}
answerOutlinks = new Outlink[][] {
{ new Outlink("http://www.nutch.org", "anchor"), },
@@ -217,7 +215,7 @@ public class TestDOMContentUtils {
new Outlink("http://www.nutch.org/docs/index.html", ""), },
{ new Outlink("http://www.nutch.org/index.html", "whitespace test"), },
{},
- { new Outlink("http://www.nutch.org/dummy.jsp", "test2"), },
+ {},
{},
{ new Outlink("http://www.nutch.org/;x", "anchor1"),
new Outlink("http://www.nutch.org/g;x", "anchor2"),
@@ -230,7 +228,7 @@ public class TestDOMContentUtils {
new Outlink("http://www.nutch.org/;something?y=1#s", "anchor4"),
new Outlink("http://www.nutch.org/;something?y=1;somethingelse",
"anchor5") },
- { new Outlink("http://www.nutch.org/movie.mp4", "") } };
+ {} };
}
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestFeedParser.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestFeedParser.java
similarity index 99%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestFeedParser.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestFeedParser.java
index cdfe2b48c..2677395d8 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestFeedParser.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestFeedParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
import java.lang.invoke.MethodHandles;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestHtmlParser.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestHtmlParser.java
similarity index 99%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestHtmlParser.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestHtmlParser.java
index d2bc816e1..f7d01f6fb 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestHtmlParser.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestHtmlParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
import java.lang.invoke.MethodHandles;
import java.nio.charset.Charset;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestImageMetadata.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestImageMetadata.java
similarity index 98%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestImageMetadata.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestImageMetadata.java
index b1762e603..c688ee492 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestImageMetadata.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestImageMetadata.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
import org.apache.nutch.protocol.ProtocolFactory;
import org.apache.nutch.protocol.Protocol;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestMSWordParser.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestMSWordParser.java
similarity index 98%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestMSWordParser.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestMSWordParser.java
index 576b3dfdd..a3c04ca00 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestMSWordParser.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestMSWordParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
import org.apache.nutch.protocol.ProtocolFactory;
import org.apache.nutch.protocol.Protocol;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestOOParser.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestOOParser.java
similarity index 98%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestOOParser.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestOOParser.java
index 6960bad66..f9ad710c7 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestOOParser.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestOOParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
import java.io.FileInputStream;
import java.io.InputStreamReader;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestPdfParser.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestPdfParser.java
similarity index 98%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestPdfParser.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestPdfParser.java
index 9884f0cf4..c7d7d0abc 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestPdfParser.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestPdfParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
import org.apache.nutch.protocol.ProtocolFactory;
import org.apache.nutch.protocol.Protocol;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestRTFParser.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRTFParser.java
similarity index 98%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestRTFParser.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRTFParser.java
index f15d821fe..6585d98d6 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestRTFParser.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRTFParser.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
// Nutch imports
import org.apache.nutch.crawl.CrawlDatum;
diff --git a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestRobotsMetaProcessor.java b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.java
similarity index 79%
rename from src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestRobotsMetaProcessor.java
rename to src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.java
index 4224f934d..3c7b25418 100644
--- a/src/plugin/parse-tika/src/test/org/apache/nutch/tika/TestRobotsMetaProcessor.java
+++ b/src/plugin/parse-tika/src/test/org/apache/nutch/parse/tika/TestRobotsMetaProcessor.java
@@ -15,20 +15,20 @@
* limitations under the License.
*/
-package org.apache.nutch.tika;
+package org.apache.nutch.parse.tika;
-import org.apache.nutch.parse.HTMLMetaTags;
-import org.apache.nutch.parse.tika.HTMLMetaProcessor;
-
-import java.io.ByteArrayInputStream;
import java.net.URL;
-import org.xml.sax.*;
-import org.w3c.dom.*;
-import org.apache.html.dom.*;
-import org.cyberneko.html.parsers.DOMFragmentParser;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.html.dom.HTMLDocumentImpl;
+import org.apache.nutch.metadata.Metadata;
+import org.apache.nutch.parse.HTMLMetaTags;
+import org.apache.nutch.protocol.Content;
+import org.apache.nutch.util.DomUtil;
+import org.apache.nutch.util.NutchConfiguration;
import org.junit.Assert;
import org.junit.Test;
+import org.w3c.dom.DocumentFragment;
/** Unit tests for HTMLMetaProcessor. */
public class TestRobotsMetaProcessor {
@@ -87,7 +87,8 @@ public class TestRobotsMetaProcessor {
};
- public static final boolean[][] answers = { { true, true, true }, // NONE
+ public static final boolean[][] answers = { //
+ { true, true, true }, // NONE
{ false, false, true }, // all
{ true, true, true }, // nOnE
{ true, true, false }, // none
@@ -102,8 +103,9 @@ public class TestRobotsMetaProcessor {
@Test
public void testRobotsMetaProcessor() {
- DOMFragmentParser parser = new DOMFragmentParser();
- ;
+ Configuration conf = NutchConfiguration.create();
+ TikaParser parser = new TikaParser();
+ parser.setConf(conf);
try {
currURLsAndAnswers = new URL[][] {
@@ -125,23 +127,27 @@ public class TestRobotsMetaProcessor {
for (int i = 0; i < tests.length; i++) {
byte[] bytes = tests[i].getBytes();
- DocumentFragment node = new HTMLDocumentImpl().createDocumentFragment();
+ HTMLDocumentImpl doc = new HTMLDocumentImpl();
+ doc.setErrorChecking(false);
+ DocumentFragment root = doc.createDocumentFragment();
+ Content content = new Content("http://www.nutch.org",
+ "http://www.nutch.org", bytes, "text/html", new Metadata(), conf);
try {
- parser.parse(new InputSource(new ByteArrayInputStream(bytes)), node);
+ parser.getParse(content, doc, root);
} catch (Exception e) {
e.printStackTrace();
}
HTMLMetaTags robotsMeta = new HTMLMetaTags();
- HTMLMetaProcessor.getMetaTags(robotsMeta, node, currURLsAndAnswers[i][0]);
-
- Assert.assertTrue("got index wrong on test " + i,
- robotsMeta.getNoIndex() == answers[i][0]);
- Assert.assertTrue("got follow wrong on test " + i,
- robotsMeta.getNoFollow() == answers[i][1]);
- Assert.assertTrue("got cache wrong on test " + i,
- robotsMeta.getNoCache() == answers[i][2]);
+ HTMLMetaProcessor.getMetaTags(robotsMeta, root, currURLsAndAnswers[i][0]);
+
+ Assert.assertEquals("got noindex wrong on test " + i,
+ answers[i][0], robotsMeta.getNoIndex());
+ Assert.assertEquals("got nofollow wrong on test " + i,
+ answers[i][1], robotsMeta.getNoFollow());
+ Assert.assertEquals("got nocache wrong on test " + i,
+ answers[i][2], robotsMeta.getNoCache());
Assert
.assertTrue(
"got base href wrong on test " + i + " (got "