Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2464 get textual content from nested items on heading nodes
Jorge Luis Betancourt Gonzalezb8580b3dd3d4
diff --git a/src/plugin/headings/ivy.xml b/src/plugin/headings/ivy.xmlindex 5b8393b6b..b8482ffa9 100644--- a/src/plugin/headings/ivy.xml+++ b/src/plugin/headings/ivy.xml@@ -36,6 +36,7 @@</publications><dependencies>+ <dependency org="net.sourceforge.nekohtml" name="nekohtml" rev="1.9.19" conf="test->master"/></dependencies></ivy-module>diff --git a/src/plugin/headings/src/java/org/apache/nutch/parse/headings/HeadingsParseFilter.java b/src/plugin/headings/src/java/org/apache/nutch/parse/headings/HeadingsParseFilter.javaindex e20a2da09..eaa2a7020 100644--- a/src/plugin/headings/src/java/org/apache/nutch/parse/headings/HeadingsParseFilter.java+++ b/src/plugin/headings/src/java/org/apache/nutch/parse/headings/HeadingsParseFilter.java@@ -108,12 +108,13 @@ public class HeadingsParseFilter implements HtmlParseFilter {*/protected static String getNodeValue(Node node) {StringBuilder buffer = new StringBuilder();+ NodeWalker walker = new NodeWalker(node);- NodeList children = node.getChildNodes();+ while (walker.hasNext()) {+ final Node n = walker.nextNode();- for (int i = 0; i < children.getLength(); i++) {- if (children.item(i).getNodeType() == Node.TEXT_NODE) {- buffer.append(children.item(i).getNodeValue());+ if (n.getNodeType() == Node.TEXT_NODE) {+ buffer.append(n.getNodeValue());}}diff --git a/src/plugin/headings/src/test/org/apache/nutch/parse/headings/TestHeadingsParseFilter.java b/src/plugin/headings/src/test/org/apache/nutch/parse/headings/TestHeadingsParseFilter.javanew file mode 100644index 000000000..125d7567f--- /dev/null+++ b/src/plugin/headings/src/test/org/apache/nutch/parse/headings/TestHeadingsParseFilter.java@@ -0,0 +1,51 @@+package org.apache.nutch.parse.headings;++import org.apache.hadoop.conf.Configuration;+import org.apache.html.dom.HTMLDocumentImpl;+import org.apache.nutch.metadata.Metadata;+import org.apache.nutch.parse.*;+import org.apache.nutch.protocol.Content;+import org.apache.nutch.util.NutchConfiguration;++import java.io.ByteArrayInputStream;+import java.io.IOException;++import org.w3c.dom.DocumentFragment;+import org.xml.sax.InputSource;+import org.xml.sax.SAXException;+import org.cyberneko.html.parsers.DOMFragmentParser;+import org.junit.Assert;+import org.junit.Test;++public class TestHeadingsParseFilter {+ private static Configuration conf = NutchConfiguration.create();++ @Test+ public void testExtractHeadingFromNestedNodes()+ throws IOException, SAXException {++ conf.setStrings("headings", "h1", "h2");+ HtmlParseFilter filter = new HeadingsParseFilter();+ filter.setConf(conf);++ Content content = new Content("http://www.foo.com/", "http://www.foo.com/",+ "".getBytes("UTF8"), "text/html; charset=UTF-8", new Metadata(), conf);+ ParseImpl parse = new ParseImpl("foo bar", new ParseData());+ ParseResult parseResult = ParseResult+ .createParseResult("http://www.foo.com/", parse);+ HTMLMetaTags metaTags = new HTMLMetaTags();+ DOMFragmentParser parser = new DOMFragmentParser();+ DocumentFragment node = new HTMLDocumentImpl().createDocumentFragment();++ parser.parse(new InputSource(new ByteArrayInputStream(+ ("<html><head><title>test header with span element</title></head><body><h1>header with <span>span element</span></h1></body></html>")+ .getBytes())), node);++ parseResult = filter.filter(content, parseResult, metaTags, node);++ Assert.assertEquals(+ "The h1 tag must include the content of the inner span node",+ "header with span element",+ parseResult.get(content.getUrl()).getData().getParseMeta().get("h1"));+ }+}