Search in sources :

Example 46 with POIFSFileSystem

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

the class TestHSSFEventFactory method testWithDifferentWorkbookName.

public void testWithDifferentWorkbookName() throws Exception {
    HSSFRequest req = new HSSFRequest();
    MockHSSFListener mockListen = new MockHSSFListener();
    req.addListenerForAllRecords(mockListen);
    POIFSFileSystem fs = new POIFSFileSystem(openSample("BOOK_in_capitals.xls"));
    HSSFEventFactory factory = new HSSFEventFactory();
    factory.processWorkbookEvents(req, fs);
    fs = new POIFSFileSystem(openSample("WORKBOOK_in_capitals.xls"));
    factory = new HSSFEventFactory();
    factory.processWorkbookEvents(req, fs);
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem)

Example 47 with POIFSFileSystem

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

the class TestHSSFEventFactory method testWithMissingRecords.

public void testWithMissingRecords() throws Exception {
    HSSFRequest req = new HSSFRequest();
    MockHSSFListener mockListen = new MockHSSFListener();
    req.addListenerForAllRecords(mockListen);
    POIFSFileSystem fs = new POIFSFileSystem(openSample("SimpleWithSkip.xls"));
    HSSFEventFactory factory = new HSSFEventFactory();
    factory.processWorkbookEvents(req, fs);
    Record[] recs = mockListen.getRecords();
    // Check we got the records
    assertTrue(recs.length > 100);
    // Check that the last few records are as we expect
    // (Makes sure we don't accidently skip the end ones)
    int numRec = recs.length;
    assertEquals(WindowTwoRecord.class, recs[numRec - 3].getClass());
    assertEquals(SelectionRecord.class, recs[numRec - 2].getClass());
    assertEquals(EOFRecord.class, recs[numRec - 1].getClass());
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) FeatHdrRecord(org.apache.poi.hssf.record.FeatHdrRecord) SelectionRecord(org.apache.poi.hssf.record.SelectionRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) DVALRecord(org.apache.poi.hssf.record.DVALRecord) DVRecord(org.apache.poi.hssf.record.DVRecord)

Example 48 with POIFSFileSystem

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

the class TestHSSFEventFactory method testWithPasswordProtectedWorkbooks.

public void testWithPasswordProtectedWorkbooks() throws Exception {
    HSSFRequest req = new HSSFRequest();
    MockHSSFListener mockListen = new MockHSSFListener();
    req.addListenerForAllRecords(mockListen);
    // Without a password, can't be read
    Biff8EncryptionKey.setCurrentUserPassword(null);
    POIFSFileSystem fs = new POIFSFileSystem(openSample("xor-encryption-abc.xls"));
    HSSFEventFactory factory = new HSSFEventFactory();
    try {
        factory.processWorkbookEvents(req, fs);
        fail("Shouldn't be able to process protected workbook without the password");
    } catch (EncryptedDocumentException e) {
    }
    // With the password, is properly processed
    Biff8EncryptionKey.setCurrentUserPassword("abc");
    req = new HSSFRequest();
    mockListen = new MockHSSFListener();
    req.addListenerForAllRecords(mockListen);
    factory.processWorkbookEvents(req, fs);
    // Check we got the sheet and the contents
    Record[] recs = mockListen.getRecords();
    assertTrue(recs.length > 50);
    // Has one sheet, with values 1,2,3 in column A rows 1-3
    boolean hasSheet = false, hasA1 = false, hasA2 = false, hasA3 = false;
    for (Record r : recs) {
        if (r instanceof BoundSheetRecord) {
            BoundSheetRecord bsr = (BoundSheetRecord) r;
            assertEquals("Sheet1", bsr.getSheetname());
            hasSheet = true;
        }
        if (r instanceof NumberRecord) {
            NumberRecord nr = (NumberRecord) r;
            if (nr.getColumn() == 0 && nr.getRow() == 0) {
                assertEquals(1, (int) nr.getValue());
                hasA1 = true;
            }
            if (nr.getColumn() == 0 && nr.getRow() == 1) {
                assertEquals(2, (int) nr.getValue());
                hasA2 = true;
            }
            if (nr.getColumn() == 0 && nr.getRow() == 2) {
                assertEquals(3, (int) nr.getValue());
                hasA3 = true;
            }
        }
    }
    assertTrue("Sheet record not found", hasSheet);
    assertTrue("Numeric record for A1 not found", hasA1);
    assertTrue("Numeric record for A2 not found", hasA2);
    assertTrue("Numeric record for A3 not found", hasA3);
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) FeatHdrRecord(org.apache.poi.hssf.record.FeatHdrRecord) SelectionRecord(org.apache.poi.hssf.record.SelectionRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) DVALRecord(org.apache.poi.hssf.record.DVALRecord) DVRecord(org.apache.poi.hssf.record.DVRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Example 49 with POIFSFileSystem

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

the class TestRecordFactory method testNonZeroPadding_bug46987.

@Test
public void testNonZeroPadding_bug46987() throws IOException {
    Record[] recs = { new BOFRecord(), // need *something* between BOF and EOF
    new WriteAccessRecord(), EOFRecord.instance, BOFRecord.createSheetBOF(), EOFRecord.instance };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (Record rec : recs) {
        try {
            baos.write(rec.serialize());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //simulate the bad padding at the end of the workbook stream in attachment 23483 of bug 46987
    baos.write(0x00);
    baos.write(0x11);
    baos.write(0x00);
    baos.write(0x02);
    for (int i = 0; i < 192; i++) {
        baos.write(0x00);
    }
    POIFSFileSystem fs = new POIFSFileSystem();
    fs.createDocument(new ByteArrayInputStream(baos.toByteArray()), "dummy");
    InputStream is = fs.getRoot().createDocumentInputStream("dummy");
    List<Record> outRecs = RecordFactory.createRecords(is);
    // Buffer underrun - requested 512 bytes but 192 was available
    assertEquals(5, outRecs.size());
    fs.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 50 with POIFSFileSystem

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

the class TestOLE2Embeding method getSamplePPT.

static POIFSFileSystem getSamplePPT() throws IOException {
    // scratchpad classes are not available, so we use something pre-cooked
    InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream("with_textbox.ppt");
    POIFSFileSystem poifs = new POIFSFileSystem(is);
    is.close();
    return poifs;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6