KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > findfile > FindFile


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io.findfile;
4
5 import jodd.util.StringUtil;
6
7 import java.io.File JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.net.URI JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URISyntaxException JavaDoc;
12
13 /**
14  * Generic and helpful file finder. Searches all files on specified search path.
15  *
16  * @see WildcardFindFile
17  * @see RegExpFindFile
18  * @see FilterFindFile
19  *
20  * todo: Add method for storing results in files lists and trees
21  * todo: Add sorting and comparators
22  */

23 public class FindFile {
24
25     // ---------------------------------------------------------------- flags
26

27     protected boolean recursive = false;
28     /**
29      * Activates recursive search.
30      */

31     public FindFile recursive() {
32         this.recursive = true;
33         return this;
34     }
35
36     protected boolean includeDirs = true;
37     protected boolean includeFiles = true;
38
39     /**
40      * Include directories in search.
41      */

42     public FindFile includeDirs() {
43         includeDirs = true;
44         return this;
45     }
46     /**
47      * Exclude directories from search.
48      * In recursive mode, directories are still examined.
49      */

50     public FindFile excludeDirs() {
51         includeDirs = false;
52         return this;
53     }
54
55     /**
56      * Include files in search.
57      */

58     public FindFile includeFiles() {
59         includeFiles = true;
60         return this;
61     }
62
63     /**
64      * Exclude files from search.
65      */

66     public FindFile excludeFiles() {
67         includeFiles = false;
68         return this;
69     }
70
71
72     /**
73      * Include files and folders.
74      */

75     public FindFile includeAll() {
76         includeDirs = true;
77         includeFiles = true;
78         return this;
79     }
80
81     // ---------------------------------------------------------------- search path
82

83
84     public FindFile() {
85     }
86
87     public FindFile(String JavaDoc searchPath) {
88         searchPath(searchPath);
89     }
90
91     public FindFile(String JavaDoc[] searchPath) {
92         searchPath(searchPath);
93     }
94
95     public FindFile(File JavaDoc searchPath) {
96         searchPath(searchPath);
97     }
98
99     public FindFile(URI JavaDoc searchPath) {
100         searchPath(searchPath);
101     }
102
103     public FindFile(URI JavaDoc[] searchPath) {
104         searchPath(searchPath);
105     }
106
107     public FindFile(URL JavaDoc searchPath) {
108         searchPath(searchPath);
109     }
110
111     public FindFile(URL JavaDoc[] searchPath) {
112         searchPath(searchPath);
113     }
114
115
116     protected LinkedList JavaDoc fileList = null;
117
118     /**
119      * Specifies the search path. If provided path contains
120      * {@link File#pathSeparator} than string will be tokenized
121      * and each part will be added separatly as a search path.
122      */

123     public FindFile searchPath(String JavaDoc searchPath) {
124         if (searchPath.indexOf(File.pathSeparatorChar) != -1) {
125             String JavaDoc[] paths = StringUtil.split(searchPath, File.pathSeparator);
126             for (int i = 0; i < paths.length; i++) {
127                 searchPath(paths[i]);
128             }
129             return this;
130         }
131         searchPath(new File JavaDoc(searchPath));
132         return this;
133     }
134
135     /**
136      * Specifies search paths.
137      * @see #searchPath(String)
138      */

139     public FindFile searchPath(String JavaDoc[] searchPaths) {
140         for (int i = 0; i < searchPaths.length; i++) {
141             searchPath(searchPaths[i]);
142         }
143         return this;
144     }
145
146     /**
147      * Specifies the search path.
148      */

149     public FindFile searchPath(File JavaDoc searchPath) {
150         if (searchPath.exists() == false) {
151             return this;
152         }
153         if (fileList == null) {
154             fileList = new LinkedList JavaDoc();
155         }
156         if (searchPath.isDirectory() == false) {
157             fileList.add(searchPath);
158             return this;
159         }
160         listFiles(searchPath);
161         return this;
162     }
163
164     /**
165      * Specifies the search path.
166      */

167     public FindFile searchPath(URI JavaDoc searchPath) {
168         searchPath(new File JavaDoc(searchPath));
169         return this;
170     }
171
172     /**
173      * Specifies the search path.
174      */

175     public FindFile searchPath(URI JavaDoc[] searchPath) {
176         for (int i = 0; i < searchPath.length; i++) {
177             searchPath(searchPath[i]);
178         }
179         return this;
180     }
181
182
183     /**
184      * Specifies the search path.
185      */

186     public FindFile searchPath(URL JavaDoc searchPath) {
187         try {
188             searchPath(new URI JavaDoc(searchPath.toString()));
189         } catch (URISyntaxException JavaDoc usex) {
190             throw new IllegalArgumentException JavaDoc("Invalid URL: " + usex.toString());
191         }
192         return this;
193     }
194
195
196     /**
197      * Specifies the search path.
198      */

199     public FindFile searchPath(URL JavaDoc[] searchPath) {
200         for (int i = 0; i < searchPath.length; i++) {
201             searchPath(searchPath[i]);
202         }
203         return this;
204     }
205
206
207     
208
209     // ---------------------------------------------------------------- types
210

211     protected boolean subfilesAfterFolder = true;
212
213     /**
214      * If set to <code>true</code> then all subfiles of a folder will be listed
215      * directly after the folder, while folder will be listed first. Otherwise,
216      * sub files will be listed after the all files of current folder.
217      */

218     public void subfilesAfterFolder(boolean value) {
219         subfilesAfterFolder = value;
220     }
221
222
223     // ---------------------------------------------------------------- next file
224

225     /**
226      * Finds the next file. Returns founded file that matches search configuration
227      * or <code>null</code> if no more files can be found.
228      */

229     public File JavaDoc nextFile() {
230         if (fileList == null) {
231             return null;
232         }
233
234         while (true) {
235             if (fileList.isEmpty()) {
236                 fileList = null;
237                 return null;
238             }
239             File JavaDoc currentFile = (File JavaDoc) fileList.removeFirst();
240             if (currentFile.isDirectory()) {
241                 if (recursive == true) {
242                     listFiles(currentFile);
243                 }
244                 if (includeDirs == true) {
245                     if (onFileEntry(currentFile) == true) {
246                         return currentFile;
247                     }
248                 }
249                 continue;
250             }
251             return currentFile;
252         }
253     }
254
255
256
257     // ---------------------------------------------------------------- list files
258

259     /**
260      * List all files and folders in specified directory.
261      * <b>All</b> folders are added to the list (because of possible recursion).
262      * Opposite, files are filtered first and added if matched.
263      */

264     protected void listFiles(File JavaDoc directory) {
265         File JavaDoc[] list = directory.listFiles();
266         LinkedList JavaDoc subFolders;
267         LinkedList JavaDoc subFiles;
268         if (subfilesAfterFolder == false) {
269             subFolders = fileList;
270             subFiles = fileList;
271         } else {
272             subFolders = new LinkedList JavaDoc();
273             subFiles = new LinkedList JavaDoc();
274         }
275         for (int i = 0; i < list.length; i++) {
276             File JavaDoc currentFile = list[i];
277             if (currentFile.isFile() == true) {
278                 if ((includeFiles == true) && (onFileEntry(currentFile) == true)) {
279                     subFiles.addLast(currentFile);
280                 }
281             } else if (currentFile.isDirectory() == true) {
282                 subFolders.addLast(currentFile);
283             }
284         }
285         if (subfilesAfterFolder == true) {
286             if (subFiles.isEmpty() == false) {
287                 fileList.addAll(0, subFiles);
288             }
289             if (subFolders.isEmpty() == false) {
290                 fileList.addAll(0, subFolders);
291             }
292         }
293     }
294
295     // ---------------------------------------------------------------- callback
296

297     /**
298      * Called on each file entry (file or directory) and returns <code>true</code>
299      * if file passes search criteria.
300      */

301     protected boolean onFileEntry(File JavaDoc currentFile) {
302         return true;
303     }
304 }
305
Popular Tags