KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Abstract FileFilter contains default logic and parameters for all
13  * implemented file filters. <p>
14  *
15  * By default, both files and directories are matched, but only by name (not
16  * including the path). This behaviour can be changed with following flags:
17  *
18  * <ul>
19  * <li>p - match also the path part of the file name</li>
20  * <li>d - do not match directories (skip)</li>
21  * </ul>
22  *
23  * Note: when path is also used for matching (flag p), non-unix file path
24  * separator is <b>always</b> changed to unix separator ('/'). Therefore,
25  * matching must be always done against unix file separators.
26  */

27 abstract class FileFilterAbs implements FileFilter {
28
29     // ---------------------------------------------------------------- initialization
30

31     /**
32      * Fast constructor that uses a string of options for setting file filter
33      * properties.
34      *
35      * @param pattern pattern to match against
36      * @param opt options, subset of following flags:
37      * <ul>
38      * <li>p - match also the path part of the file name</li>
39      * <li>d - do not match directories, i.e. all directories matches ok.</li>
40      * </ul>
41      */

42     public FileFilterAbs(String pattern, String opt) {
43         flags.parse(opt, this);
44         setPattern(pattern);
45     }
46
47     /**
48      * File filter with default behaviour.
49      *
50      * @param pattern
51      */

52     public FileFilterAbs(String pattern) {
53         setPattern(pattern);
54     }
55
56     /**
57      * Regular Expression file filter with no behaviour and that may be
58      * configured later.
59      */

60     public FileFilterAbs() {
61     }
62
63
64     // ---------------------------------------------------------------- flags
65

66     private static StringFlags flags = new StringFlags();
67
68     static {
69         flags.addFlag('p', "matchPath", true);
70         flags.addFlag('d', "skipDirs", true);
71     }
72
73     boolean matchPath;
74     /**
75      * Set match path flag.
76      *
77      * @param v
78      */

79     public void setMatchPath(boolean v) {
80         matchPath = v;
81     }
82     /**
83      * Returns match path flag.
84      *
85      * @return match flag
86      */

87     public boolean getMatchPath() {
88         return matchPath;
89     }
90
91
92     boolean skipDirs;
93     /**
94      * Set skip directory flag.
95      *
96      * @param v
97      */

98     public void setSkipDirs(boolean v) {
99         skipDirs = v;
100     }
101     /**
102      * Returns skip directory flag.
103      *
104      * @return skip dirs flag
105      */

106     public boolean getSkipDirs() {
107         return skipDirs;
108     }
109
110
111     String pattern;
112     /**
113      * Set pattern.
114      *
115      * @param p pattern
116      */

117     public void setPattern(String p) {
118         pattern = p;
119     }
120     /**
121      * Returns pattern.
122      *
123      * @return pattern
124      */

125     public String getPattern() {
126         return pattern;
127     }
128     
129     // ---------------------------------------------------------------- FileFilter
130

131     static boolean isUnix = false;
132     static {
133         if (File.separatorChar == '/') {
134             isUnix = true;
135         } else {
136             isUnix = false;
137         }
138     }
139     
140     /**
141      * Prepares the tests whether or not the specified File matches conditions.
142      *
143      * @param f file to be tested
144      *
145      * @return <code>true</code> if file matches ok, otherwise <code>false</code>
146      */

147     public boolean accept(File f) {
148         if (f.isDirectory()) { // file is a directory
149
if (skipDirs == true) { // return if no matching required
150
return true;
151             }
152         }
153         if (pattern == null) { // no pattern specified -> no matching possible
154
return true; // therefore file is ok
155
}
156         
157         return match(f);
158     }
159
160
161     /**
162      * Returns the file name of a File, either just file name either full name
163      * with path included, regarding of file filter parameters. Path separators
164      * are changed to unix-alike. This helper method should be used by the
165      * implementations.
166      *
167      * @param f file
168      *
169      * @return file name
170      */

171     String getFileName(File f) {
172         String fileName = null;
173         if (matchPath == false) {
174             fileName = f.getName(); // do not match the path
175
} else {
176             fileName = f.getPath(); // match complete file name, including path
177
if (isUnix == false) {
178                 fileName = StringUtil.replace(fileName, File.separatorChar, '/');
179             }
180         }
181         return fileName;
182     }
183
184     /**
185      * User-defined matching.
186      *
187      * @param file file to match
188      *
189      * @return <code>true</code> if file matches the pattern, <code>false</code>
190      * otherwise.
191      */

192     abstract boolean match(File file);
193 }
194
Popular Tags