use of org.apache.poi.hpsf.MutablePropertySet in project poi by apache.
the class TestWrite method dictionaryWithInvalidCodepage.
/**
* <p>Tests writing and reading back a proper dictionary with an invalid
* codepage. (HPSF writes Unicode dictionaries only.)</p>
* @throws IOException
* @throws HPSFException
*/
@Test(expected = IllegalPropertySetDataException.class)
public void dictionaryWithInvalidCodepage() 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");
try {
s.setDictionary(m);
s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
int codepage = 12345;
s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, Integer.valueOf(codepage));
poiFs.createDocument(ps1.toInputStream(), "Test");
poiFs.writeFilesystem(out);
} finally {
poiFs.close();
out.close();
}
}
use of org.apache.poi.hpsf.MutablePropertySet 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);
}
use of org.apache.poi.hpsf.MutablePropertySet 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);
}
use of org.apache.poi.hpsf.MutablePropertySet 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);
}
use of org.apache.poi.hpsf.MutablePropertySet 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();
}
Aggregations