KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > examples > WriteTitle


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hpsf.examples;
19
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import org.apache.poi.hpsf.MutableProperty;
25 import org.apache.poi.hpsf.MutablePropertySet;
26 import org.apache.poi.hpsf.MutableSection;
27 import org.apache.poi.hpsf.SummaryInformation;
28 import org.apache.poi.hpsf.Variant;
29 import org.apache.poi.hpsf.WritingNotSupportedException;
30 import org.apache.poi.hpsf.wellknown.PropertyIDMap;
31 import org.apache.poi.hpsf.wellknown.SectionIDMap;
32 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
33
34 /**
35  * <p>This class is a simple sample application showing how to create a property
36  * set and write it to disk.</p>
37  *
38  * @author Rainer Klute <a
39  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
40  * @version $Id: WriteTitle.java,v 1.6 2004/06/22 15:36:17 klute Exp $
41  * @since 2003-09-12
42  */

43 public class WriteTitle
44 {
45     /**
46      * <p>Runs the example program.</p>
47      *
48      * @param args Command-line arguments. The first and only command-line
49      * argument is the name of the POI file system to create.
50      * @throws IOException if any I/O exception occurs.
51      * @throws WritingNotSupportedException if HPSF does not (yet) support
52      * writing a certain property type.
53      */

54     public static void main(final String JavaDoc[] args)
55     throws WritingNotSupportedException, IOException JavaDoc
56     {
57         /* Check whether we have exactly one command-line argument. */
58         if (args.length != 1)
59         {
60             System.err.println("Usage: " + WriteTitle.class.getName() +
61                                "destinationPOIFS");
62             System.exit(1);
63         }
64
65         final String JavaDoc fileName = args[0];
66
67         /* Create a mutable property set. Initially it contains a single section
68          * with no properties. */

69         final MutablePropertySet mps = new MutablePropertySet();
70
71         /* Retrieve the section the property set already contains. */
72         final MutableSection ms = (MutableSection) mps.getSections().get(0);
73
74         /* Turn the property set into a summary information property. This is
75          * done by setting the format ID of its first section to
76          * SectionIDMap.SUMMARY_INFORMATION_ID. */

77         ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
78
79         /* Create an empty property. */
80         final MutableProperty p = new MutableProperty();
81
82         /* Fill the property with appropriate settings so that it specifies the
83          * document's title. */

84         p.setID(PropertyIDMap.PID_TITLE);
85         p.setType(Variant.VT_LPWSTR);
86         p.setValue("Sample title");
87
88         /* Place the property into the section. */
89         ms.setProperty(p);
90
91         /* Create the POI file system the property set is to be written to. */
92         final POIFSFileSystem poiFs = new POIFSFileSystem();
93
94         /* For writing the property set into a POI file system it has to be
95          * handed over to the POIFS.createDocument() method as an input stream
96          * which produces the bytes making out the property set stream. */

97         final InputStream JavaDoc is = mps.toInputStream();
98
99         /* Create the summary information property set in the POI file
100          * system. It is given the default name most (if not all) summary
101          * information property sets have. */

102         poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
103
104         /* Write the whole POI file system to a disk file. */
105         poiFs.writeFilesystem(new FileOutputStream JavaDoc(fileName));
106     }
107
108 }
109
Popular Tags