1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import org.apache.poi.hpsf.ClassID; 26 import org.apache.poi.poifs.dev.POIFSViewable; 27 import org.apache.poi.poifs.property.DirectoryProperty; 28 import org.apache.poi.poifs.property.DocumentProperty; 29 import org.apache.poi.poifs.property.Property; 30 31 36 37 public class DirectoryNode 38 extends EntryNode 39 implements DirectoryEntry, POIFSViewable 40 { 41 42 private Map _entries; 44 45 private POIFSFileSystem _filesystem; 47 48 private POIFSDocumentPath _path; 50 51 59 60 DirectoryNode(final DirectoryProperty property, 61 final POIFSFileSystem filesystem, 62 final DirectoryNode parent) 63 { 64 super(property, parent); 65 if (parent == null) 66 { 67 _path = new POIFSDocumentPath(); 68 } 69 else 70 { 71 _path = new POIFSDocumentPath(parent._path, new String [] 72 { 73 property.getName() 74 }); 75 } 76 _filesystem = filesystem; 77 _entries = new HashMap(); 78 Iterator iter = property.getChildren(); 79 80 while (iter.hasNext()) 81 { 82 Property child = ( Property ) iter.next(); 83 Entry childNode = null; 84 85 if (child.isDirectory()) 86 { 87 childNode = new DirectoryNode(( DirectoryProperty ) child, 88 _filesystem, this); 89 } 90 else 91 { 92 childNode = new DocumentNode(( DocumentProperty ) child, 93 this); 94 } 95 _entries.put(childNode.getName(), childNode); 96 } 97 } 98 99 102 103 public POIFSDocumentPath getPath() 104 { 105 return _path; 106 } 107 108 117 118 DocumentEntry createDocument(final POIFSDocument document) 119 throws IOException 120 { 121 DocumentProperty property = document.getDocumentProperty(); 122 DocumentNode rval = new DocumentNode(property, this); 123 124 (( DirectoryProperty ) getProperty()).addChild(property); 125 _filesystem.addDocument(document); 126 _entries.put(property.getName(), rval); 127 return rval; 128 } 129 130 138 139 boolean changeName(final String oldName, final String newName) 140 { 141 boolean rval = false; 142 EntryNode child = ( EntryNode ) _entries.get(oldName); 143 144 if (child != null) 145 { 146 rval = (( DirectoryProperty ) getProperty()) 147 .changeName(child.getProperty(), newName); 148 if (rval) 149 { 150 _entries.remove(oldName); 151 _entries.put(child.getProperty().getName(), child); 152 } 153 } 154 return rval; 155 } 156 157 164 165 boolean deleteEntry(final EntryNode entry) 166 { 167 boolean rval = 168 (( DirectoryProperty ) getProperty()) 169 .deleteChild(entry.getProperty()); 170 171 if (rval) 172 { 173 _entries.remove(entry.getName()); 174 _filesystem.remove(entry); 175 } 176 return rval; 177 } 178 179 180 181 191 192 public Iterator getEntries() 193 { 194 return _entries.values().iterator(); 195 } 196 197 202 203 public boolean isEmpty() 204 { 205 return _entries.isEmpty(); 206 } 207 208 215 216 public int getEntryCount() 217 { 218 return _entries.size(); 219 } 220 221 232 233 public Entry getEntry(final String name) 234 throws FileNotFoundException 235 { 236 Entry rval = null; 237 238 if (name != null) 239 { 240 rval = ( Entry ) _entries.get(name); 241 } 242 if (rval == null) 243 { 244 245 throw new FileNotFoundException("no such entry: \"" + name 247 + "\""); 248 } 249 return rval; 250 } 251 252 263 264 public DocumentEntry createDocument(final String name, 265 final InputStream stream) 266 throws IOException 267 { 268 return createDocument(new POIFSDocument(name, stream)); 269 } 270 271 282 283 public DocumentEntry createDocument(final String name, final int size, 284 final POIFSWriterListener writer) 285 throws IOException 286 { 287 return createDocument(new POIFSDocument(name, size, _path, writer)); 288 } 289 290 299 300 public DirectoryEntry createDirectory(final String name) 301 throws IOException 302 { 303 DirectoryProperty property = new DirectoryProperty(name); 304 DirectoryNode rval = new DirectoryNode(property, _filesystem, 305 this); 306 307 (( DirectoryProperty ) getProperty()).addChild(property); 308 _filesystem.addDirectory(property); 309 _entries.put(name, rval); 310 return rval; 311 } 312 313 318 public ClassID getStorageClsid() 319 { 320 return getProperty().getStorageClsid(); 321 } 322 323 328 public void setStorageClsid(ClassID clsidStorage) 329 { 330 getProperty().setStorageClsid(clsidStorage); 331 } 332 333 334 335 336 341 342 public boolean isDirectoryEntry() 343 { 344 return true; 345 } 346 347 348 349 350 357 358 protected boolean isDeleteOK() 359 { 360 361 return isEmpty(); 363 } 364 365 366 367 368 374 375 public Object [] getViewableArray() 376 { 377 return new Object [ 0 ]; 378 } 379 380 387 388 public Iterator getViewableIterator() 389 { 390 List components = new ArrayList(); 391 392 components.add(getProperty()); 393 SortedMap sortedEntries = new TreeMap(_entries); 394 Iterator iter = sortedEntries.values().iterator(); 395 396 while (iter.hasNext()) 397 { 398 components.add(iter.next()); 399 } 400 return components.iterator(); 401 } 402 403 410 411 public boolean preferArray() 412 { 413 return false; 414 } 415 416 422 423 public String getShortDescription() 424 { 425 return getName(); 426 } 427 428 429 } 431 | Popular Tags |