FileDocCategorySizeDatePackage
ModifyDocumentSummaryInformation.javaAPI DocApache Poi 3.0.17998Mon Jan 01 12:39:42 GMT 2007org.apache.poi.hpsf.examples

ModifyDocumentSummaryInformation

public class ModifyDocumentSummaryInformation extends Object

This is a sample application showing how to easily modify properties in the summary information and in the document summary information. The application reads the name of a POI filesystem from the command line and performs the following actions:

  • Open the POI filesystem.

  • Read the summary information.

  • Read and print the "author" property.

  • Change the author to "Rainer Klute".

  • Read the document summary information.

  • Read and print the "category" property.

  • Change the category to "POI example".

  • Read the custom properties (if available).

  • Insert a new custom property.

  • Write the custom properties back to the document summary information.

  • Write the summary information to the POI filesystem.

  • Write the document summary information to the POI filesystem.

  • Write the POI filesystem back to the original file.

author
Rainer Klute klute@rainer-klute.de
since
2006-02-09
version
$Id: TestWrite.java 353637 2005-04-13 16:33:22Z klute $

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

Main method - see class description.

param
args The command-line parameters.
throws
IOException
throws
MarkUnsupportedException
throws
NoPropertySetStreamException
throws
UnexpectedPropertySetTypeException
throws
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 poiFilesystem = new File(args[0]);

        /* Open the POI filesystem. */
        InputStream is = new FileInputStream(poiFilesystem);
        POIFSFileSystem poifs = new POIFSFileSystem(is);
        is.close();

        /* Read the summary information. */
        DirectoryEntry dir = poifs.getRoot();
        SummaryInformation si;
        try
        {
            DocumentEntry siEntry = (DocumentEntry)
                dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            dis.close();
            si = new SummaryInformation(ps);
        }
        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
        {
            DocumentEntry dsiEntry = (DocumentEntry)
                dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(dsiEntry);
            PropertySet ps = new PropertySet(dis);
            dis.close();
            dsi = new DocumentSummaryInformation(ps);
        }
        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", new Boolean(true));
        customProperties.put("Sample Date", new Date());

        /* Read a custom property. */
        Object value = customProperties.get("Sample Number");

        /* 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 never write directly to the origin
         * file! In case of a writing error everything would be lost. */
        OutputStream out = new FileOutputStream(poiFilesystem);
        poifs.writeFilesystem(out);
        out.close();