1 2 17 18 19 package org.apache.poi.contrib.poibrowser; 20 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.swing.tree.DefaultMutableTreeNode ; 26 import javax.swing.tree.MutableTreeNode ; 27 28 import org.apache.poi.hpsf.HPSFException; 29 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 30 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 31 import org.apache.poi.poifs.filesystem.DocumentInputStream; 32 import org.apache.poi.poifs.filesystem.POIFSDocumentPath; 33 34 64 public class TreeReaderListener implements POIFSReaderListener 65 { 66 67 71 protected MutableTreeNode rootNode; 72 73 77 protected Map pathToNode; 78 79 85 protected String filename; 86 87 88 89 101 public TreeReaderListener(final String filename, 102 final MutableTreeNode rootNode) 103 { 104 this.filename = filename; 105 this.rootNode = rootNode; 106 pathToNode = new HashMap (15); } 108 109 110 111 112 private int nrOfBytes = 50; 113 114 public void setNrOfBytes(final int nrOfBytes) 115 { 116 this.nrOfBytes = nrOfBytes; 117 } 118 119 public int getNrOfBytes() 120 { 121 return nrOfBytes; 122 } 123 124 125 126 131 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 132 { 133 DocumentDescriptor d; 134 final DocumentInputStream is = event.getStream(); 135 if (!is.markSupported()) 136 throw new UnsupportedOperationException (is.getClass().getName() + 137 " does not support mark()."); 138 139 143 try 144 { 145 d = new PropertySetDescriptor(event.getName(), event.getPath(), 146 is, nrOfBytes); 147 } 148 catch (HPSFException ex) 149 { 150 d = new DocumentDescriptor(event.getName(), event.getPath(), 151 is, nrOfBytes); 152 } 153 catch (Throwable t) 154 { 155 System.err.println 156 ("Unexpected exception while processing " + 157 event.getName() + " in " + event.getPath().toString()); 158 t.printStackTrace(System.err); 159 throw new RuntimeException (t.getMessage()); 160 } 161 162 try 163 { 164 is.close(); 165 } 166 catch (IOException ex) 167 { 168 System.err.println 169 ("Unexpected exception while closing " + 170 event.getName() + " in " + event.getPath().toString()); 171 ex.printStackTrace(System.err); 172 } 173 174 final MutableTreeNode parentNode = getNode(d.path, filename, rootNode); 175 final MutableTreeNode nameNode = new DefaultMutableTreeNode (d.name); 176 parentNode.insert(nameNode, 0); 177 final MutableTreeNode dNode = new DefaultMutableTreeNode (d); 178 nameNode.insert(dNode, 0); 179 } 180 181 182 183 195 private MutableTreeNode getNode(final POIFSDocumentPath path, 196 final String fsName, 197 final MutableTreeNode root) 198 { 199 MutableTreeNode n = (MutableTreeNode ) pathToNode.get(path); 200 if (n != null) 201 202 return n; 203 if (path.length() == 0) 204 { 205 210 n = (MutableTreeNode ) pathToNode.get(fsName); 211 if (n == null) 212 { 213 215 n = new DefaultMutableTreeNode (fsName); 216 pathToNode.put(fsName, n); 217 root.insert(n, 0); 218 } 219 return n; 220 } 221 else 222 { 223 226 final String name = path.getComponent(path.length() - 1); 227 final POIFSDocumentPath parentPath = path.getParent(); 228 final MutableTreeNode parentNode = 229 getNode(parentPath, fsName, root); 230 n = new DefaultMutableTreeNode (name); 231 pathToNode.put(path, n); 232 parentNode.insert(n, 0); 233 return n; 234 } 235 } 236 237 } 238 | Popular Tags |