use of org.apache.poi.hpsf.DocumentSummaryInformation in project poi by apache.
the class ModifyDocumentSummaryInformation method main.
/**
* <p>Main method - see class description.</p>
*
* @param args The command-line parameters.
* @throws IOException
* @throws MarkUnsupportedException
* @throws NoPropertySetStreamException
* @throws UnexpectedPropertySetTypeException
* @throws WritingNotSupportedException
*/
public static void main(final String[] args) throws IOException, NoPropertySetStreamException, MarkUnsupportedException, UnexpectedPropertySetTypeException, WritingNotSupportedException {
/* Read the name of the POI filesystem to modify from the command line.
* For brevity to boundary check is performed on the command-line
* arguments. */
File summaryFile = new File(args[0]);
/* Open the POI filesystem. */
NPOIFSFileSystem poifs = new NPOIFSFileSystem(summaryFile, false);
/* Read the summary information. */
DirectoryEntry dir = poifs.getRoot();
SummaryInformation si;
try {
si = (SummaryInformation) PropertySetFactory.create(dir, SummaryInformation.DEFAULT_STREAM_NAME);
} catch (FileNotFoundException ex) {
// There is no summary information yet. We have to create a new one
si = PropertySetFactory.newSummaryInformation();
}
/* Change the author to "Rainer Klute". Any former author value will
* be lost. If there has been no author yet, it will be created. */
si.setAuthor("Rainer Klute");
System.out.println("Author changed to " + si.getAuthor() + ".");
/* Handling the document summary information is analogous to handling
* the summary information. An additional feature, however, are the
* custom properties. */
/* Read the document summary information. */
DocumentSummaryInformation dsi;
try {
dsi = (DocumentSummaryInformation) PropertySetFactory.create(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
} catch (FileNotFoundException ex) {
/* There is no document summary information yet. We have to create a
* new one. */
dsi = PropertySetFactory.newDocumentSummaryInformation();
}
/* Change the category to "POI example". Any former category value will
* be lost. If there has been no category yet, it will be created. */
dsi.setCategory("POI example");
System.out.println("Category changed to " + dsi.getCategory() + ".");
/* Read the custom properties. If there are no custom properties yet,
* the application has to create a new CustomProperties object. It will
* serve as a container for custom properties. */
CustomProperties customProperties = dsi.getCustomProperties();
if (customProperties == null)
customProperties = new CustomProperties();
/* Insert some custom properties into the container. */
customProperties.put("Key 1", "Value 1");
customProperties.put("Schlüssel 2", "Wert 2");
customProperties.put("Sample Number", new Integer(12345));
customProperties.put("Sample Boolean", Boolean.TRUE);
customProperties.put("Sample Date", new Date());
/* Read a custom property. */
Object value = customProperties.get("Sample Number");
System.out.println("Custom Sample Number is now " + value);
/* Write the custom properties back to the document summary
* information. */
dsi.setCustomProperties(customProperties);
/* Write the summary information and the document summary information
* to the POI filesystem. */
si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
/* Write the POI filesystem back to the original file. Please note that
* in production code you should take care when write directly to the
* origin, to make sure you don't loose things on error */
poifs.writeFilesystem();
poifs.close();
}
use of org.apache.poi.hpsf.DocumentSummaryInformation in project poi by apache.
the class TestReadAllFiles method readCustomPropertiesFromFiles.
/**
* <p>Tests the simplified custom properties by reading them from the
* available test files.</p>
*
* @throws Throwable if anything goes wrong.
*/
@Test
public void readCustomPropertiesFromFiles() throws Exception {
/* Read a test document <em>doc</em> into a POI filesystem. */
NPOIFSFileSystem poifs = new NPOIFSFileSystem(file);
try {
/*
* If there is a document summry information stream, read it from
* the POI filesystem, else create a new one.
*/
DocumentSummaryInformation dsi = TestWriteWellKnown.getDocumentSummaryInformation(poifs);
if (dsi == null) {
dsi = PropertySetFactory.newDocumentSummaryInformation();
}
final CustomProperties cps = dsi.getCustomProperties();
if (cps == null) {
/* The document does not have custom properties. */
return;
}
for (CustomProperty cp : cps.properties()) {
assertNotNull(cp.getName());
assertNotNull(cp.getValue());
}
} finally {
poifs.close();
}
}
use of org.apache.poi.hpsf.DocumentSummaryInformation 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());
}
use of org.apache.poi.hpsf.DocumentSummaryInformation in project poi by apache.
the class TestEmptyProperties method testPropertySetMethods.
/**
* <p>Tests the {@link PropertySet} 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 an HPSF operation fails
*/
@Test
public void testPropertySetMethods() throws IOException, HPSFException {
byte[] b = poiFiles.get(1).getBytes();
PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b));
SummaryInformation s = (SummaryInformation) ps;
assertNull(s.getTitle());
assertNull(s.getSubject());
assertNotNull(s.getAuthor());
assertNull(s.getKeywords());
assertNull(s.getComments());
assertNotNull(s.getTemplate());
assertNotNull(s.getLastAuthor());
assertNotNull(s.getRevNumber());
assertEquals(s.getEditTime(), 0);
assertNull(s.getLastPrinted());
assertNull(s.getCreateDateTime());
assertNull(s.getLastSaveDateTime());
assertEquals(s.getPageCount(), 0);
assertEquals(s.getWordCount(), 0);
assertEquals(s.getCharCount(), 0);
assertNull(s.getThumbnail());
assertNull(s.getApplicationName());
}
use of org.apache.poi.hpsf.DocumentSummaryInformation in project poi by apache.
the class HPSFFileHandler method handleFile.
@Override
public void handleFile(InputStream stream, String path) throws Exception {
Assume.assumeFalse(EXCLUDES_HANDLE_FILE.contains(path));
POIFSFileSystem poifs = new POIFSFileSystem(stream);
HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(poifs);
DocumentSummaryInformation dsi = hpsf.getDocumentSummaryInformation();
SummaryInformation si = hpsf.getSummaryInformation();
boolean hasDSI = hasPropertyStream(poifs, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
boolean hasSI = hasPropertyStream(poifs, SummaryInformation.DEFAULT_STREAM_NAME);
assertEquals(hasDSI, dsi != null);
assertEquals(hasSI, si != null);
handlePOIDocument(hpsf);
}
Aggregations