1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.finder;
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class FileFinder implements Finder {
39
40
41 private List findListeners;
42
43
44
45
46 public FileFinder() {
47 super();
48 }
49
50
51
52
53
54
55
56 public File[] find(File directory) {
57 return find(directory, Collections.EMPTY_MAP);
58 }
59
60
61
62
63
64
65
66 public File[] find(File directory, Map options) {
67 List retlist = new ArrayList();
68 find(directory, options, retlist);
69 File[] files = (File[]) retlist.toArray(new File[retlist.size()]);
70 return files;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84 public void find(File directory, Map options, List foundFiles) {
85 if (foundFiles == null) {
86 throw new IllegalArgumentException("foundFiles list cannot be null");
87 }
88
89 notifyDirectoryStarted(directory);
90 boolean depthFirst = toBoolean(options.get(Finder.DEPTH));
91 int maxDepth = toInt(options.get(Finder.MAXDEPTH));
92 int minDepth = toInt(options.get(Finder.MINDEPTH));
93 boolean ignoreHiddenDirs = toBoolean(options.get(Finder.IGNORE_HIDDEN_DIRS));
94
95 FindingFilter filter = new FindingFilter(options);
96 int startIdx = foundFiles.size();
97 find(directory, filter, depthFirst, foundFiles);
98 if (filter.accept(directory)) {
99 if (depthFirst) {
100 foundFiles.add(directory);
101 } else {
102 foundFiles.add(0, directory);
103 }
104 }
105 int endIdx = foundFiles.size();
106 notifyDirectoryFinished(directory, foundFiles, startIdx, endIdx);
107 }
108
109 private void find(File directory, FindingFilter filter, boolean depthFirst, List retlist) {
110
111 File[] list = directory.listFiles();
112 if (list == null) {
113 return;
114 }
115 int sz = list.length;
116 for (int i = 0; i < sz; i++) {
117 File tmp = list[i];
118 if (!depthFirst && filter.accept(tmp)) {
119 retlist.add(tmp);
120 notifyFileFound(directory, tmp);
121 }
122 if (tmp.isDirectory()) {
123 notifyDirectoryStarted(tmp);
124 int startIdx = retlist.size();
125 find(tmp, filter, depthFirst, retlist);
126 int endIdx = retlist.size();
127 notifyDirectoryFinished(tmp, retlist, startIdx, endIdx);
128 }
129 if (depthFirst && filter.accept(tmp)) {
130 retlist.add(tmp);
131 notifyFileFound(directory, tmp);
132 }
133 }
134 }
135
136
137
138
139
140
141
142 public void addFindListener(FindListener listener) {
143 if (findListeners == null) {
144 findListeners = new ArrayList();
145 }
146 findListeners.add(listener);
147 }
148
149
150
151
152
153
154 public void removeFindListener(FindListener listener) {
155 if (findListeners != null) {
156 findListeners.remove(listener);
157 }
158 }
159
160
161
162
163
164
165
166 protected void notifyDirectoryStarted(File directory) {
167 if (!directory.isDirectory()) {
168 return;
169 }
170 if (findListeners != null) {
171 FindEvent fe = new FindEvent(this, "directoryStarted", directory);
172 Iterator it = findListeners.iterator();
173 while (it.hasNext()) {
174 FindListener findListener = (FindListener) it.next();
175 findListener.directoryStarted(fe);
176 }
177 }
178 }
179
180
181
182
183
184
185
186 protected void notifyDirectoryFinished(File directory, File[] files) {
187 if (!directory.isDirectory()) {
188 return;
189 }
190 if (findListeners != null) {
191 FindEvent fe = new FindEvent(this, "directoryFinished", directory, files);
192 Iterator it = findListeners.iterator();
193 while (it.hasNext()) {
194 FindListener findListener = (FindListener) it.next();
195 findListener.directoryFinished(fe);
196 }
197 }
198 }
199
200 private void notifyDirectoryFinished(File directory, List files, int startIdx, int endIdx) {
201
202 if (!directory.isDirectory() || findListeners == null || findListeners.size() == 0) {
203 return;
204 }
205
206
207 File[] filesa = (File[]) files.subList(startIdx, endIdx).toArray(new File[endIdx - startIdx]);
208 notifyDirectoryFinished(directory, filesa);
209
210 }
211
212
213
214
215
216
217
218 protected void notifyFileFound(File directory, File file) {
219 if (file.isDirectory()) {
220 return;
221 }
222 if (findListeners != null) {
223 FindEvent fe = new FindEvent(this, "fileFound", directory, file);
224 Iterator it = findListeners.iterator();
225 while (it.hasNext()) {
226 FindListener findListener = (FindListener) it.next();
227 findListener.fileFound(fe);
228 }
229 }
230 }
231
232
233
234
235
236
237
238
239 private static int toInt(Object obj) {
240 if (obj == null) {
241 return 0;
242 } else if (obj instanceof Number) {
243 return ((Number) obj).intValue();
244 } else {
245 String str = obj.toString();
246 try {
247 return Integer.parseInt(str.toString());
248 } catch(NumberFormatException nfe) {
249 throw new IllegalArgumentException("String argument " + str + " must be parseable as an integer");
250 }
251 }
252 }
253
254
255
256
257
258
259
260 private static boolean toBoolean(Object obj) {
261 if (obj == null) {
262 return false;
263 } else if (obj instanceof Boolean) {
264 return ((Boolean) obj).booleanValue();
265 } else if (obj instanceof Number) {
266 return ((Number) obj).intValue() != 0;
267 } else {
268 String str = obj.toString();
269 return new Boolean(str).booleanValue();
270 }
271 }
272
273 }