PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/openmicroscopy/bioformats
Java | 457 lines | 187 code | 67 blank | 203 comment | 13 complexity | a149450acd4d1363e1060108fcdd5c42 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.*;
  39. import java.util.*;
  40. import loci.common.*;
  41. import loci.poi.hpsf.ClassID;
  42. import loci.poi.poifs.dev.POIFSViewable;
  43. import loci.poi.poifs.property.DirectoryProperty;
  44. import loci.poi.poifs.property.DocumentProperty;
  45. import loci.poi.poifs.property.Property;
  46. import loci.poi.util.*;
  47. /**
  48. * Simple implementation of DirectoryEntry
  49. *
  50. * @author Marc Johnson (mjohnson at apache dot org)
  51. */
  52. public class DirectoryNode
  53. extends EntryNode
  54. implements DirectoryEntry, POIFSViewable
  55. {
  56. // Map of Entry instances, keyed by their names
  57. private Map _entries;
  58. // the POIFSFileSystem we belong to
  59. private POIFSFileSystem _filesystem;
  60. // the path described by this document
  61. private POIFSDocumentPath _path;
  62. /**
  63. * create a DirectoryNode. This method is not public by design; it
  64. * is intended strictly for the internal use of this package
  65. *
  66. * @param property the DirectoryProperty for this DirectoryEntry
  67. * @param filesystem the POIFSFileSystem we belong to
  68. * @param parent the parent of this entry
  69. */
  70. DirectoryNode(final DirectoryProperty property,
  71. final POIFSFileSystem filesystem,
  72. final DirectoryNode parent)
  73. {
  74. super(property, parent);
  75. if (parent == null)
  76. {
  77. _path = new POIFSDocumentPath();
  78. }
  79. else
  80. {
  81. _path = new POIFSDocumentPath(parent._path, new String[]
  82. {
  83. property.getName()
  84. });
  85. }
  86. _filesystem = filesystem;
  87. _entries = new HashMap();
  88. Iterator iter = property.getChildren();
  89. while (iter.hasNext())
  90. {
  91. Property child = ( Property ) iter.next();
  92. Entry childNode = null;
  93. if (child.isDirectory())
  94. {
  95. childNode = new DirectoryNode(( DirectoryProperty ) child,
  96. _filesystem, this);
  97. }
  98. else
  99. {
  100. childNode = new DocumentNode(( DocumentProperty ) child,
  101. this);
  102. }
  103. _entries.put(childNode.getName(), childNode);
  104. }
  105. }
  106. /**
  107. * @return this directory's path representation
  108. */
  109. public POIFSDocumentPath getPath()
  110. {
  111. return _path;
  112. }
  113. /**
  114. * create a new DocumentEntry
  115. *
  116. * @param document the new document
  117. *
  118. * @return the new DocumentEntry
  119. *
  120. * @exception IOException
  121. */
  122. DocumentEntry createDocument(final POIFSDocument document)
  123. throws IOException
  124. {
  125. DocumentProperty property = document.getDocumentProperty();
  126. DocumentNode rval = new DocumentNode(property, this);
  127. (( DirectoryProperty ) getProperty()).addChild(property);
  128. _filesystem.addDocument(document);
  129. _entries.put(property.getName(), rval);
  130. return rval;
  131. }
  132. /**
  133. * Change a contained Entry's name
  134. *
  135. * @param oldName the original name
  136. * @param newName the new name
  137. *
  138. * @return true if the operation succeeded, else false
  139. */
  140. boolean changeName(final String oldName, final String newName)
  141. {
  142. boolean rval = false;
  143. EntryNode child = ( EntryNode ) _entries.get(oldName);
  144. if (child != null)
  145. {
  146. rval = (( DirectoryProperty ) getProperty())
  147. .changeName(child.getProperty(), newName);
  148. if (rval)
  149. {
  150. _entries.remove(oldName);
  151. _entries.put(child.getProperty().getName(), child);
  152. }
  153. }
  154. return rval;
  155. }
  156. /**
  157. * Delete an entry
  158. *
  159. * @param entry the EntryNode to be deleted
  160. *
  161. * @return true if the entry was deleted, else false
  162. */
  163. boolean deleteEntry(final EntryNode entry)
  164. {
  165. boolean rval =
  166. (( DirectoryProperty ) getProperty())
  167. .deleteChild(entry.getProperty());
  168. if (rval)
  169. {
  170. _entries.remove(entry.getName());
  171. _filesystem.remove(entry);
  172. }
  173. return rval;
  174. }
  175. /* ********** START implementation of DirectoryEntry ********** */
  176. /**
  177. * get an iterator of the Entry instances contained directly in
  178. * this instance (in other words, children only; no grandchildren
  179. * etc.)
  180. *
  181. * @return iterator; never null, but hasNext() may return false
  182. * immediately (i.e., this DirectoryEntry is empty). All
  183. * objects retrieved by next() are guaranteed to be
  184. * implementations of Entry.
  185. */
  186. public Iterator getEntries()
  187. {
  188. return _entries.values().iterator();
  189. }
  190. /**
  191. * is this DirectoryEntry empty?
  192. *
  193. * @return true if this instance contains no Entry instances
  194. */
  195. public boolean isEmpty()
  196. {
  197. return _entries.isEmpty();
  198. }
  199. /**
  200. * find out how many Entry instances are contained directly within
  201. * this DirectoryEntry
  202. *
  203. * @return number of immediately (no grandchildren etc.) contained
  204. * Entry instances
  205. */
  206. public int getEntryCount()
  207. {
  208. return _entries.size();
  209. }
  210. /**
  211. * get a specified Entry by name
  212. *
  213. * @param name the name of the Entry to obtain.
  214. *
  215. * @return the specified Entry, if it is directly contained in
  216. * this DirectoryEntry
  217. *
  218. * @exception FileNotFoundException if no Entry with the specified
  219. * name exists in this DirectoryEntry
  220. */
  221. public Entry getEntry(final String name)
  222. throws FileNotFoundException
  223. {
  224. Entry rval = null;
  225. if (name != null)
  226. {
  227. rval = ( Entry ) _entries.get(name);
  228. }
  229. if (rval == null)
  230. {
  231. // either a null name was given, or there is no such name
  232. throw new FileNotFoundException("no such entry: \"" + name
  233. + "\"");
  234. }
  235. return rval;
  236. }
  237. /**
  238. * create a new DocumentEntry
  239. *
  240. * @param name the name of the new DocumentEntry
  241. * @param stream the InputStream from which to create the new
  242. * DocumentEntry
  243. *
  244. * @return the new DocumentEntry
  245. *
  246. * @exception IOException
  247. */
  248. public DocumentEntry createDocument(final String name,
  249. final RandomAccessInputStream stream)
  250. throws IOException
  251. {
  252. return createDocument(new POIFSDocument(name, stream, 512));
  253. }
  254. /**
  255. * create a new DocumentEntry; the data will be provided later
  256. *
  257. * @param name the name of the new DocumentEntry
  258. * @param size the size of the new DocumentEntry
  259. * @param writer the writer of the new DocumentEntry
  260. *
  261. * @return the new DocumentEntry
  262. *
  263. * @exception IOException
  264. */
  265. public DocumentEntry createDocument(final String name, final int size,
  266. final POIFSWriterListener writer)
  267. throws IOException
  268. {
  269. return createDocument(new POIFSDocument(name, size, _path, writer,
  270. 512));
  271. }
  272. /**
  273. * create a new DirectoryEntry
  274. *
  275. * @param name the name of the new DirectoryEntry
  276. *
  277. * @return the new DirectoryEntry
  278. *
  279. * @exception IOException
  280. */
  281. public DirectoryEntry createDirectory(final String name)
  282. throws IOException
  283. {
  284. DirectoryProperty property = new DirectoryProperty(name);
  285. DirectoryNode rval = new DirectoryNode(property, _filesystem,
  286. this);
  287. (( DirectoryProperty ) getProperty()).addChild(property);
  288. _filesystem.addDirectory(property);
  289. _entries.put(name, rval);
  290. return rval;
  291. }
  292. /**
  293. * Gets the storage clsid of the directory entry
  294. *
  295. * @return storage Class ID
  296. */
  297. public ClassID getStorageClsid()
  298. {
  299. return getProperty().getStorageClsid();
  300. }
  301. /**
  302. * Sets the storage clsid for the directory entry
  303. *
  304. * @param clsidStorage storage Class ID
  305. */
  306. public void setStorageClsid(ClassID clsidStorage)
  307. {
  308. getProperty().setStorageClsid(clsidStorage);
  309. }
  310. /* ********** END implementation of DirectoryEntry ********** */
  311. /* ********** START implementation of Entry ********** */
  312. /**
  313. * is this a DirectoryEntry?
  314. *
  315. * @return true if the Entry is a DirectoryEntry, else false
  316. */
  317. public boolean isDirectoryEntry()
  318. {
  319. return true;
  320. }
  321. /* ********** END implementation of Entry ********** */
  322. /* ********** START extension of Entry ********** */
  323. /**
  324. * extensions use this method to verify internal rules regarding
  325. * deletion of the underlying store.
  326. *
  327. * @return true if it's ok to delete the underlying store, else
  328. * false
  329. */
  330. protected boolean isDeleteOK()
  331. {
  332. // if this directory is empty, we can delete it
  333. return isEmpty();
  334. }
  335. /* ********** END extension of Entry ********** */
  336. /* ********** START begin implementation of POIFSViewable ********** */
  337. /**
  338. * Get an array of objects, some of which may implement
  339. * POIFSViewable
  340. *
  341. * @return an array of Object; may not be null, but may be empty
  342. */
  343. public Object [] getViewableArray()
  344. {
  345. return new Object[ 0 ];
  346. }
  347. /**
  348. * Get an Iterator of objects, some of which may implement
  349. * POIFSViewable
  350. *
  351. * @return an Iterator; may not be null, but may have an empty
  352. * back end store
  353. */
  354. public Iterator getViewableIterator()
  355. {
  356. List components = new ArrayList();
  357. components.add(getProperty());
  358. SortedMap sortedEntries = new TreeMap(_entries);
  359. Iterator iter = sortedEntries.values().iterator();
  360. while (iter.hasNext())
  361. {
  362. components.add(iter.next());
  363. }
  364. return components.iterator();
  365. }
  366. /**
  367. * Give viewers a hint as to whether to call getViewableArray or
  368. * getViewableIterator
  369. *
  370. * @return true if a viewer should call getViewableArray, false if
  371. * a viewer should call getViewableIterator
  372. */
  373. public boolean preferArray()
  374. {
  375. return false;
  376. }
  377. /**
  378. * Provides a short description of the object, to be used when a
  379. * POIFSViewable object has not provided its contents.
  380. *
  381. * @return short description
  382. */
  383. public String getShortDescription()
  384. {
  385. return getName();
  386. }
  387. /* ********** END begin implementation of POIFSViewable ********** */
  388. } // end public class DirectoryNode