001: //zipParser.java
002: //------------------------
003: //part of YaCy
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2005
007: //
008: //this file is contributed by Martin Thelian
009: //last major change: 16.05.2005
010: //
011: //This program is free software; you can redistribute it and/or modify
012: //it under the terms of the GNU General Public License as published by
013: //the Free Software Foundation; either version 2 of the License, or
014: //(at your option) any later version.
015: //
016: //This program is distributed in the hope that it will be useful,
017: //but WITHOUT ANY WARRANTY; without even the implied warranty of
018: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: //GNU General Public License for more details.
020: //
021: //You should have received a copy of the GNU General Public License
022: //along with this program; if not, write to the Free Software
023: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: //
025: //Using this software in any meaning (reading, learning, copying, compiling,
026: //running) means that you agree that the Author(s) is (are) not responsible
027: //for cost, loss of data or any harm that may be caused directly or indirectly
028: //by usage of this softare or this documentation. The usage of this software
029: //is on your own risk. The installation and usage (starting/running) of this
030: //software may allow other people or application to access your computer and
031: //any attached devices and is highly dependent on the configuration of the
032: //software which must be done by the user of the software; the author(s) is
033: //(are) also not responsible for proper configuration and usage of the
034: //software, even if provoked by documentation provided together with
035: //the software.
036: //
037: //Any changes to this file according to the GPL as documented in the file
038: //gpl.txt aside this file in the shipment you received can be done to the
039: //lines that follows this copyright notice here, but changes must not be
040: //done inside the copyright notive above. A re-distribution must contain
041: //the intact and unchanged copyright notice.
042: //Contributions and changes to the program code must be marked as such.
043:
044: package de.anomic.plasma.parser.odt;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.File;
048: import java.io.FileOutputStream;
049: import java.io.InputStream;
050: import java.io.OutputStreamWriter;
051: import java.io.Writer;
052: import java.util.Enumeration;
053: import java.util.Hashtable;
054: import java.util.zip.ZipEntry;
055: import java.util.zip.ZipFile;
056:
057: import com.catcode.odf.ODFMetaFileAnalyzer;
058: import com.catcode.odf.OpenDocumentMetadata;
059: import com.catcode.odf.OpenDocumentTextInputStream;
060:
061: import de.anomic.http.httpc;
062: import de.anomic.plasma.plasmaParserDocument;
063: import de.anomic.plasma.parser.AbstractParser;
064: import de.anomic.plasma.parser.Parser;
065: import de.anomic.plasma.parser.ParserException;
066: import de.anomic.server.serverCharBuffer;
067: import de.anomic.server.serverFileUtils;
068: import de.anomic.server.logging.serverLog;
069: import de.anomic.yacy.yacyURL;
070:
071: public class odtParser extends AbstractParser implements Parser {
072:
073: /**
074: * a list of mime types that are supported by this parser class
075: * @see #getSupportedMimeTypes()
076: */
077: public static final Hashtable<String, String> SUPPORTED_MIME_TYPES = new Hashtable<String, String>();
078: static {
079: SUPPORTED_MIME_TYPES.put(
080: "application/vnd.oasis.opendocument.text", "odt");
081: SUPPORTED_MIME_TYPES.put(
082: "application/x-vnd.oasis.opendocument.text", "odt");
083: }
084:
085: /**
086: * a list of library names that are needed by this parser
087: * @see Parser#getLibxDependences()
088: */
089: private static final String[] LIBX_DEPENDENCIES = new String[] { "odf_utils_05_11_29.jar" };
090:
091: public odtParser() {
092: super (LIBX_DEPENDENCIES);
093: this .parserName = "OASIS OpenDocument V2 Text Document Parser";
094: }
095:
096: public Hashtable<String, String> getSupportedMimeTypes() {
097: return SUPPORTED_MIME_TYPES;
098: }
099:
100: public plasmaParserDocument parse(yacyURL location,
101: String mimeType, String charset, File dest)
102: throws ParserException, InterruptedException {
103:
104: Writer writer = null;
105: File writerFile = null;
106: try {
107: String docDescription = null;
108: String docKeywordStr = null;
109: String docShortTitle = null;
110: String docLongTitle = null;
111: String docAuthor = null;
112:
113: // opening the file as zip file
114: ZipFile zipFile = new ZipFile(dest);
115: Enumeration<? extends ZipEntry> zipEnum = zipFile.entries();
116:
117: // looping through all containing files
118: while (zipEnum.hasMoreElements()) {
119: // check for interruption
120: checkInterruption();
121:
122: // getting the next zip file entry
123: ZipEntry zipEntry = (ZipEntry) zipEnum.nextElement();
124: String entryName = zipEntry.getName();
125:
126: // content.xml contains the document content in xml format
127: if (entryName.equals("content.xml")) {
128: long contentSize = zipEntry.getSize();
129:
130: // creating a writer for output
131: if ((contentSize == -1)
132: || (contentSize > Parser.MAX_KEEP_IN_MEMORY_SIZE)) {
133: writerFile = File.createTempFile("odtParser",
134: ".tmp");
135: writer = new OutputStreamWriter(
136: new FileOutputStream(writerFile),
137: "UTF-8");
138: } else {
139: writer = new serverCharBuffer();
140: }
141:
142: // extract data
143: InputStream zipFileEntryStream = zipFile
144: .getInputStream(zipEntry);
145: OpenDocumentTextInputStream odStream = new OpenDocumentTextInputStream(
146: zipFileEntryStream);
147: serverFileUtils.copy(odStream, writer, "UTF-8");
148:
149: // close readers and writers
150: odStream.close();
151: writer.close();
152:
153: } else if (entryName.equals("meta.xml")) {
154: // meta.xml contains metadata about the document
155: InputStream zipFileEntryStream = zipFile
156: .getInputStream(zipEntry);
157: ODFMetaFileAnalyzer metaAnalyzer = new ODFMetaFileAnalyzer();
158: OpenDocumentMetadata metaData = metaAnalyzer
159: .analyzeMetaData(zipFileEntryStream);
160: docDescription = metaData.getDescription();
161: docKeywordStr = metaData.getKeyword();
162: docShortTitle = metaData.getTitle();
163: docLongTitle = metaData.getSubject();
164: docAuthor = metaData.getCreator();
165: }
166: }
167:
168: // if there is no title availabe we generate one
169: if (docLongTitle == null) {
170: if (docShortTitle != null) {
171: docLongTitle = docShortTitle;
172: }
173: }
174:
175: // split the keywords
176: String[] docKeywords = null;
177: if (docKeywordStr != null)
178: docKeywords = docKeywordStr.split(" |,");
179:
180: // create the parser document
181: plasmaParserDocument theDoc = null;
182: if (writer instanceof serverCharBuffer) {
183: byte[] contentBytes = ((serverCharBuffer) writer)
184: .toString().getBytes("UTF-8");
185: theDoc = new plasmaParserDocument(location, mimeType,
186: "UTF-8", docKeywords, docLongTitle, docAuthor,
187: null, docDescription, contentBytes, null, null);
188: } else {
189: theDoc = new plasmaParserDocument(location, mimeType,
190: "UTF-8", docKeywords, docLongTitle, docAuthor,
191: null, docDescription, writerFile, null, null);
192: }
193: return theDoc;
194: } catch (Exception e) {
195: if (e instanceof InterruptedException)
196: throw (InterruptedException) e;
197: if (e instanceof ParserException)
198: throw (ParserException) e;
199:
200: // close the writer
201: if (writer != null)
202: try {
203: writer.close();
204: } catch (Exception ex) {/* ignore this */
205: }
206:
207: // delete the file
208: if (writerFile != null)
209: try {
210: writerFile.delete();
211: } catch (Exception ex) {/* ignore this */
212: }
213:
214: throw new ParserException(
215: "Unexpected error while parsing odt file. "
216: + e.getMessage(), location);
217: }
218: }
219:
220: public plasmaParserDocument parse(yacyURL location,
221: String mimeType, String charset, InputStream source)
222: throws ParserException, InterruptedException {
223: File dest = null;
224: try {
225: // creating a tempfile
226: dest = File.createTempFile("OpenDocument", ".odt");
227: dest.deleteOnExit();
228:
229: // copying the stream into a file
230: serverFileUtils.copy(source, dest);
231:
232: // parsing the content
233: return parse(location, mimeType, charset, dest);
234: } catch (Exception e) {
235: if (e instanceof InterruptedException)
236: throw (InterruptedException) e;
237: if (e instanceof ParserException)
238: throw (ParserException) e;
239:
240: throw new ParserException(
241: "Unexpected error while parsing odt file. "
242: + e.getMessage(), location);
243: } finally {
244: if (dest != null)
245: try {
246: dest.delete();
247: } catch (Exception e) {/* ignore this */
248: }
249: }
250: }
251:
252: public void reset() {
253: // Nothing todo here at the moment
254: super .reset();
255: }
256:
257: public static void main(String[] args) {
258: try {
259: if (args.length != 1)
260: return;
261:
262: // getting the content URL
263: yacyURL contentUrl = new yacyURL(args[0], null);
264:
265: // creating a new parser
266: odtParser testParser = new odtParser();
267:
268: // setting the parser logger
269: testParser.setLogger(new serverLog("PARSER.ODT"));
270:
271: // downloading the document content
272: byte[] content = httpc.singleGET(contentUrl, contentUrl
273: .getHost(), 10000, null, null, null, null);
274: ByteArrayInputStream input = new ByteArrayInputStream(
275: content);
276:
277: // parsing the document
278: testParser.parse(contentUrl,
279: "application/vnd.oasis.opendocument.text", null,
280: input);
281: } catch (Exception e) {
282: e.printStackTrace();
283: }
284: }
285: }
|