Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Make use of lib-parsems in word, powerpoint and excel parsers

Jerome Charron207b1e555016
diff --git a/src/plugin/parse-msexcel/build.xml b/src/plugin/parse-msexcel/build.xml
index 246afa2e9..1b888991e 100644
--- a/src/plugin/parse-msexcel/build.xml
+++ b/src/plugin/parse-msexcel/build.xml
@@ -2,19 +2,23 @@
<project name="parse-msexcel" default="jar">
- <import file="../build-plugin.xml" />
+ <import file="../build-plugin.xml" />
<path id="plugin.deps">
<fileset dir="../lib-jakarta-poi/lib">
<include name="*.jar" />
</fileset>
+ <fileset dir="../../../build/lib-parsems">
+ <include name="*.jar" />
+ </fileset>
</path>
- <!-- for junit test -->
- <mkdir dir="${build.test}/data" />
- <copy todir="${build.test}/data">
- <fileset dir="sample">
- <include name="*.xls" />
- </fileset>
- </copy>
+ <!-- for junit test -->
+ <mkdir dir="${build.test}/data" />
+ <copy todir="${build.test}/data">
+ <fileset dir="sample">
+ <include name="*.xls" />
+ </fileset>
+ </copy>
+
</project>
diff --git a/src/plugin/parse-msexcel/plugin.xml b/src/plugin/parse-msexcel/plugin.xml
index 7df5d7da7..e396e5ba4 100644
--- a/src/plugin/parse-msexcel/plugin.xml
+++ b/src/plugin/parse-msexcel/plugin.xml
@@ -14,6 +14,7 @@
<requires>
<import plugin="nutch-extensionpoints"/>
<import plugin="lib-jakarta-poi"/>
+ <import plugin="lib-parsems"/>
</requires>
<extension id="org.apache.nutch.parse.msexcel"
diff --git a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/ExcelExtractor.java b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/ExcelExtractor.java
index eade22467..0b601140b 100644
--- a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/ExcelExtractor.java
+++ b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/ExcelExtractor.java
@@ -16,17 +16,17 @@
package org.apache.nutch.parse.msexcel;
// JDK imports
-import java.io.IOException;
import java.io.InputStream;
-import java.util.Date;
-import java.util.Properties;
// Jakarta POI imports
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.poifs.eventfilesystem.POIFSReader;
+
+// Nutch imports
+import org.apache.nutch.parse.ms.MSExtractor;
+
/**
* Excel Text and Properties extractor.
@@ -34,10 +34,10 @@ import org.apache.poi.poifs.eventfilesystem.POIFSReader;
* @author Rohit Kulkarni & Ashish Vaidya
* @author J&eacute;r&ocirc;me Charron
*/
-public class ExcelExtractor {
+class ExcelExtractor extends MSExtractor {
- public String extractText(InputStream input) throws IOException {
+ protected String extractText(InputStream input) throws Exception {
String resultText = "";
HSSFWorkbook wb = new HSSFWorkbook(input);
@@ -88,45 +88,5 @@ public class ExcelExtractor {
return resultText;
}
-
- public Properties extractProperties(InputStream input) throws IOException {
-
- PropertiesBroker propertiesBroker = new PropertiesBroker();
- POIFSReader reader = new POIFSReader();
- reader.registerListener(new PropertiesReaderListener(propertiesBroker),
- "\005SummaryInformation");
- reader.read(input);
- return propertiesBroker.getProperties();
- }
-
-
- class PropertiesBroker {
-
- private Properties properties;
- private int timeoutMillis = 2 * 1000;
-
-
- public synchronized Properties getProperties() {
-
- long start = new Date().getTime();
- long now = start;
-
- while ((properties == null) && (now-start < timeoutMillis)) {
- try {
- wait(timeoutMillis / 10);
- } catch (InterruptedException e) {}
- now = new Date().getTime();
- }
-
- notifyAll();
- return properties;
- }
-
- public synchronized void setProperties(Properties properties) {
- this.properties = properties;
- notifyAll();
- }
- }
-
}
diff --git a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/MSExcelParser.java b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/MSExcelParser.java
index 672615c8f..56435829d 100644
--- a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/MSExcelParser.java
+++ b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/MSExcelParser.java
@@ -15,111 +15,36 @@
*/
package org.apache.nutch.parse.msexcel;
-// JDK imports
-import java.io.ByteArrayInputStream;
-import java.util.Properties;
-import java.util.logging.Logger;
-
-// Hadoop imports
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.util.LogFormatter;
-
// Nutch imports
-import org.apache.nutch.metadata.DublinCore;
-import org.apache.nutch.metadata.Metadata;
-import org.apache.nutch.parse.Outlink;
-import org.apache.nutch.parse.OutlinkExtractor;
import org.apache.nutch.parse.Parse;
-import org.apache.nutch.parse.ParseData;
-import org.apache.nutch.parse.ParseImpl;
-import org.apache.nutch.parse.ParseStatus;
-import org.apache.nutch.parse.Parser;
+import org.apache.nutch.parse.ms.MSBaseParser;
import org.apache.nutch.protocol.Content;
+
/**
* An Excel document parser.
*
* @author Rohit Kulkarni & Ashish Vaidya
* @author J&eacute;r&ocirc;me Charron
*/
-public class MSExcelParser implements Parser {
-
- private Configuration conf;
-
- private static final Logger LOG = LogFormatter.getLogger(MSExcelParser.class.getName());
-
- /** Creates a new instance of MSExcelParser */
- public MSExcelParser() { }
-
- public Parse getParse(Content content) {
-
- String text = null;
- String title = null;
- Properties properties = null;
-
- try {
- byte[] raw = content.getContent();
- String contentLength = content.getMetadata().get(Metadata.CONTENT_LENGTH);
- if ((contentLength != null) &&
- (raw.length != Integer.parseInt(contentLength))) {
- return new ParseStatus(ParseStatus.FAILED,
- ParseStatus.FAILED_TRUNCATED,
- "Content truncated at " + raw.length +" bytes. " +
- "Parser can't handle incomplete msexcelfile.")
- .getEmptyParse(this.conf);
- }
-
- ExcelExtractor extractor = new ExcelExtractor();
- // Extract text
- text = extractor.extractText(new ByteArrayInputStream(raw));
- // Extract properties
- properties = extractor.extractProperties(new ByteArrayInputStream(raw));
-
- //currently returning empty outlinks array
- //outlinks = this.fetchOutlinks(resultText);
-
- } catch (Exception e) {
- return new ParseStatus(ParseStatus.FAILED,
- "Can't be handled as msexcel document. " + e)
- .getEmptyParse(this.conf);
- } finally {
- // nothing so far
- }
+public class MSExcelParser extends MSBaseParser {
- // collect meta data
- Metadata metadata = new Metadata();
- title = properties.getProperty(DublinCore.TITLE);
- properties.remove(DublinCore.TITLE);
- metadata.setAll(properties);
-
- if (text == null) { text = ""; }
- if (title == null) { title = ""; }
-
- // collect outlink
- Outlink[] outlinks = OutlinkExtractor.getOutlinks(text, this.conf);
-
- ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, title,
- outlinks, content.getMetadata(),
- metadata);
- parseData.setConf(this.conf);
- return new ParseImpl(text, parseData);
- }
+ /**
+ * Associated Mime type for Excel files
+ * (<code>application/vnd.ms-excel</code>).
+ */
+ public static final String MIME_TYPE = "application/vnd.ms-excel";
-
- /* ---------------------------- *
- * <implemenation:Configurable> *
- * ---------------------------- */
- public void setConf(Configuration conf) {
- this.conf = conf;
+ public Parse getParse(Content content) {
+ return getParse(new ExcelExtractor(), content);
}
- public Configuration getConf() {
- return this.conf;
+ /**
+ * Main for testing. Pass an excel document as argument
+ */
+ public static void main(String args[]) {
+ main(MIME_TYPE, new MSExcelParser(), args);
}
-
- /* ----------------------------- *
- * </implemenation:Configurable> *
- * ----------------------------- */
-
+
}
diff --git a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/PropertiesReaderListener.java b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/PropertiesReaderListener.java
deleted file mode 100644
index a3b45a9e9..000000000
--- a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/PropertiesReaderListener.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed 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.
- */
-package org.apache.nutch.parse.msexcel;
-
-// JDK imports
-import java.util.Date;
-import java.util.Properties;
-
-// Jakarta POI imports
-import org.apache.poi.hpsf.PropertySetFactory;
-import org.apache.poi.hpsf.SummaryInformation;
-import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
-import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
-
-// Nutch imports
-import org.apache.nutch.metadata.Metadata;
-import org.apache.nutch.parse.msexcel.ExcelExtractor.PropertiesBroker;
-
-
-/**
- * @author Rohit Kulkarni & Ashish Vaidya
- * @author J&eacute;r&ocirc;me Charron
- */
-public class PropertiesReaderListener implements POIFSReaderListener {
-
- private PropertiesBroker propertiesBroker;
- private Properties metaData = new Properties();
-
- public PropertiesReaderListener(PropertiesBroker propertiesBroker) {
- this.propertiesBroker = propertiesBroker;
- }
-
- public void processPOIFSReaderEvent(POIFSReaderEvent event) {
-
- SummaryInformation si = null;
- Properties properties = new Properties();
-
- try {
- si = (SummaryInformation)PropertySetFactory.create(event.getStream());
- } catch (Exception ex) {
- properties = null;
- }
-
- Date tmp = null;
-
- String title = si.getTitle();
- String applicationName = si.getApplicationName();
- String author = si.getAuthor();
- int charCount = si.getCharCount();
- String comments = si.getComments();
- Date createDateTime = si.getCreateDateTime();
- long editTime = si.getEditTime();
- String keywords = si.getKeywords();
- String lastAuthor = si.getLastAuthor();
- Date lastPrinted = si.getLastPrinted();
- Date lastSaveDateTime = si.getLastSaveDateTime();
- int pageCount = si.getPageCount();
- String revNumber = si.getRevNumber();
- int security = si.getSecurity();
- String subject = si.getSubject();
- String template = si.getTemplate();
- int wordCount = si.getWordCount();
-
- /*Dates are being stored in millis since the epoch to aid
- localization*/
- if(title != null)
- properties.setProperty(Metadata.TITLE, title);
- if(applicationName != null)
- properties.setProperty(Metadata.APPLICATION_NAME, applicationName);
- if(author != null)
- properties.setProperty(Metadata.AUTHOR, author);
- if(charCount != 0)
- properties.setProperty(Metadata.CHARACTER_COUNT, charCount + "");
- if(comments != null)
- properties.setProperty(Metadata.COMMENTS, comments);
- if(createDateTime != null)
- properties.setProperty(Metadata.DATE,
- Metadata.DATE_FORMAT.format(createDateTime));
- if(editTime != 0)
- properties.setProperty(Metadata.LAST_MODIFIED, editTime + "");
- if(keywords != null)
- properties.setProperty(Metadata.KEYWORDS, keywords);
- if(lastAuthor != null)
- properties.setProperty(Metadata.LAST_AUTHOR, lastAuthor);
- if(lastPrinted != null)
- properties.setProperty(Metadata.LAST_PRINTED, lastPrinted.getTime() + "");
- if(lastSaveDateTime != null)
- properties.setProperty(Metadata.LAST_SAVED, lastSaveDateTime.getTime() + "");
- if(pageCount != 0)
- properties.setProperty(Metadata.PAGE_COUNT, pageCount + "");
- if(revNumber != null)
- properties.setProperty(Metadata.REVISION_NUMBER, revNumber);
- if(security != 0)
- properties.setProperty(Metadata.RIGHTS, security + "");
- if(subject != null)
- properties.setProperty(Metadata.SUBJECT, subject);
- if(template != null)
- properties.setProperty(Metadata.TEMPLATE, template);
- if(wordCount != 0)
- properties.setProperty(Metadata.WORD_COUNT, wordCount + "");
- propertiesBroker.setProperties(properties);
- }
-
-}
diff --git a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/package.html b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/package.html
index a1916837f..8c6dcc166 100644
--- a/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/package.html
+++ b/src/plugin/parse-msexcel/src/java/org/apache/nutch/parse/msexcel/package.html
@@ -1,6 +1,6 @@
<html>
<body>
-<p>An Excel document parsing plugin.</p>
+<p>A Microsoft &copy; Excel document parsing plugin.</p>
<p>This package relies on Jakarta <a href="http://jakarta.apache.org/poi/index.html">POI</a>.</p>
</body>
</html>
diff --git a/src/plugin/parse-mspowerpoint/build.xml b/src/plugin/parse-mspowerpoint/build.xml
index 667a9cfe7..972c0bc99 100644
--- a/src/plugin/parse-mspowerpoint/build.xml
+++ b/src/plugin/parse-mspowerpoint/build.xml
@@ -8,6 +8,9 @@
<fileset dir="../lib-jakarta-poi/lib">
<include name="*.jar" />
</fileset>
+ <fileset dir="../../../build/lib-parsems">
+ <include name="*.jar" />
+ </fileset>
</path>
<!-- for junit test -->
diff --git a/src/plugin/parse-mspowerpoint/plugin.xml b/src/plugin/parse-mspowerpoint/plugin.xml
index 46694a8e8..e24f145af 100644
--- a/src/plugin/parse-mspowerpoint/plugin.xml
+++ b/src/plugin/parse-mspowerpoint/plugin.xml
@@ -14,6 +14,7 @@
<requires>
<import plugin="lib-jakarta-poi"/>
<import plugin="nutch-extensionpoints"/>
+ <import plugin="lib-parsems"/>
</requires>
<extension id="net.nutch.parse.mspowerpoint"
diff --git a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/MSPowerPointParser.java b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/MSPowerPointParser.java
index f67c11497..da76f8795 100644
--- a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/MSPowerPointParser.java
+++ b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/MSPowerPointParser.java
@@ -13,29 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.nutch.parse.mspowerpoint;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.util.Properties;
-import java.util.logging.Logger;
-
-import org.apache.nutch.parse.Outlink;
-import org.apache.nutch.parse.OutlinkExtractor;
+// Nutch imports
import org.apache.nutch.parse.Parse;
-import org.apache.nutch.parse.ParseData;
-import org.apache.nutch.parse.ParseImpl;
-import org.apache.nutch.parse.ParseStatus;
-import org.apache.nutch.parse.Parser;
+import org.apache.nutch.parse.ms.MSBaseParser;
import org.apache.nutch.protocol.Content;
-import org.apache.nutch.metadata.Metadata;
-import org.apache.nutch.net.protocols.Response;
-
-import org.apache.hadoop.util.LogFormatter;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.nutch.util.NutchConfiguration;
/**
@@ -45,133 +28,27 @@ import org.apache.nutch.util.NutchConfiguration;
* It is based on org.apache.poi.*.
*
* @author Stephan Strittmatter - http://www.sybit.de
+ * @author J&eacute;r&ocirc;me Charron
* @see <a href="http://jakarta.apache.org/poi">Jakarta POI</a>
- * @version 1.0
*/
-public class MSPowerPointParser implements Parser {
-
- /** associated Mime type for PowerPoint files (application/vnd.ms-powerpoint) */
- public static final String MIME_TYPE = "application/vnd.ms-powerpoint";
-
- private static final Logger LOG = LogFormatter
- .getLogger(MSPowerPointParser.class.getName());
-
- private Configuration conf;
+public class MSPowerPointParser extends MSBaseParser {
/**
- *
+ * Associated Mime type for PowerPoint files
+ * (<code>application/vnd.ms-powerpoint</code>).
*/
- public MSPowerPointParser() {
- }
-
- /**
- * Main for testing. Pass a ppt-file as argument
- *
- * @param args
- */
- public static void main(String args[]) {
- if (args.length < 1) {
- System.err.println("Useage:");
- System.err.println("\tMSPowerPointParser <file>");
- System.exit(1);
- }
-
- String file = args[0];
- MSPowerPointParser ppe = new MSPowerPointParser();
-
- byte[] raw = getRawBytes(new File(file));
-
- Metadata meta = new Metadata();
- meta.set(Response.CONTENT_LENGTH, "" + raw.length);
- Content content = new Content(file, file, raw, MIME_TYPE, meta, NutchConfiguration.create());
+ public static final String MIME_TYPE = "application/vnd.ms-powerpoint";
- System.out.println(ppe.getParse(content).getText());
- }
- /**
- * Parses the MS PowerPoint file.
- *
- * @see org.apache.nutch.parse.Parser#getParse(Content)
- */
public Parse getParse(final Content content) {
-
- String plainText = null;
- String title = null;
- Outlink[] outlinks = null;
- Properties properties = null;
-
- try {
- final String contentLen = content.getMetadata().get(Response.CONTENT_LENGTH);
- final byte[] raw = content.getContent();
-
- if (contentLen != null && raw.length != Integer.parseInt(contentLen)) {
- return new ParseStatus(
- ParseStatus.FAILED,
- ParseStatus.FAILED_TRUNCATED,
- "Content truncated at "
- + raw.length
- + " bytes. Please increase <protocol>.content.limit at nutch-default.xml. "
- + "Parser can't handle incomplete PowerPoint files.")
- .getEmptyParse(getConf());
- }
-
- final PPTExtractor extractor = new PPTExtractor(new ByteArrayInputStream(
- raw));
-
- plainText = extractor.getText();
- properties = extractor.getProperties();
- outlinks = OutlinkExtractor.getOutlinks(plainText, content.getUrl(), getConf());
-
- } catch (Exception e) {
- LOG.throwing(this.getClass().getName(), "getParse", e);
- return new ParseStatus(e).getEmptyParse(getConf());
- }
-
- Metadata metadata = new Metadata();
-
- if (properties != null) {
- title = properties.getProperty(Metadata.TITLE);
- properties.remove(Metadata.TITLE);
- metadata.setAll(properties);
- }
-
- if (plainText == null) {
- plainText = "";
- }
-
- if (title == null) {
- title = "";
- }
-
- final ParseStatus status = new ParseStatus(ParseStatus.SUCCESS);
- final ParseData parseData = new ParseData(status, title, outlinks, metadata);
- parseData.setConf(this.conf);
-
- LOG.finest("PowerPoint file parsed sucessful.");
- return new ParseImpl(plainText, parseData);
+ return getParse(new PPTExtractor(), content);
}
- private final static byte[] getRawBytes(File f) {
- try {
- if (!f.exists())
- return null;
- FileInputStream fin = new FileInputStream(f);
- byte[] buffer = new byte[(int) f.length()];
- fin.read(buffer);
- fin.close();
- return buffer;
- } catch (Exception err) {
- err.printStackTrace();
- return null;
- }
-
+ /**
+ * Main for testing. Pass a powerpoint document as argument
+ */
+ public static void main(String args[]) {
+ main(MIME_TYPE, new MSPowerPointParser(), args);
}
- public void setConf(Configuration conf) {
- this.conf = conf;
- }
-
- public Configuration getConf() {
- return this.conf;
- }
}
diff --git a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PPTExtractor.java b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PPTExtractor.java
index 340884a63..d53f5f2e4 100644
--- a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PPTExtractor.java
+++ b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PPTExtractor.java
@@ -13,141 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.nutch.parse.mspowerpoint;
+// JDK imports
import java.io.IOException;
import java.io.InputStream;
-import java.util.Date;
-import java.util.Properties;
-import java.util.logging.Logger;
-import org.apache.hadoop.util.LogFormatter;
-import org.apache.poi.hpsf.SummaryInformation;
+// Nutch imports
+import org.apache.nutch.parse.ms.MSExtractor;
+
+// Jakarta POI imports
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
+
/**
* Converts the Powerpoint document content to plain text.
*
* @author Stephan Strittmatter - http://www.sybit.de
- *
- * @version 1.0
+ * @author J&eacute;r&ocirc;me Charron
*/
-
-public class PPTExtractor {
-
- private static final Logger LOG = LogFormatter.getLogger(PPTExtractor.class
- .getName());
-
- /** Parsed plain Powerpoint Text */
- private final transient StringBuffer contentBuf;
-
- private final PropertiesBroker propertiesBroker;
-
- private final POIFSReader poireader;
-
- /**
- * Constructor that takes a PowerPoint file as <code>InputStream</code> to
- * parse it.
- *
- * @param in
- * <code>InputStream</code> containing the PowerPoint file
- * @throws PowerPointDocumentException
- * thrown if parsing failed
- */
- public PPTExtractor(final InputStream in) throws PowerPointDocumentException {
- this.poireader = new POIFSReader();
- this.propertiesBroker = new PropertiesBroker();
- this.contentBuf = new StringBuffer();
-
- this.init(in);
- }
-
- /**
- * Get the PowerPoint content text as plain text
- *
- * @return String the content text
- */
- public String getText() {
- return this.contentBuf.toString();
- }
-
- /**
- * Get the <code>Properties</code> of the PowerPoint document.
- *
- * @return the properties of the document
- */
- public Properties getProperties() {
- return this.propertiesBroker.getProperties();
- }
-
- /**
- * @param input
- * @throws PowerPointDocumentException
- */
- private void init(final InputStream input) throws PowerPointDocumentException {
- // register listener for SummaryInformation
- this.poireader.registerListener(new PropertiesReaderListener(
- this.propertiesBroker), SummaryInformation.DEFAULT_STREAM_NAME);
-
- // register listener for PPT-document content
- this.poireader.registerListener(new ContentReaderListener(this.contentBuf),
- PPTConstants.POWERPOINT_DOCUMENT);
-
- try {
- input.reset();
- if (input.available() > 0) {
- this.poireader.read(input);
- } else {
- LOG.warning("Input <=0 :" + input.available());
- }
- } catch (IOException e) {
- throw new PowerPointDocumentException(e);
+class PPTExtractor extends MSExtractor {
+
+ private StringBuffer text = null;
+ private POIFSReader reader = null;
+
+
+ protected String extractText(InputStream input) throws Exception {
+ this.reader = new POIFSReader();
+ this.text = new StringBuffer();
+ reader.registerListener(
+ new ContentReaderListener(this.text),
+ PPTConstants.POWERPOINT_DOCUMENT);
+ input.reset();
+ if (input.available() > 0) {
+ this.reader.read(input);
+ } else {
+ LOG.warning("Input <=0 :" + input.available());
}
+ return (this.text != null) ? text.toString() : null;
}
- /**
- * The PropertiesBroker
- *
- * @author Stephan Strittmatter
- * @version 1.0
- */
- static class PropertiesBroker {
-
- private final static int TIMEOUT = 2 * 1000;
-
- private Properties properties = null;
-
- /**
- * Get the collected properties.
- *
- * @return properties of the PowerPoint file
- */
- public synchronized Properties getProperties() {
-
- final long start = new Date().getTime();
- long now = start;
-
- while (this.properties == null && now - start < TIMEOUT) {
- try {
- wait(TIMEOUT / 10);
- } catch (InterruptedException e) {
- }
- now = new Date().getTime();
- }
-
- notifyAll();
-
- return this.properties;
- }
-
- /**
- *
- * @param properties
- */
- public synchronized void setProperties(Properties properties) {
- this.properties = properties;
- notifyAll();
- }
- }
}
diff --git a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PowerPointDocumentException.java b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PowerPointDocumentException.java
deleted file mode 100644
index 67852e061..000000000
--- a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PowerPointDocumentException.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed 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.
- */
-package org.apache.nutch.parse.mspowerpoint;
-
-
-/**
- * Exception class used for catching the runtime exceptions for the Powerpoint
- * slides.
- *
- * @author Stephan Strittmatter - http://www.sybit.de
- *
- * @version 1.0
- */
-
-public class PowerPointDocumentException extends Exception {
-
- /** Comment for <code>serialVersionUID</code> */
- private static final long serialVersionUID = 3256438093031487028L;
-
- /**
- * A constructor that builds the Exception object
- *
- * @param message
- */
- public PowerPointDocumentException(String message) {
- super(message);
- }
-
- /**
- * A constructor that builds the Exception object
- *
- * @param message
- * @param cause
- */
- public PowerPointDocumentException(String message, Throwable cause) {
- super(message, cause);
- }
-
- /**
- * @param e
- */
- public PowerPointDocumentException(Exception e) {
- super(e);
- }
-}
\ No newline at end of file
diff --git a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PropertiesReaderListener.java b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PropertiesReaderListener.java
deleted file mode 100644
index 05c3f4a84..000000000
--- a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/PropertiesReaderListener.java
+++ /dev/null
@@ -1,112 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed 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.
- */
-package org.apache.nutch.parse.mspowerpoint;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Properties;
-import java.util.TimeZone;
-import java.util.logging.Logger;
-
-import org.apache.nutch.parse.mspowerpoint.PPTExtractor.PropertiesBroker;
-import org.apache.hadoop.util.LogFormatter;
-import org.apache.nutch.metadata.Metadata;
-import org.apache.poi.hpsf.PropertySetFactory;
-import org.apache.poi.hpsf.SummaryInformation;
-import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
-import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
-
-/**
- * Listener for retrieving the properties of document.
- *
- * @author Stephan Strittmatter
- *
- * @version 1.0
- */
-class PropertiesReaderListener implements POIFSReaderListener {
- private static final Logger LOG = LogFormatter
- .getLogger(PropertiesReaderListener.class.getName());
-
- private static final String TIME_ZONE_ID = "GTM";
-
- private final transient PropertiesBroker propertiesBroker;
-
- /** DateFormatter for transfereing dates do strings. */
- private final transient SimpleDateFormat dateFormatter = new SimpleDateFormat();
-
- /** Properties of the powerpoint Document */
- private final transient Properties properties;
-
- /**
- * Listener for retrieving the properties of document.
- *
- * @param propertiesBroker
- */
- public PropertiesReaderListener(final PropertiesBroker propertiesBroker) {
- this.propertiesBroker = propertiesBroker;
- this.dateFormatter.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_ID));
- this.properties = new Properties();
- }
-
- /**
- * Process the properties of the document and adds them to property object.
- *
- * @param event
- * contains the document to be parsed
- */
- public void processPOIFSReaderEvent(POIFSReaderEvent event) {
-
- if (event.getName().startsWith(SummaryInformation.DEFAULT_STREAM_NAME)) {
-
- try {
- final SummaryInformation sInfo = (SummaryInformation) PropertySetFactory
- .create(event.getStream());
-
- addProperty(Metadata.TITLE, sInfo.getTitle());
- addProperty(Metadata.SUBJECT, sInfo.getSubject());
- addProperty(Metadata.KEYWORDS, sInfo.getKeywords());
- addProperty(Metadata.COMMENTS, sInfo.getComments());
- addProperty(Metadata.CREATOR, sInfo.getAuthor());
- addProperty(Metadata.LAST_AUTHOR, sInfo.getLastAuthor());
- } catch (Exception ex) {
- LOG.throwing(this.getClass().getName(), "processPOIFSReaderEvent", ex);
- }
-
- } else {
- LOG.warning("Wrong stream not processed: " + event.getName());
- }
-
- this.propertiesBroker.setProperties(this.properties);
- }
-
- protected void addProperty(final String name, final long value) {
- if (value != 0) {
- this.properties.setProperty(name, String.valueOf(value));
- }
- }
-
- protected void addProperty(final String name, final String value) {
- if (value != null) {
- this.properties.setProperty(name, value);
- }
- }
-
- protected void addProperty(final String name, final Date value) {
- if (value != null) {
- this.properties.setProperty(name, this.dateFormatter.format(value));
- }
- }
-}
diff --git a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/package.html b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/package.html
index f406d81af..33fb2e9aa 100644
--- a/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/package.html
+++ b/src/plugin/parse-mspowerpoint/src/java/org/apache/nutch/parse/mspowerpoint/package.html
@@ -21,8 +21,9 @@
</head>
<body>
<p>A Microsoft &copy; PowerPoint document parsing plugin.</p>
- <p>This package relies on <a
- href="http://www.apache.org/poi/index.html">POI</a>.</p>
+ <p>This package relies on Jakarta
+ <a href="http://jakarta.apache.org/poi/index.html">POI</a>.
+ </p>
<p> Implementation based on sources found at <a
href="http://groups.google.com/groups?selm=a4f8800541bc694d5af7dabb35e83b72%40localhost.talkaboutsoftware.com">Google
Groups </a>. It can also be found at <a
diff --git a/src/plugin/parse-msword/build.xml b/src/plugin/parse-msword/build.xml
index 06adcc076..7ad7894d0 100644
--- a/src/plugin/parse-msword/build.xml
+++ b/src/plugin/parse-msword/build.xml
@@ -8,6 +8,9 @@
<fileset dir="../lib-jakarta-poi/lib">
<include name="*.jar" />
</fileset>
+ <fileset dir="../../../build/lib-parsems">
+ <include name="*.jar" />
+ </fileset>
</path>
<!-- for junit test -->
diff --git a/src/plugin/parse-msword/plugin.xml b/src/plugin/parse-msword/plugin.xml
index d21e0d8de..f8270f2eb 100644
--- a/src/plugin/parse-msword/plugin.xml
+++ b/src/plugin/parse-msword/plugin.xml
@@ -14,6 +14,7 @@
<requires>
<import plugin="nutch-extensionpoints"/>
<import plugin="lib-jakarta-poi"/>
+ <import plugin="lib-parsems"/>
</requires>
<extension id="org.apache.nutch.parse.msword"
diff --git a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/FastSavedException.java b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/FastSavedException.java
index 6b89229ab..79f124d75 100644
--- a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/FastSavedException.java
+++ b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/FastSavedException.java
@@ -12,22 +12,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.nutch.parse.msword;
-/**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
-public class FastSavedException extends Exception
-{
- public FastSavedException(String msg)
- {
+public class FastSavedException extends Exception {
+
+ public FastSavedException(String msg) {
super(msg);
}
diff --git a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/MSWordParser.java b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/MSWordParser.java
index bbf843eec..45e423213 100644
--- a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/MSWordParser.java
+++ b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/MSWordParser.java
@@ -13,118 +13,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.nutch.parse.msword;
-import org.apache.nutch.metadata.DublinCore;
-import org.apache.nutch.metadata.Metadata;
-import org.apache.nutch.net.protocols.Response;
+// Nutch imports
import org.apache.nutch.protocol.Content;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.nutch.parse.ParseStatus;
-import org.apache.nutch.parse.Parser;
import org.apache.nutch.parse.Parse;
-import org.apache.nutch.parse.ParseData;
-import org.apache.nutch.parse.ParseImpl;
-import org.apache.nutch.parse.Outlink;
-import org.apache.nutch.parse.OutlinkExtractor;
-import org.apache.nutch.parse.ParseException;
+import org.apache.nutch.parse.ms.MSBaseParser;
-import java.util.Properties;
-import java.io.ByteArrayInputStream;
/**
- * parser for mime type application/msword.
+ * Parser for mime type application/msword.
* It is based on org.apache.poi.*. We have to see how well it performs.
*
* @author John Xing
- *
- * Note on 20040614 by Xing:
- * Some codes are stacked here for convenience (see inline comments).
- * They may be moved to more appropriate places when new codebase
- * stabilizes, especially after code for indexing is written.
- *
* @author Andy Hedges
- * code to extract all msword properties.
- *
+ * @author J&eacute;r&ocirc;me Charron
*/
-public class MSWordParser implements Parser {
- private Configuration conf;
-
-// public static final Logger LOG =
-// LogFormatter.getLogger("org.apache.nutch.parse.msword");
+public class MSWordParser extends MSBaseParser {
- public MSWordParser () {}
+ /**
+ * Associated Mime type for Word files
+ * (<code>application/msword</code>).
+ */
+ public static final String MIME_TYPE = "application/msword";
+
public Parse getParse(Content content) {
-
- String text = null;
- String title = null;
- Properties properties = null;
-
- try {
-
- byte[] raw = content.getContent();
-
- String contentLength = content.getMetadata().get(Response.CONTENT_LENGTH);
- if (contentLength != null
- && raw.length != Integer.parseInt(contentLength)) {
- return new ParseStatus(ParseStatus.FAILED, ParseStatus.FAILED_TRUNCATED,
- "Content truncated at " + raw.length
- +" bytes. Parser can't handle incomplete msword file.").getEmptyParse(this.conf);
- }
-
- WordExtractor extractor = new WordExtractor();
-
- // collect text
- text = extractor.extractText(new ByteArrayInputStream(raw));
-
- // collect meta info
- properties = extractor.extractProperties(new ByteArrayInputStream(raw));
-
- extractor = null;
-
- } catch (ParseException e) {
- return new ParseStatus(e).getEmptyParse(this.conf);
- } catch (FastSavedException e) {
- return new ParseStatus(e).getEmptyParse(this.conf);
- } catch (PasswordProtectedException e) {
- return new ParseStatus(e).getEmptyParse(this.conf);
- } catch (Exception e) { // run time exception
- return new ParseStatus(ParseStatus.FAILED,
- "Can't be handled as msword document. " + e).getEmptyParse(this.conf);
- } finally {
- // nothing so far
- }
-
- // collect meta data
- Metadata metadata = new Metadata();
- title = properties.getProperty(DublinCore.TITLE);
- properties.remove(DublinCore.TITLE);
- metadata.setAll(properties);
-
- if (text == null) { text = ""; }
- if (title == null) { title = ""; }
-
- // collect outlink
- Outlink[] outlinks = OutlinkExtractor.getOutlinks(text, this.conf);
-
- ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, title,
- outlinks, content.getMetadata(),
- metadata);
- parseData.setConf(this.conf);
- return new ParseImpl(text, parseData);
- // any filter?
- //return HtmlParseFilters.filter(content, parse, root);
- }
-
- public void setConf(Configuration conf) {
- this.conf = conf;
+ return getParse(new WordExtractor(), content);
}
- public Configuration getConf() {
- return this.conf;
+ /**
+ * Main for testing. Pass an word document as argument
+ */
+ public static void main(String args[]) {
+ main(MIME_TYPE, new MSWordParser(), args);
}
}
diff --git a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/PasswordProtectedException.java b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/PasswordProtectedException.java
index 93e037b01..424d36fb7 100644
--- a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/PasswordProtectedException.java
+++ b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/PasswordProtectedException.java
@@ -14,11 +14,10 @@
*/
package org.apache.nutch.parse.msword;
-public class PasswordProtectedException
- extends Exception
-{
- public PasswordProtectedException(String msg)
- {
+
+public class PasswordProtectedException extends Exception {
+
+ public PasswordProtectedException(String msg) {
super(msg);
}
diff --git a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/WordExtractor.java b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/WordExtractor.java
index 5bd69f3f6..35cc80210 100644
--- a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/WordExtractor.java
+++ b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/WordExtractor.java
@@ -14,42 +14,47 @@
*/
package org.apache.nutch.parse.msword;
-import org.apache.poi.hpsf.*;
-import org.apache.poi.hwpf.model.*;
-import org.apache.poi.hwpf.sprm.*;
-import org.apache.poi.poifs.eventfilesystem.*;
-import org.apache.poi.poifs.filesystem.*;
+// JDK imports
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+// Jakarta POI imports
+import org.apache.poi.hwpf.model.CHPBinTable;
+import org.apache.poi.hwpf.model.CHPX;
+import org.apache.poi.hwpf.model.ComplexFileTable;
+import org.apache.poi.hwpf.model.TextPiece;
+import org.apache.poi.hwpf.model.TextPieceTable;
+import org.apache.poi.hwpf.sprm.SprmIterator;
+import org.apache.poi.hwpf.sprm.SprmOperation;
+import org.apache.poi.poifs.filesystem.DocumentEntry;
+import org.apache.poi.poifs.filesystem.DocumentInputStream;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LittleEndian;
-import org.apache.nutch.metadata.Metadata;
-import java.util.*;
-import java.io.*;
+// Nutch imports
+import org.apache.nutch.parse.ms.MSExtractor;
+
/**
* This class extracts the text from a Word 6.0/95/97/2000/XP word doc
*
* @author Ryan Ackley
- *
* @author Andy Hedges
- * code to extract all msword properties.
+ * @author J&eacute;r&ocirc;me Charron
*
*/
-public class WordExtractor {
+class WordExtractor extends MSExtractor {
- /**
- * Constructor
- */
- public WordExtractor()
- {
- }
/**
* Gets the text from a Word document.
*
* @param in The InputStream representing the Word file.
*/
- public String extractText(InputStream in) throws Exception
- {
+ protected String extractText(InputStream in) throws Exception {
+
ArrayList text = new ArrayList();
POIFSFileSystem fsys = new POIFSFileSystem(in);
@@ -221,128 +226,5 @@ public class WordExtractor {
return false;
}
- public Properties extractProperties(InputStream in)
- throws IOException {
-
- PropertiesBroker propertiesBroker = new PropertiesBroker();
- POIFSReader reader = new POIFSReader();
- reader.registerListener(new PropertiesReaderListener(propertiesBroker),
- "\005SummaryInformation");
- reader.read(in);
- return propertiesBroker.getProperties();
- }
-
- class PropertiesReaderListener
- implements POIFSReaderListener {
-
- private PropertiesBroker propertiesBroker;
- private Properties metaData = new Properties();
-
- public PropertiesReaderListener(PropertiesBroker propertiesBroker) {
- this.propertiesBroker = propertiesBroker;
- }
-
- public void processPOIFSReaderEvent(POIFSReaderEvent event) {
-
- SummaryInformation si = null;
- Properties properties = new Properties();
-
- try {
- si = (SummaryInformation)PropertySetFactory.create(event.getStream());
- } catch (Exception ex) {
- properties = null;
- }
-
- Date tmp = null;
-
- String title = si.getTitle();
- String applicationName = si.getApplicationName();
- String author = si.getAuthor();
- int charCount = si.getCharCount();
- String comments = si.getComments();
- Date createDateTime = si.getCreateDateTime();
- long editTime = si.getEditTime();
- String keywords = si.getKeywords();
- String lastAuthor = si.getLastAuthor();
- Date lastPrinted = si.getLastPrinted();
- Date lastSaveDateTime = si.getLastSaveDateTime();
- int pageCount = si.getPageCount();
- String revNumber = si.getRevNumber();
- int security = si.getSecurity();
- String subject = si.getSubject();
- String template = si.getTemplate();
- int wordCount = si.getWordCount();
-
- /*Dates are being stored in millis since the epoch to aid
- localization*/
- if(title != null)
- properties.setProperty(Metadata.TITLE, title);
- if(applicationName != null)
- properties.setProperty(Metadata.APPLICATION_NAME, applicationName);
- if(author != null)
- properties.setProperty(Metadata.AUTHOR, author);
- if(charCount != 0)
- properties.setProperty(Metadata.CHARACTER_COUNT, charCount + "");
- if(comments != null)
- properties.setProperty(Metadata.COMMENTS, comments);
- if(createDateTime != null)
- properties.setProperty(Metadata.DATE,
- Metadata.DATE_FORMAT.format(createDateTime));
- if(editTime != 0)
- properties.setProperty(Metadata.LAST_MODIFIED, editTime + "");
- if(keywords != null)
- properties.setProperty(Metadata.KEYWORDS, keywords);
- if(lastAuthor != null)
- properties.setProperty(Metadata.LAST_AUTHOR, lastAuthor);
- if(lastPrinted != null)
- properties.setProperty(Metadata.LAST_PRINTED, lastPrinted.getTime() + "");
- if(lastSaveDateTime != null)
- properties.setProperty(Metadata.LAST_SAVED, lastSaveDateTime.getTime() + "");
- if(pageCount != 0)
- properties.setProperty(Metadata.PAGE_COUNT, pageCount + "");
- if(revNumber != null)
- properties.setProperty(Metadata.REVISION_NUMBER, revNumber);
- if(security != 0)
- properties.setProperty(Metadata.RIGHTS, security + "");
- if(subject != null)
- properties.setProperty(Metadata.SUBJECT, subject);
- if(template != null)
- properties.setProperty(Metadata.TEMPLATE, template);
- if(wordCount != 0)
- properties.setProperty(Metadata.WORD_COUNT, wordCount + "");
- propertiesBroker.setProperties(properties);
-
- //si.getThumbnail(); // can't think of a sensible way of turning this into a string.
- }
- }
-
- class PropertiesBroker {
-
- private Properties properties;
- private int timeoutMillis = 2 * 1000;
-
-
- public synchronized Properties getProperties() {
-
- long start = new Date().getTime();
- long now = start;
-
- while (properties == null && now - start < timeoutMillis) {
- try {
- wait(timeoutMillis / 10);
- } catch (InterruptedException e) {}
- now = new Date().getTime();
- }
-
- notifyAll();
-
- return properties;
- }
-
- public synchronized void setProperties(Properties properties) {
- this.properties = properties;
- notifyAll();
- }
- }
}
diff --git a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/package.html b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/package.html
index f0ea30520..b155c104a 100644
--- a/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/package.html
+++ b/src/plugin/parse-msword/src/java/org/apache/nutch/parse/msword/package.html
@@ -1,5 +1,6 @@
<html>
<body>
-<p>A Word document parsing plugin.</p><p>This package relies on <a href="http://jakarta.apache.org/poi/index.html">POI</a>.</p>
+<p>A Microsoft &copy; Word document parsing plugin.</p>
+<p>This package relies on <a href="http://jakarta.apache.org/poi/index.html">POI</a>.</p>
</body>
</html>