1 2 17 18 19 package org.apache.poi.poifs.eventfilesystem; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import org.apache.poi.poifs.filesystem.DocumentInputStream; 26 import org.apache.poi.poifs.filesystem.POIFSDocument; 27 import org.apache.poi.poifs.filesystem.POIFSDocumentPath; 28 import org.apache.poi.poifs.property.DirectoryProperty; 29 import org.apache.poi.poifs.property.Property; 30 import org.apache.poi.poifs.property.PropertyTable; 31 import org.apache.poi.poifs.storage.BlockAllocationTableReader; 32 import org.apache.poi.poifs.storage.BlockList; 33 import org.apache.poi.poifs.storage.HeaderBlockReader; 34 import org.apache.poi.poifs.storage.RawDataBlockList; 35 import org.apache.poi.poifs.storage.SmallBlockTableReader; 36 37 47 48 public class POIFSReader 49 { 50 private POIFSReaderRegistry registry; 51 private boolean registryClosed; 52 53 56 57 public POIFSReader() 58 { 59 registry = new POIFSReaderRegistry(); 60 registryClosed = false; 61 } 62 63 70 71 public void read(final InputStream stream) 72 throws IOException 73 { 74 registryClosed = true; 75 76 HeaderBlockReader header_block_reader = new HeaderBlockReader(stream); 78 79 RawDataBlockList data_blocks = new RawDataBlockList(stream); 81 82 new BlockAllocationTableReader(header_block_reader.getBATCount(), 85 header_block_reader.getBATArray(), 86 header_block_reader.getXBATCount(), 87 header_block_reader.getXBATIndex(), 88 data_blocks); 89 90 PropertyTable properties = 92 new PropertyTable(header_block_reader.getPropertyStart(), 93 data_blocks); 94 95 processProperties(SmallBlockTableReader 97 .getSmallDocumentBlocks(data_blocks, properties 98 .getRoot(), header_block_reader 99 .getSBATStart()), data_blocks, properties.getRoot() 100 .getChildren(), new POIFSDocumentPath()); 101 } 102 103 112 113 public void registerListener(final POIFSReaderListener listener) 114 { 115 if (listener == null) 116 { 117 throw new NullPointerException (); 118 } 119 if (registryClosed) 120 { 121 throw new IllegalStateException (); 122 } 123 registry.registerListener(listener); 124 } 125 126 138 139 public void registerListener(final POIFSReaderListener listener, 140 final String name) 141 { 142 registerListener(listener, null, name); 143 } 144 145 159 160 public void registerListener(final POIFSReaderListener listener, 161 final POIFSDocumentPath path, 162 final String name) 163 { 164 if ((listener == null) || (name == null) || (name.length() == 0)) 165 { 166 throw new NullPointerException (); 167 } 168 if (registryClosed) 169 { 170 throw new IllegalStateException (); 171 } 172 registry.registerListener(listener, 173 (path == null) ? new POIFSDocumentPath() 174 : path, name); 175 } 176 177 184 185 public static void main(String args[]) 186 throws IOException 187 { 188 if (args.length == 0) 189 { 190 System.err 191 .println("at least one argument required: input filename(s)"); 192 System.exit(1); 193 } 194 195 for (int j = 0; j < args.length; j++) 197 { 198 POIFSReader reader = new POIFSReader(); 199 POIFSReaderListener listener = new SampleListener(); 200 201 reader.registerListener(listener); 202 System.out.println("reading " + args[ j ]); 203 FileInputStream istream = new FileInputStream(args[ j ]); 204 205 reader.read(istream); 206 istream.close(); 207 } 208 } 209 210 private void processProperties(final BlockList small_blocks, 211 final BlockList big_blocks, 212 final Iterator properties, 213 final POIFSDocumentPath path) 214 throws IOException 215 { 216 while (properties.hasNext()) 217 { 218 Property property = ( Property ) properties.next(); 219 String name = property.getName(); 220 221 if (property.isDirectory()) 222 { 223 POIFSDocumentPath new_path = new POIFSDocumentPath(path, 224 new String [] 225 { 226 name 227 }); 228 229 processProperties( 230 small_blocks, big_blocks, 231 (( DirectoryProperty ) property).getChildren(), new_path); 232 } 233 else 234 { 235 int startBlock = property.getStartBlock(); 236 Iterator listeners = registry.getListeners(path, name); 237 238 if (listeners.hasNext()) 239 { 240 int size = property.getSize(); 241 POIFSDocument document = null; 242 243 if (property.shouldUseSmallBlocks()) 244 { 245 document = 246 new POIFSDocument(name, small_blocks 247 .fetchBlocks(startBlock), size); 248 } 249 else 250 { 251 document = 252 new POIFSDocument(name, big_blocks 253 .fetchBlocks(startBlock), size); 254 } 255 while (listeners.hasNext()) 256 { 257 POIFSReaderListener listener = 258 ( POIFSReaderListener ) listeners.next(); 259 260 listener.processPOIFSReaderEvent( 261 new POIFSReaderEvent( 262 new DocumentInputStream(document), path, 263 name)); 264 } 265 } 266 else 267 { 268 269 if (property.shouldUseSmallBlocks()) 271 { 272 small_blocks.fetchBlocks(startBlock); 273 } 274 else 275 { 276 big_blocks.fetchBlocks(startBlock); 277 } 278 } 279 } 280 } 281 } 282 283 private static class SampleListener 284 implements POIFSReaderListener 285 { 286 287 290 291 SampleListener() 292 { 293 } 294 295 300 301 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 302 { 303 DocumentInputStream istream = event.getStream(); 304 POIFSDocumentPath path = event.getPath(); 305 String name = event.getName(); 306 307 try 308 { 309 byte[] data = new byte[ istream.available() ]; 310 311 istream.read(data); 312 int pathLength = path.length(); 313 314 for (int k = 0; k < pathLength; k++) 315 { 316 System.out.print("/" + path.getComponent(k)); 317 } 318 System.out.println("/" + name + ": " + data.length 319 + " bytes read"); 320 } 321 catch (IOException ignored) 322 { 323 } 324 } 325 } } 328 | Popular Tags |