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.

FilteringDirectoryNode.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.Collection;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.NoSuchElementException;
  27. import java.util.Set;
  28. import java.util.Spliterator;
  29. import java.util.Spliterators;
  30. import org.apache.poi.hpsf.ClassID;
  31. /**
  32. * A DirectoryEntry filter, which exposes another
  33. * DirectoryEntry less certain parts.
  34. * This is typically used when copying or comparing
  35. * Filesystems.
  36. */
  37. public class FilteringDirectoryNode implements DirectoryEntry
  38. {
  39. /**
  40. * The names of our entries to exclude
  41. */
  42. private final Set<String> excludes;
  43. /**
  44. * Excludes of our child directories
  45. */
  46. private final Map<String,List<String>> childExcludes;
  47. private final DirectoryEntry directory;
  48. /**
  49. * Creates a filter round the specified directory, which
  50. * will exclude entries such as "MyNode" and "MyDir/IgnoreNode".
  51. * The excludes can stretch into children, if they contain a /.
  52. *
  53. * @param directory The Directory to filter
  54. * @param excludes The Entries to exclude
  55. * @throws IllegalArgumentException if directory is null
  56. */
  57. public FilteringDirectoryNode(DirectoryEntry directory, Collection<String> excludes) {
  58. if (directory == null) {
  59. throw new IllegalArgumentException("directory cannot be null");
  60. }
  61. this.directory = directory;
  62. // Process the excludes
  63. this.excludes = new HashSet<>();
  64. this.childExcludes = new HashMap<>();
  65. for (String excl : excludes) {
  66. int splitAt = excl.indexOf('/');
  67. if (splitAt == -1) {
  68. // Applies to us
  69. this.excludes.add(excl);
  70. } else {
  71. // Applies to a child
  72. String child = excl.substring(0, splitAt);
  73. String childExcl = excl.substring(splitAt+1);
  74. if (! this.childExcludes.containsKey(child)) {
  75. this.childExcludes.put(child, new ArrayList<>());
  76. }
  77. this.childExcludes.get(child).add(childExcl);
  78. }
  79. }
  80. }
  81. @Override
  82. public DirectoryEntry createDirectory(String name) throws IOException {
  83. return directory.createDirectory(name);
  84. }
  85. @Override
  86. public DocumentEntry createDocument(String name, InputStream stream)
  87. throws IOException {
  88. return directory.createDocument(name, stream);
  89. }
  90. @Override
  91. public DocumentEntry createDocument(String name, int size,
  92. POIFSWriterListener writer) throws IOException {
  93. return directory.createDocument(name, size, writer);
  94. }
  95. @Override
  96. public Iterator<Entry> getEntries() {
  97. return new FilteringIterator();
  98. }
  99. @Override
  100. public Iterator<Entry> iterator() {
  101. return getEntries();
  102. }
  103. /**
  104. * @since POI 5.2.0
  105. */
  106. @Override
  107. public Spliterator<Entry> spliterator() {
  108. return Spliterators.spliterator(iterator(), getEntryCount(), 0);
  109. }
  110. @Override
  111. public int getEntryCount() {
  112. int size = directory.getEntryCount();
  113. for (String excl : excludes) {
  114. if (directory.hasEntry(excl)) {
  115. size--;
  116. }
  117. }
  118. return size;
  119. }
  120. @Override
  121. public Set<String> getEntryNames() {
  122. Set<String> names = new HashSet<>();
  123. for (String name : directory.getEntryNames()) {
  124. if (!excludes.contains(name)) {
  125. names.add(name);
  126. }
  127. }
  128. return names;
  129. }
  130. @Override
  131. public boolean isEmpty() {
  132. return (getEntryCount() == 0);
  133. }
  134. @Override
  135. public boolean hasEntry(String name) {
  136. if (excludes.contains(name)) {
  137. return false;
  138. }
  139. return directory.hasEntry(name);
  140. }
  141. @Override
  142. public Entry getEntry(String name) throws FileNotFoundException {
  143. if (excludes.contains(name)) {
  144. throw new FileNotFoundException(name);
  145. }
  146. Entry entry = directory.getEntry(name);
  147. return wrapEntry(entry);
  148. }
  149. private Entry wrapEntry(Entry entry) {
  150. String name = entry.getName();
  151. if (childExcludes.containsKey(name) && entry instanceof DirectoryEntry) {
  152. return new FilteringDirectoryNode(
  153. (DirectoryEntry)entry, childExcludes.get(name));
  154. }
  155. return entry;
  156. }
  157. @Override
  158. public ClassID getStorageClsid() {
  159. return directory.getStorageClsid();
  160. }
  161. @Override
  162. public void setStorageClsid(ClassID clsidStorage) {
  163. directory.setStorageClsid(clsidStorage);
  164. }
  165. @Override
  166. public boolean delete() {
  167. return directory.delete();
  168. }
  169. @Override
  170. public boolean renameTo(String newName) {
  171. return directory.renameTo(newName);
  172. }
  173. @Override
  174. public String getName() {
  175. return directory.getName();
  176. }
  177. @Override
  178. public DirectoryEntry getParent() {
  179. return directory.getParent();
  180. }
  181. @Override
  182. public boolean isDirectoryEntry() {
  183. return true;
  184. }
  185. @Override
  186. public boolean isDocumentEntry() {
  187. return false;
  188. }
  189. private class FilteringIterator implements Iterator<Entry> {
  190. private final Iterator<Entry> parent;
  191. private Entry next;
  192. private FilteringIterator() {
  193. parent = directory.getEntries();
  194. locateNext();
  195. }
  196. private void locateNext() {
  197. next = null;
  198. Entry e;
  199. while (parent.hasNext() && next == null) {
  200. e = parent.next();
  201. if (! excludes.contains(e.getName())) {
  202. next = wrapEntry(e);
  203. }
  204. }
  205. }
  206. @Override
  207. public boolean hasNext() {
  208. return (next != null);
  209. }
  210. @Override
  211. public Entry next() {
  212. if (!hasNext()) {
  213. throw new NoSuchElementException();
  214. }
  215. Entry e = next;
  216. locateNext();
  217. return e;
  218. }
  219. @Override
  220. public void remove() {
  221. throw new UnsupportedOperationException("Remove not supported");
  222. }
  223. }
  224. }