KickJava   Java API By Example, From Geeks To Geeks.

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


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.poifs.property.Property;
26
27 /**
28  * Abstract implementation of Entry
29  *
30  * Extending classes should override isDocument() or isDirectory(), as
31  * appropriate
32  *
33  * Extending classes must override isDeleteOK()
34  *
35  * @author Marc Johnson (mjohnson at apache dot org)
36  */

37
38 public abstract class EntryNode
39     implements Entry
40 {
41
42     // the DocumentProperty backing this object
43
private Property _property;
44
45     // this object's parent Entry
46
private DirectoryNode _parent;
47
48     /**
49      * create a DocumentNode. This method is not public by design; it
50      * is intended strictly for the internal use of extending classes
51      *
52      * @param property the Property for this Entry
53      * @param parent the parent of this entry
54      */

55
56     protected EntryNode(final Property property, final DirectoryNode parent)
57     {
58         _property = property;
59         _parent = parent;
60     }
61
62     /**
63      * grant access to the property
64      *
65      * @return the property backing this entry
66      */

67
68     protected Property getProperty()
69     {
70         return _property;
71     }
72
73     /**
74      * is this the root of the tree?
75      *
76      * @return true if so, else false
77      */

78
79     protected boolean isRoot()
80     {
81
82         // only the root Entry has no parent ...
83
return (_parent == null);
84     }
85
86     /**
87      * extensions use this method to verify internal rules regarding
88      * deletion of the underlying store.
89      *
90      * @return true if it's ok to delete the underlying store, else
91      * false
92      */

93
94     protected abstract boolean isDeleteOK();
95
96     /* ********** START implementation of Entry ********** */
97
98     /**
99      * get the name of the Entry
100      *
101      * @return name
102      */

103
104     public String JavaDoc getName()
105     {
106         return _property.getName();
107     }
108
109     /**
110      * is this a DirectoryEntry?
111      *
112      * @return true if the Entry is a DirectoryEntry, else false
113      */

114
115     public boolean isDirectoryEntry()
116     {
117         return false;
118     }
119
120     /**
121      * is this a DocumentEntry?
122      *
123      * @return true if the Entry is a DocumentEntry, else false
124      */

125
126     public boolean isDocumentEntry()
127     {
128         return false;
129     }
130
131     /**
132      * get this Entry's parent (the DocumentEntry that owns this
133      * Entry). All Entry objects, except the root Entry, has a parent.
134      *
135      * @return this Entry's parent; null iff this is the root Entry
136      */

137
138     public DirectoryEntry getParent()
139     {
140         return _parent;
141     }
142
143     /**
144      * Delete this Entry. This operation should succeed, but there are
145      * special circumstances when it will not:
146      *
147      * If this Entry is the root of the Entry tree, it cannot be
148      * deleted, as there is no way to create another one.
149      *
150      * If this Entry is a directory, it cannot be deleted unless it is
151      * empty.
152      *
153      * @return true if the Entry was successfully deleted, else false
154      */

155
156     public boolean delete()
157     {
158         boolean rval = false;
159
160         if ((!isRoot()) && isDeleteOK())
161         {
162             rval = _parent.deleteEntry(this);
163         }
164         return rval;
165     }
166
167     /**
168      * Rename this Entry. This operation will fail if:
169      *
170      * There is a sibling Entry (i.e., an Entry whose parent is the
171      * same as this Entry's parent) with the same name.
172      *
173      * This Entry is the root of the Entry tree. Its name is dictated
174      * by the Filesystem and many not be changed.
175      *
176      * @param newName the new name for this Entry
177      *
178      * @return true if the operation succeeded, else false
179      */

180
181     public boolean renameTo(final String JavaDoc newName)
182     {
183         boolean rval = false;
184
185         if (!isRoot())
186         {
187             rval = _parent.changeName(getName(), newName);
188         }
189         return rval;
190     }
191
192     /* ********** END implementation of Entry ********** */
193 } // end public class EntryNode
194

195
Popular Tags