您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DirectoryProperty.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.property;
  16. import java.io.IOException;
  17. import java.util.*;
  18. /**
  19. * Directory property
  20. *
  21. * @author Marc Johnson (mjohnson at apache dot org)
  22. */
  23. public class DirectoryProperty extends Property implements Parent { // TODO - fix instantiable superclass
  24. /** List of Property instances */
  25. private List<Property> _children;
  26. /** set of children's names */
  27. private Set<String> _children_names;
  28. /**
  29. * Default constructor
  30. *
  31. * @param name the name of the directory
  32. */
  33. public DirectoryProperty(String name)
  34. {
  35. super();
  36. _children = new ArrayList<Property>();
  37. _children_names = new HashSet<String>();
  38. setName(name);
  39. setSize(0);
  40. setPropertyType(PropertyConstants.DIRECTORY_TYPE);
  41. setStartBlock(0);
  42. setNodeColor(_NODE_BLACK); // simplification
  43. }
  44. /**
  45. * reader constructor
  46. *
  47. * @param index index number
  48. * @param array byte data
  49. * @param offset offset into byte data
  50. */
  51. protected DirectoryProperty(final int index, final byte [] array,
  52. final int offset)
  53. {
  54. super(index, array, offset);
  55. _children = new ArrayList<Property>();
  56. _children_names = new HashSet<String>();
  57. }
  58. /**
  59. * Change a Property's name
  60. *
  61. * @param property the Property whose name is being changed
  62. * @param newName the new name for the Property
  63. *
  64. * @return true if the name change could be made, else false
  65. */
  66. public boolean changeName(Property property, String newName)
  67. {
  68. boolean result;
  69. String oldName = property.getName();
  70. property.setName(newName);
  71. String cleanNewName = property.getName();
  72. if (_children_names.contains(cleanNewName))
  73. {
  74. // revert the change
  75. property.setName(oldName);
  76. result = false;
  77. }
  78. else
  79. {
  80. _children_names.add(cleanNewName);
  81. _children_names.remove(oldName);
  82. result = true;
  83. }
  84. return result;
  85. }
  86. /**
  87. * Delete a Property
  88. *
  89. * @param property the Property being deleted
  90. *
  91. * @return true if the Property could be deleted, else false
  92. */
  93. public boolean deleteChild(Property property)
  94. {
  95. boolean result = _children.remove(property);
  96. if (result)
  97. {
  98. _children_names.remove(property.getName());
  99. }
  100. return result;
  101. }
  102. public static class PropertyComparator implements Comparator<Property> {
  103. /**
  104. * Object equality, implemented as object identity
  105. *
  106. * @param o Object we're being compared to
  107. *
  108. * @return true if identical, else false
  109. */
  110. public boolean equals(Object o)
  111. {
  112. return this == o;
  113. }
  114. /**
  115. * compare method. Assumes both parameters are non-null
  116. * instances of Property. One property is less than another if
  117. * its name is shorter than the other property's name. If the
  118. * names are the same length, the property whose name comes
  119. * before the other property's name, alphabetically, is less
  120. * than the other property.
  121. *
  122. * @param o1 first object to compare, better be a Property
  123. * @param o2 second object to compare, better be a Property
  124. *
  125. * @return negative value if o1 < o2,
  126. * zero if o1 == o2,
  127. * positive value if o1 > o2.
  128. */
  129. public int compare(Property o1, Property o2)
  130. {
  131. String VBA_PROJECT = "_VBA_PROJECT";
  132. String name1 = o1.getName();
  133. String name2 = o2.getName();
  134. int result = name1.length() - name2.length();
  135. if (result == 0)
  136. {
  137. // _VBA_PROJECT, it seems, will always come last
  138. if (name1.compareTo(VBA_PROJECT) == 0)
  139. result = 1;
  140. else if (name2.compareTo(VBA_PROJECT) == 0)
  141. result = -1;
  142. else
  143. {
  144. if (name1.startsWith("__") && name2.startsWith("__"))
  145. {
  146. // Betweeen __SRP_0 and __SRP_1 just sort as normal
  147. result = name1.compareToIgnoreCase(name2);
  148. }
  149. else if (name1.startsWith("__"))
  150. {
  151. // If only name1 is __XXX then this will be placed after name2
  152. result = 1;
  153. }
  154. else if (name2.startsWith("__"))
  155. {
  156. // If only name2 is __XXX then this will be placed after name1
  157. result = -1;
  158. }
  159. else
  160. // result = name1.compareTo(name2);
  161. // The default case is to sort names ignoring case
  162. result = name1.compareToIgnoreCase(name2);
  163. }
  164. }
  165. return result;
  166. }
  167. }
  168. /**
  169. * @return true if a directory type Property
  170. */
  171. public boolean isDirectory()
  172. {
  173. return true;
  174. }
  175. /**
  176. * Perform whatever activities need to be performed prior to
  177. * writing
  178. */
  179. protected void preWrite()
  180. {
  181. if (_children.size() > 0)
  182. {
  183. Property[] children = _children.toArray(new Property[ 0 ]);
  184. Arrays.sort(children, new PropertyComparator());
  185. int midpoint = children.length / 2;
  186. setChildProperty(children[ midpoint ].getIndex());
  187. children[ 0 ].setPreviousChild(null);
  188. children[ 0 ].setNextChild(null);
  189. for (int j = 1; j < midpoint; j++)
  190. {
  191. children[ j ].setPreviousChild(children[ j - 1 ]);
  192. children[ j ].setNextChild(null);
  193. }
  194. if (midpoint != 0)
  195. {
  196. children[ midpoint ]
  197. .setPreviousChild(children[ midpoint - 1 ]);
  198. }
  199. if (midpoint != (children.length - 1))
  200. {
  201. children[ midpoint ].setNextChild(children[ midpoint + 1 ]);
  202. for (int j = midpoint + 1; j < children.length - 1; j++)
  203. {
  204. children[ j ].setPreviousChild(null);
  205. children[ j ].setNextChild(children[ j + 1 ]);
  206. }
  207. children[ children.length - 1 ].setPreviousChild(null);
  208. children[ children.length - 1 ].setNextChild(null);
  209. }
  210. else
  211. {
  212. children[ midpoint ].setNextChild(null);
  213. }
  214. }
  215. }
  216. /**
  217. * Get an iterator over the children of this Parent; all elements
  218. * are instances of Property.
  219. *
  220. * @return Iterator of children; may refer to an empty collection
  221. */
  222. public Iterator<Property> getChildren()
  223. {
  224. return _children.iterator();
  225. }
  226. /**
  227. * Add a new child to the collection of children
  228. *
  229. * @param property the new child to be added; must not be null
  230. *
  231. * @exception IOException if we already have a child with the same
  232. * name
  233. */
  234. public void addChild(final Property property)
  235. throws IOException
  236. {
  237. String name = property.getName();
  238. if (_children_names.contains(name))
  239. {
  240. throw new IOException("Duplicate name \"" + name + "\"");
  241. }
  242. _children_names.add(name);
  243. _children.add(property);
  244. }
  245. }