Search in sources :

Example 1 with Section

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

the class PropertySetDescriptorRenderer method sectionsToString.

/**
     * <p>Returns a string representation of a list of {@link
     * Section}s.</p>
     */
protected String sectionsToString(final List<Section> sections) {
    final StringBuffer b = new StringBuffer();
    int count = 1;
    for (Iterator<Section> i = sections.iterator(); i.hasNext(); ) {
        Section s = i.next();
        String d = toString(s, "Section " + count++);
        b.append(d);
    }
    return b.toString();
}
Also used : Section(org.apache.poi.hpsf.Section)

Example 2 with Section

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

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

the class TestBasic method testSectionMethods.

/**
     * <p>Tests the {@link Section} 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 any HPSF exception occurs
     */
@Test
public void testSectionMethods() throws IOException, HPSFException {
    InputStream is = new ByteArrayInputStream(poiFiles.get(0).getBytes());
    final SummaryInformation si = (SummaryInformation) PropertySetFactory.create(is);
    final List<Section> sections = si.getSections();
    final Section s = sections.get(0);
    assertEquals(s.getFormatID(), SectionIDMap.SUMMARY_INFORMATION_ID);
    assertNotNull(s.getProperties());
    assertEquals(17, s.getPropertyCount());
    assertEquals("Titel", s.getProperty(2));
    assertEquals(1764, s.getSize());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) Section(org.apache.poi.hpsf.Section) Test(org.junit.Test)

Example 4 with Section

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

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

Aggregations

Section (org.apache.poi.hpsf.Section)6 Test (org.junit.Test)5 PropertySet (org.apache.poi.hpsf.PropertySet)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)3 MutableSection (org.apache.poi.hpsf.MutableSection)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HPSFException (org.apache.poi.hpsf.HPSFException)2 IllegalPropertySetDataException (org.apache.poi.hpsf.IllegalPropertySetDataException)2 MutableProperty (org.apache.poi.hpsf.MutableProperty)2 NoFormatIDException (org.apache.poi.hpsf.NoFormatIDException)2 NoPropertySetStreamException (org.apache.poi.hpsf.NoPropertySetStreamException)2 ReadingNotSupportedException (org.apache.poi.hpsf.ReadingNotSupportedException)2