You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectoryProperty.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Comparator;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Set;
  25. /**
  26. * Directory property
  27. */
  28. public class DirectoryProperty extends Property implements Parent, Iterable<Property> {
  29. /** List of Property instances */
  30. private final List<Property> _children = new ArrayList<>();
  31. /** set of children's names */
  32. private final Set<String> _children_names = new HashSet<>();
  33. /**
  34. * Default constructor
  35. *
  36. * @param name the name of the directory
  37. */
  38. public DirectoryProperty(String name)
  39. {
  40. super();
  41. setName(name);
  42. setSize(0);
  43. setPropertyType(PropertyConstants.DIRECTORY_TYPE);
  44. setStartBlock(0);
  45. setNodeColor(_NODE_BLACK); // simplification
  46. }
  47. /**
  48. * reader constructor
  49. *
  50. * @param index index number
  51. * @param array byte data
  52. * @param offset offset into byte data
  53. */
  54. protected DirectoryProperty(final int index, final byte [] array,
  55. final int offset)
  56. {
  57. super(index, array, offset);
  58. }
  59. /**
  60. * Change a Property's name
  61. *
  62. * @param property the Property whose name is being changed
  63. * @param newName the new name for the Property
  64. *
  65. * @return true if the name change could be made, else false
  66. */
  67. public boolean changeName(Property property, String newName)
  68. {
  69. boolean result;
  70. String oldName = property.getName();
  71. property.setName(newName);
  72. String cleanNewName = property.getName();
  73. if (_children_names.contains(cleanNewName))
  74. {
  75. // revert the change
  76. property.setName(oldName);
  77. result = false;
  78. }
  79. else
  80. {
  81. _children_names.add(cleanNewName);
  82. _children_names.remove(oldName);
  83. result = true;
  84. }
  85. return result;
  86. }
  87. /**
  88. * Delete a Property
  89. *
  90. * @param property the Property being deleted
  91. *
  92. * @return true if the Property could be deleted, else false
  93. */
  94. public boolean deleteChild(Property property)
  95. {
  96. boolean result = _children.remove(property);
  97. if (result)
  98. {
  99. _children_names.remove(property.getName());
  100. }
  101. return result;
  102. }
  103. public static class PropertyComparator implements Comparator<Property>, Serializable {
  104. /**
  105. * compare method. Assumes both parameters are non-null
  106. * instances of Property. One property is less than another if
  107. * its name is shorter than the other property's name. If the
  108. * names are the same length, the property whose name comes
  109. * before the other property's name, alphabetically, is less
  110. * than the other property.
  111. *
  112. * @param o1 first object to compare, better be a Property
  113. * @param o2 second object to compare, better be a Property
  114. *
  115. * @return negative value if o1 &lt; o2,
  116. * zero if o1 == o2,
  117. * positive value if o1 &gt; o2.
  118. */
  119. public int compare(Property o1, Property o2)
  120. {
  121. String VBA_PROJECT = "_VBA_PROJECT";
  122. String name1 = o1.getName();
  123. String name2 = o2.getName();
  124. int result = name1.length() - name2.length();
  125. if (result == 0)
  126. {
  127. // _VBA_PROJECT, it seems, will always come last
  128. if (name1.compareTo(VBA_PROJECT) == 0)
  129. result = 1;
  130. else if (name2.compareTo(VBA_PROJECT) == 0)
  131. result = -1;
  132. else
  133. {
  134. if (name1.startsWith("__") && name2.startsWith("__"))
  135. {
  136. // Betweeen __SRP_0 and __SRP_1 just sort as normal
  137. result = name1.compareToIgnoreCase(name2);
  138. }
  139. else if (name1.startsWith("__"))
  140. {
  141. // If only name1 is __XXX then this will be placed after name2
  142. result = 1;
  143. }
  144. else if (name2.startsWith("__"))
  145. {
  146. // If only name2 is __XXX then this will be placed after name1
  147. result = -1;
  148. }
  149. else
  150. // result = name1.compareTo(name2);
  151. // The default case is to sort names ignoring case
  152. result = name1.compareToIgnoreCase(name2);
  153. }
  154. }
  155. return result;
  156. }
  157. }
  158. /**
  159. * @return true if a directory type Property
  160. */
  161. public boolean isDirectory()
  162. {
  163. return true;
  164. }
  165. /**
  166. * Perform whatever activities need to be performed prior to
  167. * writing
  168. */
  169. protected void preWrite()
  170. {
  171. if (_children.size() > 0)
  172. {
  173. Property[] children = _children.toArray(new Property[ 0 ]);
  174. Arrays.sort(children, new PropertyComparator());
  175. int midpoint = children.length / 2;
  176. setChildProperty(children[ midpoint ].getIndex());
  177. children[ 0 ].setPreviousChild(null);
  178. children[ 0 ].setNextChild(null);
  179. for (int j = 1; j < midpoint; j++)
  180. {
  181. children[ j ].setPreviousChild(children[ j - 1 ]);
  182. children[ j ].setNextChild(null);
  183. }
  184. if (midpoint != 0)
  185. {
  186. children[ midpoint ]
  187. .setPreviousChild(children[ midpoint - 1 ]);
  188. }
  189. if (midpoint != (children.length - 1))
  190. {
  191. children[ midpoint ].setNextChild(children[ midpoint + 1 ]);
  192. for (int j = midpoint + 1; j < children.length - 1; j++)
  193. {
  194. children[ j ].setPreviousChild(null);
  195. children[ j ].setNextChild(children[ j + 1 ]);
  196. }
  197. children[ children.length - 1 ].setPreviousChild(null);
  198. children[ children.length - 1 ].setNextChild(null);
  199. }
  200. else
  201. {
  202. children[ midpoint ].setNextChild(null);
  203. }
  204. }
  205. }
  206. /**
  207. * Get an iterator over the children of this Parent; all elements
  208. * are instances of Property.
  209. *
  210. * @return Iterator of children; may refer to an empty collection
  211. */
  212. public Iterator<Property> getChildren()
  213. {
  214. return _children.iterator();
  215. }
  216. /**
  217. * Get an iterator over the children of this Parent, alias for
  218. * {@link #getChildren()} which supports foreach use
  219. */
  220. public Iterator<Property> iterator() {
  221. return getChildren();
  222. }
  223. /**
  224. * Add a new child to the collection of children
  225. *
  226. * @param property the new child to be added; must not be null
  227. *
  228. * @throws IOException if we already have a child with the same
  229. * name
  230. */
  231. public void addChild(final Property property)
  232. throws IOException
  233. {
  234. String name = property.getName();
  235. if (_children_names.contains(name))
  236. {
  237. throw new IOException("Duplicate name \"" + name + "\"");
  238. }
  239. _children_names.add(name);
  240. _children.add(property);
  241. }
  242. }