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);
}
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());
}
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();
}
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);
}
Aggregations