1
2
3
4
5 package org.abraracourcix.alipes.common;
6 import java.io.File;
7 import java.io.FileFilter;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 /***
13 *
14 * A file filter which takes a regular expression and filters files in a single
15 * directory
16 * @author jdt
17 */
18 public final class RegExpFileFilter implements FileFilter {
19 /***
20 * Standard commons logging
21 */
22 private Log log = LogFactory.getLog("RegExpFileFilter.class");
23 /***
24 * Inherited
25 * @param file file to be tested
26 * @return true if file matches
27 * @see java.io.FileFilter#accept(java.io.File)
28 */
29 public boolean accept(final File file) {
30 if (file == null) {
31 return false;
32 }
33 final String filename = file.getName();
34 final File parent = file.getParentFile();
35 log.debug("Test file " + file);
36 log.debug("Filename " + filename);
37 log.debug("Parent " + parent);
38 if (rootDir!=null && !(rootDir.equals(parent))) {
39 return false;
40 }
41 final Matcher matcher = pattern.matcher(filename);
42 return matcher.matches();
43 }
44 /***
45 * The pattern against which we compare
46 */
47 private Pattern pattern;
48 /***
49 * Files must be in this folder
50 */
51 private File rootDir;
52 /***
53 * Constructor
54 *
55 * @param regexp
56 * the regular expression to match
57 * @param rootDirectory
58 * the directory in which to search - can be null if we don't care
59 */
60 public RegExpFileFilter(final String regexp, final File rootDirectory) {
61 pattern = Pattern.compile(regexp);
62 this.rootDir = rootDirectory;
63 log.debug("Root directory: " + rootDir);
64 }
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84