KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > property > DirectoryProperty


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.property;
20
21 import java.util.*;
22
23 import java.io.IOException JavaDoc;
24
25 import org.apache.poi.poifs.storage.SmallDocumentBlock;
26
27 /**
28  * Directory property
29  *
30  * @author Marc Johnson (mjohnson at apache dot org)
31  */

32
33 public class DirectoryProperty
34     extends Property
35     implements Parent
36 {
37
38     // List of Property instances
39
private List _children;
40
41     // set of children's names
42
private Set _children_names;
43
44     /**
45      * Default constructor
46      *
47      * @param name the name of the directory
48      */

49
50     public DirectoryProperty(String JavaDoc name)
51     {
52         super();
53         _children = new ArrayList();
54         _children_names = new HashSet();
55         setName(name);
56         setSize(0);
57         setPropertyType(PropertyConstants.DIRECTORY_TYPE);
58         setStartBlock(0);
59         setNodeColor(_NODE_BLACK); // simplification
60
}
61
62     /**
63      * reader constructor
64      *
65      * @param index index number
66      * @param array byte data
67      * @param offset offset into byte data
68      */

69
70     protected DirectoryProperty(final int index, final byte [] array,
71                                 final int offset)
72     {
73         super(index, array, offset);
74         _children = new ArrayList();
75         _children_names = new HashSet();
76     }
77
78     /**
79      * Change a Property's name
80      *
81      * @param property the Property whose name is being changed
82      * @param newName the new name for the Property
83      *
84      * @return true if the name change could be made, else false
85      */

86
87     public boolean changeName(final Property property, final String JavaDoc newName)
88     {
89         boolean result;
90         String JavaDoc oldName = property.getName();
91
92         property.setName(newName);
93         String JavaDoc cleanNewName = property.getName();
94
95         if (_children_names.contains(cleanNewName))
96         {
97
98             // revert the change
99
property.setName(oldName);
100             result = false;
101         }
102         else
103         {
104             _children_names.add(cleanNewName);
105             _children_names.remove(oldName);
106             result = true;
107         }
108         return result;
109     }
110
111     /**
112      * Delete a Property
113      *
114      * @param property the Property being deleted
115      *
116      * @return true if the Property could be deleted, else false
117      */

118
119     public boolean deleteChild(final Property property)
120     {
121         boolean result = _children.remove(property);
122
123         if (result)
124         {
125             _children_names.remove(property.getName());
126         }
127         return result;
128     }
129
130     private class PropertyComparator
131         implements Comparator
132     {
133
134         /**
135          * Object equality, implemented as object identity
136          *
137          * @param o Object we're being compared to
138          *
139          * @return true if identical, else false
140          */

141
142         public boolean equals(Object JavaDoc o)
143         {
144             return this == o;
145         }
146
147         /**
148          * compare method. Assumes both parameters are non-null
149          * instances of Property. One property is less than another if
150          * its name is shorter than the other property's name. If the
151          * names are the same length, the property whose name comes
152          * before the other property's name, alphabetically, is less
153          * than the other property.
154          *
155          * @param o1 first object to compare, better be a Property
156          * @param o2 second object to compare, better be a Property
157          *
158          * @return negative value if o1 < o2,
159          * zero if o1 == o2,
160          * positive value if o1 > o2.
161          */

162
163         public int compare(Object JavaDoc o1, Object JavaDoc o2)
164         {
165             String JavaDoc name1 = (( Property ) o1).getName();
166             String JavaDoc name2 = (( Property ) o2).getName();
167             int result = name1.length() - name2.length();
168
169             if (result == 0)
170             {
171                 result = name1.compareTo(name2);
172             }
173             return result;
174         }
175     } // end private class PropertyComparator
176

177     /* ********** START extension of Property ********** */
178
179     /**
180      * @return true if a directory type Property
181      */

182
183     public boolean isDirectory()
184     {
185         return true;
186     }
187
188     /**
189      * Perform whatever activities need to be performed prior to
190      * writing
191      */

192
193     protected void preWrite()
194     {
195         if (_children.size() > 0)
196         {
197             Property[] children =
198                 ( Property [] ) _children.toArray(new Property[ 0 ]);
199
200             Arrays.sort(children, new PropertyComparator());
201             int midpoint = children.length / 2;
202
203             setChildProperty(children[ midpoint ].getIndex());
204             children[ 0 ].setPreviousChild(null);
205             children[ 0 ].setNextChild(null);
206             for (int j = 1; j < midpoint; j++)
207             {
208                 children[ j ].setPreviousChild(children[ j - 1 ]);
209                 children[ j ].setNextChild(null);
210             }
211             if (midpoint != 0)
212             {
213                 children[ midpoint ]
214                     .setPreviousChild(children[ midpoint - 1 ]);
215             }
216             if (midpoint != (children.length - 1))
217             {
218                 children[ midpoint ].setNextChild(children[ midpoint + 1 ]);
219                 for (int j = midpoint + 1; j < children.length - 1; j++)
220                 {
221                     children[ j ].setPreviousChild(null);
222                     children[ j ].setNextChild(children[ j + 1 ]);
223                 }
224                 children[ children.length - 1 ].setPreviousChild(null);
225                 children[ children.length - 1 ].setNextChild(null);
226             }
227             else
228             {
229                 children[ midpoint ].setNextChild(null);
230             }
231         }
232     }
233
234     /* ********** END extension of Property ********** */
235     /* ********** START implementation of Parent ********** */
236
237     /**
238      * Get an iterator over the children of this Parent; all elements
239      * are instances of Property.
240      *
241      * @return Iterator of children; may refer to an empty collection
242      */

243
244     public Iterator getChildren()
245     {
246         return _children.iterator();
247     }
248
249     /**
250      * Add a new child to the collection of children
251      *
252      * @param property the new child to be added; must not be null
253      *
254      * @exception IOException if we already have a child with the same
255      * name
256      */

257
258     public void addChild(final Property property)
259         throws IOException JavaDoc
260     {
261         String JavaDoc name = property.getName();
262
263         if (_children_names.contains(name))
264         {
265             throw new IOException JavaDoc("Duplicate name \"" + name + "\"");
266         }
267         _children_names.add(name);
268         _children.add(property);
269     }
270
271     /* ********** END implementation of Parent ********** */
272 } // end public class DirectoryProperty
273

274
Popular Tags