KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > file > FindFile


1 package jodd.file;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import jodd.util.StringFlags;
9
10 /**
11  * Generic file finder, browses all files of specified folder. Search starts
12  * with the call to <code>first()</code> method, that will return founded
13  * file or <code>null</code> if no file found. Method <code>next()</code>
14  * then retrieves all other founded files, one by one. It will return
15  * <code>null</code> when search is over. Here is the example:
16  * <code>
17  * FindFile ff = new FindFile();
18  * File f = ff.first("class");
19  * while (f != null) {
20  *
21  * f = ff.next();
22  * }
23  * </code>
24  *
25  * This is default behaviour, and it can be changed with following
26  * flags:
27  * <ul>
28  * <li>r - search subdirectories (i.e. recursivly)</li>
29  * <li>d - also include directories in search results</li>
30  * <li>x - exclude files from search results</li>
31  * </ul>
32  *
33  * A list of <code>FileFilter</code> implementation can be added to the
34  * search. Each file is matched on every availiable filter, and only if
35  * all filters accept the file, it will be returned.
36  *
37  * @see java.io.FileFilter
38  */

39 public class FindFile {
40
41     // ---------------------------------------------------------------- flags
42

43     private boolean recursive = false;
44     /**
45      * Sets recursve flag.
46      *
47      * @param v
48      */

49     public void setRecursive(boolean v) {
50         recursive = v;
51     }
52     /**
53      * Returns recursive flag.
54      *
55      * @return recursive flag
56      */

57     public boolean getRecursive() {
58         return recursive;
59     }
60
61     private boolean incDirs;
62     /**
63      * Sets include dir flag.
64      *
65      * @param v
66      */

67     public void setIncludeDirs(boolean v) {
68         incDirs = v;
69     }
70     /**
71      * Returns include dir flag.
72      */

73     public boolean getIncludeDirs() {
74         return incDirs;
75     }
76
77     private boolean exFiles;
78     /**
79      * Sets exclude dir flag.
80      *
81      * @param v
82      */

83     public void setExcludeFiles(boolean v) {
84         exFiles = v;
85     }
86     /**
87      * Returns exclude dir flag.
88      */

89     public boolean getExcludeFiles() {
90         return exFiles;
91     }
92
93     private static StringFlags flags = new StringFlags();
94
95     static {
96         flags.addFlag('r', "recursive", true);
97         flags.addFlag('d', "includeDirs", true);
98         flags.addFlag('x', "excludeFiles", true);
99     }
100
101     // ---------------------------------------------------------------- filters
102

103     private List ffilters = null;
104     private int ffilters_size = 0;
105
106     public void setFileFilters(List ffs) {
107         ffilters = ffs;
108         if (ffilters != null) {
109             ffilters_size = ffilters.size();
110         } else {
111             ffilters_size = 0;
112         }
113     }
114
115     public void setFileFilters(FileFilter[] ff) {
116         ArrayList al = new ArrayList(ff.length);
117         for (int i = 0; i < ff.length; i++) {
118             al.add(ff[i]);
119         }
120         setFileFilters(al);
121     }
122
123     public void setFileFilters(FileFilter ff) {
124         ArrayList al = new ArrayList();
125         al.add(ff);
126         setFileFilters(al);
127     }
128
129     public void setFileFilters() {
130         setFileFilters((List) null);
131     }
132
133
134     public List getFileFilters() {
135         return ffilters;
136     }
137
138     public FileFilter getFileFilter() {
139         if (ffilters != null) {
140             return (FileFilter) ffilters.get(0);
141         } else {
142             return null;
143         }
144     }
145
146     public void addFileFilter(FileFilter ff) {
147         if (ffilters == null) {
148             ffilters = new ArrayList();
149         }
150         ffilters.add(ff);
151     }
152
153     // ---------------------------------------------------------------- finder
154

155     private ArrayList fileList = null;
156     private int ndx = 0;
157     private int fileListSize = 0;
158
159     /**
160      * Starts finding process and returns very first file.
161      *
162      * @param dir starting folder
163      * @param opt options
164      * @param ff file filter to use
165      *
166      * @return founded file, or <code>null</code> if no file found
167      */

168     public File first(String dir, String opt, FileFilter ff) {
169         setFileFilters(ff);
170         return dofirst(dir, opt);
171     }
172     /**
173      * Starts finding process and returns very first file.
174      *
175      * @param dir starting folder
176      * @param ff file filter to use
177      *
178      * @return founded file, or <code>null</code> if no file found
179      */

180     public File first(String dir, FileFilter ff) {
181         setFileFilters(ff);
182         return dofirst(dir, "");
183     }
184     /**
185      * Starts finding process and returns very first file.
186      *
187      * @param dir starting folder
188      * @param opt options
189      * @param ffa list of file filters to use
190      *
191      * @return founded file, or <code>null</code> if no file found
192      */

193     public File first(String dir, String opt, FileFilter[] ffa) {
194         setFileFilters(ffa);
195         return dofirst(dir, opt);
196     }
197     /**
198      * Starts finding process and returns very first file.
199      *
200      * @param dir starting folder
201      * @param ffa list of file filters to use
202      *
203      * @return founded file, or <code>null</code> if no file found
204      */

205     public File first(String dir, FileFilter[] ffa) {
206         setFileFilters(ffa);
207         return dofirst(dir, "");
208     }
209     /**
210      * Starts finding process and returns very first file.
211      *
212      * @param dir starting folder
213      * @param opt options
214      * @param ffs file filters list
215      *
216      * @return founded file, or <code>null</code> if no file found
217      */

218     public File first(String dir, String opt, List ffs) {
219         setFileFilters(ffs);
220         return dofirst(dir, opt);
221     }
222     /**
223      * Starts finding process and returns very first file.
224      *
225      * @param dir starting folder
226      * @param ffs file filters list
227      *
228      * @return founded file, or <code>null</code> if no file found
229      */

230     public File first(String dir, List ffs) {
231         setFileFilters(ffs);
232         return dofirst(dir, "");
233     }
234     /**
235      * Starts finding process and returns very first file.
236      *
237      * @param dir starting folder
238      * @param opt options
239      *
240      * @return founded file, or <code>null</code> if no file found
241      */

242     public File first(String dir, String opt) {
243         setFileFilters();
244         return dofirst(dir, opt);
245     }
246     /**
247      * Starts finding process and returns very first file.
248      *
249      * @param dir starting folder
250      *
251      * @return founded file, or <code>null</code> if no file found
252      */

253     public File first(String dir) {
254         return dofirst(dir, "");
255     }
256
257
258     /**
259      * Private method that starts findings
260      *
261      * @param dir starting folder
262      * @param opt options
263      *
264      * @return founded file
265      */

266     private File dofirst(String dir, String opt) {
267         flags.parse(opt, this);
268
269         // start
270
fileList = new ArrayList();
271         File f = new File(dir);
272         if (f.exists() == false) {
273             return null; // directory (or file) doesn't exist
274
}
275         if (f.isDirectory() == false) {
276             return f; // if not a directory, return it
277
}
278         File[] childs = f.listFiles();
279         for (int i = 0; i < childs.length; i++) {
280             fileList.add(childs[i]);
281         }
282         ndx = 0;
283         fileListSize = fileList.size();
284         return next();
285     }
286
287     /**
288      * Finds the next file once when search is activated.
289      *
290      * @return founded file, <code>null</code> if no file has been found or if no more files.
291      */

292     public File next() {
293         if (ndx == fileListSize) {
294             return end();
295         }
296         File f = null;
297         boolean found = false;
298
299         loop: while (ndx < fileListSize) {
300             f = (File) fileList.get(ndx);
301             ndx++;
302
303             if (f.isDirectory()) { // directory found
304
fileList.subList(0, ndx).clear(); // release previous list elements from memory
305
ndx = 0;
306                 if (recursive == true) { // recursive: append subfolder files to the list
307
File[] childs = f.listFiles();
308                     for (int i = 0; i < childs.length; i++) {
309                         fileList.add(childs[i]);
310                     }
311                 }
312                 fileListSize = fileList.size();
313                 if (incDirs == false) { // exclude dirs
314
continue;
315                 }
316             }
317             if (f.isFile()) { // exclude files
318
if (exFiles == true) {
319                     continue;
320                 }
321             }
322             if (ffilters == null) {
323                 found = true;
324                 break;
325             }
326             for (int i = 0; i < ffilters_size; i++) {
327                 FileFilter ff = (FileFilter) ffilters.get(i);
328                 if (ff.accept(f) == true) {
329                     found = true;
330                     break loop;
331                 }
332             }
333         }
334         if (found == false) {
335             return end();
336         }
337         return f;
338     }
339
340
341     /**
342      * Finishes the search and frees resources.
343      *
344      * @return always <code>null</code>: it means the search is over.
345      */

346     private File end() {
347         fileList = null;
348         ffilters = null;
349         return null;
350     }
351 }
352
353