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.

DirectoryNode.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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.filesystem;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.apache.poi.hpsf.ClassID;
  26. import org.apache.poi.poifs.dev.POIFSViewable;
  27. import org.apache.poi.poifs.property.DirectoryProperty;
  28. import org.apache.poi.poifs.property.DocumentProperty;
  29. import org.apache.poi.poifs.property.Property;
  30. /**
  31. * Simple implementation of DirectoryEntry
  32. */
  33. public class DirectoryNode
  34. extends EntryNode
  35. implements DirectoryEntry, POIFSViewable, Iterable<Entry>
  36. {
  37. // Map of Entry instances, keyed by their names
  38. private final Map<String,Entry> _byname = new HashMap<>();
  39. // Our list of entries, kept sorted to preserve order
  40. private final ArrayList<Entry> _entries = new ArrayList<>();
  41. // the POIFSFileSystem we belong to
  42. private final POIFSFileSystem _filesystem;
  43. // the path described by this document
  44. private final POIFSDocumentPath _path;
  45. /**
  46. * create a DirectoryNode. This method is not public by design; it
  47. * is intended strictly for the internal use of this package
  48. *
  49. * @param property the DirectoryProperty for this DirectoryEntry
  50. * @param filesystem the {@link POIFSFileSystem} we belong to
  51. * @param parent the parent of this entry
  52. */
  53. DirectoryNode(final DirectoryProperty property,
  54. final POIFSFileSystem filesystem,
  55. final DirectoryNode parent)
  56. {
  57. super(property, parent);
  58. this._filesystem = filesystem;
  59. if (parent == null)
  60. {
  61. _path = new POIFSDocumentPath();
  62. }
  63. else
  64. {
  65. _path = new POIFSDocumentPath(parent._path, new String[]
  66. {
  67. property.getName()
  68. });
  69. }
  70. Iterator<Property> iter = property.getChildren();
  71. while (iter.hasNext())
  72. {
  73. Property child = iter.next();
  74. Entry childNode;
  75. if (child.isDirectory())
  76. {
  77. DirectoryProperty childDir = (DirectoryProperty) child;
  78. childNode = new DirectoryNode(childDir, _filesystem, this);
  79. }
  80. else
  81. {
  82. childNode = new DocumentNode((DocumentProperty) child, this);
  83. }
  84. _entries.add(childNode);
  85. _byname.put(childNode.getName(), childNode);
  86. }
  87. }
  88. /**
  89. * @return this directory's path representation
  90. */
  91. public POIFSDocumentPath getPath()
  92. {
  93. return _path;
  94. }
  95. /**
  96. * @return the filesystem that this belongs to
  97. */
  98. public POIFSFileSystem getFileSystem()
  99. {
  100. return _filesystem;
  101. }
  102. /**
  103. * open a document in the directory's entry's list of entries
  104. *
  105. * @param documentName the name of the document to be opened
  106. *
  107. * @return a newly opened DocumentInputStream
  108. *
  109. * @throws IOException if the document does not exist or the
  110. * name is that of a DirectoryEntry
  111. */
  112. public DocumentInputStream createDocumentInputStream(
  113. final String documentName)
  114. throws IOException
  115. {
  116. return createDocumentInputStream(getEntry(documentName));
  117. }
  118. /**
  119. * open a document in the directory's entry's list of entries
  120. *
  121. * @param document the document to be opened
  122. *
  123. * @return a newly opened DocumentInputStream or DocumentInputStream
  124. *
  125. * @throws IOException if the document does not exist or the
  126. * name is that of a DirectoryEntry
  127. */
  128. public DocumentInputStream createDocumentInputStream(
  129. final Entry document)
  130. throws IOException
  131. {
  132. if (!document.isDocumentEntry()) {
  133. throw new IOException("Entry '" + document.getName()
  134. + "' is not a DocumentEntry");
  135. }
  136. DocumentEntry entry = (DocumentEntry)document;
  137. return new DocumentInputStream(entry);
  138. }
  139. /**
  140. * create a new DocumentEntry
  141. *
  142. * @param document the new document
  143. *
  144. * @return the new DocumentEntry
  145. *
  146. * @throws IOException if the document can't be created
  147. */
  148. DocumentEntry createDocument(final POIFSDocument document)
  149. throws IOException
  150. {
  151. DocumentProperty property = document.getDocumentProperty();
  152. DocumentNode rval = new DocumentNode(property, this);
  153. (( DirectoryProperty ) getProperty()).addChild(property);
  154. _filesystem.addDocument(document);
  155. _entries.add(rval);
  156. _byname.put(property.getName(), rval);
  157. return rval;
  158. }
  159. /**
  160. * Change a contained Entry's name
  161. *
  162. * @param oldName the original name
  163. * @param newName the new name
  164. *
  165. * @return true if the operation succeeded, else false
  166. */
  167. boolean changeName(final String oldName, final String newName)
  168. {
  169. boolean rval = false;
  170. EntryNode child = ( EntryNode ) _byname.get(oldName);
  171. if (child != null)
  172. {
  173. rval = (( DirectoryProperty ) getProperty())
  174. .changeName(child.getProperty(), newName);
  175. if (rval)
  176. {
  177. _byname.remove(oldName);
  178. _byname.put(child.getProperty().getName(), child);
  179. }
  180. }
  181. return rval;
  182. }
  183. /**
  184. * Delete an entry
  185. *
  186. * @param entry the EntryNode to be deleted
  187. *
  188. * @return true if the entry was deleted, else false
  189. */
  190. boolean deleteEntry(final EntryNode entry)
  191. {
  192. boolean rval =
  193. (( DirectoryProperty ) getProperty())
  194. .deleteChild(entry.getProperty());
  195. if (rval)
  196. {
  197. _entries.remove(entry);
  198. _byname.remove(entry.getName());
  199. try {
  200. _filesystem.remove(entry);
  201. } catch (IOException e) {
  202. // TODO Work out how to report this, given we can't change the method signature...
  203. throw new RuntimeException(e);
  204. }
  205. }
  206. return rval;
  207. }
  208. /* ********** START implementation of DirectoryEntry ********** */
  209. /**
  210. * get an iterator of the Entry instances contained directly in
  211. * this instance (in other words, children only; no grandchildren
  212. * etc.)
  213. *
  214. * @return iterator; never null, but hasNext() may return false
  215. * immediately (i.e., this DirectoryEntry is empty). All
  216. * objects retrieved by next() are guaranteed to be
  217. * implementations of Entry.
  218. */
  219. @Override
  220. public Iterator<Entry> getEntries()
  221. {
  222. return _entries.iterator();
  223. }
  224. /**
  225. * get the names of all the Entries contained directly in this
  226. * instance (in other words, names of children only; no grandchildren
  227. * etc).
  228. *
  229. * @return the names of all the entries that may be retrieved with
  230. * getEntry(String), which may be empty (if this
  231. * DirectoryEntry is empty)
  232. */
  233. @Override
  234. public Set<String> getEntryNames()
  235. {
  236. return _byname.keySet();
  237. }
  238. /**
  239. * is this DirectoryEntry empty?
  240. *
  241. * @return true if this instance contains no Entry instances
  242. */
  243. @Override
  244. public boolean isEmpty()
  245. {
  246. return _entries.isEmpty();
  247. }
  248. /**
  249. * find out how many Entry instances are contained directly within
  250. * this DirectoryEntry
  251. *
  252. * @return number of immediately (no grandchildren etc.) contained
  253. * Entry instances
  254. */
  255. @Override
  256. public int getEntryCount()
  257. {
  258. return _entries.size();
  259. }
  260. @Override
  261. public boolean hasEntry( String name )
  262. {
  263. return name != null && _byname.containsKey( name );
  264. }
  265. /**
  266. * get a specified Entry by name
  267. *
  268. * @param name the name of the Entry to obtain.
  269. *
  270. * @return the specified Entry, if it is directly contained in
  271. * this DirectoryEntry
  272. *
  273. * @throws FileNotFoundException if no Entry with the specified
  274. * name exists in this DirectoryEntry
  275. */
  276. @Override
  277. public Entry getEntry(final String name) throws FileNotFoundException {
  278. Entry rval = null;
  279. if (name != null) {
  280. rval = _byname.get(name);
  281. }
  282. if (rval == null) {
  283. // throw more useful exceptions for known wrong file-extensions
  284. if(_byname.containsKey("Workbook")) {
  285. throw new IllegalArgumentException("The document is really a XLS file");
  286. } else if(_byname.containsKey("PowerPoint Document")) {
  287. throw new IllegalArgumentException("The document is really a PPT file");
  288. } else if(_byname.containsKey("VisioDocument")) {
  289. throw new IllegalArgumentException("The document is really a VSD file");
  290. }
  291. // either a null name was given, or there is no such name
  292. throw new FileNotFoundException("no such entry: \"" + name
  293. + "\", had: " + _byname.keySet());
  294. }
  295. return rval;
  296. }
  297. /**
  298. * create a new DocumentEntry
  299. *
  300. * @param name the name of the new DocumentEntry
  301. * @param stream the InputStream from which to create the new
  302. * DocumentEntry
  303. *
  304. * @return the new DocumentEntry
  305. *
  306. * @throws IOException if the document can't be created
  307. */
  308. @Override
  309. public DocumentEntry createDocument(final String name,
  310. final InputStream stream)
  311. throws IOException
  312. {
  313. return createDocument(new POIFSDocument(name, _filesystem, stream));
  314. }
  315. /**
  316. * create a new DocumentEntry; the data will be provided later
  317. *
  318. * @param name the name of the new DocumentEntry
  319. * @param size the size of the new DocumentEntry
  320. * @param writer the writer of the new DocumentEntry
  321. *
  322. * @return the new DocumentEntry
  323. *
  324. * @throws IOException if the document can't be created
  325. */
  326. @Override
  327. public DocumentEntry createDocument(final String name, final int size,
  328. final POIFSWriterListener writer)
  329. throws IOException
  330. {
  331. return createDocument(new POIFSDocument(name, size, _filesystem, writer));
  332. }
  333. /**
  334. * create a new DirectoryEntry
  335. *
  336. * @param name the name of the new DirectoryEntry
  337. *
  338. * @return the new DirectoryEntry
  339. *
  340. * @throws IOException if the directory can't be created
  341. */
  342. @Override
  343. public DirectoryEntry createDirectory(final String name)
  344. throws IOException
  345. {
  346. DirectoryProperty property = new DirectoryProperty(name);
  347. DirectoryNode rval = new DirectoryNode(property, _filesystem, this);
  348. _filesystem.addDirectory(property);
  349. (( DirectoryProperty ) getProperty()).addChild(property);
  350. _entries.add(rval);
  351. _byname.put(name, rval);
  352. return rval;
  353. }
  354. /**
  355. * Set the contents of a document, creating if needed,
  356. * otherwise updating. Returns the created / updated DocumentEntry
  357. *
  358. * @param name the name of the new or existing DocumentEntry
  359. * @param stream the InputStream from which to populate the DocumentEntry
  360. *
  361. * @return the new or updated DocumentEntry
  362. *
  363. * @throws IOException if the document can't be created or its content be replaced
  364. */
  365. @SuppressWarnings("WeakerAccess")
  366. public DocumentEntry createOrUpdateDocument(final String name,
  367. final InputStream stream)
  368. throws IOException
  369. {
  370. if (! hasEntry(name)) {
  371. return createDocument(name, stream);
  372. } else {
  373. DocumentNode existing = (DocumentNode)getEntry(name);
  374. POIFSDocument nDoc = new POIFSDocument(existing);
  375. nDoc.replaceContents(stream);
  376. return existing;
  377. }
  378. }
  379. /**
  380. * Gets the storage clsid of the directory entry
  381. *
  382. * @return storage Class ID
  383. */
  384. @Override
  385. public ClassID getStorageClsid()
  386. {
  387. return getProperty().getStorageClsid();
  388. }
  389. /**
  390. * Sets the storage clsid for the directory entry
  391. *
  392. * @param clsidStorage storage Class ID
  393. */
  394. @Override
  395. public void setStorageClsid(ClassID clsidStorage)
  396. {
  397. getProperty().setStorageClsid(clsidStorage);
  398. }
  399. /* ********** END implementation of DirectoryEntry ********** */
  400. /* ********** START implementation of Entry ********** */
  401. /**
  402. * is this a DirectoryEntry?
  403. *
  404. * @return true if the Entry is a DirectoryEntry, else false
  405. */
  406. @Override
  407. public boolean isDirectoryEntry()
  408. {
  409. return true;
  410. }
  411. /* ********** END implementation of Entry ********** */
  412. /* ********** START extension of Entry ********** */
  413. /**
  414. * extensions use this method to verify internal rules regarding
  415. * deletion of the underlying store.
  416. *
  417. * @return true if it's ok to delete the underlying store, else
  418. * false
  419. */
  420. @Override
  421. protected boolean isDeleteOK()
  422. {
  423. // if this directory is empty, we can delete it
  424. return isEmpty();
  425. }
  426. /* ********** END extension of Entry ********** */
  427. /* ********** START begin implementation of POIFSViewable ********** */
  428. /**
  429. * Get an array of objects, some of which may implement
  430. * POIFSViewable
  431. *
  432. * @return an array of Object; may not be null, but may be empty
  433. */
  434. @Override
  435. public Object [] getViewableArray()
  436. {
  437. return new Object[ 0 ];
  438. }
  439. /**
  440. * Get an Iterator of objects, some of which may implement
  441. * POIFSViewable
  442. *
  443. * @return an Iterator; may not be null, but may have an empty
  444. * back end store
  445. */
  446. @Override
  447. public Iterator<Object> getViewableIterator() {
  448. List<Object> components = new ArrayList<>();
  449. components.add(getProperty());
  450. components.addAll(_entries);
  451. return components.iterator();
  452. }
  453. /**
  454. * Give viewers a hint as to whether to call getViewableArray or
  455. * getViewableIterator
  456. *
  457. * @return true if a viewer should call getViewableArray, false if
  458. * a viewer should call getViewableIterator
  459. */
  460. @Override
  461. public boolean preferArray()
  462. {
  463. return false;
  464. }
  465. /**
  466. * Provides a short description of the object, to be used when a
  467. * POIFSViewable object has not provided its contents.
  468. *
  469. * @return short description
  470. */
  471. @Override
  472. public String getShortDescription()
  473. {
  474. return getName();
  475. }
  476. /**
  477. * Returns an Iterator over all the entries
  478. */
  479. @Override
  480. public Iterator<Entry> iterator() {
  481. return getEntries();
  482. }
  483. /* ********** END begin implementation of POIFSViewable ********** */
  484. } // end public class DirectoryNode