PageRenderTime 169ms CodeModel.GetById 34ms RepoModel.GetById 4ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/poifs/filesystem/POIFSDocumentPath.java

http://github.com/openmicroscopy/bioformats
Java | 297 lines | 141 code | 30 blank | 126 comment | 33 complexity | ea6d5412b0105c393c6da14c7b688251 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.poifs.filesystem;
  38. import java.io.File;
  39. /**
  40. * Class POIFSDocumentPath
  41. *
  42. * @author Marc Johnson (mjohnson at apache dot org)
  43. * @version %I%, %G%
  44. */
  45. public class POIFSDocumentPath
  46. {
  47. private String[] components;
  48. private int hashcode = 0;
  49. /**
  50. * constructor for the path of a document that is not in the root
  51. * of the POIFSFileSystem
  52. *
  53. * @param components the Strings making up the path to a document.
  54. * The Strings must be ordered as they appear in
  55. * the directory hierarchy of the the document
  56. * -- the first string must be the name of a
  57. * directory in the root of the POIFSFileSystem,
  58. * and every Nth (for N > 1) string thereafter
  59. * must be the name of a directory in the
  60. * directory identified by the (N-1)th string.
  61. * <p>
  62. * If the components parameter is null or has
  63. * zero length, the POIFSDocumentPath is
  64. * appropriate for a document that is in the
  65. * root of a POIFSFileSystem
  66. *
  67. * @exception IllegalArgumentException if any of the elements in
  68. * the components parameter
  69. * are null or have zero
  70. * length
  71. */
  72. public POIFSDocumentPath(final String [] components)
  73. throws IllegalArgumentException
  74. {
  75. if (components == null)
  76. {
  77. this.components = new String[ 0 ];
  78. }
  79. else
  80. {
  81. this.components = new String[ components.length ];
  82. for (int j = 0; j < components.length; j++)
  83. {
  84. if ((components[ j ] == null)
  85. || (components[ j ].length() == 0))
  86. {
  87. throw new IllegalArgumentException(
  88. "components cannot contain null or empty strings");
  89. }
  90. this.components[ j ] = components[ j ];
  91. }
  92. }
  93. }
  94. /**
  95. * simple constructor for the path of a document that is in the
  96. * root of the POIFSFileSystem. The constructor that takes an
  97. * array of Strings can also be used to create such a
  98. * POIFSDocumentPath by passing it a null or empty String array
  99. */
  100. public POIFSDocumentPath()
  101. {
  102. this.components = new String[ 0 ];
  103. }
  104. /**
  105. * constructor that adds additional subdirectories to an existing
  106. * path
  107. *
  108. * @param path the existing path
  109. * @param components the additional subdirectory names to be added
  110. *
  111. * @exception IllegalArgumentException if any of the Strings in
  112. * components is null or zero
  113. * length
  114. */
  115. public POIFSDocumentPath(final POIFSDocumentPath path,
  116. final String [] components)
  117. throws IllegalArgumentException
  118. {
  119. if (components == null)
  120. {
  121. this.components = new String[ path.components.length ];
  122. }
  123. else
  124. {
  125. this.components =
  126. new String[ path.components.length + components.length ];
  127. }
  128. for (int j = 0; j < path.components.length; j++)
  129. {
  130. this.components[ j ] = path.components[ j ];
  131. }
  132. if (components != null)
  133. {
  134. for (int j = 0; j < components.length; j++)
  135. {
  136. if ((components[ j ] == null)
  137. || (components[ j ].length() == 0))
  138. {
  139. throw new IllegalArgumentException(
  140. "components cannot contain null or empty strings");
  141. }
  142. this.components[ j + path.components.length ] =
  143. components[ j ];
  144. }
  145. }
  146. }
  147. /**
  148. * equality. Two POIFSDocumentPath instances are equal if they
  149. * have the same number of component Strings, and if each
  150. * component String is equal to its coresponding component String
  151. *
  152. * @param o the object we're checking equality for
  153. *
  154. * @return true if the object is equal to this object
  155. */
  156. public boolean equals(final Object o)
  157. {
  158. boolean rval = false;
  159. if ((o != null) && (o.getClass() == this.getClass()))
  160. {
  161. if (this == o)
  162. {
  163. rval = true;
  164. }
  165. else
  166. {
  167. POIFSDocumentPath path = ( POIFSDocumentPath ) o;
  168. if (path.components.length == this.components.length)
  169. {
  170. rval = true;
  171. for (int j = 0; j < this.components.length; j++)
  172. {
  173. if (!path.components[ j ]
  174. .equals(this.components[ j ]))
  175. {
  176. rval = false;
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. return rval;
  184. }
  185. /**
  186. * calculate and return the hashcode
  187. *
  188. * @return hashcode
  189. */
  190. public int hashCode()
  191. {
  192. if (hashcode == 0)
  193. {
  194. for (int j = 0; j < components.length; j++)
  195. {
  196. hashcode += components[ j ].hashCode();
  197. }
  198. }
  199. return hashcode;
  200. }
  201. /**
  202. * @return the number of components
  203. */
  204. public int length()
  205. {
  206. return components.length;
  207. }
  208. /**
  209. * get the specified component
  210. *
  211. * @param n which component (0 ... length() - 1)
  212. *
  213. * @return the nth component;
  214. *
  215. * @exception ArrayIndexOutOfBoundsException if n < 0 or n >=
  216. * length()
  217. */
  218. public String getComponent(int n)
  219. throws ArrayIndexOutOfBoundsException
  220. {
  221. return components[ n ];
  222. }
  223. /**
  224. * <p>Returns the path's parent or <code>null</code> if this path
  225. * is the root path.</p>
  226. *
  227. * @since 2002-01-24
  228. * @return path of parent, or null if this path is the root path
  229. */
  230. public POIFSDocumentPath getParent()
  231. {
  232. final int length = components.length - 1;
  233. if (length < 0)
  234. {
  235. return null;
  236. }
  237. POIFSDocumentPath parent = new POIFSDocumentPath(null);
  238. parent.components = new String[ length ];
  239. System.arraycopy(components, 0, parent.components, 0, length);
  240. return parent;
  241. }
  242. /**
  243. * <p>Returns a string representation of the path. Components are
  244. * separated by the platform-specific file separator.</p>
  245. *
  246. * @return string representation
  247. *
  248. * @since 2002-01-24
  249. */
  250. public String toString()
  251. {
  252. final StringBuffer b = new StringBuffer();
  253. final int l = length();
  254. b.append(File.separatorChar);
  255. for (int i = 0; i < l; i++)
  256. {
  257. b.append(getComponent(i));
  258. if (i < l - 1)
  259. {
  260. b.append(File.separatorChar);
  261. }
  262. }
  263. return b.toString();
  264. }
  265. } // end public class POIFSDocumentPath