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.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.apache.poi.hpsf.MutableProperty;
025: import org.apache.poi.hpsf.MutablePropertySet;
026: import org.apache.poi.hpsf.MutableSection;
027: import org.apache.poi.hpsf.SummaryInformation;
028: import org.apache.poi.hpsf.Variant;
029: import org.apache.poi.hpsf.WritingNotSupportedException;
030: import org.apache.poi.hpsf.wellknown.PropertyIDMap;
031: import org.apache.poi.hpsf.wellknown.SectionIDMap;
032: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
033:
034: /**
035: * <p>This class is a simple sample application showing how to create a property
036: * set and write it to disk.</p>
037: *
038: * @author Rainer Klute <a
039: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
040: * @version $Id: WriteTitle.java 489730 2006-12-22 19:18:16Z bayard $
041: * @since 2003-09-12
042: */
043: public class WriteTitle {
044: /**
045: * <p>Runs the example program.</p>
046: *
047: * @param args Command-line arguments. The first and only command-line
048: * argument is the name of the POI file system to create.
049: * @throws IOException if any I/O exception occurs.
050: * @throws WritingNotSupportedException if HPSF does not (yet) support
051: * writing a certain property type.
052: */
053: public static void main(final String[] args)
054: throws WritingNotSupportedException, IOException {
055: /* Check whether we have exactly one command-line argument. */
056: if (args.length != 1) {
057: System.err.println("Usage: " + WriteTitle.class.getName()
058: + "destinationPOIFS");
059: System.exit(1);
060: }
061:
062: final String fileName = args[0];
063:
064: /* Create a mutable property set. Initially it contains a single section
065: * with no properties. */
066: final MutablePropertySet mps = new MutablePropertySet();
067:
068: /* Retrieve the section the property set already contains. */
069: final MutableSection ms = (MutableSection) mps.getSections()
070: .get(0);
071:
072: /* Turn the property set into a summary information property. This is
073: * done by setting the format ID of its first section to
074: * SectionIDMap.SUMMARY_INFORMATION_ID. */
075: ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
076:
077: /* Create an empty property. */
078: final MutableProperty p = new MutableProperty();
079:
080: /* Fill the property with appropriate settings so that it specifies the
081: * document's title. */
082: p.setID(PropertyIDMap.PID_TITLE);
083: p.setType(Variant.VT_LPWSTR);
084: p.setValue("Sample title");
085:
086: /* Place the property into the section. */
087: ms.setProperty(p);
088:
089: /* Create the POI file system the property set is to be written to. */
090: final POIFSFileSystem poiFs = new POIFSFileSystem();
091:
092: /* For writing the property set into a POI file system it has to be
093: * handed over to the POIFS.createDocument() method as an input stream
094: * which produces the bytes making out the property set stream. */
095: final InputStream is = mps.toInputStream();
096:
097: /* Create the summary information property set in the POI file
098: * system. It is given the default name most (if not all) summary
099: * information property sets have. */
100: poiFs
101: .createDocument(is,
102: SummaryInformation.DEFAULT_STREAM_NAME);
103:
104: /* Write the whole POI file system to a disk file. */
105: poiFs.writeFilesystem(new FileOutputStream(fileName));
106: }
107:
108: }
|