Apache Nutch is an extensible and scalable web crawler
NUTCH-1467 and NUTCH-1560: add all values of multi-valued metatags
Sebastian Nagelf9227f363512
diff --git a/CHANGES.txt b/CHANGES.txtindex d9a532391..b917706b9 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,10 @@ Nutch Change Log(trunk): Current Development+* NUTCH-1560 index-metadata to add all values of multivalued metadata (snagel)++* NUTCH-1467 Not able to parse mutliValued metatags (kiran via snagel)+* NUTCH-1430 Freegenerator records overwrite CrawlDB records with AdaptiveFetchSchedule (markus)* NUTCH-1522 Upgrade to Tika 1.3 (jnioche)diff --git a/src/java/org/apache/nutch/parse/HTMLMetaTags.java b/src/java/org/apache/nutch/parse/HTMLMetaTags.javaindex 218f9b24d..a5b8a77fc 100644--- a/src/java/org/apache/nutch/parse/HTMLMetaTags.java+++ b/src/java/org/apache/nutch/parse/HTMLMetaTags.java@@ -21,6 +21,8 @@ import java.net.URL;import java.util.Iterator;import java.util.Properties;+import org.apache.nutch.metadata.Metadata;+/*** This class holds the information about HTML "meta" tags extracted from* a page. Some special tags have convenience methods for easy checking.@@ -40,10 +42,10 @@ public class HTMLMetaTags {private URL refreshHref = null;- private Properties generalTags = new Properties();+ private Metadata generalTags = new Metadata();private Properties httpEquivTags = new Properties();-+/*** Sets all boolean values to <code>false</code>. Clears all other tags.*/@@ -166,7 +168,7 @@ public class HTMLMetaTags {* Returns all collected values of the general meta tags. Property names are* tag names, property values are "content" values.*/- public Properties getGeneralTags() {+ public Metadata getGeneralTags() {return generalTags;}@@ -188,12 +190,13 @@ public class HTMLMetaTags {+ ", refreshHref=" + refreshHref + "\n");sb.append(" * general tags:\n");- Iterator<Object> it = generalTags.keySet().iterator();- while (it.hasNext()) {- String key = (String)it.next();+ String[] names = generalTags.names();+ for (String name : names) {+ String key = name;sb.append(" - " + key + "\t=\t" + generalTags.get(key) + "\n");}sb.append(" * http-equiv tags:\n");+ Iterator<Object> it = httpEquivTags.keySet().iterator();it = httpEquivTags.keySet().iterator();while (it.hasNext()) {String key = (String)it.next();diff --git a/src/plugin/index-metadata/src/java/org/apache/nutch/indexer/metadata/MetadataIndexer.java b/src/plugin/index-metadata/src/java/org/apache/nutch/indexer/metadata/MetadataIndexer.javaindex 21cf58ba7..57014b02b 100644--- a/src/plugin/index-metadata/src/java/org/apache/nutch/indexer/metadata/MetadataIndexer.java+++ b/src/plugin/index-metadata/src/java/org/apache/nutch/indexer/metadata/MetadataIndexer.java@@ -65,18 +65,20 @@ public class MetadataIndexer implements IndexingFilter {// add the fields from parsemdif (parseFieldnames != null) {for (String metatag : parseFieldnames) {- String value = parse.getData().getParseMeta().get(metatag);- if (value != null)- doc.add(metatag, value);+ for (String value : parse.getData().getParseMeta().getValues(metatag)) {+ if (value != null)+ doc.add(metatag, value);+ }}}// add the fields from contentmdif (contentFieldnames != null) {for (String metatag : contentFieldnames) {- String value = parse.getData().getContentMeta().get(metatag);- if (value != null)- doc.add(metatag, value);+ for (String value : parse.getData().getContentMeta().getValues(metatag)) {+ if (value != null)+ doc.add(metatag, value);+ }}}diff --git a/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/HTMLMetaProcessor.java b/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/HTMLMetaProcessor.javaindex c57038d66..383506cf4 100644--- a/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/HTMLMetaProcessor.java+++ b/src/plugin/parse-html/src/java/org/apache/nutch/parse/html/HTMLMetaProcessor.java@@ -78,7 +78,7 @@ public class HTMLMetaProcessor {if (nameNode != null) {if (contentNode != null) {String name = nameNode.getNodeValue().toLowerCase();- metaTags.getGeneralTags().setProperty(name, contentNode.getNodeValue());+ metaTags.getGeneralTags().add(name, contentNode.getNodeValue());if ("robots".equals(name)) {if (contentNode != null) {diff --git a/src/plugin/parse-metatags/build.xml b/src/plugin/parse-metatags/build.xmlindex 5034bfeea..e30292d92 100644--- a/src/plugin/parse-metatags/build.xml+++ b/src/plugin/parse-metatags/build.xml@@ -28,6 +28,10 @@<!-- for junit test --><mkdir dir="${build.test}/data" />- <copy file="sample/testMetatags.html" todir="${build.test}/data" />+ <copy todir="${build.test}/data">+ <fileset dir="sample">+ <include name="*.html" />+ </fileset>+ </copy></project>diff --git a/src/plugin/parse-metatags/sample/testMultivalueMetatags.html b/src/plugin/parse-metatags/sample/testMultivalueMetatags.htmlnew file mode 100644index 000000000..ca8b737c2--- /dev/null+++ b/src/plugin/parse-metatags/sample/testMultivalueMetatags.html@@ -0,0 +1,12 @@+<html>+<head>+<meta name="DC.creator" content="Doug Cutting">+<meta name="DC.creator" content="Michael Cafarella">+<!-- meta keywords in different casing -->+<meta name="keywords" lang="en" content="web crawler" />+<meta name="Keywords" lang="fr" content="robot d'indexation" />+<meta name="KEYWORDS" lang="de" content="Webcrawler" />+</head>+<body>+A test for multi-valued metatags.+</body>\ No newline at end of filediff --git a/src/plugin/parse-metatags/src/java/org/apache/nutch/parse/MetaTagsParser.java b/src/plugin/parse-metatags/src/java/org/apache/nutch/parse/MetaTagsParser.javaindex 09aaa12df..1bf8e1e54 100644--- a/src/plugin/parse-metatags/src/java/org/apache/nutch/parse/MetaTagsParser.java+++ b/src/plugin/parse-metatags/src/java/org/apache/nutch/parse/MetaTagsParser.java@@ -75,16 +75,17 @@ public class MetaTagsParser implements HtmlParseFilter {}}- Properties generalMetaTags = metaTags.getGeneralTags();- for (Enumeration tagNames = generalMetaTags.propertyNames(); tagNames- .hasMoreElements();) {- String name = (String) tagNames.nextElement();- String value = generalMetaTags.getProperty(name);+ Metadata generalMetaTags = metaTags.getGeneralTags();+ for (String tagName : generalMetaTags.names() ) {+ String[] tagValues = generalMetaTags.getValues(tagName);++ for ( String tagValue : tagValues ) {// check whether the name is in the list of what we want or if// specified *- if (metatagset.contains("*") || metatagset.contains(name.toLowerCase())) {- LOG.debug("Found meta tag : " + name + "\t" + value);- metadata.add("metatag." + name.toLowerCase(), value);+ if (metatagset.contains("*") || metatagset.contains(tagName.toLowerCase())) {+ LOG.debug("Found meta tag : " + tagName + "\t" + tagValue);+ metadata.add("metatag." + tagName.toLowerCase(), tagValue);+ }}}diff --git a/src/plugin/parse-metatags/src/test/org/apache/nutch/parse/html/TestMetatagParser.java b/src/plugin/parse-metatags/src/test/org/apache/nutch/parse/html/TestMetatagParser.javaindex 0d0833d53..fa8b2b05f 100644--- a/src/plugin/parse-metatags/src/test/org/apache/nutch/parse/html/TestMetatagParser.java+++ b/src/plugin/parse-metatags/src/test/org/apache/nutch/parse/html/TestMetatagParser.java@@ -17,6 +17,9 @@package org.apache.nutch.parse.html;+import java.util.Set;+import java.util.TreeSet;+import junit.framework.TestCase;import org.apache.hadoop.conf.Configuration;@@ -35,6 +38,7 @@ public class TestMetatagParser extends TestCase {private String fileSeparator = System.getProperty("file.separator");private String sampleDir = System.getProperty("test.data", ".");private String sampleFile = "testMetatags.html";+ private String sampleFileMultival = "testMultivalueMetatags.html";private String description = "This is a test of description";private String keywords = "This is a test of keywords";@@ -42,27 +46,58 @@ public class TestMetatagParser extends TestCase {super(name);}- public void testIt() {- Configuration conf = NutchConfiguration.create();-- String urlString = "file:" + sampleDir + fileSeparator + sampleFile;-+ public Metadata parseMeta(String fileName, Configuration conf) {+ Metadata metadata = null;try {+ String urlString = "file:" + sampleDir + fileSeparator + fileName;Protocol protocol = new ProtocolFactory(conf).getProtocol(urlString);Content content = protocol.getProtocolOutput(new Text(urlString),new CrawlDatum()).getContent();-Parse parse = new ParseUtil(conf).parse(content).get(content.getUrl());-- // check that we get the same values- Metadata parseMeta = parse.getData().getParseMeta();-- assertEquals(description, parseMeta.get("metatag.description"));- assertEquals(keywords, parseMeta.get("metatag.keywords"));+ metadata = parse.getData().getParseMeta();} catch (Exception e) {e.printStackTrace();fail(e.toString());}+ return metadata;+ }++ public void testIt() {+ Configuration conf = NutchConfiguration.create();++ // check that we get the same values+ Metadata parseMeta= parseMeta(sampleFile, conf);++ assertEquals(description, parseMeta.get("metatag.description"));+ assertEquals(keywords, parseMeta.get("metatag.keywords"));+ }++ public void testMultiValueMetatags() {+ Configuration conf = NutchConfiguration.create();+ conf.set("metatags.names", "keywords;DC.creator");+ conf.set("index.parse.md", "metatag.keywords,metatag.dc.creator");++ Metadata parseMeta = parseMeta(sampleFileMultival, conf);++ String failMessage = "One value of metatag with multiple values is missing: ";++ Set<String> valueSet = new TreeSet<String>();+ for (String val : parseMeta.getValues("metatag.dc.creator")) {+ valueSet.add(val);+ }+ String[] expectedValues1 = {"Doug Cutting", "Michael Cafarella"};+ for (String val : expectedValues1) {+ assertTrue(failMessage + val, valueSet.contains(val));+ }++ valueSet.clear();+ for (String val : parseMeta.getValues("metatag.keywords")) {+ valueSet.add(val);+ }+ String[] expectedValues2 = {"robot d'indexation", "web crawler", "Webcrawler"};+ for (String val : expectedValues2) {+ assertTrue(failMessage + val, valueSet.contains(val));+ }}}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.javaindex 5f98e621f..ac754ddf9 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@@ -49,7 +49,7 @@ public class HTMLMetaProcessor {private static final void getMetaTagsHelper(HTMLMetaTags metaTags, Node node, URL currURL) {-+if (node.getNodeType() == Node.ELEMENT_NODE) {if ("body".equalsIgnoreCase(node.getNodeName())) {@@ -77,8 +77,8 @@ public class HTMLMetaProcessor {if (nameNode != null) {if (contentNode != null) {- String name = nameNode.getNodeValue().toLowerCase();- metaTags.getGeneralTags().setProperty(name, contentNode.getNodeValue());+ String name = nameNode.getNodeValue().toLowerCase();+ metaTags.getGeneralTags().add(name, contentNode.getNodeValue());if ("robots".equals(name)) {if (contentNode != null) {