Search in sources :

Example 41 with DirectoryNode

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

the class TestExtractor method assertExtractFromEmbedded.

private PowerPointExtractor assertExtractFromEmbedded(DirectoryNode root, String entryName, String expected) throws IOException {
    DirectoryNode dir = (DirectoryNode) root.getEntry(entryName);
    assertTrue(dir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT));
    // Check the first file
    HSLFSlideShowImpl ppt = new HSLFSlideShowImpl(dir);
    PowerPointExtractor ppe = new PowerPointExtractor(ppt);
    assertEquals(expected, ppe.getText(true, false));
    return ppe;
}
Also used : DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)

Example 42 with DirectoryNode

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

the class TestExtractor method testDifferentPOIFS.

/**
     * Tests that we can work with both {@link POIFSFileSystem}
     * and {@link NPOIFSFileSystem}
     */
@SuppressWarnings("resource")
@Test
public void testDifferentPOIFS() throws IOException {
    // Open the two filesystems
    File pptFile = slTests.getFile("basic_test_ppt_file.ppt");
    InputStream is1 = new FileInputStream(pptFile);
    OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is1);
    is1.close();
    NPOIFSFileSystem npoifs = new NPOIFSFileSystem(pptFile);
    DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
    // Open directly
    for (DirectoryNode dir : files) {
        PowerPointExtractor extractor = new PowerPointExtractor(dir);
        assertEquals(expectText, extractor.getText());
    }
    // Open via a HSLFSlideShow
    for (DirectoryNode dir : files) {
        HSLFSlideShowImpl slideshow = new HSLFSlideShowImpl(dir);
        PowerPointExtractor extractor = new PowerPointExtractor(slideshow);
        assertEquals(expectText, extractor.getText());
        extractor.close();
        slideshow.close();
    }
    npoifs.close();
}
Also used : 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) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Test(org.junit.Test)

Example 43 with DirectoryNode

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

the class HSSFPatriarch method createObjectData.

@Override
public HSSFObjectData createObjectData(ClientAnchor anchor, int storageId, int pictureIndex) {
    ObjRecord obj = new ObjRecord();
    CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord();
    ftCmo.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE);
    // ftCmo.setObjectId(oleShape.getShapeId()); ... will be set by onCreate(...)
    ftCmo.setLocked(true);
    ftCmo.setPrintable(true);
    ftCmo.setAutofill(true);
    ftCmo.setAutoline(true);
    ftCmo.setReserved1(0);
    ftCmo.setReserved2(0);
    ftCmo.setReserved3(0);
    obj.addSubRecord(ftCmo);
    // FtCf (pictFormat) 
    FtCfSubRecord ftCf = new FtCfSubRecord();
    HSSFPictureData pictData = getSheet().getWorkbook().getAllPictures().get(pictureIndex - 1);
    switch(pictData.getFormat()) {
        case Workbook.PICTURE_TYPE_WMF:
        case Workbook.PICTURE_TYPE_EMF:
            // this needs patch #49658 to be applied to actually work 
            ftCf.setFlags(FtCfSubRecord.METAFILE_BIT);
            break;
        case Workbook.PICTURE_TYPE_DIB:
        case Workbook.PICTURE_TYPE_PNG:
        case Workbook.PICTURE_TYPE_JPEG:
        case Workbook.PICTURE_TYPE_PICT:
            ftCf.setFlags(FtCfSubRecord.BITMAP_BIT);
            break;
        default:
            throw new IllegalStateException("Invalid picture type: " + pictData.getFormat());
    }
    obj.addSubRecord(ftCf);
    // FtPioGrbit (pictFlags)
    FtPioGrbitSubRecord ftPioGrbit = new FtPioGrbitSubRecord();
    ftPioGrbit.setFlagByBit(FtPioGrbitSubRecord.AUTO_PICT_BIT, true);
    obj.addSubRecord(ftPioGrbit);
    EmbeddedObjectRefSubRecord ftPictFmla = new EmbeddedObjectRefSubRecord();
    ftPictFmla.setUnknownFormulaData(new byte[] { 2, 0, 0, 0, 0 });
    ftPictFmla.setOleClassname("Paket");
    ftPictFmla.setStorageId(storageId);
    obj.addSubRecord(ftPictFmla);
    obj.addSubRecord(new EndSubRecord());
    String entryName = "MBD" + HexDump.toHex(storageId);
    DirectoryEntry oleRoot;
    try {
        DirectoryNode dn = _sheet.getWorkbook().getDirectory();
        if (dn == null) {
            throw new FileNotFoundException();
        }
        oleRoot = (DirectoryEntry) dn.getEntry(entryName);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("trying to add ole shape without actually adding data first - use HSSFWorkbook.addOlePackage first", e);
    }
    // create picture shape, which need to be minimal modified for oleshapes
    HSSFPicture shape = new HSSFPicture(null, (HSSFClientAnchor) anchor);
    shape.setPictureIndex(pictureIndex);
    EscherContainerRecord spContainer = shape.getEscherContainer();
    EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
    spRecord.setFlags(spRecord.getFlags() | EscherSpRecord.FLAG_OLESHAPE);
    HSSFObjectData oleShape = new HSSFObjectData(spContainer, obj, oleRoot);
    addShape(oleShape);
    onCreate(oleShape);
    return oleShape;
}
Also used : FtPioGrbitSubRecord(org.apache.poi.hssf.record.FtPioGrbitSubRecord) EmbeddedObjectRefSubRecord(org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) EndSubRecord(org.apache.poi.hssf.record.EndSubRecord) FileNotFoundException(java.io.FileNotFoundException) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) EscherSpRecord(org.apache.poi.ddf.EscherSpRecord) FtCfSubRecord(org.apache.poi.hssf.record.FtCfSubRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord)

Example 44 with DirectoryNode

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

the class HSSFWorkbook method write.

/**
     * Write out this workbook to the currently open {@link File} via the
     *  writeable {@link POIFSFileSystem} it was opened as. 
     *  
     * <p>This will fail (with an {@link IllegalStateException} if the
     *  Workbook was opened read-only, opened from an {@link InputStream}
     *   instead of a File, or if this is not the root document. For those cases, 
     *   you must use {@link #write(OutputStream)} or {@link #write(File)} to 
     *   write to a brand new document.
     */
@Override
public void write() throws IOException {
    validateInPlaceWritePossible();
    final DirectoryNode dir = getDirectory();
    // Update the Workbook stream in the file
    DocumentNode workbookNode = (DocumentNode) dir.getEntry(getWorkbookDirEntryName(dir));
    NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode);
    workbookDoc.replaceContents(new ByteArrayInputStream(getBytes()));
    // Update the properties streams in the file
    writeProperties();
    // Sync with the File on disk
    dir.getFileSystem().writeFilesystem();
}
Also used : DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) FilteringDirectoryNode(org.apache.poi.poifs.filesystem.FilteringDirectoryNode) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) NPOIFSDocument(org.apache.poi.poifs.filesystem.NPOIFSDocument)

Example 45 with DirectoryNode

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

the class TestHSSFWorkbook method wordDocEmbeddedInXls.

@Test
public void wordDocEmbeddedInXls() throws IOException {
    // Open the two filesystems
    DirectoryNode[] files = new DirectoryNode[2];
    POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"));
    try {
        files[0] = poifsFileSystem.getRoot();
        NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"));
        try {
            files[1] = npoifsFileSystem.getRoot();
            // Check the embedded parts
            for (DirectoryNode root : files) {
                HSSFWorkbook hw = new HSSFWorkbook(root, true);
                List<HSSFObjectData> objects = hw.getAllEmbeddedObjects();
                boolean found = false;
                for (HSSFObjectData embeddedObject : objects) {
                    if (embeddedObject.hasDirectoryEntry()) {
                        DirectoryEntry dir = embeddedObject.getDirectory();
                        if (dir instanceof DirectoryNode) {
                            DirectoryNode dNode = (DirectoryNode) dir;
                            if (hasEntry(dNode, "WordDocument")) {
                                found = true;
                            }
                        }
                    }
                }
                assertTrue(found);
                hw.close();
            }
        } finally {
            npoifsFileSystem.close();
        }
    } finally {
        poifsFileSystem.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) Test(org.junit.Test)

Aggregations

DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)47 Test (org.junit.Test)16 InputStream (java.io.InputStream)15 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)13 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)12 Entry (org.apache.poi.poifs.filesystem.Entry)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 IOException (java.io.IOException)8 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)6 FileInputStream (java.io.FileInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)5 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)4 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 AttachmentChunks (org.apache.poi.hsmf.datatypes.AttachmentChunks)3 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)3 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)3