001: /*
002: * Copyright 2003-2004 Michael Franken, Zilverline.
003: *
004: * The contents of this file, or the files included with this file, are subject to
005: * the current version of ZILVERLINE Collaborative Source License for the
006: * Zilverline Search Engine (the "License"); You may not use this file except in
007: * compliance with the License.
008: *
009: * You may obtain a copy of the License at
010: *
011: * http://www.zilverline.org.
012: *
013: * See the License for the rights, obligations and
014: * limitations governing use of the contents of the file.
015: *
016: * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
017: * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
018: * copyrights in the portions it created. All Rights Reserved.
019: *
020: */
021:
022: package org.zilverline.extractors;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.Reader;
031:
032: import org.apache.poi.poifs.eventfilesystem.POIFSReader;
033: import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
034: import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
035: import org.apache.poi.poifs.filesystem.DocumentInputStream;
036: import org.apache.poi.util.LittleEndian;
037:
038: /**
039: * This class extracts text from MS Powerpoint files by using the POI library.
040: *
041: * @author Michael Franken
042: * @version $Revision: 1.7 $
043: */
044: public class PowerPointExtractor extends AbstractExtractor implements
045: POIFSReaderListener {
046:
047: /** Writer to store parsed content. */
048: private ByteArrayOutputStream writer = new ByteArrayOutputStream();
049:
050: /**
051: * Extract the content from the given Powerpoint file. As a side effect the type is set too.
052: *
053: * @see org.zilverline.extractors.AbstractExtractor#getContent(java.io.File)
054: */
055: public final Reader getContent(final File f) {
056:
057: setType("POWERPOINT");
058:
059: try {
060: POIFSReader reader = new POIFSReader();
061: reader.registerListener(this );
062: reader.read(new FileInputStream(f));
063: setSummary(getSummaryFromContent(writer.toString()));
064:
065: return new InputStreamReader(new ByteArrayInputStream(
066: writer.toByteArray()));
067: } catch (Exception e) {
068: log.warn("Can't extract contents for: " + f.getName(), e);
069: }
070:
071: return null;
072: }
073:
074: /**
075: * Extract the content from the given Powerpoint file. As a side effect the type is set too.
076: *
077: * @see org.zilverline.extractors.AbstractExtractor#getContent(java.io.File)
078: */
079: public final String getContent(final InputStream is) {
080: try {
081: POIFSReader reader = new POIFSReader();
082: reader.registerListener(this );
083: reader.read(is);
084: return new String(writer.toByteArray());
085: } catch (Exception e) {
086: log.warn("Can't extract contents", e);
087: }
088:
089: return "";
090: }
091:
092: /**
093: * @see org.apache.poi.poifs.eventfilesystem.POIFSReaderListener#processPOIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent)
094: */
095: public void processPOIFSReaderEvent(POIFSReaderEvent event) {
096: try {
097: if (!event.getName()
098: .equalsIgnoreCase("PowerPoint Document")) {
099: return;
100: }
101: DocumentInputStream input = event.getStream();
102: byte[] buffer = new byte[input.available()];
103: input.read(buffer, 0, input.available());
104: for (int i = 0; i < buffer.length - 20; i++) {
105: long type = LittleEndian.getUShort(buffer, i + 2);
106: long size = LittleEndian.getUInt(buffer, i + 4);
107: if (type == 4008) {
108: writer.write(' ');
109: writer.write(buffer, i + 4 + 4, (int) size);
110: i = i + 4 + 4 + (int) size - 1;
111: }
112: }
113: } catch (Exception e) {
114: log.warn("Error parsing powerpoint", e);
115: }
116: }
117: }
|