KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > DirectoryNode


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 /**
32  * Simple implementation of DirectoryEntry
33  *
34  * @author Marc Johnson (mjohnson at apache dot org)
35  */

36
37 public class DirectoryNode
38     extends EntryNode
39     implements DirectoryEntry, POIFSViewable
40 {
41
42     // Map of Entry instances, keyed by their names
43
private Map _entries;
44
45     // the POIFSFileSystem we belong to
46
private POIFSFileSystem _filesystem;
47
48     // the path described by this document
49
private POIFSDocumentPath _path;
50
51     /**
52      * create a DirectoryNode. This method is not public by design; it
53      * is intended strictly for the internal use of this package
54      *
55      * @param property the DirectoryProperty for this DirectoryEntry
56      * @param filesystem the POIFSFileSystem we belong to
57      * @param parent the parent of this entry
58      */

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 JavaDoc[]
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     /**
100      * @return this directory's path representation
101      */

102
103     public POIFSDocumentPath getPath()
104     {
105         return _path;
106     }
107
108     /**
109      * create a new DocumentEntry
110      *
111      * @param document the new document
112      *
113      * @return the new DocumentEntry
114      *
115      * @exception IOException
116      */

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     /**
131      * Change a contained Entry's name
132      *
133      * @param oldName the original name
134      * @param newName the new name
135      *
136      * @return true if the operation succeeded, else false
137      */

138
139     boolean changeName(final String JavaDoc oldName, final String JavaDoc 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     /**
158      * Delete an entry
159      *
160      * @param entry the EntryNode to be deleted
161      *
162      * @return true if the entry was deleted, else false
163      */

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     /* ********** START implementation of DirectoryEntry ********** */
180
181     /**
182      * get an iterator of the Entry instances contained directly in
183      * this instance (in other words, children only; no grandchildren
184      * etc.)
185      *
186      * @return iterator; never null, but hasNext() may return false
187      * immediately (i.e., this DirectoryEntry is empty). All
188      * objects retrieved by next() are guaranteed to be
189      * implementations of Entry.
190      */

191
192     public Iterator getEntries()
193     {
194         return _entries.values().iterator();
195     }
196
197     /**
198      * is this DirectoryEntry empty?
199      *
200      * @return true if this instance contains no Entry instances
201      */

202
203     public boolean isEmpty()
204     {
205         return _entries.isEmpty();
206     }
207
208     /**
209      * find out how many Entry instances are contained directly within
210      * this DirectoryEntry
211      *
212      * @return number of immediately (no grandchildren etc.) contained
213      * Entry instances
214      */

215
216     public int getEntryCount()
217     {
218         return _entries.size();
219     }
220
221     /**
222      * get a specified Entry by name
223      *
224      * @param name the name of the Entry to obtain.
225      *
226      * @return the specified Entry, if it is directly contained in
227      * this DirectoryEntry
228      *
229      * @exception FileNotFoundException if no Entry with the specified
230      * name exists in this DirectoryEntry
231      */

232
233     public Entry getEntry(final String JavaDoc 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             // either a null name was given, or there is no such name
246
throw new FileNotFoundException("no such entry: \"" + name
247                                             + "\"");
248         }
249         return rval;
250     }
251
252     /**
253      * create a new DocumentEntry
254      *
255      * @param name the name of the new DocumentEntry
256      * @param stream the InputStream from which to create the new
257      * DocumentEntry
258      *
259      * @return the new DocumentEntry
260      *
261      * @exception IOException
262      */

263
264     public DocumentEntry createDocument(final String JavaDoc name,
265                                         final InputStream stream)
266         throws IOException
267     {
268         return createDocument(new POIFSDocument(name, stream));
269     }
270
271     /**
272      * create a new DocumentEntry; the data will be provided later
273      *
274      * @param name the name of the new DocumentEntry
275      * @param size the size of the new DocumentEntry
276      * @param writer the writer of the new DocumentEntry
277      *
278      * @return the new DocumentEntry
279      *
280      * @exception IOException
281      */

282
283     public DocumentEntry createDocument(final String JavaDoc name, final int size,
284                                         final POIFSWriterListener writer)
285         throws IOException
286     {
287         return createDocument(new POIFSDocument(name, size, _path, writer));
288     }
289
290     /**
291      * create a new DirectoryEntry
292      *
293      * @param name the name of the new DirectoryEntry
294      *
295      * @return the new DirectoryEntry
296      *
297      * @exception IOException
298      */

299
300     public DirectoryEntry createDirectory(final String JavaDoc 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     /**
314      * Gets the storage clsid of the directory entry
315      *
316      * @return storage Class ID
317      */

318     public ClassID getStorageClsid()
319     {
320         return getProperty().getStorageClsid();
321     }
322
323     /**
324      * Sets the storage clsid for the directory entry
325      *
326      * @param clsidStorage storage Class ID
327      */

328     public void setStorageClsid(ClassID clsidStorage)
329     {
330         getProperty().setStorageClsid(clsidStorage);
331     }
332
333     /* ********** END implementation of DirectoryEntry ********** */
334     /* ********** START implementation of Entry ********** */
335
336     /**
337      * is this a DirectoryEntry?
338      *
339      * @return true if the Entry is a DirectoryEntry, else false
340      */

341
342     public boolean isDirectoryEntry()
343     {
344         return true;
345     }
346
347     /* ********** END implementation of Entry ********** */
348     /* ********** START extension of Entry ********** */
349
350     /**
351      * extensions use this method to verify internal rules regarding
352      * deletion of the underlying store.
353      *
354      * @return true if it's ok to delete the underlying store, else
355      * false
356      */

357
358     protected boolean isDeleteOK()
359     {
360
361         // if this directory is empty, we can delete it
362
return isEmpty();
363     }
364
365     /* ********** END extension of Entry ********** */
366     /* ********** START begin implementation of POIFSViewable ********** */
367
368     /**
369      * Get an array of objects, some of which may implement
370      * POIFSViewable
371      *
372      * @return an array of Object; may not be null, but may be empty
373      */

374
375     public Object JavaDoc [] getViewableArray()
376     {
377         return new Object JavaDoc[ 0 ];
378     }
379
380     /**
381      * Get an Iterator of objects, some of which may implement
382      * POIFSViewable
383      *
384      * @return an Iterator; may not be null, but may have an empty
385      * back end store
386      */

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     /**
404      * Give viewers a hint as to whether to call getViewableArray or
405      * getViewableIterator
406      *
407      * @return true if a viewer should call getViewableArray, false if
408      * a viewer should call getViewableIterator
409      */

410
411     public boolean preferArray()
412     {
413         return false;
414     }
415
416     /**
417      * Provides a short description of the object, to be used when a
418      * POIFSViewable object has not provided its contents.
419      *
420      * @return short description
421      */

422
423     public String JavaDoc getShortDescription()
424     {
425         return getName();
426     }
427
428     /* ********** END begin implementation of POIFSViewable ********** */
429 } // end public class DirectoryNode
430

431
Popular Tags