Search in sources :

Example 11 with SummaryInformation

use of org.apache.poi.hpsf.SummaryInformation in project poi by apache.

the class TestNPOIFSFileSystem method getDocumentEntry.

/**
    * Tests that we can get the correct contents for
    *  a document in the filesystem 
    */
@Test
public void getDocumentEntry() throws Exception {
    for (NPOIFSFileSystem fs : get512and4kFileAndInput()) {
        DirectoryEntry root = fs.getRoot();
        Entry si = root.getEntry("SummaryInformation");
        assertEquals(true, si.isDocumentEntry());
        DocumentNode doc = (DocumentNode) si;
        // Check we can read it
        assertContentsMatches(null, doc);
        // Now try to build the property set
        DocumentInputStream inp = new NDocumentInputStream(doc);
        PropertySet ps = PropertySetFactory.create(inp);
        SummaryInformation inf = (SummaryInformation) ps;
        // Check some bits in it
        assertEquals(null, inf.getApplicationName());
        assertEquals(null, inf.getAuthor());
        assertEquals(null, inf.getSubject());
        assertEquals(131333, inf.getOSVersion());
        // Finish with this one
        inp.close();
        // Try the other summary information
        si = root.getEntry("DocumentSummaryInformation");
        assertEquals(true, si.isDocumentEntry());
        doc = (DocumentNode) si;
        assertContentsMatches(null, doc);
        inp = new NDocumentInputStream(doc);
        ps = PropertySetFactory.create(inp);
        DocumentSummaryInformation dinf = (DocumentSummaryInformation) ps;
        assertEquals(131333, dinf.getOSVersion());
        fs.close();
    }
}
Also used : SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) PropertySet(org.apache.poi.hpsf.PropertySet) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) Test(org.junit.Test)

Example 12 with SummaryInformation

use of org.apache.poi.hpsf.SummaryInformation in project poi by apache.

the class TestNPOIFSFileSystem method readWriteRead.

/**
    * Read a file, write it and read it again.
    * Then, alter+add some streams, write and read
    */
@Test
public void readWriteRead() throws Exception {
    SummaryInformation sinf;
    DocumentSummaryInformation dinf;
    DirectoryEntry root, testDir;
    for (NPOIFSFileSystem fs1 : get512and4kFileAndInput()) {
        // Check we can find the entries we expect
        root = fs1.getRoot();
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Tags"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        // Write out, re-load
        NPOIFSFileSystem fs2 = writeOutAndReadBack(fs1);
        fs1.close();
        // Check they're still there
        root = fs2.getRoot();
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Tags"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        // Check the contents of them - parse the summary block and check
        sinf = (SummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, sinf.getOSVersion());
        dinf = (DocumentSummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, dinf.getOSVersion());
        // Add a test mini stream
        testDir = root.createDirectory("Testing 123");
        testDir.createDirectory("Testing 456");
        testDir.createDirectory("Testing 789");
        byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
        testDir.createDocument("Mini", new ByteArrayInputStream(mini));
        // Write out, re-load
        NPOIFSFileSystem fs3 = writeOutAndReadBack(fs2);
        fs2.close();
        root = fs3.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(6, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Tags"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        // Check old and new are there
        sinf = (SummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, sinf.getOSVersion());
        dinf = (DocumentSummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, dinf.getOSVersion());
        assertContentsMatches(mini, (DocumentEntry) testDir.getEntry("Mini"));
        // Write out and read once more, just to be sure
        NPOIFSFileSystem fs4 = writeOutAndReadBack(fs3);
        fs3.close();
        root = fs4.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(6, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Tags"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        sinf = (SummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, sinf.getOSVersion());
        dinf = (DocumentSummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, dinf.getOSVersion());
        assertContentsMatches(mini, (DocumentEntry) testDir.getEntry("Mini"));
        // Add a full stream, delete a full stream
        byte[] main4096 = new byte[4096];
        main4096[0] = -10;
        main4096[4095] = -11;
        testDir.createDocument("Normal4096", new ByteArrayInputStream(main4096));
        root.getEntry("Tags").delete();
        // Write out, re-load
        NPOIFSFileSystem fs5 = writeOutAndReadBack(fs4);
        fs4.close();
        // Check it's all there
        root = fs5.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        // Check old and new are there
        sinf = (SummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, sinf.getOSVersion());
        dinf = (DocumentSummaryInformation) PropertySetFactory.create(new NDocumentInputStream((DocumentEntry) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
        assertEquals(131333, dinf.getOSVersion());
        assertContentsMatches(mini, (DocumentEntry) testDir.getEntry("Mini"));
        assertContentsMatches(main4096, (DocumentEntry) testDir.getEntry("Normal4096"));
        // Delete a directory, and add one more
        testDir.getEntry("Testing 456").delete();
        testDir.createDirectory("Testing ABC");
        // Save
        NPOIFSFileSystem fs6 = writeOutAndReadBack(fs5);
        fs5.close();
        // Check
        root = fs6.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        assertEquals(4, testDir.getEntryCount());
        assertThat(testDir.getEntryNames(), hasItem("Mini"));
        assertThat(testDir.getEntryNames(), hasItem("Normal4096"));
        assertThat(testDir.getEntryNames(), hasItem("Testing 789"));
        assertThat(testDir.getEntryNames(), hasItem("Testing ABC"));
        // Add another mini stream
        byte[] mini2 = new byte[] { -42, 0, -1, -2, -3, -4, -42 };
        testDir.createDocument("Mini2", new ByteArrayInputStream(mini2));
        // Save, load, check
        NPOIFSFileSystem fs7 = writeOutAndReadBack(fs6);
        fs6.close();
        root = fs7.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        assertEquals(5, testDir.getEntryCount());
        assertThat(testDir.getEntryNames(), hasItem("Mini"));
        assertThat(testDir.getEntryNames(), hasItem("Mini2"));
        assertThat(testDir.getEntryNames(), hasItem("Normal4096"));
        assertThat(testDir.getEntryNames(), hasItem("Testing 789"));
        assertThat(testDir.getEntryNames(), hasItem("Testing ABC"));
        assertContentsMatches(mini, (DocumentEntry) testDir.getEntry("Mini"));
        assertContentsMatches(mini2, (DocumentEntry) testDir.getEntry("Mini2"));
        assertContentsMatches(main4096, (DocumentEntry) testDir.getEntry("Normal4096"));
        // Delete a mini stream, add one more
        testDir.getEntry("Mini").delete();
        byte[] mini3 = new byte[] { 42, 0, 42, 0, 42, 0, 42 };
        testDir.createDocument("Mini3", new ByteArrayInputStream(mini3));
        // Save, load, check
        NPOIFSFileSystem fs8 = writeOutAndReadBack(fs7);
        fs7.close();
        root = fs8.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        assertEquals(5, testDir.getEntryCount());
        assertThat(testDir.getEntryNames(), hasItem("Mini2"));
        assertThat(testDir.getEntryNames(), hasItem("Mini3"));
        assertThat(testDir.getEntryNames(), hasItem("Normal4096"));
        assertThat(testDir.getEntryNames(), hasItem("Testing 789"));
        assertThat(testDir.getEntryNames(), hasItem("Testing ABC"));
        assertContentsMatches(mini2, (DocumentEntry) testDir.getEntry("Mini2"));
        assertContentsMatches(mini3, (DocumentEntry) testDir.getEntry("Mini3"));
        assertContentsMatches(main4096, (DocumentEntry) testDir.getEntry("Normal4096"));
        // Change some existing streams
        NPOIFSDocument mini2Doc = new NPOIFSDocument((DocumentNode) testDir.getEntry("Mini2"));
        mini2Doc.replaceContents(new ByteArrayInputStream(mini));
        byte[] main4106 = new byte[4106];
        main4106[0] = 41;
        main4106[4105] = 42;
        NPOIFSDocument mainDoc = new NPOIFSDocument((DocumentNode) testDir.getEntry("Normal4096"));
        mainDoc.replaceContents(new ByteArrayInputStream(main4106));
        // Re-check
        NPOIFSFileSystem fs9 = writeOutAndReadBack(fs8);
        fs8.close();
        root = fs9.getRoot();
        testDir = (DirectoryEntry) root.getEntry("Testing 123");
        assertEquals(5, root.getEntryCount());
        assertThat(root.getEntryNames(), hasItem("Thumbnail"));
        assertThat(root.getEntryNames(), hasItem("Image"));
        assertThat(root.getEntryNames(), hasItem("Testing 123"));
        assertThat(root.getEntryNames(), hasItem("DocumentSummaryInformation"));
        assertThat(root.getEntryNames(), hasItem("SummaryInformation"));
        assertEquals(5, testDir.getEntryCount());
        assertThat(testDir.getEntryNames(), hasItem("Mini2"));
        assertThat(testDir.getEntryNames(), hasItem("Mini3"));
        assertThat(testDir.getEntryNames(), hasItem("Normal4096"));
        assertThat(testDir.getEntryNames(), hasItem("Testing 789"));
        assertThat(testDir.getEntryNames(), hasItem("Testing ABC"));
        assertContentsMatches(mini, (DocumentEntry) testDir.getEntry("Mini2"));
        assertContentsMatches(mini3, (DocumentEntry) testDir.getEntry("Mini3"));
        assertContentsMatches(main4106, (DocumentEntry) testDir.getEntry("Normal4096"));
        // All done
        fs9.close();
    }
}
Also used : SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) Test(org.junit.Test)

Example 13 with SummaryInformation

use of org.apache.poi.hpsf.SummaryInformation in project poi by apache.

the class POIDocument method readProperties.

/**
     * Find, and create objects for, the standard
     *  Document Information Properties (HPSF).
     * If a given property set is missing or corrupt,
     *  it will remain null;
     */
protected void readProperties() {
    PropertySet ps;
    // DocumentSummaryInformation
    ps = getPropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    if (ps instanceof DocumentSummaryInformation) {
        dsInf = (DocumentSummaryInformation) ps;
    } else if (ps != null) {
        logger.log(POILogger.WARN, "DocumentSummaryInformation property set came back with wrong class - ", ps.getClass());
    } else {
        logger.log(POILogger.WARN, "DocumentSummaryInformation property set came back as null");
    }
    // SummaryInformation
    ps = getPropertySet(SummaryInformation.DEFAULT_STREAM_NAME);
    if (ps instanceof SummaryInformation) {
        sInf = (SummaryInformation) ps;
    } else if (ps != null) {
        logger.log(POILogger.WARN, "SummaryInformation property set came back with wrong class - ", ps.getClass());
    } else {
        logger.log(POILogger.WARN, "SummaryInformation property set came back as null");
    }
    // Mark the fact that we've now loaded up the properties
    initialized = true;
}
Also used : SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) PropertySet(org.apache.poi.hpsf.PropertySet) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation)

Example 14 with SummaryInformation

use of org.apache.poi.hpsf.SummaryInformation in project poi by apache.

the class PropertySetDescriptorRenderer method getTreeCellRendererComponent.

@Override
public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean selectedCell, final boolean expanded, final boolean leaf, final int row, final boolean hasCellFocus) {
    final PropertySetDescriptor d = (PropertySetDescriptor) ((DefaultMutableTreeNode) value).getUserObject();
    final PropertySet ps = d.getPropertySet();
    final JPanel p = new JPanel();
    final JTextArea text = new JTextArea();
    text.setBackground(new Color(200, 255, 200));
    text.setFont(new Font("Monospaced", Font.PLAIN, 10));
    text.append(renderAsString(d));
    text.append("\nByte order: ");
    text.append(HexDump.toHex((short) ps.getByteOrder()));
    text.append("\nFormat: ");
    text.append(HexDump.toHex((short) ps.getFormat()));
    text.append("\nOS version: ");
    text.append(HexDump.toHex(ps.getOSVersion()));
    text.append("\nClass ID: ");
    text.append(HexDump.toHex(ps.getClassID().getBytes()));
    text.append("\nSection count: " + ps.getSectionCount());
    text.append(sectionsToString(ps.getSections()));
    p.add(text);
    if (ps instanceof SummaryInformation) {
        /* Use the convenience methods. */
        final SummaryInformation si = (SummaryInformation) ps;
        text.append("\n");
        text.append("\nTitle:               " + si.getTitle());
        text.append("\nSubject:             " + si.getSubject());
        text.append("\nAuthor:              " + si.getAuthor());
        text.append("\nKeywords:            " + si.getKeywords());
        text.append("\nComments:            " + si.getComments());
        text.append("\nTemplate:            " + si.getTemplate());
        text.append("\nLast Author:         " + si.getLastAuthor());
        text.append("\nRev. Number:         " + si.getRevNumber());
        text.append("\nEdit Time:           " + si.getEditTime());
        text.append("\nLast Printed:        " + si.getLastPrinted());
        text.append("\nCreate Date/Time:    " + si.getCreateDateTime());
        text.append("\nLast Save Date/Time: " + si.getLastSaveDateTime());
        text.append("\nPage Count:          " + si.getPageCount());
        text.append("\nWord Count:          " + si.getWordCount());
        text.append("\nChar Count:          " + si.getCharCount());
        // text.append("\nThumbnail:           " + si.getThumbnail());
        text.append("\nApplication Name:    " + si.getApplicationName());
        text.append("\nSecurity:            " + si.getSecurity());
    }
    if (selectedCell)
        Util.invert(text);
    return p;
}
Also used : JPanel(javax.swing.JPanel) JTextArea(javax.swing.JTextArea) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) Color(java.awt.Color) PropertySet(org.apache.poi.hpsf.PropertySet) Font(java.awt.Font)

Example 15 with SummaryInformation

use of org.apache.poi.hpsf.SummaryInformation in project poi by apache.

the class TestWrite method inPlaceNPOIFSWrite.

/**
     * Tests that when using NPOIFS, we can do an in-place write
     *  without needing to stream in + out the whole kitchen sink
     */
@Test
public void inPlaceNPOIFSWrite() throws Exception {
    NPOIFSFileSystem fs = null;
    DirectoryEntry root = null;
    DocumentNode sinfDoc = null;
    DocumentNode dinfDoc = null;
    SummaryInformation sinf = null;
    DocumentSummaryInformation dinf = null;
    // We need to work on a File for in-place changes, so create a temp one
    final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
    copy.deleteOnExit();
    // Copy a test file over to our temp location
    InputStream inp = _samples.openResourceAsStream("TestShiftJIS.doc");
    FileOutputStream out = new FileOutputStream(copy);
    IOUtils.copy(inp, out);
    inp.close();
    out.close();
    // Open the copy in read/write mode
    fs = new NPOIFSFileSystem(copy, false);
    root = fs.getRoot();
    // Read the properties in there
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream);
    sinfStream.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream);
    dinfStream.close();
    assertEquals(131077, dinf.getOSVersion());
    // Check they start as we expect
    assertEquals("Reiichiro Hori", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("第1章", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals(null, dinf.getManager());
    // Do an in-place replace via an InputStream
    new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream());
    new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream());
    // Check it didn't get changed
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream2 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream2);
    sinfStream2.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream2 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream2);
    dinfStream2.close();
    assertEquals(131077, dinf.getOSVersion());
    // Start again!
    fs.close();
    inp = _samples.openResourceAsStream("TestShiftJIS.doc");
    out = new FileOutputStream(copy);
    IOUtils.copy(inp, out);
    inp.close();
    out.close();
    fs = new NPOIFSFileSystem(copy, false);
    root = fs.getRoot();
    // Read the properties in once more
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream3 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream3);
    sinfStream3.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream3 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream3);
    dinfStream3.close();
    assertEquals(131077, dinf.getOSVersion());
    // Have them write themselves in-place with no changes, as an OutputStream
    OutputStream soufStream = new NDocumentOutputStream(sinfDoc);
    sinf.write(soufStream);
    soufStream.close();
    OutputStream doufStream = new NDocumentOutputStream(dinfDoc);
    dinf.write(doufStream);
    doufStream.close();
    // And also write to some bytes for checking
    ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream();
    sinf.write(sinfBytes);
    ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream();
    dinf.write(dinfBytes);
    // Check that the filesystem can give us back the same bytes
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream4 = new NDocumentInputStream(sinfDoc);
    byte[] sinfData = IOUtils.toByteArray(sinfStream4);
    sinfStream4.close();
    InputStream dinfStream4 = new NDocumentInputStream(dinfDoc);
    byte[] dinfData = IOUtils.toByteArray(dinfStream4);
    dinfStream4.close();
    assertThat(sinfBytes.toByteArray(), equalTo(sinfData));
    assertThat(dinfBytes.toByteArray(), equalTo(dinfData));
    // Read back in as-is
    InputStream sinfStream5 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream5);
    sinfStream5.close();
    assertEquals(131077, sinf.getOSVersion());
    InputStream dinfStream5 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream5);
    dinfStream5.close();
    assertEquals(131077, dinf.getOSVersion());
    assertEquals("Reiichiro Hori", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("第1章", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals(null, dinf.getManager());
    // Now alter a few of them
    sinf.setAuthor("Changed Author");
    sinf.setTitle("Le titre était changé");
    dinf.setManager("Changed Manager");
    // Save this into the filesystem
    OutputStream soufStream2 = new NDocumentOutputStream(sinfDoc);
    sinf.write(soufStream2);
    soufStream2.close();
    OutputStream doufStream2 = new NDocumentOutputStream(dinfDoc);
    dinf.write(doufStream2);
    doufStream2.close();
    // Read them back in again
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream6 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream6);
    sinfStream6.close();
    assertEquals(131077, sinf.getOSVersion());
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream dinfStream6 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream6);
    dinfStream6.close();
    assertEquals(131077, dinf.getOSVersion());
    assertEquals("Changed Author", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("Le titre était changé", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals("Changed Manager", dinf.getManager());
    // Close the whole filesystem, and open it once more
    fs.writeFilesystem();
    fs.close();
    fs = new NPOIFSFileSystem(copy);
    root = fs.getRoot();
    // Re-check on load
    sinfDoc = (DocumentNode) root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    InputStream sinfStream7 = new NDocumentInputStream(sinfDoc);
    sinf = (SummaryInformation) PropertySetFactory.create(sinfStream7);
    sinfStream7.close();
    assertEquals(131077, sinf.getOSVersion());
    dinfDoc = (DocumentNode) root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    InputStream dinfStream7 = new NDocumentInputStream(dinfDoc);
    dinf = (DocumentSummaryInformation) PropertySetFactory.create(dinfStream7);
    dinfStream7.close();
    assertEquals(131077, dinf.getOSVersion());
    assertEquals("Changed Author", sinf.getAuthor());
    assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
    assertEquals("Le titre était changé", sinf.getTitle());
    assertEquals("", dinf.getCompany());
    assertEquals("Changed Manager", dinf.getManager());
    // Tidy up
    fs.close();
    copy.delete();
}
Also used : DocumentNode(org.apache.poi.poifs.filesystem.DocumentNode) NDocumentInputStream(org.apache.poi.poifs.filesystem.NDocumentInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) NPOIFSDocument(org.apache.poi.poifs.filesystem.NPOIFSDocument) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) NDocumentInputStream(org.apache.poi.poifs.filesystem.NDocumentInputStream) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) FileOutputStream(java.io.FileOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Aggregations

SummaryInformation (org.apache.poi.hpsf.SummaryInformation)22 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)17 Test (org.junit.Test)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)7 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)7 PropertySet (org.apache.poi.hpsf.PropertySet)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 CustomProperties (org.apache.poi.hpsf.CustomProperties)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2 HPSFPropertiesOnlyDocument (org.apache.poi.hpsf.HPSFPropertiesOnlyDocument)2 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)2 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)2 Color (java.awt.Color)1 Font (java.awt.Font)1