Search in sources :

Example 76 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem 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 77 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem 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 78 with NPOIFSFileSystem

use of org.apache.poi.poifs.filesystem.NPOIFSFileSystem 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)

Example 79 with NPOIFSFileSystem

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

the class TestHPSFBugs method test56138.

/**
    * CodePage Strings can be zero length
    */
@Test
public void test56138() throws IOException, NoPropertySetStreamException {
    InputStream is = _samples.openResourceAsStream("TestZeroLengthCodePage.mpp");
    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("MSProject", si.getApplicationName());
    assertEquals("project1", si.getTitle());
    assertEquals("Jon Iles", si.getAuthor());
    assertEquals("", dsi.getCompany());
    assertEquals(2, dsi.getSectionCount());
    fs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) 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) Test(org.junit.Test)

Example 80 with NPOIFSFileSystem

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

the class TestPOIFSDump method testFailToWrite.

@Test(expected = IndexOutOfBoundsException.class)
public void testFailToWrite() throws IOException {
    File dir = TempFile.createTempFile("TestPOIFSDump", ".tst");
    assertTrue("Had: " + dir, dir.exists());
    assertTrue("Had: " + dir, dir.delete());
    assertTrue("Had: " + dir, dir.mkdirs());
    FileInputStream is = new FileInputStream(TEST_FILE);
    NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
    is.close();
    NPropertyTable props = fs.getPropertyTable();
    assertNotNull(props);
    // try with an invalid startBlock to trigger an exception
    // to validate that file-handles are closed properly
    POIFSDump.dump(fs, 999999999, "mini-stream", dir);
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)101 Test (org.junit.Test)57 File (java.io.File)35 InputStream (java.io.InputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)14 FileOutputStream (java.io.FileOutputStream)12 TempFile (org.apache.poi.util.TempFile)12 FileInputStream (java.io.FileInputStream)11 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)10 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)10 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)9 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)9 IOException (java.io.IOException)8 OutputStream (java.io.OutputStream)8 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)7 TikaInputStream (org.apache.tika.io.TikaInputStream)6 AgileDecryptor (org.apache.poi.poifs.crypt.agile.AgileDecryptor)5 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)5