1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.poifs.filesystem;
57   
58   import java.io.*;
59   
60   import java.util.*;
61   
62   import org.apache.poi.poifs.property.Property;
63   
64   /**
65    * Abstract implementation of Entry
66    *
67    * Extending classes should override isDocument() or isDirectory(), as
68    * appropriate
69    *
70    * Extending classes must override isDeleteOK()
71    *
72    * @author Marc Johnson (mjohnson at apache dot org)
73    */
74   
75   public abstract class EntryNode
76       implements Entry
77   {
78   
79       // the DocumentProperty backing this object
80       private Property      _property;
81   
82       // this object's parent Entry
83       private DirectoryNode _parent;
84   
85       /**
86        * create a DocumentNode. This method is not public by design; it
87        * is intended strictly for the internal use of extending classes
88        *
89        * @param property the Property for this Entry
90        * @param parent the parent of this entry
91        */
92   
93       protected EntryNode(final Property property, final DirectoryNode parent)
94       {
95           _property = property;
96           _parent   = parent;
97       }
98   
99       /**
100       * grant access to the property
101       *
102       * @return the property backing this entry
103       */
104  
105      protected Property getProperty()
106      {
107          return _property;
108      }
109  
110      /**
111       * is this the root of the tree?
112       *
113       * @return true if so, else false
114       */
115  
116      protected boolean isRoot()
117      {
118  
119          // only the root Entry has no parent ...
120          return (_parent == null);
121      }
122  
123      /**
124       * extensions use this method to verify internal rules regarding
125       * deletion of the underlying store.
126       *
127       * @return true if it's ok to delete the underlying store, else
128       *         false
129       */
130  
131      protected abstract boolean isDeleteOK();
132  
133      /* ********** START implementation of Entry ********** */
134  
135      /**
136       * get the name of the Entry
137       *
138       * @return name
139       */
140  
141      public String getName()
142      {
143          return _property.getName();
144      }
145  
146      /**
147       * is this a DirectoryEntry?
148       *
149       * @return true if the Entry is a DirectoryEntry, else false
150       */
151  
152      public boolean isDirectoryEntry()
153      {
154          return false;
155      }
156  
157      /**
158       * is this a DocumentEntry?
159       *
160       * @return true if the Entry is a DocumentEntry, else false
161       */
162  
163      public boolean isDocumentEntry()
164      {
165          return false;
166      }
167  
168      /**
169       * get this Entry's parent (the DocumentEntry that owns this
170       * Entry). All Entry objects, except the root Entry, has a parent.
171       *
172       * @return this Entry's parent; null iff this is the root Entry
173       */
174  
175      public DirectoryEntry getParent()
176      {
177          return _parent;
178      }
179  
180      /**
181       * Delete this Entry. This operation should succeed, but there are
182       * special circumstances when it will not:
183       *
184       * If this Entry is the root of the Entry tree, it cannot be
185       * deleted, as there is no way to create another one.
186       *
187       * If this Entry is a directory, it cannot be deleted unless it is
188       * empty.
189       *
190       * @return true if the Entry was successfully deleted, else false
191       */
192  
193      public boolean delete()
194      {
195          boolean rval = false;
196  
197          if ((!isRoot()) && isDeleteOK())
198          {
199              rval = _parent.deleteEntry(this);
200          }
201          return rval;
202      }
203  
204      /**
205       * Rename this Entry. This operation will fail if:
206       *
207       * There is a sibling Entry (i.e., an Entry whose parent is the
208       * same as this Entry's parent) with the same name.
209       *
210       * This Entry is the root of the Entry tree. Its name is dictated
211       * by the Filesystem and many not be changed.
212       *
213       * @param newName the new name for this Entry
214       *
215       * @return true if the operation succeeded, else false
216       */
217  
218      public boolean renameTo(final String newName)
219      {
220          boolean rval = false;
221  
222          if (!isRoot())
223          {
224              rval = _parent.changeName(getName(), newName);
225          }
226          return rval;
227      }
228  
229      /* **********  END  implementation of Entry ********** */
230  }   // end public class EntryNode
231  
232