Search in sources :

Example 6 with MutableSection

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

the class WriteTitle method main.

/**
     * <p>Runs the example program.</p>
     *
     * @param args Command-line arguments. The first and only command-line 
     * argument is the name of the POI file system to create.
     * @throws IOException if any I/O exception occurs.
     * @throws WritingNotSupportedException if HPSF does not (yet) support 
     * writing a certain property type.
     */
public static void main(final String[] args) throws WritingNotSupportedException, IOException {
    /* Check whether we have exactly one command-line argument. */
    if (args.length != 1) {
        System.err.println("Usage: " + WriteTitle.class.getName() + "destinationPOIFS");
        System.exit(1);
    }
    final String fileName = args[0];
    /* Create a mutable property set. Initially it contains a single section
         * with no properties. */
    final MutablePropertySet mps = new MutablePropertySet();
    /* Retrieve the section the property set already contains. */
    final MutableSection ms = (MutableSection) mps.getSections().get(0);
    /* Turn the property set into a summary information property. This is
         * done by setting the format ID of its first section to
         * SectionIDMap.SUMMARY_INFORMATION_ID. */
    ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
    /* Create an empty property. */
    final MutableProperty p = new MutableProperty();
    /* Fill the property with appropriate settings so that it specifies the
         * document's title. */
    p.setID(PropertyIDMap.PID_TITLE);
    p.setType(Variant.VT_LPWSTR);
    p.setValue("Sample title");
    /* Place the property into the section. */
    ms.setProperty(p);
    /* Create the POI file system the property set is to be written to. */
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    /* For writing the property set into a POI file system it has to be
         * handed over to the POIFS.createDocument() method as an input stream
         * which produces the bytes making out the property set stream. */
    final InputStream is = mps.toInputStream();
    /* Create the summary information property set in the POI file
         * system. It is given the default name most (if not all) summary
         * information property sets have. */
    poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
    /* Write the whole POI file system to a disk file. */
    FileOutputStream fos = new FileOutputStream(fileName);
    poiFs.writeFilesystem(fos);
    fos.close();
    poiFs.close();
}
Also used : MutableProperty(org.apache.poi.hpsf.MutableProperty) MutableSection(org.apache.poi.hpsf.MutableSection) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet)

Example 7 with MutableSection

use of org.apache.poi.hpsf.MutableSection 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 8 with MutableSection

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

the class TestWrite method writeEmptyPropertySet.

/**
     * <p>Writes an empty property set 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 writeEmptyPropertySet() throws IOException, UnsupportedVariantTypeException {
    final File dataDir = _samples.getFile("");
    final File filename = new File(dataDir, POI_FS);
    filename.deleteOnExit();
    /* Create a mutable property set and write it to a POIFS: */
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    final MutableSection s = (MutableSection) ps.getSections().get(0);
    s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
    final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
    ps.write(psStream);
    psStream.close();
    final byte[] streamData = psStream.toByteArray();
    poiFs.createDocument(new ByteArrayInputStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME);
    poiFs.writeFilesystem(out);
    poiFs.close();
    out.close();
    /* Read the POIFS: */
    final POIFSReader r = new POIFSReader();
    r.registerListener(new MyPOIFSReaderListener(), SummaryInformation.DEFAULT_STREAM_NAME);
    FileInputStream stream = new FileInputStream(filename);
    try {
        r.read(stream);
    } finally {
        stream.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) 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) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) POIFSReader(org.apache.poi.poifs.eventfilesystem.POIFSReader) FileInputStream(java.io.FileInputStream) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Example 9 with MutableSection

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

the class TestWrite method withoutAFormatID.

/**
     * <p>Writes an empty property set to a POIFS and reads it back
     * in.</p>
     *
     * @exception IOException if an I/O exception occurs
     */
@Test(expected = NoFormatIDException.class)
public void withoutAFormatID() throws Exception {
    final File filename = TempFile.createTempFile(POI_FS, ".doc");
    /* Create a mutable property set with a section that does not have the
         * formatID set: */
    final OutputStream out = new FileOutputStream(filename);
    final POIFSFileSystem poiFs = new POIFSFileSystem();
    final MutablePropertySet ps = new MutablePropertySet();
    ps.clearSections();
    ps.addSection(new MutableSection());
    /* Write it to a POIFS and the latter to disk: */
    try {
        final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
        ps.write(psStream);
        psStream.close();
        final byte[] streamData = psStream.toByteArray();
        poiFs.createDocument(new ByteArrayInputStream(streamData), SummaryInformation.DEFAULT_STREAM_NAME);
        poiFs.writeFilesystem(out);
    } finally {
        poiFs.close();
        out.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) 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) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TempFile(org.apache.poi.util.TempFile) File(java.io.File) MutablePropertySet(org.apache.poi.hpsf.MutablePropertySet) Test(org.junit.Test)

Aggregations

MutableSection (org.apache.poi.hpsf.MutableSection)9 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)8 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)7 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)7 File (java.io.File)6 OutputStream (java.io.OutputStream)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)6 TempFile (org.apache.poi.util.TempFile)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 MutableProperty (org.apache.poi.hpsf.MutableProperty)4 PropertySet (org.apache.poi.hpsf.PropertySet)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 Section (org.apache.poi.hpsf.Section)3 POIFSReader (org.apache.poi.poifs.eventfilesystem.POIFSReader)3 IOException (java.io.IOException)2