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.FileInputStream;
021: import java.io.IOException;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.poi.hpsf.NoPropertySetStreamException;
026: import org.apache.poi.hpsf.Property;
027: import org.apache.poi.hpsf.PropertySet;
028: import org.apache.poi.hpsf.PropertySetFactory;
029: import org.apache.poi.hpsf.Section;
030: import org.apache.poi.poifs.eventfilesystem.POIFSReader;
031: import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
032: import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
033: import org.apache.poi.util.HexDump;
034:
035: /**
036: * <p>Sample application showing how to read a document's custom property set.
037: * Call it with the document's file name as command-line parameter.</p>
038: *
039: * <p>Explanations can be found in the HPSF HOW-TO.</p>
040: *
041: * @author Rainer Klute <a
042: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
043: * @version $Id: ReadCustomPropertySets.java 489730 2006-12-22 19:18:16Z bayard $
044: * @since 2003-02-01
045: */
046: public class ReadCustomPropertySets {
047:
048: /**
049: * <p>Runs the example program.</p>
050: *
051: * @param args Command-line arguments (unused).
052: * @throws IOException if any I/O exception occurs.
053: */
054: public static void main(final String[] args) throws IOException {
055: final String filename = args[0];
056: POIFSReader r = new POIFSReader();
057:
058: /* Register a listener for *all* documents. */
059: r.registerListener(new MyPOIFSReaderListener());
060: r.read(new FileInputStream(filename));
061: }
062:
063: static class MyPOIFSReaderListener implements POIFSReaderListener {
064: public void processPOIFSReaderEvent(final POIFSReaderEvent event) {
065: PropertySet ps = null;
066: try {
067: ps = PropertySetFactory.create(event.getStream());
068: } catch (NoPropertySetStreamException ex) {
069: out("No property set stream: \"" + event.getPath()
070: + event.getName() + "\"");
071: return;
072: } catch (Exception ex) {
073: throw new RuntimeException("Property set stream \""
074: + event.getPath() + event.getName() + "\": "
075: + ex);
076: }
077:
078: /* Print the name of the property set stream: */
079: out("Property set stream \"" + event.getPath()
080: + event.getName() + "\":");
081:
082: /* Print the number of sections: */
083: final long sectionCount = ps.getSectionCount();
084: out(" No. of sections: " + sectionCount);
085:
086: /* Print the list of sections: */
087: List sections = ps.getSections();
088: int nr = 0;
089: for (Iterator i = sections.iterator(); i.hasNext();) {
090: /* Print a single section: */
091: Section sec = (Section) i.next();
092: out(" Section " + nr++ + ":");
093: String s = hex(sec.getFormatID().getBytes());
094: s = s.substring(0, s.length() - 1);
095: out(" Format ID: " + s);
096:
097: /* Print the number of properties in this section. */
098: int propertyCount = sec.getPropertyCount();
099: out(" No. of properties: " + propertyCount);
100:
101: /* Print the properties: */
102: Property[] properties = sec.getProperties();
103: for (int i2 = 0; i2 < properties.length; i2++) {
104: /* Print a single property: */
105: Property p = properties[i2];
106: long id = p.getID();
107: long type = p.getType();
108: Object value = p.getValue();
109: out(" Property ID: " + id + ", type: " + type
110: + ", value: " + value);
111: }
112: }
113: }
114: }
115:
116: static void out(final String msg) {
117: System.out.println(msg);
118: }
119:
120: static String hex(final byte[] bytes) {
121: return HexDump.dump(bytes, 0L, 0);
122: }
123:
124: }
|