1 /
55
56 package org.apache.poi.poifs.property;
57
58 import java.util.*;
59
60 import java.io.IOException;
61
62 import org.apache.poi.poifs.storage.SmallDocumentBlock;
63
64
69
70 public class DirectoryProperty
71 extends Property
72 implements Parent
73 {
74
75
76 private List _children;
77
78
79 private Set _children_names;
80
81
86
87 public DirectoryProperty(String name)
88 {
89 super();
90 _children = new ArrayList();
91 _children_names = new HashSet();
92 setName(name);
93 setSize(0);
94 setPropertyType(PropertyConstants.DIRECTORY_TYPE);
95 setStartBlock(0);
96 setNodeColor(_NODE_BLACK);
97 }
98
99
106
107 protected DirectoryProperty(final int index, final byte [] array,
108 final int offset)
109 {
110 super(index, array, offset);
111 _children = new ArrayList();
112 _children_names = new HashSet();
113 }
114
115
123
124 public boolean changeName(final Property property, final String newName)
125 {
126 boolean result;
127 String oldName = property.getName();
128
129 property.setName(newName);
130 String cleanNewName = property.getName();
131
132 if (_children_names.contains(cleanNewName))
133 {
134
135
136 property.setName(oldName);
137 result = false;
138 }
139 else
140 {
141 _children_names.add(cleanNewName);
142 _children_names.remove(oldName);
143 result = true;
144 }
145 return result;
146 }
147
148
155
156 public boolean deleteChild(final Property property)
157 {
158 boolean result = _children.remove(property);
159
160 if (result)
161 {
162 _children_names.remove(property.getName());
163 }
164 return result;
165 }
166
167 private class PropertyComparator
168 implements Comparator
169 {
170
171
178
179 public boolean equals(Object o)
180 {
181 return this == o;
182 }
183
184
199
200 public int compare(Object o1, Object o2)
201 {
202 String name1 = (( Property ) o1).getName();
203 String name2 = (( Property ) o2).getName();
204 int result = name1.length() - name2.length();
205
206 if (result == 0)
207 {
208 result = name1.compareTo(name2);
209 }
210 return result;
211 }
212 }
213
214
215
216
219
220 public boolean isDirectory()
221 {
222 return true;
223 }
224
225
229
230 protected void preWrite()
231 {
232 if (_children.size() > 0)
233 {
234 Property[] children =
235 ( Property [] ) _children.toArray(new Property[ 0 ]);
236
237 Arrays.sort(children, new PropertyComparator());
238 int midpoint = children.length / 2;
239
240 setChildProperty(children[ midpoint ].getIndex());
241 children[ 0 ].setPreviousChild(null);
242 children[ 0 ].setNextChild(null);
243 for (int j = 1; j < midpoint; j++)
244 {
245 children[ j ].setPreviousChild(children[ j - 1 ]);
246 children[ j ].setNextChild(null);
247 }
248 if (midpoint != 0)
249 {
250 childrensetPreviousChild .setPreviousChild(children[ midpoint - 1 ]);
251 }
252 if (midpoint != (children.length - 1))
253 {
254 children[ midpoint ].setNextChild(children[ midpoint + 1 ]);
255 for (int j = midpoint + 1; j < children.length - 1; j++)
256 {
257 children[ j ].setPreviousChild(null);
258 children[ j ].setNextChild(children[ j + 1 ]);
259 }
260 children[ children.length - 1 ].setPreviousChild(null);
261 children[ children.length - 1 ].setNextChild(null);
262 }
263 else
264 {
265 children[ midpoint ].setNextChild(null);
266 }
267 }
268 }
269
270 /* ********** END extension of Property ********** */
271 /* ********** START implementation of Parent ********** */
272
273 /**
274 * Get an iterator over the children of this Parent; all elements
275 * are instances of Property.
276 *
277 * @return Iterator of children; may refer to an empty collection
278 */
279
280 public Iterator getChildren()
281 {
282 return _children.iterator();
283 }
284
285 /**
286 * Add a new child to the collection of children
287 *
288 * @param property the new child to be added; must not be null
289 *
290 * @exception IOException if we already have a child with the same
291 * name
292 */
293
294 public void addChild(final Property property)
295 throws IOException
296 {
297 String name = property.getName();
298
299 if (_children_names.contains(name))
300 {
301 throw new IOException("Duplicate name \"" + name + "\"");
302 }
303 _children_names.add(name);
304 _children.add(property);
305 }
306
307 /* ********** END implementation of Parent ********** */
308 } // end public class DirectoryProperty
309
310 midpointchildrenmidpointmidpointchildrenchildrenmidpointsetNextChildchildrenmidpointmidpointjchildrenjchildrenjsetPreviousChildchildrenjsetNextChildchildrenjchildrenchildrensetPreviousChildchildrenchildrensetNextChildchildrenmidpointsetNextChildgetChildren_childrenaddChildPropertypropertygetName_children_namesnamename_children_namesname_childrenproperty