Search in sources :

Example 1 with OPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.

the class TestExtractorFactory method testOPOIFS.

@Test
public void testOPOIFS() throws Exception {
    // Excel
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(xls))) instanceof ExcelExtractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(xls))).getText().length() > 200);
    // Word
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc))) instanceof WordExtractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc))).getText().length() > 120);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc6))) instanceof Word6Extractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc6))).getText().length() > 20);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc95))) instanceof Word6Extractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(doc95))).getText().length() > 120);
    // PowerPoint
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(ppt))) instanceof PowerPointExtractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(ppt))).getText().length() > 120);
    // Visio
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(vsd))) instanceof VisioTextExtractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(vsd))).getText().length() > 50);
    // Publisher
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(pub))) instanceof PublisherTextExtractor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(pub))).getText().length() > 50);
    // Outlook msg
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(msg))) instanceof OutlookTextExtactor);
    assertTrue(ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(msg))).getText().length() > 50);
    // Text
    try {
        ExtractorFactory.createExtractor(new OPOIFSFileSystem(new FileInputStream(txt)));
        fail();
    } catch (IOException e) {
    // Good
    }
}
Also used : OutlookTextExtactor(org.apache.poi.hsmf.extractor.OutlookTextExtactor) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) XSSFEventBasedExcelExtractor(org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor) EventBasedExcelExtractor(org.apache.poi.hssf.extractor.EventBasedExcelExtractor) Word6Extractor(org.apache.poi.hwpf.extractor.Word6Extractor) PowerPointExtractor(org.apache.poi.hslf.extractor.PowerPointExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) PublisherTextExtractor(org.apache.poi.hpbf.extractor.PublisherTextExtractor) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) IOException(java.io.IOException) VisioTextExtractor(org.apache.poi.hdgf.extractor.VisioTextExtractor) FileInputStream(java.io.FileInputStream) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) Test(org.junit.Test)

Example 2 with OPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.

the class TestWordExtractor method testDifferentPOIFS.

/**
     * Tests that we can work with both {@link POIFSFileSystem}
     *  and {@link NPOIFSFileSystem}
     */
@Test
public void testDifferentPOIFS() throws Exception {
    // Open the two filesystems
    File file = docTests.getFile("test2.doc");
    InputStream is = new FileInputStream(file);
    OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is);
    is.close();
    NPOIFSFileSystem npoifs = new NPOIFSFileSystem(file);
    DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
    // Open directly 
    for (DirectoryNode dir : files) {
        @SuppressWarnings("resource") WordExtractor extractor = new WordExtractor(dir);
        assertEqualsTrim(p_text1_block, extractor.getText());
    // extractor.close();
    }
    // Open via a HWPFDocument
    for (DirectoryNode dir : files) {
        HWPFDocument doc = new HWPFDocument(dir);
        WordExtractor extractor = new WordExtractor(doc);
        assertEqualsTrim(p_text1_block, extractor.getText());
        extractor.close();
    }
    npoifs.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with OPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.

the class TestHSSFWorkbook method invalidInPlaceWrite.

@Test
public void invalidInPlaceWrite() throws Exception {
    HSSFWorkbook wb;
    // Can't work for new files
    wb = new HSSFWorkbook();
    try {
        wb.write();
        fail("Shouldn't work for new files");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
    // Can't work for InputStream opened files
    wb = new HSSFWorkbook(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
    try {
        wb.write();
        fail("Shouldn't work for InputStream");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
    // Can't work for OPOIFS
    OPOIFSFileSystem ofs = new OPOIFSFileSystem(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls"));
    wb = new HSSFWorkbook(ofs.getRoot(), true);
    try {
        wb.write();
        fail("Shouldn't work for OPOIFSFileSystem");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
    // Can't work for Read-Only files
    NPOIFSFileSystem fs = new NPOIFSFileSystem(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls"), true);
    wb = new HSSFWorkbook(fs);
    try {
        wb.write();
        fail("Shouldn't work for Read Only");
    } catch (IllegalStateException e) {
    // expected here
    }
    wb.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) Test(org.junit.Test)

Example 4 with OPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.

the class TestBugs method bug46904.

/**
     * java.io.IOException: block[ 0 ] already removed
     * (is an excel 95 file though)
     */
@Test
public void bug46904() throws Exception {
    try {
        OPOIFSFileSystem fs = new OPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
        new HSSFWorkbook(fs.getRoot(), false).close();
        fail("Should catch exception here");
    } catch (OldExcelFormatException e) {
        assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
    }
    try {
        NPOIFSFileSystem fs = new NPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
        try {
            new HSSFWorkbook(fs.getRoot(), false).close();
            fail("Should catch exception here");
        } finally {
            fs.close();
        }
    } catch (OldExcelFormatException e) {
        assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) Test(org.junit.Test)

Example 5 with OPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.OPOIFSFileSystem in project poi by apache.

the class TestHWPFWrite method testInvalidInPlaceWriteOPOIFS.

@Test(expected = IllegalStateException.class)
public void testInvalidInPlaceWriteOPOIFS() throws Exception {
    // Can't work for OPOIFS
    OPOIFSFileSystem ofs = new OPOIFSFileSystem(SAMPLES.openResourceAsStream("SampleDoc.doc"));
    HWPFDocument doc = new HWPFDocument(ofs.getRoot());
    try {
        doc.write();
    } finally {
        doc.close();
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) Test(org.junit.Test)

Aggregations

OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)7 Test (org.junit.Test)7 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)5 FileInputStream (java.io.FileInputStream)3 File (java.io.File)2 InputStream (java.io.InputStream)2 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)2 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 VisioTextExtractor (org.apache.poi.hdgf.extractor.VisioTextExtractor)1 PublisherTextExtractor (org.apache.poi.hpbf.extractor.PublisherTextExtractor)1 HPSFPropertiesOnlyDocument (org.apache.poi.hpsf.HPSFPropertiesOnlyDocument)1 PowerPointExtractor (org.apache.poi.hslf.extractor.PowerPointExtractor)1 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)1 OutlookTextExtactor (org.apache.poi.hsmf.extractor.OutlookTextExtactor)1 OldExcelFormatException (org.apache.poi.hssf.OldExcelFormatException)1 EventBasedExcelExtractor (org.apache.poi.hssf.extractor.EventBasedExcelExtractor)1 ExcelExtractor (org.apache.poi.hssf.extractor.ExcelExtractor)1