KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > file > filters > RegExpFileFilter


1 package jodd.file.filters;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 import jodd.util.StringFlags;
9 import jodd.util.StringUtil;
10
11 /**
12  * FileFilter that matches files with use of Regular Expression.
13  *
14  * Here are some tips for regular expressions:
15  * <ul>
16  * <li>.* : matches any number of character</li>
17  * <li>.? : matches zero or one character</li>
18  * </ul>
19  *
20  * For more details check the <code>FileFilterAbs</code> documentation.
21  *
22  * @see jodd.file.filters.FileFilterAbs
23  */

24 public class RegExpFileFilter extends FileFilterAbs {
25
26     // ---------------------------------------------------------------- construction
27

28     /**
29      * Regular Expression file filter.
30      */

31     public RegExpFileFilter(String pattern, String opt) {
32         super(pattern, opt);
33     }
34
35     /**
36      * Regular Expression file filter with default behaviour.
37      *
38      * @param pattern pattern
39      */

40     public RegExpFileFilter(String pattern) {
41         super(pattern);
42     }
43
44     /**
45      * Regular Expression file filter with no behaviour and that may
46      * be configured later.
47      */

48     public RegExpFileFilter() {
49         super();
50     }
51
52     // ---------------------------------------------------------------- speed optimization
53

54     private Pattern regexpPattern = null;
55
56     /**
57      * Sets the new pattern. It overrides default method because of speed
58      * optimization.
59      *
60      * @param p pattern
61      */

62     public void setPattern(String p) {
63         super.setPattern(p);
64         if (pattern != null) {
65             regexpPattern = Pattern.compile(pattern);
66         }
67     }
68
69     // ---------------------------------------------------------------- FileFilterAbs.match()
70

71     /**
72      * Regular expression matching.
73      *
74      * @param file File to match.
75      *
76      * @return <code>true</code> if file name matches regular expression pattern,
77      * <code>false</code> otherwise.
78      */

79     public boolean match(File file) {
80         Matcher matcher = regexpPattern.matcher(getFileName(file)); // regexp match
81
return matcher.matches();
82     }
83 }
84
85
Popular Tags