2009-07-09 22:13:11 +02:00
|
|
|
//odtParser.java
|
2005-11-29 08:27:58 +01:00
|
|
|
//------------------------
|
|
|
|
//part of YaCy
|
2008-07-20 19:14:51 +02:00
|
|
|
//(C) by Michael Peter Christen; mc@yacy.net
|
2005-11-29 08:27:58 +01: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-11-29 08:27:58 +01: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-11-29 08:27:58 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Enumeration;
|
2008-09-20 00:19:11 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2005-11-29 08:27:58 +01:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
|
2009-07-18 17:04:34 +02:00
|
|
|
import javax.xml.parsers.SAXParser;
|
|
|
|
import javax.xml.parsers.SAXParserFactory;
|
2005-11-29 08:27:58 +01:00
|
|
|
|
2010-05-25 14:54:57 +02:00
|
|
|
import net.yacy.cora.document.MultiProtocolURI;
|
2011-03-11 00:25:07 +01:00
|
|
|
import net.yacy.cora.document.UTF8;
|
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-18 02:53:43 +02:00
|
|
|
import net.yacy.document.parser.xml.ODContentHandler;
|
|
|
|
import net.yacy.document.parser.xml.ODMetaHandler;
|
|
|
|
import net.yacy.kelondro.io.CharBuffer;
|
2009-10-10 03:14:19 +02:00
|
|
|
import net.yacy.kelondro.util.FileUtils;
|
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
public class odtParser extends AbstractParser implements Parser {
|
2005-11-29 08:27:58 +01:00
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
public odtParser() {
|
|
|
|
super("OASIS OpenDocument V2 Text Document Parser");
|
2009-07-14 13:01:05 +02:00
|
|
|
SUPPORTED_EXTENSIONS.add("odt");
|
|
|
|
SUPPORTED_EXTENSIONS.add("ods");
|
|
|
|
SUPPORTED_EXTENSIONS.add("odp");
|
2009-07-15 16:10:29 +02:00
|
|
|
SUPPORTED_EXTENSIONS.add("odg");
|
|
|
|
SUPPORTED_EXTENSIONS.add("odc");
|
|
|
|
SUPPORTED_EXTENSIONS.add("odf");
|
|
|
|
SUPPORTED_EXTENSIONS.add("odb");
|
|
|
|
SUPPORTED_EXTENSIONS.add("odi");
|
|
|
|
SUPPORTED_EXTENSIONS.add("odm");
|
|
|
|
SUPPORTED_EXTENSIONS.add("ott");
|
|
|
|
SUPPORTED_EXTENSIONS.add("ots");
|
|
|
|
SUPPORTED_EXTENSIONS.add("otp");
|
|
|
|
SUPPORTED_EXTENSIONS.add("otg");
|
2009-07-14 15:32:53 +02:00
|
|
|
SUPPORTED_EXTENSIONS.add("sxw"); // Star Office Writer file format
|
|
|
|
SUPPORTED_EXTENSIONS.add("sxc"); // Star Office Calc file format
|
2009-07-14 13:01:05 +02:00
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.text");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.spreadsheet");
|
2009-07-15 16:10:29 +02:00
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.presentation");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.graphics");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.chart");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.formula");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.database");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.image");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.text-master");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.text-template");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.spreadsheet-template");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.presentation-template");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/vnd.oasis.opendocument.graphics-template");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/x-vnd.oasis.opendocument.text");
|
2009-07-14 15:32:53 +02:00
|
|
|
SUPPORTED_MIME_TYPES.add("application/OOo-calc");
|
|
|
|
SUPPORTED_MIME_TYPES.add("application/OOo-writer");
|
2009-07-14 13:01:05 +02:00
|
|
|
}
|
|
|
|
|
2010-12-27 16:37:11 +01:00
|
|
|
private Document[] parse(final MultiProtocolURI location, final String mimeType,
|
|
|
|
final String charset, final File dest)
|
|
|
|
throws Parser.Failure, InterruptedException {
|
2005-11-29 08:27:58 +01:00
|
|
|
|
2010-06-26 12:32:47 +02:00
|
|
|
CharBuffer writer = null;
|
2005-11-29 08:27:58 +01:00
|
|
|
try {
|
|
|
|
String docDescription = null;
|
2007-03-18 13:33:19 +01:00
|
|
|
String docKeywordStr = null;
|
2006-09-18 02:37:02 +02:00
|
|
|
String docShortTitle = null;
|
|
|
|
String docLongTitle = null;
|
2007-03-18 13:33:19 +01:00
|
|
|
String docAuthor = null;
|
2008-09-20 00:19:11 +02:00
|
|
|
String docLanguage = null;
|
2005-11-29 08:27:58 +01:00
|
|
|
|
|
|
|
// opening the file as zip file
|
2010-06-29 21:20:45 +02:00
|
|
|
final ZipFile zipFile = new ZipFile(dest);
|
2008-08-02 14:12:04 +02:00
|
|
|
final Enumeration<? extends ZipEntry> zipEnum = zipFile.entries();
|
2009-07-18 17:04:34 +02:00
|
|
|
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
|
2005-11-29 08:27:58 +01:00
|
|
|
|
|
|
|
// looping through all containing files
|
|
|
|
while (zipEnum.hasMoreElements()) {
|
2006-09-03 16:59:00 +02:00
|
|
|
|
|
|
|
// getting the next zip file entry
|
2008-08-02 14:12:04 +02:00
|
|
|
final ZipEntry zipEntry= zipEnum.nextElement();
|
|
|
|
final String entryName = zipEntry.getName();
|
2005-11-29 08:27:58 +01:00
|
|
|
|
|
|
|
// content.xml contains the document content in xml format
|
|
|
|
if (entryName.equals("content.xml")) {
|
2006-10-03 14:00:26 +02:00
|
|
|
|
2010-04-15 00:17:18 +02:00
|
|
|
// create a writer for output
|
|
|
|
writer = new CharBuffer();
|
2006-10-03 14:00:26 +02:00
|
|
|
|
|
|
|
// extract data
|
2008-08-02 14:12:04 +02:00
|
|
|
final InputStream zipFileEntryStream = zipFile.getInputStream(zipEntry);
|
2009-07-18 17:04:34 +02:00
|
|
|
final SAXParser saxParser = saxParserFactory.newSAXParser();
|
|
|
|
saxParser.parse(zipFileEntryStream, new ODContentHandler(writer));
|
2005-11-29 08:27:58 +01:00
|
|
|
|
2006-10-03 14:00:26 +02:00
|
|
|
// close readers and writers
|
2009-07-18 17:04:34 +02:00
|
|
|
zipFileEntryStream.close();
|
2006-10-03 14:00:26 +02:00
|
|
|
writer.close();
|
|
|
|
|
2005-11-29 08:27:58 +01:00
|
|
|
} else if (entryName.equals("meta.xml")) {
|
2006-10-03 14:00:26 +02:00
|
|
|
// meta.xml contains metadata about the document
|
2008-08-02 14:12:04 +02:00
|
|
|
final InputStream zipFileEntryStream = zipFile.getInputStream(zipEntry);
|
2009-07-18 17:04:34 +02:00
|
|
|
final SAXParser saxParser = saxParserFactory.newSAXParser();
|
|
|
|
final ODMetaHandler metaData = new ODMetaHandler();
|
|
|
|
saxParser.parse(zipFileEntryStream, metaData);
|
2005-11-29 08:27:58 +01:00
|
|
|
docDescription = metaData.getDescription();
|
2007-03-18 13:33:19 +01:00
|
|
|
docKeywordStr = metaData.getKeyword();
|
2005-11-29 08:27:58 +01:00
|
|
|
docShortTitle = metaData.getTitle();
|
|
|
|
docLongTitle = metaData.getSubject();
|
2007-03-18 13:33:19 +01:00
|
|
|
docAuthor = metaData.getCreator();
|
2008-09-20 00:19:11 +02:00
|
|
|
docLanguage = metaData.getLanguage();
|
2005-11-29 08:27:58 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-03 14:00:26 +02:00
|
|
|
|
2008-09-20 00:19:11 +02:00
|
|
|
// make the languages set
|
2010-12-27 16:37:11 +01:00
|
|
|
final Set<String> languages = new HashSet<String>(1);
|
2008-09-20 00:19:11 +02:00
|
|
|
if (docLanguage != null) languages.add(docLanguage);
|
|
|
|
|
2006-10-03 14:00:26 +02:00
|
|
|
// if there is no title availabe we generate one
|
|
|
|
if (docLongTitle == null) {
|
|
|
|
if (docShortTitle != null) {
|
|
|
|
docLongTitle = docShortTitle;
|
|
|
|
}
|
|
|
|
}
|
2005-11-29 08:27:58 +01:00
|
|
|
|
2006-09-20 14:25:07 +02:00
|
|
|
// split the keywords
|
|
|
|
String[] docKeywords = null;
|
|
|
|
if (docKeywordStr != null) docKeywords = docKeywordStr.split(" |,");
|
|
|
|
|
|
|
|
// create the parser document
|
2010-06-29 21:20:45 +02:00
|
|
|
Document[] docs = null;
|
2011-03-11 00:25:07 +01:00
|
|
|
final byte[] contentBytes = UTF8.getBytes(writer.toString());
|
2010-06-29 21:20:45 +02:00
|
|
|
docs = new Document[]{new Document(
|
2010-06-26 12:32:47 +02:00
|
|
|
location,
|
|
|
|
mimeType,
|
|
|
|
"UTF-8",
|
|
|
|
languages,
|
|
|
|
docKeywords,
|
|
|
|
docLongTitle,
|
|
|
|
docAuthor,
|
|
|
|
"",
|
|
|
|
null,
|
|
|
|
docDescription,
|
|
|
|
contentBytes,
|
|
|
|
null,
|
|
|
|
null,
|
2010-08-25 20:24:54 +02:00
|
|
|
null,
|
2010-06-29 21:20:45 +02:00
|
|
|
false)};
|
|
|
|
return docs;
|
2008-08-02 14:12:04 +02: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;
|
2006-09-20 14:25:07 +02:00
|
|
|
|
2006-10-03 14:00:26 +02:00
|
|
|
// close the writer
|
2008-08-02 14:12:04 +02:00
|
|
|
if (writer != null) try { writer.close(); } catch (final Exception ex) {/* ignore this */}
|
2006-10-03 14:00:26 +02:00
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
throw new Parser.Failure("Unexpected error while parsing odt file. " + e.getMessage(),location);
|
2005-11-29 08:27:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
public Document[] parse(final MultiProtocolURI location, final String mimeType, final String charset, final InputStream source) throws Parser.Failure, InterruptedException {
|
2005-11-29 08:27:58 +01:00
|
|
|
File dest = null;
|
|
|
|
try {
|
|
|
|
// creating a tempfile
|
|
|
|
dest = File.createTempFile("OpenDocument", ".odt");
|
|
|
|
dest.deleteOnExit();
|
|
|
|
|
|
|
|
// copying the stream into a file
|
2009-01-31 02:06:56 +01:00
|
|
|
FileUtils.copy(source, dest);
|
2005-11-29 08:27:58 +01:00
|
|
|
|
|
|
|
// parsing the content
|
2006-09-15 14:52:46 +02:00
|
|
|
return parse(location, mimeType, charset, dest);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final Exception e) {
|
2006-09-20 14:25:07 +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;
|
2006-09-20 14:25:07 +02:00
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
throw new Parser.Failure("Unexpected error while parsing odt file. " + e.getMessage(),location);
|
2005-11-29 08:27:58 +01:00
|
|
|
} finally {
|
2009-03-30 17:31:25 +02:00
|
|
|
if (dest != null) FileUtils.deletedelete(dest);
|
2005-11-29 08:27:58 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-29 21:20:45 +02:00
|
|
|
|
2005-11-29 08:27:58 +01:00
|
|
|
}
|