1 19 20 package org.openidex.search; 21 22 import java.util.ArrayList ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.NoSuchElementException ; 27 import org.openide.filesystems.FileObject; 28 import org.openide.loaders.DataFolder; 29 import org.openide.loaders.DataObject; 30 31 35 class SimpleSearchIterator implements Iterator { 36 37 38 private Enumeration childrenEnum; 39 43 private List filters; 44 47 private List filtersCopy; 48 49 private final boolean recursive; 50 51 private final ArrayList enums = new ArrayList (); 56 private final ArrayList filterLists = new ArrayList (); 58 private boolean upToDate = false; 59 63 private DataObject nextObject; 64 65 67 SimpleSearchIterator(DataFolder folder, 68 boolean recursive, 69 List filters) { 70 this.childrenEnum = folder.children(false); 71 this.recursive = recursive; 72 this.filters = (filters != null) ? new ArrayList (filters) 73 : null; 74 } 75 76 78 public boolean hasNext() { 79 if (!upToDate) { 80 update(); 81 } 82 return nextObject != null; 83 } 84 85 87 public Object next() { 88 if (!hasNext()) { 89 throw new NoSuchElementException (); 90 } 91 92 upToDate = false; 93 return nextObject; 94 } 95 96 98 private void update() { 99 assert upToDate == false; 100 assert childrenEnum != null; 101 do { 102 if (childrenEnum.hasMoreElements()) { 103 Object next = childrenEnum.nextElement(); 104 DataObject dataObject = (DataObject) next; 105 FileObject file = dataObject.getPrimaryFile(); 106 if (file.isFolder()) { 107 if (!recursive) { 108 continue; 109 } 110 111 if (filters != null) { 112 final List subfolderFilters = checkFolderFilters(file); 113 if (subfolderFilters == null) { 114 continue; 115 } 116 117 filterLists.add(filters); 118 if (subfolderFilters.size() != filters.size()) { 119 filters = (!subfolderFilters.isEmpty()) 120 ? subfolderFilters 121 : null; 122 } 123 } else { 124 filterLists.add(null); 125 } 126 enums.add(childrenEnum); 127 childrenEnum = ((DataFolder) dataObject).children(false); 128 } else { 129 if ((filters != null) && !checkFileFilters(file)) { 130 continue; 131 } 132 133 nextObject = (DataObject) dataObject; 134 break; 135 } 136 } else { 137 assert enums.isEmpty() == filterLists.isEmpty(); 138 139 nextObject = null; 140 141 if (enums.isEmpty()) { 142 childrenEnum = null; 143 continue; 144 } 145 146 147 childrenEnum = (Enumeration ) enums.remove(enums.size() - 1); 148 149 150 filters = (List ) filterLists.remove(filterLists.size() - 1); 151 if ((filtersCopy != null) 152 && (filtersCopy.size() != filters.size())) { 153 filtersCopy = null; 154 } 155 } 156 } while (childrenEnum != null); 157 158 upToDate = true; 159 } 160 161 178 private List checkFolderFilters(final FileObject folder) { 179 assert folder.isFolder(); 180 assert filters != null; 181 182 if (filtersCopy == null) { 183 filtersCopy = new ArrayList (filters); 184 } 185 186 List result = filtersCopy; 187 cycle: 188 for (Iterator i = result.iterator(); i.hasNext(); ) { 189 FileObjectFilter filter = (FileObjectFilter) i.next(); 190 final int traverseCommand = filter.traverseFolder(folder); 191 switch (traverseCommand) { 192 case FileObjectFilter.TRAVERSE: 193 break; 194 case FileObjectFilter.DO_NOT_TRAVERSE: 195 result = null; 196 break cycle; 197 case FileObjectFilter.TRAVERSE_ALL_SUBFOLDERS: 198 i.remove(); 199 filtersCopy = null; 200 break; 201 default: 202 assert false; 203 break; 204 } 205 } 206 207 return result; 208 } 209 210 218 private boolean checkFileFilters(FileObject file) { 219 assert file.isFolder() == false; 220 assert filters != null; 221 222 for (Iterator i = filters.iterator(); i.hasNext(); ) { 223 FileObjectFilter filter = (FileObjectFilter) i.next(); 224 if (!filter.searchFile(file)) { 225 return false; 226 } 227 } 228 229 return true; 230 } 231 232 234 public void remove() { 235 throw new UnsupportedOperationException (); 236 } 237 238 } 239 | Popular Tags |