1 2 17 18 19 package org.apache.poi.poifs.property; 20 21 import java.util.*; 22 23 import java.io.IOException ; 24 25 import org.apache.poi.poifs.storage.SmallDocumentBlock; 26 27 32 33 public class DirectoryProperty 34 extends Property 35 implements Parent 36 { 37 38 private List _children; 40 41 private Set _children_names; 43 44 49 50 public DirectoryProperty(String 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); } 61 62 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 86 87 public boolean changeName(final Property property, final String newName) 88 { 89 boolean result; 90 String oldName = property.getName(); 91 92 property.setName(newName); 93 String cleanNewName = property.getName(); 94 95 if (_children_names.contains(cleanNewName)) 96 { 97 98 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 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 141 142 public boolean equals(Object o) 143 { 144 return this == o; 145 } 146 147 162 163 public int compare(Object o1, Object o2) 164 { 165 String name1 = (( Property ) o1).getName(); 166 String 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 } 177 178 179 182 183 public boolean isDirectory() 184 { 185 return true; 186 } 187 188 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 235 236 237 243 244 public Iterator getChildren() 245 { 246 return _children.iterator(); 247 } 248 249 257 258 public void addChild(final Property property) 259 throws IOException 260 { 261 String name = property.getName(); 262 263 if (_children_names.contains(name)) 264 { 265 throw new IOException ("Duplicate name \"" + name + "\""); 266 } 267 _children_names.add(name); 268 _children.add(property); 269 } 270 271 272 } 274 | Popular Tags |