Search in sources :

Example 1 with MutableProperty

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

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

the class TestWriteWellKnown method testGetCustomerProperties.

/**
     * Tests reading custom properties from a section including reading
     * custom properties which are not pure.
     */
@Test
public void testGetCustomerProperties() {
    final int ID_1 = 2;
    final int ID_2 = 3;
    final String NAME_1 = "Schlüssel ä";
    final String VALUE_1 = "Wert 1";
    final Map<Long, String> dictionary = new HashMap<Long, String>();
    DocumentSummaryInformation dsi = PropertySetFactory.newDocumentSummaryInformation();
    CustomProperties cps;
    MutableSection s;
    /* A document summary information set stream by default does have custom properties. */
    cps = dsi.getCustomProperties();
    assertNull(cps);
    /* Test an empty custom properties set. */
    s = new MutableSection();
    s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[1]);
    // s.setCodepage(CodePageUtil.CP_UNICODE);
    dsi.addSection(s);
    cps = dsi.getCustomProperties();
    assertEquals(0, cps.size());
    /* Add a custom property. */
    MutableProperty p = new MutableProperty();
    p.setID(ID_1);
    p.setType(Variant.VT_LPWSTR);
    p.setValue(VALUE_1);
    s.setProperty(p);
    dictionary.put(Long.valueOf(ID_1), NAME_1);
    s.setDictionary(dictionary);
    cps = dsi.getCustomProperties();
    assertEquals(1, cps.size());
    assertTrue(cps.isPure());
    /* Add another custom property. */
    s.setProperty(ID_2, Variant.VT_LPWSTR, VALUE_1);
    dictionary.put(Long.valueOf(ID_2), NAME_1);
    s.setDictionary(dictionary);
    cps = dsi.getCustomProperties();
    assertEquals(1, cps.size());
    assertFalse(cps.isPure());
}
Also used : MutableProperty(org.apache.poi.hpsf.MutableProperty) HashMap(java.util.HashMap) MutableSection(org.apache.poi.hpsf.MutableSection) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 3 with MutableProperty

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

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

MutableProperty (org.apache.poi.hpsf.MutableProperty)4 MutableSection (org.apache.poi.hpsf.MutableSection)4 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 PropertySet (org.apache.poi.hpsf.PropertySet)2 Section (org.apache.poi.hpsf.Section)2 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 CustomProperties (org.apache.poi.hpsf.CustomProperties)1 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)1 HPSFException (org.apache.poi.hpsf.HPSFException)1