Search in sources :

Example 1 with UnsupportedVariantTypeException

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

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HPSFException (org.apache.poi.hpsf.HPSFException)1 IllegalPropertySetDataException (org.apache.poi.hpsf.IllegalPropertySetDataException)1 MutableProperty (org.apache.poi.hpsf.MutableProperty)1 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)1 MutableSection (org.apache.poi.hpsf.MutableSection)1 NoFormatIDException (org.apache.poi.hpsf.NoFormatIDException)1 NoPropertySetStreamException (org.apache.poi.hpsf.NoPropertySetStreamException)1 PropertySet (org.apache.poi.hpsf.PropertySet)1 ReadingNotSupportedException (org.apache.poi.hpsf.ReadingNotSupportedException)1 Section (org.apache.poi.hpsf.Section)1 UnsupportedVariantTypeException (org.apache.poi.hpsf.UnsupportedVariantTypeException)1