2012-06-11 16:48:53 +02:00
|
|
|
//rtfParser.java
|
2005-05-17 10:25:04 +02:00
|
|
|
//------------------------
|
|
|
|
//part of YaCy
|
2008-07-20 19:14:51 +02:00
|
|
|
//(C) by Michael Peter Christen; mc@yacy.net
|
2005-05-17 10:25:04 +02:00
|
|
|
//first published on http://www.anomic.de
|
|
|
|
//Frankfurt, Germany, 2005
|
|
|
|
//
|
|
|
|
//this file is contributed by Martin Thelian
|
2009-07-09 22:13:11 +02:00
|
|
|
//
|
|
|
|
// $LastChangedDate$
|
|
|
|
// $LastChangedRevision$
|
|
|
|
// $LastChangedBy$
|
2005-05-17 10:25:04 +02:00
|
|
|
//
|
|
|
|
//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
|
|
|
|
|
2009-10-18 02:53:43 +02:00
|
|
|
package net.yacy.document.parser;
|
2005-05-17 10:25:04 +02:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
2013-09-10 10:31:57 +02:00
|
|
|
import java.util.Date;
|
2009-07-14 13:01:05 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
import javax.swing.text.DefaultStyledDocument;
|
|
|
|
import javax.swing.text.rtf.RTFEditorKit;
|
|
|
|
|
2013-09-15 23:27:04 +02:00
|
|
|
import net.yacy.cora.document.id.AnchorURL;
|
2009-10-18 02:53:43 +02:00
|
|
|
import net.yacy.document.AbstractParser;
|
|
|
|
import net.yacy.document.Document;
|
2010-06-29 21:20:45 +02:00
|
|
|
import net.yacy.document.Parser;
|
2009-10-11 02:12:19 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
public class rtfParser extends AbstractParser implements Parser {
|
2005-05-17 10:25:04 +02:00
|
|
|
|
2010-12-27 16:37:11 +01:00
|
|
|
public rtfParser() {
|
|
|
|
super("Rich Text Format Parser");
|
2012-06-11 16:48:53 +02:00
|
|
|
this.SUPPORTED_EXTENSIONS.add("rtf");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/rtf");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("text/richtext");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/rtf");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-rtf");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-soffice");
|
2010-12-27 16:37:11 +01:00
|
|
|
}
|
2005-05-17 10:25:04 +02:00
|
|
|
|
2012-08-31 10:30:43 +02:00
|
|
|
@Override
|
2013-09-15 23:27:04 +02:00
|
|
|
public Document[] parse(final AnchorURL location, final String mimeType,
|
2010-12-27 16:37:11 +01:00
|
|
|
final String charset, final InputStream source)
|
|
|
|
throws Parser.Failure, InterruptedException {
|
2005-05-17 10:25:04 +02:00
|
|
|
|
2012-06-11 16:48:53 +02:00
|
|
|
try {
|
2008-08-02 14:12:04 +02:00
|
|
|
final DefaultStyledDocument doc = new DefaultStyledDocument();
|
2012-06-11 16:48:53 +02:00
|
|
|
|
|
|
|
final RTFEditorKit theRtfEditorKit = new RTFEditorKit();
|
|
|
|
theRtfEditorKit.read(source, doc, 0);
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
final String bodyText = doc.getText(0, doc.getLength());
|
2012-06-11 16:48:53 +02:00
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
return new Document[]{new Document(
|
2005-05-17 10:25:04 +02:00
|
|
|
location,
|
|
|
|
mimeType,
|
2006-09-15 15:18:12 +02:00
|
|
|
"UTF-8",
|
2011-04-21 15:58:49 +02:00
|
|
|
this,
|
2005-05-17 10:25:04 +02:00
|
|
|
null,
|
2008-09-20 00:19:11 +02:00
|
|
|
null,
|
2012-08-31 10:30:43 +02:00
|
|
|
singleList(((bodyText.length() > 80)? bodyText.substring(0, 80):bodyText.trim()).
|
2005-05-23 10:24:15 +02:00
|
|
|
replaceAll("\r\n"," ").
|
|
|
|
replaceAll("\n"," ").
|
|
|
|
replaceAll("\r"," ").
|
2012-08-31 10:30:43 +02:00
|
|
|
replaceAll("\t"," ")),
|
2007-03-17 02:18:34 +01:00
|
|
|
"", // TODO: AUTHOR
|
2010-05-11 13:14:05 +02:00
|
|
|
"", // TODO: publisher
|
2005-05-17 10:25:04 +02:00
|
|
|
null,
|
|
|
|
null,
|
2012-06-11 16:48:53 +02:00
|
|
|
0.0f, 0.0f,
|
2012-07-04 21:15:10 +02:00
|
|
|
bodyText,
|
2005-05-17 10:25:04 +02:00
|
|
|
null,
|
2010-03-04 00:32:56 +01:00
|
|
|
null,
|
2010-08-25 20:24:54 +02:00
|
|
|
null,
|
2013-09-10 10:31:57 +02:00
|
|
|
false,
|
|
|
|
new Date())};
|
2010-12-27 16:37:11 +01:00
|
|
|
} catch (final Exception e) {
|
2006-09-03 16:59:00 +02:00
|
|
|
if (e instanceof InterruptedException) throw (InterruptedException) e;
|
2010-06-29 21:20:45 +02:00
|
|
|
if (e instanceof Parser.Failure) throw (Parser.Failure) e;
|
2012-06-11 16:48:53 +02:00
|
|
|
|
|
|
|
throw new Parser.Failure("Unexpected error while parsing rtf resource." + e.getMessage(),location);
|
2010-12-27 16:37:11 +01:00
|
|
|
}
|
|
|
|
}
|
2005-05-17 10:25:04 +02:00
|
|
|
|
|
|
|
}
|