001: //odtDetector.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.mimeType;
045:
046: import java.io.File;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.util.Map;
050: import java.util.zip.ZipEntry;
051: import java.util.zip.ZipFile;
052:
053: import net.sf.jmimemagic.MagicDetector;
054: import de.anomic.server.serverFileUtils;
055:
056: public class odtDetector implements MagicDetector {
057:
058: public String getDisplayName() {
059: return "ODT MimeType Detector";
060: }
061:
062: public String[] getHandledExtensions() {
063: return new String[] { "zip", "odt" };
064: }
065:
066: public String[] getHandledTypes() {
067: return new String[] {
068: "application/vnd.oasis.opendocument.text",
069: "application/x-vnd.oasis.opendocument.text" };
070: }
071:
072: public String getName() {
073: return "odtfiledetector";
074: }
075:
076: public String getVersion() {
077: return "0.1";
078: }
079:
080: @SuppressWarnings("unchecked")
081: public String[] process(byte[] data, int offset, int length,
082: long bitmask, char comparator, String mimeType, Map params) {
083: File dstFile = null;
084: try {
085: dstFile = File.createTempFile("mimeTypeParser", ".tmp");
086: serverFileUtils.write(data, dstFile);
087: return process(dstFile, offset, length, bitmask,
088: comparator, mimeType, params);
089: } catch (IOException e) {
090: return null;
091: } finally {
092: if (dstFile != null) {
093: dstFile.delete();
094: }
095: }
096: }
097:
098: @SuppressWarnings("unchecked")
099: public String[] process(File file, int offset, int length,
100: long bitmask, char comparator, String mimeType, Map params) {
101: try {
102: // opening the zip file
103: ZipFile zipFile = new ZipFile(file);
104:
105: // searching for a file named mimetype
106: ZipEntry mimeTypeInfo = zipFile.getEntry("mimetype");
107: if (mimeTypeInfo == null)
108: return null;
109:
110: // read in the content of the file
111: InputStream zippedContent = zipFile
112: .getInputStream(mimeTypeInfo);
113: String realMimeType = new String(serverFileUtils.read(
114: zippedContent, mimeTypeInfo.getSize()));
115:
116: return new String[] { realMimeType };
117: } catch (Exception e) {
118: return null;
119: }
120:
121: }
122:
123: }
|