001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.filesystem;
019:
020: import org.apache.poi.poifs.property.Property;
021:
022: /**
023: * Abstract implementation of Entry
024: *
025: * Extending classes should override isDocument() or isDirectory(), as
026: * appropriate
027: *
028: * Extending classes must override isDeleteOK()
029: *
030: * @author Marc Johnson (mjohnson at apache dot org)
031: */
032:
033: public abstract class EntryNode implements Entry {
034:
035: // the DocumentProperty backing this object
036: private Property _property;
037:
038: // this object's parent Entry
039: private DirectoryNode _parent;
040:
041: /**
042: * create a DocumentNode. This method is not public by design; it
043: * is intended strictly for the internal use of extending classes
044: *
045: * @param property the Property for this Entry
046: * @param parent the parent of this entry
047: */
048:
049: protected EntryNode(final Property property,
050: final DirectoryNode parent) {
051: _property = property;
052: _parent = parent;
053: }
054:
055: /**
056: * grant access to the property
057: *
058: * @return the property backing this entry
059: */
060:
061: protected Property getProperty() {
062: return _property;
063: }
064:
065: /**
066: * is this the root of the tree?
067: *
068: * @return true if so, else false
069: */
070:
071: protected boolean isRoot() {
072:
073: // only the root Entry has no parent ...
074: return (_parent == null);
075: }
076:
077: /**
078: * extensions use this method to verify internal rules regarding
079: * deletion of the underlying store.
080: *
081: * @return true if it's ok to delete the underlying store, else
082: * false
083: */
084:
085: protected abstract boolean isDeleteOK();
086:
087: /* ********** START implementation of Entry ********** */
088:
089: /**
090: * get the name of the Entry
091: *
092: * @return name
093: */
094:
095: public String getName() {
096: return _property.getName();
097: }
098:
099: /**
100: * is this a DirectoryEntry?
101: *
102: * @return true if the Entry is a DirectoryEntry, else false
103: */
104:
105: public boolean isDirectoryEntry() {
106: return false;
107: }
108:
109: /**
110: * is this a DocumentEntry?
111: *
112: * @return true if the Entry is a DocumentEntry, else false
113: */
114:
115: public boolean isDocumentEntry() {
116: return false;
117: }
118:
119: /**
120: * get this Entry's parent (the DocumentEntry that owns this
121: * Entry). All Entry objects, except the root Entry, has a parent.
122: *
123: * @return this Entry's parent; null iff this is the root Entry
124: */
125:
126: public DirectoryEntry getParent() {
127: return _parent;
128: }
129:
130: /**
131: * Delete this Entry. This operation should succeed, but there are
132: * special circumstances when it will not:
133: *
134: * If this Entry is the root of the Entry tree, it cannot be
135: * deleted, as there is no way to create another one.
136: *
137: * If this Entry is a directory, it cannot be deleted unless it is
138: * empty.
139: *
140: * @return true if the Entry was successfully deleted, else false
141: */
142:
143: public boolean delete() {
144: boolean rval = false;
145:
146: if ((!isRoot()) && isDeleteOK()) {
147: rval = _parent.deleteEntry(this );
148: }
149: return rval;
150: }
151:
152: /**
153: * Rename this Entry. This operation will fail if:
154: *
155: * There is a sibling Entry (i.e., an Entry whose parent is the
156: * same as this Entry's parent) with the same name.
157: *
158: * This Entry is the root of the Entry tree. Its name is dictated
159: * by the Filesystem and many not be changed.
160: *
161: * @param newName the new name for this Entry
162: *
163: * @return true if the operation succeeded, else false
164: */
165:
166: public boolean renameTo(final String newName) {
167: boolean rval = false;
168:
169: if (!isRoot()) {
170: rval = _parent.changeName(getName(), newName);
171: }
172: return rval;
173: }
174:
175: /* ********** END implementation of Entry ********** */
176: } // end public class EntryNode
|