001: //bzipParser.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.bzip;
045:
046: import java.io.File;
047: import java.io.FileOutputStream;
048: import java.io.InputStream;
049: import java.util.Hashtable;
050:
051: import org.apache.tools.bzip2.CBZip2InputStream;
052:
053: import de.anomic.plasma.plasmaParser;
054: import de.anomic.plasma.plasmaParserDocument;
055: import de.anomic.plasma.parser.AbstractParser;
056: import de.anomic.plasma.parser.Parser;
057: import de.anomic.plasma.parser.ParserException;
058: import de.anomic.yacy.yacyURL;
059:
060: public class bzipParser extends AbstractParser implements Parser {
061:
062: /**
063: * a list of mime types that are supported by this parser class
064: * @see #getSupportedMimeTypes()
065: */
066: public static final Hashtable<String, String> SUPPORTED_MIME_TYPES = new Hashtable<String, String>();
067: static String fileExtensions = "bz2,tbz,tbz2";
068: static {
069: SUPPORTED_MIME_TYPES.put("application/x-bzip2", fileExtensions);
070: SUPPORTED_MIME_TYPES.put("application/bzip2", fileExtensions);
071: SUPPORTED_MIME_TYPES.put("application/x-bz2", fileExtensions);
072: }
073:
074: /**
075: * a list of library names that are needed by this parser
076: * @see Parser#getLibxDependences()
077: */
078: private static final String[] LIBX_DEPENDENCIES = new String[] { "bzip2.jar" };
079:
080: public bzipParser() {
081: super (LIBX_DEPENDENCIES);
082: this .parserName = "Bzip 2 UNIX Compressed File Parser";
083: }
084:
085: public Hashtable<String, String> getSupportedMimeTypes() {
086: return SUPPORTED_MIME_TYPES;
087: }
088:
089: public plasmaParserDocument parse(yacyURL location,
090: String mimeType, String charset, InputStream source)
091: throws ParserException, InterruptedException {
092:
093: File tempFile = null;
094: try {
095: /*
096: * First we have to consume the first two char from the stream. Otherwise
097: * the bzip decompression will fail with a nullpointerException!
098: */
099: int b = source.read();
100: if (b != 'B') {
101: throw new Exception("Invalid bz2 content.");
102: }
103: b = source.read();
104: if (b != 'Z') {
105: throw new Exception("Invalid bz2 content.");
106: }
107:
108: int read = 0;
109: byte[] data = new byte[1024];
110: CBZip2InputStream zippedContent = new CBZip2InputStream(
111: source);
112:
113: tempFile = File.createTempFile("bunzip", "tmp");
114: tempFile.deleteOnExit();
115:
116: // creating a temp file to store the uncompressed data
117: FileOutputStream out = new FileOutputStream(tempFile);
118:
119: // reading gzip file and store it uncompressed
120: while ((read = zippedContent.read(data, 0, 1024)) != -1) {
121: out.write(data, 0, read);
122: }
123: zippedContent.close();
124: out.close();
125:
126: // check for interruption
127: checkInterruption();
128:
129: // creating a new parser class to parse the unzipped content
130: plasmaParser theParser = new plasmaParser();
131: return theParser
132: .parseSource(location, null, null, tempFile);
133: } catch (Exception e) {
134: if (e instanceof InterruptedException)
135: throw (InterruptedException) e;
136: if (e instanceof ParserException)
137: throw (ParserException) e;
138:
139: throw new ParserException(
140: "Unexpected error while parsing bzip file. "
141: + e.getMessage(), location);
142: } finally {
143: if (tempFile != null)
144: tempFile.delete();
145: }
146: }
147:
148: public void reset() {
149: // Nothing todo here at the moment
150: super.reset();
151: }
152: }
|