Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2545 Revert syntax correction to original implementation, add commons-rdf-api dependency

Lewis John McGibbney40e92a54ebb5
diff --git a/src/plugin/any23/ivy.xml b/src/plugin/any23/ivy.xml
index 3b59f1d36..0e65d931d 100644
--- a/src/plugin/any23/ivy.xml
+++ b/src/plugin/any23/ivy.xml
@@ -43,6 +43,7 @@
<exclude org="org.slf4j" name="slf4j-api" />
<exclude org="xerces" />
</dependency>
+ <dependency org="org.apache.commons" name="commons-rdf-api" rev="0.5.0" conf="*->default"/>
</dependencies>
</ivy-module>
diff --git a/src/plugin/any23/plugin.xml b/src/plugin/any23/plugin.xml
index 9d6c6088b..71c552215 100644
--- a/src/plugin/any23/plugin.xml
+++ b/src/plugin/any23/plugin.xml
@@ -50,6 +50,7 @@
<library name="commons-io-2.4.jar"/>
<library name="commons-lang-2.6.jar"/>
<library name="commons-logging-1.2.jar"/>
+ <library name="commons-rdf-api-0.5.0.jar"/>
<library name="curvesapi-1.04.jar"/>
<library name="cxf-core-3.0.16.jar"/>
<library name="cxf-rt-frontend-jaxrs-3.0.16.jar"/>
diff --git a/src/plugin/any23/src/java/org/apache/nutch/any23/Any23ParseFilter.java b/src/plugin/any23/src/java/org/apache/nutch/any23/Any23ParseFilter.java
index 95635bc78..fcbc9aa1e 100644
--- a/src/plugin/any23/src/java/org/apache/nutch/any23/Any23ParseFilter.java
+++ b/src/plugin/any23/src/java/org/apache/nutch/any23/Any23ParseFilter.java
@@ -18,6 +18,8 @@ package org.apache.nutch.any23;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.StringReader;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Set;
@@ -38,9 +40,14 @@ import org.apache.nutch.parse.HtmlParseFilter;
import org.apache.nutch.parse.Parse;
import org.apache.nutch.parse.ParseResult;
import org.apache.nutch.protocol.Content;
+import org.ccil.cowan.tagsoup.XMLWriter;
+import org.ccil.cowan.tagsoup.jaxp.SAXParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DocumentFragment;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
/**
* <p>This implementation of {@link org.apache.nutch.parse.HtmlParseFilter}
@@ -102,11 +109,18 @@ public class Any23ParseFilter implements HtmlParseFilter {
any23.setMIMETypeDetector(null);
try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // Fix input to avoid extraction error (https://github.com/semarglproject/semargl/issues/37#issuecomment-69381281)
+ XMLReader reader = SAXParserImpl.newInstance(null).getXMLReader();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ XMLWriter writer = new XMLWriter(new OutputStreamWriter(baos));
+ reader.setContentHandler(writer);
+ reader.parse(new InputSource(new StringReader(htmlContent)));
+ String input = new String(baos.toByteArray(), Charset.forName("UTF-8"));
+ baos = new ByteArrayOutputStream();
TripleHandler tHandler = new NTriplesWriter(baos);
BenchmarkTripleHandler bHandler = new BenchmarkTripleHandler(tHandler);
try {
- any23.extract(htmlContent, url, contentType, "UTF-8", bHandler);
+ any23.extract(input, url, contentType, "UTF-8", bHandler);
} catch (IOException e) {
LOG.error("Error while reading the source", e);
} catch (ExtractionException e) {
@@ -121,6 +135,8 @@ public class Any23ParseFilter implements HtmlParseFilter {
String n3 = baos.toString("UTF-8");
String[] triplesStrings = n3.split("\n");
Collections.addAll(triples, triplesStrings);
+ } catch (SAXException e) {
+ LOG.error("Unexpected SAXException", e);
} catch (IOException e) {
LOG.error("Unexpected IOException", e);
}