Search in sources :

Example 11 with PropertySet

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

the class TestUnicode method testPropertySetMethods.

/**
     * Tests the {@link PropertySet} methods. The test file has two
     * property set: the first one is a {@link SummaryInformation},
     * the second one is a {@link DocumentSummaryInformation}.
     *
     * @exception IOException if an I/O exception occurs
     * @exception HPSFException if an HPSF exception occurs
     */
@Test
public void testPropertySetMethods() throws IOException, HPSFException {
    POIFile poiFile = Util.readPOIFiles(data, POI_FILES).get(0);
    byte[] b = poiFile.getBytes();
    PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b));
    assertTrue(ps.isDocumentSummaryInformation());
    assertEquals(ps.getSectionCount(), 2);
    Section s = ps.getSections().get(1);
    assertEquals(s.getProperty(1), CodePageUtil.CP_UTF16);
    assertEquals(s.getProperty(2), -96070278);
    assertEquals(s.getProperty(3), "MCon_Info zu Office bei Schreiner");
    assertEquals(s.getProperty(4), "petrovitsch@schreiner-online.de");
    assertEquals(s.getProperty(5), "Petrovitsch, Wilhelm");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PropertySet(org.apache.poi.hpsf.PropertySet) Section(org.apache.poi.hpsf.Section) Test(org.junit.Test)

Example 12 with PropertySet

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

the class TestReadAllFiles method recreate.

/**
     * This test method does a write and read back test with all POI
     * filesystems in the "data" directory by performing the following
     * actions for each file:<p>
     *
     * <ul>
     * <li>Read its property set streams.
     * <li>Create a new POI filesystem containing the origin file's property set streams.
     * <li>Read the property set streams from the POI filesystem just created.
     * <li>Compare each property set stream with the corresponding one from
     * the origin file and check whether they are equal.
     * </ul>
     */
@Test
public void recreate() throws IOException, HPSFException {
    /* Read the POI filesystem's property set streams: */
    Map<String, PropertySet> psMap = new HashMap<String, PropertySet>();
    /* Create a new POI filesystem containing the origin file's
         * property set streams: */
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    for (POIFile poifile : Util.readPropertySets(file)) {
        final InputStream in = new ByteArrayInputStream(poifile.getBytes());
        final PropertySet psIn = PropertySetFactory.create(in);
        psMap.put(poifile.getName(), psIn);
        bos.reset();
        psIn.write(bos);
        poiFs.createDocument(new ByteArrayInputStream(bos.toByteArray()), poifile.getName());
    }
    /* Read the property set streams from the POI filesystem just
         * created. */
    for (Map.Entry<String, PropertySet> me : psMap.entrySet()) {
        final PropertySet ps1 = me.getValue();
        final PropertySet ps2 = PropertySetFactory.create(poiFs.getRoot(), me.getKey());
        assertNotNull(ps2);
        /* Compare the property set stream with the corresponding one
             * from the origin file and check whether they are equal. */
        // Because of missing 0-paddings in the original input files, the bytes might differ.
        // This fixes the comparison
        String ps1str = ps1.toString().replace(" 00", "   ").replace(".", " ").replaceAll("(?m)( +$|(size|offset): [0-9]+)", "");
        String ps2str = ps2.toString().replace(" 00", "   ").replace(".", " ").replaceAll("(?m)( +$|(size|offset): [0-9]+)", "");
        assertEquals("Equality for file " + file.getName(), ps1str, ps2str);
    }
    poiFs.close();
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PropertySet(org.apache.poi.hpsf.PropertySet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with PropertySet

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

the class TestWrite method writeSimplePropertySet.

/**
     * <p>Writes a simple property set with a SummaryInformation section to a
     * POIFS and reads it back in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception UnsupportedVariantTypeException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeSimplePropertySet() throws IOException, UnsupportedVariantTypeException {
    final String AUTHOR = "Rainer Klute";
    final String TITLE = "Test Document";
    final File dataDir = _samples.getFile("");
    final File filename = new File(dataDir, POI_FS);
    filename.deleteOnExit();
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    final MutableSection si = new MutableSection();
    si.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
    ps.clearSections();
    ps.addSection(si);
    final MutableProperty p = new MutableProperty();
    p.setID(PropertyIDMap.PID_AUTHOR);
    p.setType(Variant.VT_LPWSTR);
    p.setValue(AUTHOR);
    si.setProperty(p);
    si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE);
    poiFs.createDocument(ps.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final PropertySet[] psa = new PropertySet[1];
    final POIFSReader r = new POIFSReader();
    r.registerListener(new POIFSReaderListener() {

        @Override
        public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
            try {
                psa[0] = PropertySetFactory.create(event.getStream());
            } catch (Exception ex) {
                fail(ex.getMessage());
            }
        }
    }, SummaryInformation.DEFAULT_STREAM_NAME);
    InputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    assertNotNull(psa[0]);
    assertTrue(psa[0].isSummaryInformation());
    final Section s = (psa[0].getSections().get(0));
    Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR);
    Object p2 = s.getProperty(PropertyIDMap.PID_TITLE);
    assertEquals(AUTHOR, p1);
    assertEquals(TITLE, p2);
}
Also used : MutableProperty(org.apache.poi.hpsf.MutableProperty) MutableSection(org.apache.poi.hpsf.MutableSection) 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) POIFSReaderListener(org.apache.poi.poifs.eventfilesystem.POIFSReaderListener) MutableSection(org.apache.poi.hpsf.MutableSection) Section(org.apache.poi.hpsf.Section) NoPropertySetStreamException(org.apache.poi.hpsf.NoPropertySetStreamException) WritingNotSupportedException(org.apache.poi.hpsf.WritingNotSupportedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalPropertySetDataException(org.apache.poi.hpsf.IllegalPropertySetDataException) UnsupportedVariantTypeException(org.apache.poi.hpsf.UnsupportedVariantTypeException) IOException(java.io.IOException) NoFormatIDException(org.apache.poi.hpsf.NoFormatIDException) ReadingNotSupportedException(org.apache.poi.hpsf.ReadingNotSupportedException) HPSFException(org.apache.poi.hpsf.HPSFException) FileInputStream(java.io.FileInputStream) POIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) FileOutputStream(java.io.FileOutputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) PropertySet(org.apache.poi.hpsf.PropertySet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 14 with PropertySet

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

the class TestWriteWellKnown method getSummaryInformation.

private static SummaryInformation getSummaryInformation(NPOIFSFileSystem poifs) throws Exception {
    DocumentInputStream dis = poifs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME);
    PropertySet ps = new PropertySet(dis);
    SummaryInformation si = new SummaryInformation(ps);
    dis.close();
    return si;
}
Also used : SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) PropertySet(org.apache.poi.hpsf.PropertySet) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 15 with PropertySet

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

Aggregations

PropertySet (org.apache.poi.hpsf.PropertySet)17 Test (org.junit.Test)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)6 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)6 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)5 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)5 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)5 MutableSection (org.apache.poi.hpsf.MutableSection)4 Section (org.apache.poi.hpsf.Section)4 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 NoPropertySetStreamException (org.apache.poi.hpsf.NoPropertySetStreamException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2