Search in sources :

Example 16 with DocumentSummaryInformation

use of org.apache.poi.hpsf.DocumentSummaryInformation 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)

Example 17 with DocumentSummaryInformation

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

the class TestWriteWellKnown method write1stFile.

/*
     * Write all properties supported by HPSF to the summary information
     * (e.g. author, edit date, application name) and to the document
     * summary information (e.g. company, manager).
     */
private static CustomProperties write1stFile(File fileIn, File fileOut) throws Exception {
    /* Read a test document <em>doc1</em> into a POI filesystem. */
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(fileIn, false);
    /*
         * Read the summary information stream and the document summary
         * information stream from the POI filesystem.
         *
         * Please note that the result consists of SummaryInformation and
         * DocumentSummaryInformation instances which are in memory only. To
         * make them permanent they have to be written to a POI filesystem
         * explicitly (overwriting the former contents). Then the POI filesystem
         * should be saved to a file.
         */
    SummaryInformation si = getSummaryInformation(poifs);
    DocumentSummaryInformation dsi = getDocumentSummaryInformation(poifs);
    si.setApplicationName(P_APPLICATION_NAME);
    si.setAuthor(P_AUTHOR);
    si.setCharCount(P_CHAR_COUNT);
    si.setComments(P_COMMENTS);
    si.setCreateDateTime(P_CREATE_DATE_TIME);
    si.setEditTime(P_EDIT_TIME);
    si.setKeywords(P_KEYWORDS);
    si.setLastAuthor(P_LAST_AUTHOR);
    si.setLastPrinted(P_LAST_PRINTED);
    si.setLastSaveDateTime(P_LAST_SAVE_DATE_TIME);
    si.setPageCount(P_PAGE_COUNT);
    si.setRevNumber(P_REV_NUMBER);
    si.setSecurity(P_SECURITY);
    si.setSubject(P_SUBJECT);
    si.setTemplate(P_TEMPLATE);
    // FIXME (byte array properties not yet implemented): si.setThumbnail(P_THUMBNAIL);
    si.setTitle(P_TITLE);
    si.setWordCount(P_WORD_COUNT);
    dsi.setByteCount(P_BYTE_COUNT);
    dsi.setCategory(P_CATEGORY);
    dsi.setCompany(P_COMPANY);
    // FIXME (byte array properties not yet implemented): dsi.setDocparts(P_DOCPARTS);
    // FIXME (byte array properties not yet implemented): dsi.setHeadingPair(P_HEADING_PAIR);
    dsi.setHiddenCount(P_HIDDEN_COUNT);
    dsi.setLineCount(P_LINE_COUNT);
    dsi.setLinksDirty(P_LINKS_DIRTY);
    dsi.setManager(P_MANAGER);
    dsi.setMMClipCount(P_MM_CLIP_COUNT);
    dsi.setNoteCount(P_NOTE_COUNT);
    dsi.setParCount(P_PAR_COUNT);
    dsi.setPresentationFormat(P_PRESENTATION_FORMAT);
    dsi.setScale(P_SCALE);
    dsi.setSlideCount(P_SLIDE_COUNT);
    CustomProperties cps = dsi.getCustomProperties();
    assertNull(cps);
    cps = new CustomProperties();
    cps.put("Schlüssel ä", "Wert ä");
    cps.put("Schlüssel äö", "Wert äö");
    cps.put("Schlüssel äöü", "Wert äöü");
    cps.put("Schlüssel äöüÖ", "Wert äöüÖ");
    cps.put("positive_Integer", POSITIVE_INTEGER);
    cps.put("positive_Long", POSITIVE_LONG);
    cps.put("positive_Double", POSITIVE_DOUBLE);
    cps.put("negative_Integer", NEGATIVE_INTEGER);
    cps.put("negative_Long", NEGATIVE_LONG);
    cps.put("negative_Double", NEGATIVE_DOUBLE);
    cps.put("Boolean", Boolean.TRUE);
    cps.put("Date", now);
    cps.put("max_Integer", MAX_INTEGER);
    cps.put("min_Integer", MIN_INTEGER);
    cps.put("max_Long", MAX_LONG);
    cps.put("min_Long", MIN_LONG);
    cps.put("max_Double", MAX_DOUBLE);
    cps.put("min_Double", MIN_DOUBLE);
    // Check the keys went in
    assertTrue(cps.containsKey("Schlüssel ä"));
    assertTrue(cps.containsKey("Boolean"));
    // Check the values went in
    assertEquals("Wert ä", cps.get("Schlüssel ä"));
    assertEquals(Boolean.TRUE, cps.get("Boolean"));
    assertTrue(cps.containsValue(Boolean.TRUE));
    assertTrue(cps.containsValue("Wert ä"));
    // Check that things that aren't in aren't in
    assertFalse(cps.containsKey("False Boolean"));
    assertFalse(cps.containsValue(Boolean.FALSE));
    // Save as our custom properties
    dsi.setCustomProperties(cps);
    /* Write the summary information stream and the document summary
         * information stream to the POI filesystem. */
    si.write(poifs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    dsi.write(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    /* Write the POI filesystem to a (temporary) file <em>doc2</em>
         * and close the latter. */
    OutputStream out = new FileOutputStream(fileOut);
    poifs.writeFilesystem(out);
    out.close();
    poifs.close();
    return cps;
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties)

Example 18 with DocumentSummaryInformation

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

the class TestWriteWellKnown method write3rdFile.

/*
     * Open {@code doc3} for reading and check summary information
     * and document summary information. All properties removed before must not
     * be found in the property streams of {@code doc3}.
     */
private static CustomProperties write3rdFile(File fileIn, File fileOut) throws Exception {
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(fileIn, false);
    SummaryInformation si = getSummaryInformation(poifs);
    DocumentSummaryInformation dsi = getDocumentSummaryInformation(poifs);
    assertNull(si.getApplicationName());
    assertNull(si.getAuthor());
    assertEquals(0, si.getCharCount());
    assertTrue(si.wasNull());
    assertNull(si.getComments());
    assertNull(si.getCreateDateTime());
    assertEquals(0, si.getEditTime());
    assertTrue(si.wasNull());
    assertNull(si.getKeywords());
    assertNull(si.getLastAuthor());
    assertNull(si.getLastPrinted());
    assertNull(si.getLastSaveDateTime());
    assertEquals(0, si.getPageCount());
    assertTrue(si.wasNull());
    assertNull(si.getRevNumber());
    assertEquals(0, si.getSecurity());
    assertTrue(si.wasNull());
    assertNull(si.getSubject());
    assertNull(si.getTemplate());
    assertNull(si.getThumbnail());
    assertNull(si.getTitle());
    assertEquals(0, si.getWordCount());
    assertTrue(si.wasNull());
    assertEquals(0, dsi.getByteCount());
    assertTrue(dsi.wasNull());
    assertNull(dsi.getCategory());
    assertNull(dsi.getCustomProperties());
    // FIXME (byte array properties not yet implemented): assertNull(dsi.getDocparts());
    // FIXME (byte array properties not yet implemented): assertNull(dsi.getHeadingPair());
    assertEquals(0, dsi.getHiddenCount());
    assertTrue(dsi.wasNull());
    assertEquals(0, dsi.getLineCount());
    assertTrue(dsi.wasNull());
    assertFalse(dsi.getLinksDirty());
    assertTrue(dsi.wasNull());
    assertNull(dsi.getManager());
    assertEquals(0, dsi.getMMClipCount());
    assertTrue(dsi.wasNull());
    assertEquals(0, dsi.getNoteCount());
    assertTrue(dsi.wasNull());
    assertEquals(0, dsi.getParCount());
    assertTrue(dsi.wasNull());
    assertNull(dsi.getPresentationFormat());
    assertFalse(dsi.getScale());
    assertTrue(dsi.wasNull());
    assertEquals(0, dsi.getSlideCount());
    assertTrue(dsi.wasNull());
    poifs.close();
    return dsi.getCustomProperties();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation)

Example 19 with DocumentSummaryInformation

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

the class TestWriteWellKnown method getDocumentSummaryInformation.

static DocumentSummaryInformation getDocumentSummaryInformation(NPOIFSFileSystem poifs) throws IOException, NoPropertySetStreamException, UnexpectedPropertySetTypeException, MarkUnsupportedException {
    if (!poifs.getRoot().hasEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)) {
        return null;
    }
    DocumentInputStream dis = poifs.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    PropertySet ps = new PropertySet(dis);
    DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps);
    dis.close();
    return dsi;
}
Also used : PropertySet(org.apache.poi.hpsf.PropertySet) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 20 with DocumentSummaryInformation

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

the class TestHPSFBugs method test54233.

/**
    * Some files seem to want the length and data to be on a 4-byte boundary,
    * and without that you'll hit an ArrayIndexOutOfBoundsException after
    * reading junk
    */
@Test
public void test54233() throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
    InputStream is = _samples.openResourceAsStream("TestNon4ByteBoundary.doc");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
    is.close();
    SummaryInformation si = (SummaryInformation) PropertySetFactory.create(fs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    DocumentSummaryInformation dsi = (DocumentSummaryInformation) PropertySetFactory.create(fs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    // Test
    assertEquals("Microsoft Word 10.0", si.getApplicationName());
    assertEquals("", si.getTitle());
    assertEquals("", si.getAuthor());
    assertEquals("Cour de Justice", dsi.getCompany());
    // Write out and read back, should still be valid
    POIDocument doc = new HPSFPropertiesOnlyDocument(fs);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.write(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    doc = new HPSFPropertiesOnlyDocument(new NPOIFSFileSystem(bais));
    // Check properties are still there
    assertEquals("Microsoft Word 10.0", si.getApplicationName());
    assertEquals("", si.getTitle());
    assertEquals("", si.getAuthor());
    assertEquals("Cour de Justice", dsi.getCompany());
    doc.close();
    fs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) POIDocument(org.apache.poi.POIDocument) HPSFPropertiesOnlyDocument(org.apache.poi.hpsf.HPSFPropertiesOnlyDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)22 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)15 Test (org.junit.Test)10 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6 CustomProperties (org.apache.poi.hpsf.CustomProperties)6 PropertySet (org.apache.poi.hpsf.PropertySet)5 InputStream (java.io.InputStream)4 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)4 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 HPSFPropertiesOnlyDocument (org.apache.poi.hpsf.HPSFPropertiesOnlyDocument)2 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1