Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-161 Change Plain text parser to use parser.character.encoding.default property for fall back encoding spotted by KuroSaka TeruHiko

Sami Siren26da6449c01b
diff --git a/CHANGES.txt b/CHANGES.txt
index 22ca9d6d9..ebf3205d6 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -19,6 +19,10 @@ Unreleased changes (1.0-dev)
7. NUTCH-483 - Remove redundant commons-logging jar from ontology plugin
(siren)
+
+ 8. NUTCH-161 - Change Plain text parser to
+ use parser.character.encoding.default property for fall back encoding
+ (KuroSaka TeruHiko, siren)
Release 0.9 - 2007-04-02
diff --git a/src/plugin/parse-text/src/java/org/apache/nutch/parse/text/TextParser.java b/src/plugin/parse-text/src/java/org/apache/nutch/parse/text/TextParser.java
index 136a73b2b..d974c850a 100644
--- a/src/plugin/parse-text/src/java/org/apache/nutch/parse/text/TextParser.java
+++ b/src/plugin/parse-text/src/java/org/apache/nutch/parse/text/TextParser.java
@@ -24,35 +24,42 @@ import org.apache.nutch.util.*;
import org.apache.hadoop.conf.Configuration;
public class TextParser implements Parser {
+
private Configuration conf;
+
+ /**
+ * Encoding to be used when character set isn't specified
+ * as HTTP header.
+ */
+ private String defaultEncoding;
+ /**
+ * Parses plain text document. This code uses configured default encoding
+ * {@code parser.character.encoding.default} if character set isn't specified
+ * as HTTP header. FIXME: implement charset detector
+ */
public ParseResult getParse(Content content) {
- // ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, "", new
- // Outlink[0], metadata);
-
String encoding = StringUtil.parseCharacterEncoding(content
.getContentType());
String text;
- if (encoding != null) { // found an encoding header
- try { // try to use named encoding
- text = new String(content.getContent(), encoding);
- } catch (java.io.UnsupportedEncodingException e) {
- return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf());
- }
- } else {
- // FIXME: implement charset detector. This code causes problem when
- // character set isn't specified in HTTP header.
- text = new String(content.getContent()); // use default encoding
+ try {
+ text = new String(content.getContent(), encoding != null ? encoding
+ : defaultEncoding);
+ } catch (java.io.UnsupportedEncodingException e) {
+ return new ParseStatus(e)
+ .getEmptyParseResult(content.getUrl(), getConf());
}
+
ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, "",
OutlinkExtractor.getOutlinks(text, getConf()), content.getMetadata());
parseData.setConf(this.conf);
return ParseResult.createParseResult(content.getUrl(), new ParseImpl(text, parseData));
-
}
public void setConf(Configuration conf) {
+ defaultEncoding = conf.get("parser.character.encoding.default",
+ "windows-1252");
this.conf = conf;
}