You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.9 KiB
111 lines
3.9 KiB
20 years ago
|
//docParser.java
|
||
|
//------------------------
|
||
|
//part of YaCy
|
||
17 years ago
|
//(C) by Michael Peter Christen; mc@yacy.net
|
||
20 years ago
|
//first published on http://www.anomic.de
|
||
|
//Frankfurt, Germany, 2005
|
||
|
//
|
||
|
//this file is contributed by Martin Thelian
|
||
16 years ago
|
//last major change: 24.04.2005
|
||
20 years ago
|
//
|
||
|
//This program is free software; you can redistribute it and/or modify
|
||
|
//it under the terms of the GNU General Public License as published by
|
||
|
//the Free Software Foundation; either version 2 of the License, or
|
||
|
//(at your option) any later version.
|
||
|
//
|
||
|
//This program is distributed in the hope that it will be useful,
|
||
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
//GNU General Public License for more details.
|
||
|
//
|
||
|
//You should have received a copy of the GNU General Public License
|
||
|
//along with this program; if not, write to the Free Software
|
||
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
20 years ago
|
|
||
16 years ago
|
package de.anomic.document.parser;
|
||
20 years ago
|
|
||
|
import java.io.InputStream;
|
||
20 years ago
|
import java.util.Hashtable;
|
||
|
|
||
16 years ago
|
import org.textmining.extraction.TextExtractor;
|
||
|
import org.textmining.extraction.word.WordTextExtractorFactory;
|
||
20 years ago
|
|
||
16 years ago
|
import de.anomic.document.AbstractParser;
|
||
|
import de.anomic.document.Parser;
|
||
|
import de.anomic.document.ParserException;
|
||
|
import de.anomic.document.Document;
|
||
18 years ago
|
import de.anomic.yacy.yacyURL;
|
||
20 years ago
|
|
||
18 years ago
|
public class docParser extends AbstractParser implements Parser {
|
||
20 years ago
|
|
||
|
/**
|
||
|
* a list of mime types that are supported by this parser class
|
||
20 years ago
|
* @see #getSupportedMimeTypes()
|
||
|
*/
|
||
17 years ago
|
public static final Hashtable<String, String> SUPPORTED_MIME_TYPES = new Hashtable<String, String>();
|
||
16 years ago
|
static { SUPPORTED_MIME_TYPES.put("application/msword","doc"); }
|
||
20 years ago
|
|
||
20 years ago
|
/**
|
||
|
* a list of library names that are needed by this parser
|
||
|
* @see Parser#getLibxDependences()
|
||
|
*/
|
||
|
private static final String[] LIBX_DEPENDENCIES = new String[] {
|
||
16 years ago
|
"tm-extractors-1.0.jar"
|
||
20 years ago
|
};
|
||
|
|
||
20 years ago
|
public docParser() {
|
||
20 years ago
|
super(LIBX_DEPENDENCIES);
|
||
19 years ago
|
this.parserName = "Word Document Parser";
|
||
20 years ago
|
}
|
||
|
|
||
16 years ago
|
public Document parse(final yacyURL location, final String mimeType, final String charset,
|
||
17 years ago
|
final InputStream source) throws ParserException, InterruptedException {
|
||
20 years ago
|
|
||
|
|
||
|
try {
|
||
16 years ago
|
final WordTextExtractorFactory extractorFactory = new WordTextExtractorFactory();
|
||
|
final TextExtractor extractor = extractorFactory.textExtractor(source);
|
||
16 years ago
|
final String contents = extractor.getText().trim();
|
||
|
String title = contents.replaceAll("\r"," ").replaceAll("\n"," ").replaceAll("\t"," ").trim();
|
||
|
if (title.length() > 80) title = title.substring(0, 80);
|
||
|
int l = title.length();
|
||
|
while (true) {
|
||
|
title = title.replaceAll(" ", " ");
|
||
|
if (title.length() == l) break;
|
||
|
l = title.length();
|
||
|
}
|
||
16 years ago
|
final Document theDoc = new Document(
|
||
20 years ago
|
location,
|
||
|
mimeType,
|
||
19 years ago
|
"UTF-8",
|
||
20 years ago
|
null,
|
||
17 years ago
|
null,
|
||
16 years ago
|
title,
|
||
18 years ago
|
"", // TODO: AUTHOR
|
||
20 years ago
|
null,
|
||
|
null,
|
||
19 years ago
|
contents.getBytes("UTF-8"),
|
||
20 years ago
|
null,
|
||
|
null);
|
||
|
|
||
|
return theDoc;
|
||
16 years ago
|
} catch (final Exception e) {
|
||
|
e.printStackTrace();
|
||
19 years ago
|
if (e instanceof InterruptedException) throw (InterruptedException) e;
|
||
19 years ago
|
if (e instanceof ParserException) throw (ParserException) e;
|
||
|
|
||
|
throw new ParserException("Unexpected error while parsing doc file. " + e.getMessage(),location);
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
17 years ago
|
public java.util.Hashtable<String, String> getSupportedMimeTypes() {
|
||
20 years ago
|
return docParser.SUPPORTED_MIME_TYPES;
|
||
|
}
|
||
|
|
||
|
public void reset() {
|
||
20 years ago
|
// Nothing todo here at the moment
|
||
19 years ago
|
super.reset();
|
||
20 years ago
|
}
|
||
|
|
||
|
}
|