01: package net.sourceforge.pmd.util;
02:
03: import java.io.File;
04: import java.io.FilenameFilter;
05: import java.util.ArrayList;
06: import java.util.List;
07:
08: /**
09: * A utility class for finding files within a directory.
10: */
11: public class FileFinder {
12:
13: private FilenameFilter filter;
14: private static final String FILE_SEP = System
15: .getProperty("file.separator");
16:
17: public List<File> findFilesFrom(String dir, FilenameFilter filter,
18: boolean recurse) {
19: this .filter = filter;
20: List<File> files = new ArrayList<File>();
21: scanDirectory(new File(dir), files, recurse);
22: return files;
23: }
24:
25: /**
26: * Implements a tail recursive file scanner
27: */
28: private void scanDirectory(File dir, List<File> list,
29: boolean recurse) {
30: String[] candidates = dir.list(filter);
31: if (candidates == null) {
32: return;
33: }
34: for (int i = 0; i < candidates.length; i++) {
35: File tmp = new File(dir + FILE_SEP + candidates[i]);
36: if (tmp.isDirectory()) {
37: if (recurse) {
38: scanDirectory(tmp, list, true);
39: }
40: } else {
41: list.add(new File(dir + FILE_SEP + candidates[i]));
42: }
43: }
44: }
45: }
|