Search in sources :

Example 1 with PropertySet

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

the class CopyCompare method equal.

/**
     * <p>Compares two {@link DocumentEntry} instances of a POI file system.
     * Documents that are not property set streams must be bitwise identical.
     * Property set streams must be logically equal.</p>
     *
     * @param d1 The first document.
     * @param d2 The second document.
     * @param msg The method may append human-readable comparison messages to
     * this string buffer. 
     * @return <code>true</code> if the documents are equal, else
     * <code>false</code>.
     * @exception MarkUnsupportedException if a POI document stream does not
     * support the mark() operation.
     * @exception NoPropertySetStreamException if the application tries to
     * create a property set from a POI document stream that is not a property
     * set stream.
     * @throws UnsupportedEncodingException 
     * @exception IOException if any I/O exception occurs.
     */
private static boolean equal(final DocumentEntry d1, final DocumentEntry d2, final StringBuffer msg) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException {
    final DocumentInputStream dis1 = new DocumentInputStream(d1);
    final DocumentInputStream dis2 = new DocumentInputStream(d2);
    try {
        if (PropertySet.isPropertySetStream(dis1) && PropertySet.isPropertySetStream(dis2)) {
            final PropertySet ps1 = PropertySetFactory.create(dis1);
            final PropertySet ps2 = PropertySetFactory.create(dis2);
            if (!ps1.equals(ps2)) {
                msg.append("Property sets are not equal.\n");
                return false;
            }
        } else {
            int i1, i2;
            do {
                i1 = dis1.read();
                i2 = dis2.read();
                if (i1 != i2) {
                    msg.append("Documents are not equal.\n");
                    return false;
                }
            } while (i1 > -1);
        }
    } finally {
        dis2.close();
        dis1.close();
    }
    return true;
}
Also used : PropertySet(org.apache.poi.hpsf.PropertySet) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) DocumentInputStream(org.apache.poi.poifs.filesystem.DocumentInputStream)

Example 2 with PropertySet

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

the class TestWrite method writeTwoSections.

/**
     * <p>Writes a simple property set with two sections to a POIFS and reads it
     * back in.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception WritingNotSupportedException if HPSF does not yet support
     * a variant type to be written
     */
@Test
public void writeTwoSections() throws WritingNotSupportedException, IOException {
    final String STREAM_NAME = "PropertySetStream";
    final String SECTION1 = "Section 1";
    final String SECTION2 = "Section 2";
    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();
    ps.clearSections();
    final ClassID formatID = new ClassID();
    formatID.setBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
    final MutableSection s1 = new MutableSection();
    s1.setFormatID(formatID);
    s1.setProperty(2, SECTION1);
    ps.addSection(s1);
    final MutableSection s2 = new MutableSection();
    s2.setFormatID(formatID);
    s2.setProperty(2, SECTION2);
    ps.addSection(s2);
    poiFs.createDocument(ps.toInputStream(), 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) {
                throw new RuntimeException(ex);
            }
        }
    }, STREAM_NAME);
    FileInputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
    assertNotNull(psa[0]);
    Section s = (psa[0].getSections().get(0));
    assertEquals(s.getFormatID(), formatID);
    Object p = s.getProperty(2);
    assertEquals(SECTION1, p);
    s = (psa[0].getSections().get(1));
    p = s.getProperty(2);
    assertEquals(SECTION2, p);
}
Also used : MutableSection(org.apache.poi.hpsf.MutableSection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NDocumentOutputStream(org.apache.poi.poifs.filesystem.NDocumentOutputStream) FileOutputStream(java.io.FileOutputStream) ClassID(org.apache.poi.hpsf.ClassID) 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 3 with PropertySet

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

the class TestEmptyProperties method testPropertySetMethods.

/**
     * <p>Tests the {@link PropertySet} methods. The test file has two
     * property sets: the first one is a {@link SummaryInformation},
     * the second one is a {@link DocumentSummaryInformation}.</p>
     *
     * @exception IOException if an I/O exception occurs
     * @exception HPSFException if an HPSF operation fails
     */
@Test
public void testPropertySetMethods() throws IOException, HPSFException {
    byte[] b = poiFiles.get(1).getBytes();
    PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b));
    SummaryInformation s = (SummaryInformation) ps;
    assertNull(s.getTitle());
    assertNull(s.getSubject());
    assertNotNull(s.getAuthor());
    assertNull(s.getKeywords());
    assertNull(s.getComments());
    assertNotNull(s.getTemplate());
    assertNotNull(s.getLastAuthor());
    assertNotNull(s.getRevNumber());
    assertEquals(s.getEditTime(), 0);
    assertNull(s.getLastPrinted());
    assertNull(s.getCreateDateTime());
    assertNull(s.getLastSaveDateTime());
    assertEquals(s.getPageCount(), 0);
    assertEquals(s.getWordCount(), 0);
    assertEquals(s.getCharCount(), 0);
    assertNull(s.getThumbnail());
    assertNull(s.getApplicationName());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) PropertySet(org.apache.poi.hpsf.PropertySet) Test(org.junit.Test)

Example 4 with PropertySet

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

the class TestWrite method unicodeWrite8Bit.

/**
     * Tests whether writing 8-bit characters to a Unicode property succeeds.
     */
@Test
public void unicodeWrite8Bit() throws WritingNotSupportedException, IOException, NoPropertySetStreamException {
    final String TITLE = "This is a sample title";
    final MutablePropertySet mps = new MutablePropertySet();
    final MutableSection ms = (MutableSection) mps.getSections().get(0);
    ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
    final MutableProperty p = new MutableProperty();
    p.setID(PropertyIDMap.PID_TITLE);
    p.setType(Variant.VT_LPSTR);
    p.setValue(TITLE);
    ms.setProperty(p);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    mps.write(out);
    out.close();
    byte[] bytes = out.toByteArray();
    PropertySet psr = new PropertySet(bytes);
    assertTrue(psr.isSummaryInformation());
    Section sr = psr.getSections().get(0);
    String title = (String) sr.getProperty(PropertyIDMap.PID_TITLE);
    assertEquals(TITLE, title);
}
Also used : MutableProperty(org.apache.poi.hpsf.MutableProperty) MutableSection(org.apache.poi.hpsf.MutableSection) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) PropertySet(org.apache.poi.hpsf.PropertySet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MutableSection(org.apache.poi.hpsf.MutableSection) Section(org.apache.poi.hpsf.Section) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 5 with PropertySet

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

the class TestWrite method dictionary.

/**
     * <p>Tests writing and reading back a proper dictionary.</p>
     * @throws IOException 
     * @throws HPSFException 
     */
@Test
public void dictionary() throws IOException, HPSFException {
    final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
    copy.deleteOnExit();
    /* Write: */
    final OutputStream out = new FileOutputStream(copy);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps1 = new MutablePropertySet();
    final MutableSection s = (MutableSection) ps1.getSections().get(0);
    final Map<Long, String> m = new HashMap<Long, String>(3, 1.0f);
    m.put(Long.valueOf(1), "String 1");
    m.put(Long.valueOf(2), "String 2");
    m.put(Long.valueOf(3), "String 3");
    s.setDictionary(m);
    s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
    int codepage = CodePageUtil.CP_UNICODE;
    s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, codepage);
    poiFs.createDocument(ps1.toInputStream(), "Test");
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read back: */
    final List<POIFile> psf = Util.readPropertySets(copy);
    assertEquals(1, psf.size());
    final byte[] bytes = psf.get(0).getBytes();
    final InputStream in = new ByteArrayInputStream(bytes);
    final PropertySet ps2 = PropertySetFactory.create(in);
    /* Check if the result is a DocumentSummaryInformation stream, as
         * specified. */
    assertTrue(ps2.isDocumentSummaryInformation());
    /* Compare the property set stream with the corresponding one
         * from the origin file and check whether they are equal. */
    assertEquals(ps1, ps2);
}
Also used : HashMap(java.util.HashMap) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

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