1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.util.LittleEndian;
/**
*
* @author Durot
*/
public class MSPowerPointParser implements YourParserGeneral, POIFSReaderListener {
private InputStream input;
private String title;
private ByteArrayOutputStream writer;
/** Creates a new instance of OooWriter */
public MSPowerPointParser() {
}
public Document parse(File file) throws Exception {
input = new FileInputStream(file);
title = file.getName().substring(0, file.getName().lastIndexOf("."));
return parse();
}
public Document parse(InputStream in) throws Exception {
this.input = in;
title = "";
return parse();
}
public Document parse(String fileName) throws Exception {
input = new FileInputStream(fileName);
title = fileName.substring(0, fileName.lastIndexOf("."));
return parse();
}
private Document parse() throws Exception {
try {
Document doc = new Document();
doc.add(Field.Text("title", this.title));
doc.add(Field.Text("contents", this.getContents()));
return doc;
} catch (Exception ex) {
throw ex;
} finally {
if (input != null) {
try {
input.close();
} catch (Exception ex) {
}
}
}
}
private String getContents() throws Exception {
String contents = "";
try {
POIFSReader reader = new POIFSReader();
writer = new ByteArrayOutputStream();
reader.registerListener(this);
reader.read(input);
contents = writer.toString();
} catch (Exception ex) {
throw ex;
}
return contents;
}
public void processPOIFSReaderEvent(POIFSReaderEvent event) {
try{
if(!event.getName().equalsIgnoreCase("PowerPoint Document"))
return;
DocumentInputStream input = event.getStream();
byte[] buffer = new byte[input.available()];
input.read(buffer, 0, input.available());
for(int i=0; i<buffer.length-20; i++) {
long type = LittleEndian.getUShort(buffer,i+2);
long size = LittleEndian.getUInt(buffer,i+4);
if(type==4008) {
writer.write(buffer, i + 4 + 1, (int) size +3);
i = i + 4 + 1 + (int) size - 1;
}
try {
Thread.sleep(10);
} catch (Exception ex) {
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
} |
Partager