Search in sources :

Example 6 with Property

use of org.apache.poi.poifs.property.Property in project poi by apache.

the class TestPropertySorter method getVBAProperties.

/**
     * @return array of properties read from ROOT._VBA_PROJECT_CUR.VBA node
     */
protected Property[] getVBAProperties(POIFSFileSystem fs) throws IOException {
    String _VBA_PROJECT_CUR = "_VBA_PROJECT_CUR";
    String VBA = "VBA";
    DirectoryEntry root = fs.getRoot();
    DirectoryEntry vba_project = (DirectoryEntry) root.getEntry(_VBA_PROJECT_CUR);
    DirectoryNode vba = (DirectoryNode) vba_project.getEntry(VBA);
    DirectoryProperty p = (DirectoryProperty) vba.getProperty();
    List<Property> lst = new ArrayList<Property>();
    for (Iterator<Property> it = p.getChildren(); it.hasNext(); ) {
        Property ch = it.next();
        lst.add(ch);
    }
    return lst.toArray(new Property[0]);
}
Also used : DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) ArrayList(java.util.ArrayList) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty)

Example 7 with Property

use of org.apache.poi.poifs.property.Property in project poi by apache.

the class TestPropertySorter method testSerialization.

/**
     * Serialize file system and verify that the order of properties is the same as in the original file.
     */
public void testSerialization() throws IOException {
    POIFSFileSystem fs = openSampleFS();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    fs.writeFilesystem(out);
    out.close();
    InputStream is = new ByteArrayInputStream(out.toByteArray());
    fs = new POIFSFileSystem(is);
    is.close();
    Property[] props = getVBAProperties(fs);
    Arrays.sort(props, new DirectoryProperty.PropertyComparator());
    assertEquals(_entries.length, props.length);
    for (int i = 0; i < props.length; i++) {
        assertEquals(_entries[i], props[i].getName());
    }
}
Also used : DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty)

Example 8 with Property

use of org.apache.poi.poifs.property.Property in project poi by apache.

the class TestNPOIFSFileSystem method propertiesAndFatOnRead.

@Test
public void propertiesAndFatOnRead() throws IOException {
    NPOIFSFileSystem fsA, fsB;
    // With a simple 512 block file
    fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
    fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
    for (NPOIFSFileSystem fs : new NPOIFSFileSystem[] { fsA, fsB }) {
        // Check the FAT was properly processed:
        // Verify we only got one block
        fs.getBATBlockAndIndex(0);
        fs.getBATBlockAndIndex(1);
        try {
            fs.getBATBlockAndIndex(140);
            fail("Should only be one BAT, but a 2nd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few next offsets
        // 97 -> 98 -> END
        assertEquals(98, fs.getNextBlock(97));
        assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(98));
        // Check the properties
        NPropertyTable props = fs._get_property_table();
        assertEquals(90, props.getStartBlock());
        assertEquals(7, props.countBlocks());
        // Root property tells us about the Mini Stream
        RootProperty root = props.getRoot();
        assertEquals("Root Entry", root.getName());
        assertEquals(11564, root.getSize());
        assertEquals(0, root.getStartBlock());
        // Check its children too
        Property prop;
        Iterator<Property> pi = root.getChildren();
        prop = pi.next();
        assertEquals("Thumbnail", prop.getName());
        prop = pi.next();
        assertEquals("DocumentSummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("SummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("Image", prop.getName());
        prop = pi.next();
        assertEquals("Tags", prop.getName());
        assertEquals(false, pi.hasNext());
        // Check the SBAT (Small Blocks FAT) was properly processed
        NPOIFSMiniStore ministore = fs.getMiniStore();
        // Verify we only got two SBAT blocks
        ministore.getBATBlockAndIndex(0);
        ministore.getBATBlockAndIndex(128);
        try {
            ministore.getBATBlockAndIndex(256);
            fail("Should only be two SBATs, but a 3rd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few offsets: 0->50 is a stream
        for (int i = 0; i < 50; i++) {
            assertEquals(i + 1, ministore.getNextBlock(i));
        }
        assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
        fs.close();
    }
    // Now with a simple 4096 block file
    fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
    fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
    for (NPOIFSFileSystem fs : new NPOIFSFileSystem[] { fsA, fsB }) {
        // Check the FAT was properly processed
        // Verify we only got one block
        fs.getBATBlockAndIndex(0);
        fs.getBATBlockAndIndex(1);
        try {
            fs.getBATBlockAndIndex(1040);
            fail("Should only be one BAT, but a 2nd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few next offsets
        // 0 -> 1 -> 2 -> END
        assertEquals(1, fs.getNextBlock(0));
        assertEquals(2, fs.getNextBlock(1));
        assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
        // Check the properties
        NPropertyTable props = fs._get_property_table();
        assertEquals(12, props.getStartBlock());
        assertEquals(1, props.countBlocks());
        // Root property tells us about the Mini Stream
        RootProperty root = props.getRoot();
        assertEquals("Root Entry", root.getName());
        assertEquals(11564, root.getSize());
        assertEquals(0, root.getStartBlock());
        // Check its children too
        Property prop;
        Iterator<Property> pi = root.getChildren();
        prop = pi.next();
        assertEquals("Thumbnail", prop.getName());
        prop = pi.next();
        assertEquals("DocumentSummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("SummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("Image", prop.getName());
        prop = pi.next();
        assertEquals("Tags", prop.getName());
        assertEquals(false, pi.hasNext());
        // Check the SBAT (Small Blocks FAT) was properly processed
        NPOIFSMiniStore ministore = fs.getMiniStore();
        // Verify we only got one SBAT block
        ministore.getBATBlockAndIndex(0);
        ministore.getBATBlockAndIndex(128);
        ministore.getBATBlockAndIndex(1023);
        try {
            ministore.getBATBlockAndIndex(1024);
            fail("Should only be one SBAT, but a 2nd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few offsets: 0->50 is a stream
        for (int i = 0; i < 50; i++) {
            assertEquals(i + 1, ministore.getNextBlock(i));
        }
        assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
        fs.close();
    }
}
Also used : RootProperty(org.apache.poi.poifs.property.RootProperty) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) RootProperty(org.apache.poi.poifs.property.RootProperty) Test(org.junit.Test)

Example 9 with Property

use of org.apache.poi.poifs.property.Property in project poi by apache.

the class TestNPOIFSFileSystem method readZeroLengthEntries.

@Test
public void readZeroLengthEntries() throws IOException {
    NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.getFile("only-zero-byte-streams.ole2"));
    DirectoryNode testDir = fs.getRoot();
    assertEquals(3, testDir.getEntryCount());
    DocumentEntry entry;
    entry = (DocumentEntry) testDir.getEntry("test-zero-1");
    assertNotNull(entry);
    assertEquals(0, entry.getSize());
    entry = (DocumentEntry) testDir.getEntry("test-zero-2");
    assertNotNull(entry);
    assertEquals(0, entry.getSize());
    entry = (DocumentEntry) testDir.getEntry("test-zero-3");
    assertNotNull(entry);
    assertEquals(0, entry.getSize());
    // Check properties, all have zero length, no blocks
    NPropertyTable props = fs._get_property_table();
    assertEquals(POIFSConstants.END_OF_CHAIN, props.getRoot().getStartBlock());
    for (Property prop : props.getRoot()) {
        assertEquals("test-zero-", prop.getName().substring(0, 10));
        assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
    }
    // All done
    fs.close();
}
Also used : NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) RootProperty(org.apache.poi.poifs.property.RootProperty) Test(org.junit.Test)

Aggregations

Property (org.apache.poi.poifs.property.Property)9 DirectoryProperty (org.apache.poi.poifs.property.DirectoryProperty)8 RootProperty (org.apache.poi.poifs.property.RootProperty)4 NPropertyTable (org.apache.poi.poifs.property.NPropertyTable)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)1 OPOIFSDocument (org.apache.poi.poifs.filesystem.OPOIFSDocument)1 POIFSDocumentPath (org.apache.poi.poifs.filesystem.POIFSDocumentPath)1