Wire up the new HSMFTextExtactor to the ExtractorFactory

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@897246 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-01-08 16:14:27 +00:00
parent c01272208e
commit f7ccc5d5f5
4 changed files with 220 additions and 167 deletions

View File

@ -31,6 +31,7 @@ import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor; import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hdgf.extractor.VisioTextExtractor; import org.apache.poi.hdgf.extractor.VisioTextExtractor;
import org.apache.poi.hslf.extractor.PowerPointExtractor; import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hsmf.extractor.HSMFTextExtactor;
import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -138,6 +139,11 @@ public class ExtractorFactory {
if(entry.getName().equals("VisioDocument")) { if(entry.getName().equals("VisioDocument")) {
return new VisioTextExtractor(poifsDir, fs); return new VisioTextExtractor(poifsDir, fs);
} }
if(entry.getName().equals("__substg1.0_1000001E") ||
entry.getName().equals("__substg1.0_0047001E") ||
entry.getName().equals("__substg1.0_0037001E")) {
return new HSMFTextExtactor(poifsDir, fs);
}
} }
throw new IllegalArgumentException("No supported documents found in the OLE2 stream"); throw new IllegalArgumentException("No supported documents found in the OLE2 stream");
} }

View File

@ -25,6 +25,7 @@ import org.apache.poi.POITextExtractor;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hdgf.extractor.VisioTextExtractor; import org.apache.poi.hdgf.extractor.VisioTextExtractor;
import org.apache.poi.hslf.extractor.PowerPointExtractor; import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hsmf.extractor.HSMFTextExtactor;
import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@ -42,132 +43,145 @@ import org.apache.poi.openxml4j.opc.OPCPackage;
*/ */
public class TestExtractorFactory extends TestCase { public class TestExtractorFactory extends TestCase {
private File txt; private File txt;
private File xls; private File xls;
private File xlsx; private File xlsx;
private File xltx; private File xltx;
private File xlsEmb; private File xlsEmb;
private File doc; private File doc;
private File docx; private File docx;
private File dotx; private File dotx;
private File docEmb; private File docEmb;
private File ppt; private File ppt;
private File pptx; private File pptx;
private File vsd; private File msg;
private File vsd;
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
POIDataSamples ssTests = POIDataSamples.getSpreadSheetInstance(); POIDataSamples ssTests = POIDataSamples.getSpreadSheetInstance();
xls = ssTests.getFile("SampleSS.xls"); xls = ssTests.getFile("SampleSS.xls");
xlsx = ssTests.getFile("SampleSS.xlsx"); xlsx = ssTests.getFile("SampleSS.xlsx");
xltx = ssTests.getFile("test.xltx"); xltx = ssTests.getFile("test.xltx");
xlsEmb = ssTests.getFile("excel_with_embeded.xls"); xlsEmb = ssTests.getFile("excel_with_embeded.xls");
POIDataSamples wpTests = POIDataSamples.getDocumentInstance(); POIDataSamples wpTests = POIDataSamples.getDocumentInstance();
doc = wpTests.getFile("SampleDoc.doc"); doc = wpTests.getFile("SampleDoc.doc");
docx = wpTests.getFile("SampleDoc.docx"); docx = wpTests.getFile("SampleDoc.docx");
dotx = wpTests.getFile("test.dotx"); dotx = wpTests.getFile("test.dotx");
docEmb = wpTests.getFile("word_with_embeded.doc"); docEmb = wpTests.getFile("word_with_embeded.doc");
POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
ppt = slTests.getFile("SampleShow.ppt"); ppt = slTests.getFile("SampleShow.ppt");
pptx = slTests.getFile("SampleShow.pptx"); pptx = slTests.getFile("SampleShow.pptx");
txt = slTests.getFile("SampleShow.txt"); txt = slTests.getFile("SampleShow.txt");
POIDataSamples dgTests = POIDataSamples.getDiagramInstance(); POIDataSamples dgTests = POIDataSamples.getDiagramInstance();
vsd = dgTests.getFile("Test_Visio-Some_Random_Text.vsd"); vsd = dgTests.getFile("Test_Visio-Some_Random_Text.vsd");
}
public void testFile() throws Exception { POIDataSamples olTests = POIDataSamples.getHSMFInstance();
// Excel msg = olTests.getFile("quick.msg");
assertTrue( }
ExtractorFactory.createExtractor(xls)
instanceof ExcelExtractor
);
assertTrue(
ExtractorFactory.createExtractor(xls).getText().length() > 200
);
assertTrue( public void testFile() throws Exception {
ExtractorFactory.createExtractor(xlsx) // Excel
instanceof XSSFExcelExtractor assertTrue(
); ExtractorFactory.createExtractor(xls)
assertTrue( instanceof ExcelExtractor
ExtractorFactory.createExtractor(xlsx).getText().length() > 200 );
); assertTrue(
ExtractorFactory.createExtractor(xls).getText().length() > 200
);
assertTrue( assertTrue(
ExtractorFactory.createExtractor(xltx) ExtractorFactory.createExtractor(xlsx)
instanceof XSSFExcelExtractor instanceof XSSFExcelExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(xltx).getText().contains("test") ExtractorFactory.createExtractor(xlsx).getText().length() > 200
); );
assertTrue(
ExtractorFactory.createExtractor(xltx)
instanceof XSSFExcelExtractor
);
assertTrue(
ExtractorFactory.createExtractor(xltx).getText().contains("test")
);
// Word // Word
assertTrue( assertTrue(
ExtractorFactory.createExtractor(doc) ExtractorFactory.createExtractor(doc)
instanceof WordExtractor instanceof WordExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(doc).getText().length() > 120 ExtractorFactory.createExtractor(doc).getText().length() > 120
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(docx) ExtractorFactory.createExtractor(docx)
instanceof XWPFWordExtractor instanceof XWPFWordExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(docx).getText().length() > 120 ExtractorFactory.createExtractor(docx).getText().length() > 120
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(dotx) ExtractorFactory.createExtractor(dotx)
instanceof XWPFWordExtractor instanceof XWPFWordExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(dotx).getText().contains("Test") ExtractorFactory.createExtractor(dotx).getText().contains("Test")
); );
// PowerPoint // PowerPoint
assertTrue( assertTrue(
ExtractorFactory.createExtractor(ppt) ExtractorFactory.createExtractor(ppt)
instanceof PowerPointExtractor instanceof PowerPointExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(ppt).getText().length() > 120 ExtractorFactory.createExtractor(ppt).getText().length() > 120
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(pptx) ExtractorFactory.createExtractor(pptx)
instanceof XSLFPowerPointExtractor instanceof XSLFPowerPointExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(pptx).getText().length() > 120 ExtractorFactory.createExtractor(pptx).getText().length() > 120
); );
// Visio // Visio
assertTrue( assertTrue(
ExtractorFactory.createExtractor(vsd) ExtractorFactory.createExtractor(vsd)
instanceof VisioTextExtractor instanceof VisioTextExtractor
); );
assertTrue( assertTrue(
ExtractorFactory.createExtractor(vsd).getText().length() > 50 ExtractorFactory.createExtractor(vsd).getText().length() > 50
); );
// Text // Outlook msg
try { assertTrue(
ExtractorFactory.createExtractor(txt); ExtractorFactory.createExtractor(msg)
fail(); instanceof HSMFTextExtactor
} catch(IllegalArgumentException e) { );
// Good assertTrue(
} ExtractorFactory.createExtractor(msg).getText().length() > 50
);
// Text
try {
ExtractorFactory.createExtractor(txt);
fail();
} catch(IllegalArgumentException e) {
// Good
}
} }
public void testInputStream() throws Exception { public void testInputStream() throws Exception {
@ -231,6 +245,15 @@ public class TestExtractorFactory extends TestCase {
ExtractorFactory.createExtractor(new FileInputStream(vsd)).getText().length() > 50 ExtractorFactory.createExtractor(new FileInputStream(vsd)).getText().length() > 50
); );
// Outlook msg
assertTrue(
ExtractorFactory.createExtractor(new FileInputStream(msg))
instanceof HSMFTextExtactor
);
assertTrue(
ExtractorFactory.createExtractor(new FileInputStream(msg)).getText().length() > 50
);
// Text // Text
try { try {
ExtractorFactory.createExtractor(new FileInputStream(txt)); ExtractorFactory.createExtractor(new FileInputStream(txt));
@ -277,6 +300,15 @@ public class TestExtractorFactory extends TestCase {
ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(vsd))).getText().length() > 50 ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(vsd))).getText().length() > 50
); );
// Outlook msg
assertTrue(
ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(msg)))
instanceof HSMFTextExtactor
);
assertTrue(
ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(msg))).getText().length() > 50
);
// Text // Text
try { try {
ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(txt))); ExtractorFactory.createExtractor(new POIFSFileSystem(new FileInputStream(txt)));
@ -323,57 +355,58 @@ public class TestExtractorFactory extends TestCase {
} }
} }
/** /**
* Test embeded docs text extraction. For now, only * Test embeded docs text extraction. For now, only
* does poifs embeded, but will do ooxml ones * does poifs embeded, but will do ooxml ones
* at some point. * at some point.
*/ */
public void testEmbeded() throws Exception { public void testEmbeded() throws Exception {
POIOLE2TextExtractor ext; POIOLE2TextExtractor ext;
POITextExtractor[] embeds; POITextExtractor[] embeds;
// No embedings // No embedings
ext = (POIOLE2TextExtractor) ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(xls); ExtractorFactory.createExtractor(xls);
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext); embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
assertEquals(0, embeds.length); assertEquals(0, embeds.length);
// Excel // Excel
ext = (POIOLE2TextExtractor) ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(xlsEmb); ExtractorFactory.createExtractor(xlsEmb);
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext); embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
assertEquals(6, embeds.length); assertEquals(6, embeds.length);
int numWord = 0, numXls = 0, numPpt = 0; int numWord = 0, numXls = 0, numPpt = 0;
for(int i=0; i<embeds.length; i++) { for(int i=0; i<embeds.length; i++) {
assertTrue(embeds[i].getText().length() > 20); assertTrue(embeds[i].getText().length() > 20);
if(embeds[i] instanceof PowerPointExtractor) numPpt++; if(embeds[i] instanceof PowerPointExtractor) numPpt++;
else if(embeds[i] instanceof ExcelExtractor) numXls++; else if(embeds[i] instanceof ExcelExtractor) numXls++;
else if(embeds[i] instanceof WordExtractor) numWord++; else if(embeds[i] instanceof WordExtractor) numWord++;
} }
assertEquals(2, numPpt); assertEquals(2, numPpt);
assertEquals(2, numXls); assertEquals(2, numXls);
assertEquals(2, numWord); assertEquals(2, numWord);
// Word // Word
ext = (POIOLE2TextExtractor) ext = (POIOLE2TextExtractor)
ExtractorFactory.createExtractor(docEmb); ExtractorFactory.createExtractor(docEmb);
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext); embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
numWord = 0; numXls = 0; numPpt = 0; numWord = 0; numXls = 0; numPpt = 0;
assertEquals(4, embeds.length); assertEquals(4, embeds.length);
for(int i=0; i<embeds.length; i++) { for(int i=0; i<embeds.length; i++) {
assertTrue(embeds[i].getText().length() > 20); assertTrue(embeds[i].getText().length() > 20);
if(embeds[i] instanceof PowerPointExtractor) numPpt++; if(embeds[i] instanceof PowerPointExtractor) numPpt++;
else if(embeds[i] instanceof ExcelExtractor) numXls++; else if(embeds[i] instanceof ExcelExtractor) numXls++;
else if(embeds[i] instanceof WordExtractor) numWord++; else if(embeds[i] instanceof WordExtractor) numWord++;
} }
assertEquals(1, numPpt); assertEquals(1, numPpt);
assertEquals(2, numXls); assertEquals(2, numXls);
assertEquals(1, numWord); assertEquals(1, numWord);
// TODO - PowerPoint // TODO - PowerPoint
// TODO - Visio // TODO - Visio
} // TODO - Outlook
}
} }

View File

@ -34,6 +34,7 @@ import org.apache.poi.hsmf.datatypes.RecipientChunks;
import org.apache.poi.hsmf.datatypes.StringChunk; import org.apache.poi.hsmf.datatypes.StringChunk;
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
import org.apache.poi.hsmf.parsers.POIFSChunkParser; import org.apache.poi.hsmf.parsers.POIFSChunkParser;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/** /**
@ -78,15 +79,24 @@ public class MAPIMessage extends POIDocument {
this(new POIFSFileSystem(in)); this(new POIFSFileSystem(in));
} }
/** /**
* Constructor for reading MSG Files from an input stream. * Constructor for reading MSG Files from a POIFS filesystem
* @param in * @param in
* @throws IOException * @throws IOException
*/ */
public MAPIMessage(POIFSFileSystem fs) throws IOException { public MAPIMessage(POIFSFileSystem fs) throws IOException {
super(fs); this(fs.getRoot(), fs);
}
/**
* Constructor for reading MSG Files from a certain
* point within a POIFS filesystem
* @param in
* @throws IOException
*/
public MAPIMessage(DirectoryNode poifsDir, POIFSFileSystem fs) throws IOException {
super(poifsDir, fs);
// Grab all the chunks // Grab all the chunks
ChunkGroup[] chunkGroups = POIFSChunkParser.parse(fs); ChunkGroup[] chunkGroups = POIFSChunkParser.parse(poifsDir);
// Grab interesting bits // Grab interesting bits
ArrayList<AttachmentChunks> attachments = new ArrayList<AttachmentChunks>(); ArrayList<AttachmentChunks> attachments = new ArrayList<AttachmentChunks>();

View File

@ -23,12 +23,16 @@ import java.text.SimpleDateFormat;
import org.apache.poi.POIOLE2TextExtractor; import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.MAPIMessage;
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException; import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class HSMFTextExtactor extends POIOLE2TextExtractor { public class HSMFTextExtactor extends POIOLE2TextExtractor {
public HSMFTextExtactor(MAPIMessage msg) { public HSMFTextExtactor(MAPIMessage msg) {
super(msg); super(msg);
} }
public HSMFTextExtactor(DirectoryNode poifsDir, POIFSFileSystem fs) throws IOException {
this(new MAPIMessage(poifsDir, fs));
}
public HSMFTextExtactor(POIFSFileSystem fs) throws IOException { public HSMFTextExtactor(POIFSFileSystem fs) throws IOException {
this(new MAPIMessage(fs)); this(new MAPIMessage(fs));
} }