001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hpsf.examples;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.util.Date;
028:
029: import org.apache.poi.hpsf.CustomProperties;
030: import org.apache.poi.hpsf.DocumentSummaryInformation;
031: import org.apache.poi.hpsf.MarkUnsupportedException;
032: import org.apache.poi.hpsf.NoPropertySetStreamException;
033: import org.apache.poi.hpsf.PropertySet;
034: import org.apache.poi.hpsf.PropertySetFactory;
035: import org.apache.poi.hpsf.SummaryInformation;
036: import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
037: import org.apache.poi.hpsf.WritingNotSupportedException;
038: import org.apache.poi.poifs.filesystem.DirectoryEntry;
039: import org.apache.poi.poifs.filesystem.DocumentEntry;
040: import org.apache.poi.poifs.filesystem.DocumentInputStream;
041: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
042:
043: /**
044: * <p>This is a sample application showing how to easily modify properties in
045: * the summary information and in the document summary information. The
046: * application reads the name of a POI filesystem from the command line and
047: * performs the following actions:</p>
048: *
049: * <ul>
050: *
051: * <li><p>Open the POI filesystem.</p></li>
052: *
053: * <li><p>Read the summary information.</p></li>
054: *
055: * <li><p>Read and print the "author" property.</p></li>
056: *
057: * <li><p>Change the author to "Rainer Klute".</p></li>
058: *
059: * <li><p>Read the document summary information.</p></li>
060: *
061: * <li><p>Read and print the "category" property.</p></li>
062: *
063: * <li><p>Change the category to "POI example".</p></li>
064: *
065: * <li><p>Read the custom properties (if available).</p></li>
066: *
067: * <li><p>Insert a new custom property.</p></li>
068: *
069: * <li><p>Write the custom properties back to the document summary
070: * information.</p></li>
071: *
072: * <li><p>Write the summary information to the POI filesystem.</p></li>
073: *
074: * <li><p>Write the document summary information to the POI filesystem.</p></li>
075: *
076: * <li><p>Write the POI filesystem back to the original file.</p></li>
077: *
078: * </ol>
079: *
080: * @author Rainer Klute <a
081: * href="mailto:klute@rainer-klute.de">klute@rainer-klute.de</a>
082: * @since 2006-02-09
083: * @version $Id: TestWrite.java 353637 2005-04-13 16:33:22Z klute $
084: */
085: public class ModifyDocumentSummaryInformation {
086:
087: /**
088: * <p>Main method - see class description.</p>
089: *
090: * @param args The command-line parameters.
091: * @throws IOException
092: * @throws MarkUnsupportedException
093: * @throws NoPropertySetStreamException
094: * @throws UnexpectedPropertySetTypeException
095: * @throws WritingNotSupportedException
096: */
097: public static void main(final String[] args) throws IOException,
098: NoPropertySetStreamException, MarkUnsupportedException,
099: UnexpectedPropertySetTypeException,
100: WritingNotSupportedException {
101: /* Read the name of the POI filesystem to modify from the command line.
102: * For brevity to boundary check is performed on the command-line
103: * arguments. */
104: File poiFilesystem = new File(args[0]);
105:
106: /* Open the POI filesystem. */
107: InputStream is = new FileInputStream(poiFilesystem);
108: POIFSFileSystem poifs = new POIFSFileSystem(is);
109: is.close();
110:
111: /* Read the summary information. */
112: DirectoryEntry dir = poifs.getRoot();
113: SummaryInformation si;
114: try {
115: DocumentEntry siEntry = (DocumentEntry) dir
116: .getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
117: DocumentInputStream dis = new DocumentInputStream(siEntry);
118: PropertySet ps = new PropertySet(dis);
119: dis.close();
120: si = new SummaryInformation(ps);
121: } catch (FileNotFoundException ex) {
122: /* There is no summary information yet. We have to create a new
123: * one. */
124: si = PropertySetFactory.newSummaryInformation();
125: }
126:
127: /* Change the author to "Rainer Klute". Any former author value will
128: * be lost. If there has been no author yet, it will be created. */
129: si.setAuthor("Rainer Klute");
130: System.out.println("Author changed to " + si.getAuthor() + ".");
131:
132: /* Handling the document summary information is analogous to handling
133: * the summary information. An additional feature, however, are the
134: * custom properties. */
135:
136: /* Read the document summary information. */
137: DocumentSummaryInformation dsi;
138: try {
139: DocumentEntry dsiEntry = (DocumentEntry) dir
140: .getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
141: DocumentInputStream dis = new DocumentInputStream(dsiEntry);
142: PropertySet ps = new PropertySet(dis);
143: dis.close();
144: dsi = new DocumentSummaryInformation(ps);
145: } catch (FileNotFoundException ex) {
146: /* There is no document summary information yet. We have to create a
147: * new one. */
148: dsi = PropertySetFactory.newDocumentSummaryInformation();
149: }
150:
151: /* Change the category to "POI example". Any former category value will
152: * be lost. If there has been no category yet, it will be created. */
153: dsi.setCategory("POI example");
154: System.out.println("Category changed to " + dsi.getCategory()
155: + ".");
156:
157: /* Read the custom properties. If there are no custom properties yet,
158: * the application has to create a new CustomProperties object. It will
159: * serve as a container for custom properties. */
160: CustomProperties customProperties = dsi.getCustomProperties();
161: if (customProperties == null)
162: customProperties = new CustomProperties();
163:
164: /* Insert some custom properties into the container. */
165: customProperties.put("Key 1", "Value 1");
166: customProperties.put("Schlüssel 2", "Wert 2");
167: customProperties.put("Sample Number", new Integer(12345));
168: customProperties.put("Sample Boolean", new Boolean(true));
169: customProperties.put("Sample Date", new Date());
170:
171: /* Read a custom property. */
172: Object value = customProperties.get("Sample Number");
173:
174: /* Write the custom properties back to the document summary
175: * information. */
176: dsi.setCustomProperties(customProperties);
177:
178: /* Write the summary information and the document summary information
179: * to the POI filesystem. */
180: si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
181: dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
182:
183: /* Write the POI filesystem back to the original file. Please note that
184: * in production code you should never write directly to the origin
185: * file! In case of a writing error everything would be lost. */
186: OutputStream out = new FileOutputStream(poiFilesystem);
187: poifs.writeFilesystem(out);
188: out.close();
189: }
190:
191: }
|