KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.poi.hpsf.NoPropertySetStreamException;
26 import org.apache.poi.hpsf.Property;
27 import org.apache.poi.hpsf.PropertySet;
28 import org.apache.poi.hpsf.PropertySetFactory;
29 import org.apache.poi.hpsf.Section;
30 import org.apache.poi.poifs.eventfilesystem.POIFSReader;
31 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
32 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
33 import org.apache.poi.util.HexDump;
34
35 /**
36  * <p>Sample application showing how to read a document's custom property set.
37  * Call it with the document's file name as command-line parameter.</p>
38  *
39  * <p>Explanations can be found in the HPSF HOW-TO.</p>
40  *
41  * @author Rainer Klute <a
42  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
43  * @version $Id: ReadCustomPropertySets.java,v 1.4 2004/04/09 13:05:14 glens Exp $
44  * @since 2003-02-01
45  */

46 public class ReadCustomPropertySets
47 {
48
49     /**
50      * <p>Runs the example program.</p>
51      *
52      * @param args Command-line arguments (unused).
53      * @throws IOException if any I/O exception occurs.
54      */

55     public static void main(final String JavaDoc[] args)
56         throws IOException JavaDoc
57     {
58         final String JavaDoc filename = args[0];
59         POIFSReader r = new POIFSReader();
60
61         /* Register a listener for *all* documents. */
62         r.registerListener(new MyPOIFSReaderListener());
63         r.read(new FileInputStream JavaDoc(filename));
64     }
65
66
67     static class MyPOIFSReaderListener implements POIFSReaderListener
68     {
69         public void processPOIFSReaderEvent(final POIFSReaderEvent event)
70         {
71             PropertySet ps = null;
72             try
73             {
74                 ps = PropertySetFactory.create(event.getStream());
75             }
76             catch (NoPropertySetStreamException ex)
77             {
78                 out("No property set stream: \"" + event.getPath() +
79                     event.getName() + "\"");
80                 return;
81             }
82             catch (Exception JavaDoc ex)
83             {
84                 throw new RuntimeException JavaDoc
85                     ("Property set stream \"" +
86                      event.getPath() + event.getName() + "\": " + ex);
87             }
88
89             /* Print the name of the property set stream: */
90             out("Property set stream \"" + event.getPath() +
91                 event.getName() + "\":");
92
93             /* Print the number of sections: */
94             final long sectionCount = ps.getSectionCount();
95             out(" No. of sections: " + sectionCount);
96
97             /* Print the list of sections: */
98             List JavaDoc sections = ps.getSections();
99             int nr = 0;
100             for (Iterator JavaDoc i = sections.iterator(); i.hasNext();)
101             {
102                 /* Print a single section: */
103                 Section sec = (Section) i.next();
104                 out(" Section " + nr++ + ":");
105                 String JavaDoc s = hex(sec.getFormatID().getBytes());
106                 s = s.substring(0, s.length() - 1);
107                 out(" Format ID: " + s);
108
109                 /* Print the number of properties in this section. */
110                 int propertyCount = sec.getPropertyCount();
111                 out(" No. of properties: " + propertyCount);
112
113                 /* Print the properties: */
114                 Property[] properties = sec.getProperties();
115                 for (int i2 = 0; i2 < properties.length; i2++)
116                 {
117                     /* Print a single property: */
118                     Property p = properties[i2];
119                     long id = p.getID();
120                     long type = p.getType();
121                     Object JavaDoc value = p.getValue();
122                     out(" Property ID: " + id + ", type: " + type +
123                         ", value: " + value);
124                 }
125             }
126         }
127     }
128
129     static void out(final String JavaDoc msg)
130     {
131         System.out.println(msg);
132     }
133
134     static String JavaDoc hex(final byte[] bytes)
135     {
136         return HexDump.dump(bytes, 0L, 0);
137     }
138
139 }
140
Popular Tags