1 2 17 18 package org.apache.poi.hpsf.examples; 19 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.List ; 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 46 public class ReadCustomPropertySets 47 { 48 49 55 public static void main(final String [] args) 56 throws IOException 57 { 58 final String filename = args[0]; 59 POIFSReader r = new POIFSReader(); 60 61 62 r.registerListener(new MyPOIFSReaderListener()); 63 r.read(new FileInputStream (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 ex) 83 { 84 throw new RuntimeException 85 ("Property set stream \"" + 86 event.getPath() + event.getName() + "\": " + ex); 87 } 88 89 90 out("Property set stream \"" + event.getPath() + 91 event.getName() + "\":"); 92 93 94 final long sectionCount = ps.getSectionCount(); 95 out(" No. of sections: " + sectionCount); 96 97 98 List sections = ps.getSections(); 99 int nr = 0; 100 for (Iterator i = sections.iterator(); i.hasNext();) 101 { 102 103 Section sec = (Section) i.next(); 104 out(" Section " + nr++ + ":"); 105 String s = hex(sec.getFormatID().getBytes()); 106 s = s.substring(0, s.length() - 1); 107 out(" Format ID: " + s); 108 109 110 int propertyCount = sec.getPropertyCount(); 111 out(" No. of properties: " + propertyCount); 112 113 114 Property[] properties = sec.getProperties(); 115 for (int i2 = 0; i2 < properties.length; i2++) 116 { 117 118 Property p = properties[i2]; 119 long id = p.getID(); 120 long type = p.getType(); 121 Object value = p.getValue(); 122 out(" Property ID: " + id + ", type: " + type + 123 ", value: " + value); 124 } 125 } 126 } 127 } 128 129 static void out(final String msg) 130 { 131 System.out.println(msg); 132 } 133 134 static String hex(final byte[] bytes) 135 { 136 return HexDump.dump(bytes, 0L, 0); 137 } 138 139 } 140 | Popular Tags |