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.6KB

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