Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2359 Parsefilter-regex raises IndexOutOfBoundsException when rules are ill-formed

Markus Jelsma9a9c4b32b9c1
diff --git a/src/plugin/parsefilter-regex/README.txt b/src/plugin/parsefilter-regex/README.txt
new file mode 100644
index 000000000..1fac05fcd
--- /dev/null
+++ b/src/plugin/parsefilter-regex/README.txt
@@ -0,0 +1,41 @@
+Parsefilter-regex plugin
+
+Allow parsing and set custom defined fields using regex. Rules can be defined
+in a separate rule file or in the nutch configuration.
+
+If a rule file is used, should create a text file regex-parsefilter.txt (which
+is the default name of the rules file). To use a different filename, either
+update the file value in plugin’s build.xml or add parsefilter.regex.file
+config to the nutch config.
+
+ie:
+ <property>
+ <name>parsefilter.regex.file</name>
+ <value>
+ /path/to/rulefile
+ </value>
+ </property
+
+
+Format of rules: <name>\t<source>\t<regex>\n
+
+ie:
+ my_first_field html h1
+ my_second_field text my_pattern
+
+
+If a rule file is not used, rules can be directly set in the nutch config:
+
+ie:
+ <property>
+ <name>parsefilter.regex.rules</name>
+ <value>
+ my_first_field html h1
+ my_second_field text my_pattern
+ </value>
+ </property
+
+source can be either html or text. If source is html, the regex is applied to
+the entire HTML tree. If source is text, the regex is applied to the
+extracted text.
+
diff --git a/src/plugin/parsefilter-regex/src/java/org/apache/nutch/parsefilter/regex/RegexParseFilter.java b/src/plugin/parsefilter-regex/src/java/org/apache/nutch/parsefilter/regex/RegexParseFilter.java
index 695516668..f799e5f48 100644
--- a/src/plugin/parsefilter-regex/src/java/org/apache/nutch/parsefilter/regex/RegexParseFilter.java
+++ b/src/plugin/parsefilter-regex/src/java/org/apache/nutch/parsefilter/regex/RegexParseFilter.java
@@ -179,13 +179,17 @@ public class RegexParseFilter implements HtmlParseFilter {
while ((line = reader.readLine()) != null) {
if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
line = line.trim();
- String[] parts = line.split("\t");
-
- String field = parts[0].trim();
- String source = parts[1].trim();
- String regex = parts[2].trim();
-
- rules.put(field, new RegexRule(source, regex));
+ String[] parts = line.split("\\s");
+
+ if (parts.length == 3) {
+ String field = parts[0].trim();
+ String source = parts[1].trim();
+ String regex = parts[2].trim();
+
+ rules.put(field, new RegexRule(source, regex));
+ } else {
+ LOG.info("RegexParseFilter rule is invalid. " + line);
+ }
}
}
}