Apache Nutch is an extensible and scalable web crawler
NUTCH-2192 NUTCH-1678 NUTCH-1014 NUTCH-1021 Migrate from Apache ORO to java.util.regex - apply Markus' patch of NUTCH-2192 - finish migration of parse-js - remove oro dependency - correct pointer to Java regex syntax (instead of "Perl5") NUTCH-1063 OutlinkExtractor test generates an exception but does not fail - fixed by adding null-check (required by java.util.regex classes)
Sebastian Nagelc532c4e4861c
diff --git a/LICENSE.txt b/LICENSE.txtindex 1b7a967d2..9badcdad6 100644--- a/LICENSE.txt+++ b/LICENSE.txt@@ -1079,62 +1079,6 @@ http://www.python.org. Full license is here:http://www.python.org/download/releases/2.4.2/license/-lib/jakarta-oro-2.0.8.jar--/* ====================================================================- * The Apache Software License, Version 1.1- *- * Copyright (c) 2000-2002 The Apache Software Foundation. All rights- * reserved.- *- * Redistribution and use in source and binary forms, with or without- * modification, are permitted provided that the following conditions- * are met:- *- * 1. Redistributions of source code must retain the above copyright- * notice, this list of conditions and the following disclaimer.- *- * 2. Redistributions in binary form must reproduce the above copyright- * notice, this list of conditions and the following disclaimer in- * the documentation and/or other materials provided with the- * distribution.- *- * 3. The end-user documentation included with the redistribution,- * if any, must include the following acknowledgment:- * "This product includes software developed by the- * Apache Software Foundation (http://www.apache.org/)."- * Alternately, this acknowledgment may appear in the software itself,- * if and wherever such third-party acknowledgments normally appear.- *- * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"- * must not be used to endorse or promote products derived from this- * software without prior written permission. For written- * permission, please contact [email protected].- *- * 5. Products derived from this software may not be called "Apache"- * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their- * name, without prior written permission of the Apache Software Foundation.- *- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF- * SUCH DAMAGE.- * ====================================================================- *- * This software consists of voluntary contributions made by many- * individuals on behalf of the Apache Software Foundation. For more- * information on the Apache Software Foundation, please see- * <http://www.apache.org/>.- */-lib/jetty-ext/commons-el.jar/*diff --git a/conf/parse-plugins.xml b/conf/parse-plugins.xmlindex 20c8724a9..2507976ec 100644--- a/conf/parse-plugins.xml+++ b/conf/parse-plugins.xml@@ -43,6 +43,10 @@<plugin id="parse-zip" /></mimeType>+ <mimeType name="application/javascript">+ <plugin id="parse-js" />+ </mimeType>+<mimeType name="application/x-javascript"><plugin id="parse-js" /></mimeType>diff --git a/conf/regex-normalize.xml.template b/conf/regex-normalize.xml.templateindex ec60c1081..ed3f7108a 100644--- a/conf/regex-normalize.xml.template+++ b/conf/regex-normalize.xml.template@@ -17,7 +17,8 @@--><!-- This is the configuration file for the RegexUrlNormalize Class.This is intended so that users can specify substitutions to be- done on URLs. The regex engine that is used is Perl5 compatible.+ done on URLs using the Java regex syntax, see+ https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.htmlThe rules are applied to URLs in the order they occur in this file. --><!-- WATCH OUT: an xml parser reads this file an ampersands must bediff --git a/ivy/ivy.xml b/ivy/ivy.xmlindex 112975ab9..5272de6cb 100644--- a/ivy/ivy.xml+++ b/ivy/ivy.xml@@ -70,7 +70,6 @@<dependency org="xerces" name="xercesImpl" rev="2.11.0" /><dependency org="xerces" name="xmlParserAPIs" rev="2.6.2" />- <dependency org="oro" name="oro" rev="2.0.8" /><dependency org="com.google.guava" name="guava" rev="25.0-jre" />diff --git a/src/java/org/apache/nutch/parse/OutlinkExtractor.java b/src/java/org/apache/nutch/parse/OutlinkExtractor.javaindex ce9a614e7..c0b61d489 100644--- a/src/java/org/apache/nutch/parse/OutlinkExtractor.java+++ b/src/java/org/apache/nutch/parse/OutlinkExtractor.java@@ -21,19 +21,14 @@ import java.lang.invoke.MethodHandles;import java.net.MalformedURLException;import java.util.ArrayList;import java.util.List;+import java.util.regex.Matcher;+import java.util.regex.Pattern;++import org.apache.hadoop.conf.Configuration;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-import org.apache.hadoop.conf.Configuration;-import org.apache.oro.text.regex.MatchResult;-import org.apache.oro.text.regex.Pattern;-import org.apache.oro.text.regex.PatternCompiler;-import org.apache.oro.text.regex.PatternMatcher;-import org.apache.oro.text.regex.PatternMatcherInput;-import org.apache.oro.text.regex.Perl5Compiler;-import org.apache.oro.text.regex.Perl5Matcher;-/*** Extractor to extract {@link org.apache.nutch.parse.Outlink}s / URLs from* plain text using Regular Expressions.@@ -60,7 +55,8 @@ public class OutlinkExtractor {* </a>*/- private static final String URL_PATTERN = "([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)";+ private static final Pattern URL_PATTERN = Pattern.compile(+ "([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)");/*** Extracts <code>Outlink</code> from given plain text. Applying this method@@ -72,7 +68,8 @@ public class OutlinkExtractor {** @return Array of <code>Outlink</code>s within found in plainText*/- public static Outlink[] getOutlinks(final String plainText, Configuration conf) {+ public static Outlink[] getOutlinks(final String plainText,+ Configuration conf) {return OutlinkExtractor.getOutlinks(plainText, "", conf);}@@ -89,23 +86,20 @@ public class OutlinkExtractor {*/public static Outlink[] getOutlinks(final String plainText, String anchor,Configuration conf) {++ if (plainText == null) {+ return new Outlink[0];+ }+long start = System.currentTimeMillis();final List<Outlink> outlinks = new ArrayList<>();try {- final PatternCompiler cp = new Perl5Compiler();- final Pattern pattern = cp.compile(URL_PATTERN,- Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK- | Perl5Compiler.MULTILINE_MASK);- final PatternMatcher matcher = new Perl5Matcher();-- final PatternMatcherInput input = new PatternMatcherInput(plainText);-- MatchResult result;+ Matcher matcher = URL_PATTERN.matcher(plainText);String url;- // loop the matches- while (matcher.contains(input, pattern)) {+ // Check for stuff!+ while (matcher.find()) {// if this is taking too long, stop matching// (SHOULD really check cpu time used so that heavily loaded systems// do not unnecessarily hit this limit.)@@ -115,8 +109,9 @@ public class OutlinkExtractor {}break;}- result = matcher.getMatch();- url = result.group(0);++ url = matcher.group().trim();+try {outlinks.add(new Outlink(url, anchor));} catch (MalformedURLException mue) {diff --git a/src/plugin/parse-js/src/java/org/apache/nutch/parse/js/JSParseFilter.java b/src/plugin/parse-js/src/java/org/apache/nutch/parse/js/JSParseFilter.javaindex 7f6d10543..0d5f6bb6a 100644--- a/src/plugin/parse-js/src/java/org/apache/nutch/parse/js/JSParseFilter.java+++ b/src/plugin/parse-js/src/java/org/apache/nutch/parse/js/JSParseFilter.java@@ -26,9 +26,8 @@ import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.List;--import org.slf4j.Logger;-import org.slf4j.LoggerFactory;+import java.util.regex.Matcher;+import java.util.regex.Pattern;import org.apache.nutch.parse.HTMLMetaTags;import org.apache.nutch.parse.HtmlParseFilter;@@ -43,19 +42,15 @@ import org.apache.nutch.parse.Parser;import org.apache.nutch.protocol.Content;import org.apache.nutch.util.NutchConfiguration;import org.apache.hadoop.conf.Configuration;-import org.apache.oro.text.regex.MatchResult;-import org.apache.oro.text.regex.Pattern;-import org.apache.oro.text.regex.PatternCompiler;-import org.apache.oro.text.regex.PatternMatcher;-import org.apache.oro.text.regex.PatternMatcherInput;-import org.apache.oro.text.regex.Perl5Compiler;-import org.apache.oro.text.regex.Perl5Matcher;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;+/*** This class is a heuristic link extractor for JavaScript files and code* snippets. The general idea of a two-pass regex matching comes from Heritrix.@@ -155,12 +150,6 @@ public class JSParseFilter implements HtmlParseFilter, Parser {}public ParseResult getParse(Content c) {- String type = c.getContentType();- if (type != null && !type.trim().equals("")- && !type.toLowerCase().startsWith("application/x-javascript"))- return new ParseStatus(ParseStatus.FAILED_INVALID_FORMAT,- "Content not JavaScript: '" + type + "'").getEmptyParseResult(- c.getUrl(), getConf());String script = new String(c.getContent());Outlink[] outlinks = getJSLinks(script, "", c.getUrl());if (outlinks == null)@@ -181,9 +170,13 @@ public class JSParseFilter implements HtmlParseFilter, Parser {return ParseResult.createParseResult(c.getUrl(), new ParseImpl(script, pd));}- private static final String STRING_PATTERN = "(\\\\*(?:\"|\'))([^\\s\"\']+?)(?:\\1)";+ private static final Pattern STRING_PATTERN = Pattern.compile(+ "(\\\\*(?:\"|\'))([^\\s\"\']+?)(?:\\1)",+ Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);// A simple pattern. This allows also invalid URL characters.- private static final String URI_PATTERN = "(^|\\s*?)/?\\S+?[/\\.]\\S+($|\\s*)";+ private static final Pattern URI_PATTERN = Pattern.compile(+ "(^|\\s*?)/?\\S+?[/\\.]\\S+($|\\s*)",+ Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);// Alternative pattern, which limits valid url characters.// private static final String URI_PATTERN =@@ -206,29 +199,15 @@ public class JSParseFilter implements HtmlParseFilter, Parser {}try {- final PatternCompiler cp = new Perl5Compiler();- final Pattern pattern = cp.compile(STRING_PATTERN,- Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK- | Perl5Compiler.MULTILINE_MASK);- final Pattern pattern1 = cp.compile(URI_PATTERN,- Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK- | Perl5Compiler.MULTILINE_MASK);- final PatternMatcher matcher = new Perl5Matcher();- final PatternMatcher matcher1 = new Perl5Matcher();- final PatternMatcherInput input = new PatternMatcherInput(plainText);+ Matcher matcher = STRING_PATTERN.matcher(plainText);- MatchResult result;String url;- // loop the matches- while (matcher.contains(input, pattern)) {- result = matcher.getMatch();- url = result.group(2);- PatternMatcherInput input1 = new PatternMatcherInput(url);- if (!matcher1.matches(input1, pattern1)) {- // if (LOG.isTraceEnabled()) { LOG.trace(" - invalid '" + url + "'");- // }+ while (matcher.find()) {+ url = matcher.group(2);+ Matcher matcherUri = URI_PATTERN.matcher(url);+ if (!matcherUri.matches()) {continue;}if (url.startsWith("www.")) {diff --git a/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xml b/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xmlindex 4d6eabcf2..3d1f7186c 100644--- a/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xml+++ b/src/plugin/urlnormalizer-regex/sample/regex-normalize-default.xml@@ -1,7 +1,24 @@<?xml version="1.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.+--><!-- This is the configuration file for the RegexUrlNormalize Class.This is intended so that users can specify substitutions to be- done on URLs. The regex engine that is used is Perl5 compatible.+ done on URLs using the Java regex syntax, see+ https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.htmlThe rules are applied to URLs in the order they occur in this file. --><!-- WATCH OUT: an xml parser reads this file an ampersands must bediff --git a/src/plugin/urlnormalizer-regex/sample/regex-normalize-scope1.xml b/src/plugin/urlnormalizer-regex/sample/regex-normalize-scope1.xmlindex 369896878..fc8e05e2d 100644--- a/src/plugin/urlnormalizer-regex/sample/regex-normalize-scope1.xml+++ b/src/plugin/urlnormalizer-regex/sample/regex-normalize-scope1.xml@@ -1,7 +1,24 @@<?xml version="1.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.+--><!-- This is the configuration file for the RegexUrlNormalize Class.This is intended so that users can specify substitutions to be- done on URLs. The regex engine that is used is Perl5 compatible.+ done on URLs using the Java regex syntax, see+ https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.htmlThe rules are applied to URLs in the order they occur in this file. --><!-- WATCH OUT: an xml parser reads this file an ampersands must be