Skip to content

Commit 4fe3559

Browse files
author
Stephen Colebourne
committed
Add WildcardFileFilter deprecating WildcardFilter
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@417092 13f79535-47bb-0310-9956-ffa450edef68
1 parent 07751ea commit 4fe3559

3 files changed

Lines changed: 199 additions & 27 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Semantic compatible - Yes
2626

2727
Deprecations from 1.2
2828
---------------------
29+
- WildcardFilter deprecated, replaced by WildcardFileFilter
30+
- old class only accepted files, thus had a confusing dual purpose
2931

3032

3133
Bug fixes from 1.2
@@ -36,6 +38,18 @@ Bug fixes from 1.2
3638

3739
Enhancements from 1.2
3840
---------------------
41+
- IOCase
42+
- New class/enumeration for case-sensitivity control
43+
44+
- FilenameUtils
45+
- New methods to handle case-sensitivity
46+
- wildcardMatch - new method that has IOCase as a parameter
47+
- equals - new method that has IOCase as a parameter
48+
49+
- WildcardFileFilter
50+
- Replacement for WildcardFilter
51+
- Accepts both files and directories
52+
- Ability to control case-sensitivity
3953

4054

4155
Feedback
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
6+
import org.apache.commons.io.FilenameUtils;
7+
import org.apache.commons.io.IOCase;
8+
9+
/**
10+
* Filters files using the supplied wildcards.
11+
* <p>
12+
* This filter selects files and directories based on one or more wildcards.
13+
* Testing is case-sensitive by default, but this can be configured.
14+
* <p>
15+
* The wildcard matcher uses the characters '?' and '*' to represent a
16+
* single or multiple wildcard characters.
17+
* This is the same as often found on Dos/Unix command lines.
18+
* The extension check is case-sensitive by .
19+
* See {@link FilenameUtils#wildcardMatchOnSystem} for more information.
20+
* <p>
21+
* For example:
22+
* <pre>
23+
* File dir = new File(".");
24+
* FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
25+
* File[] files = dir.listFiles(fileFilter);
26+
* for (int i = 0; i < files.length; i++) {
27+
* System.out.println(files[i]);
28+
* }
29+
* </pre>
30+
*
31+
* @author Jason Anderson
32+
* @version $Revision: 155419 $ $Date$
33+
* @since Commons IO 1.3
34+
*/
35+
public class WildcardFileFilter extends AbstractFileFilter {
36+
37+
/** The wildcards that will be used to match filenames. */
38+
private String[] wildcards;
39+
/** Whether the comparison is case sensitive. */
40+
private IOCase caseSensitivity;
41+
42+
/**
43+
* Construct a new case-sensitive wildcard filter for a single wildcard.
44+
*
45+
* @param wildcard the wildcard to match
46+
* @throws IllegalArgumentException if the pattern is null
47+
*/
48+
public WildcardFileFilter(String wildcard) {
49+
this(wildcard, null);
50+
}
51+
52+
/**
53+
* Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
54+
*
55+
* @param wildcard the wildcard to match, not null
56+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
57+
* @throws IllegalArgumentException if the pattern is null
58+
*/
59+
public WildcardFileFilter(String wildcard, IOCase caseSensitivity) {
60+
if (wildcard == null) {
61+
throw new IllegalArgumentException("The wildcard must not be null");
62+
}
63+
this.wildcards = new String[] { wildcard };
64+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
65+
}
66+
67+
/**
68+
* Construct a new case-sensitive wildcard filter for an array of wildcards.
69+
*
70+
* @param wildcards the array of wildcards to match
71+
* @throws IllegalArgumentException if the pattern array is null
72+
*/
73+
public WildcardFileFilter(String[] wildcards) {
74+
this(wildcards, null);
75+
}
76+
77+
/**
78+
* Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
79+
*
80+
* @param wildcards the array of wildcards to match, not null
81+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
82+
* @throws IllegalArgumentException if the pattern array is null
83+
*/
84+
public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) {
85+
if (wildcards == null) {
86+
throw new IllegalArgumentException("The wildcard array must not be null");
87+
}
88+
this.wildcards = wildcards;
89+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
90+
}
91+
92+
/**
93+
* Construct a new case-sensitive wildcard filter for a list of wildcards.
94+
*
95+
* @param wildcards the list of wildcards to match, not null
96+
* @throws IllegalArgumentException if the pattern list is null
97+
* @throws ClassCastException if the list does not contain Strings
98+
*/
99+
public WildcardFileFilter(List wildcards) {
100+
this(wildcards, null);
101+
}
102+
103+
/**
104+
* Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
105+
*
106+
* @param wildcards the list of wildcards to match, not null
107+
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
108+
* @throws IllegalArgumentException if the pattern list is null
109+
* @throws ClassCastException if the list does not contain Strings
110+
*/
111+
public WildcardFileFilter(List wildcards, IOCase caseSensitivity) {
112+
if (wildcards == null) {
113+
throw new IllegalArgumentException("The wildcard list must not be null");
114+
}
115+
this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
116+
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
117+
}
118+
119+
//-----------------------------------------------------------------------
120+
/**
121+
* Checks to see if the filename matches one of the wildcards.
122+
*
123+
* @param dir the file directory
124+
* @param name the filename
125+
* @return true if the filename matches one of the wildcards
126+
*/
127+
public boolean accept(File dir, String name) {
128+
for (int i = 0; i < wildcards.length; i++) {
129+
if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
130+
return true;
131+
}
132+
}
133+
return false;
134+
}
135+
136+
/**
137+
* Checks to see if the filename matches one of the wildcards.
138+
*
139+
* @param file the file to check
140+
* @return true if the filename matches one of the wildcards
141+
*/
142+
public boolean accept(File file) {
143+
String name = file.getName();
144+
for (int i = 0; i < wildcards.length; i++) {
145+
if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
146+
return true;
147+
}
148+
}
149+
return false;
150+
}
151+
152+
}

src/java/org/apache/commons/io/filefilter/WildcardFilter.java

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
import java.io.File;
44
import java.util.List;
5+
56
import org.apache.commons.io.FilenameUtils;
67

78
/**
8-
* Filters files using supplied wildcard(s).
9-
* <p/>
10-
* See org.apache.commons.io.find.FilenameUtils.wildcardMatch() for wildcard matching rules
11-
* <p/>
12-
*
13-
* <p/>
14-
* e.g.
9+
* Filters files using the supplied wildcards.
10+
* <p>
11+
* This filter selects files, but not directories, based on one or more wildcards
12+
* and using case-sensitive comparison.
13+
* <p>
14+
* The wildcard matcher uses the characters '?' and '*' to represent a
15+
* single or multiple wildcard characters.
16+
* This is the same as often found on Dos/Unix command lines.
17+
* The extension check is case-sensitive.
18+
* See {@link FilenameUtils#wildcardMatch} for more information.
19+
* <p>
20+
* For example:
1521
* <pre>
1622
* File dir = new File(".");
1723
* FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
@@ -24,73 +30,73 @@
2430
* @author Jason Anderson
2531
* @version $Revision$ $Date$
2632
* @since Commons IO 1.1
33+
* @deprecated Use WilcardFileFilter. Deprecated as this class performs directory
34+
* filtering which it shouldn't do, but that can't be removed due to compatability.
2735
*/
2836
public class WildcardFilter extends AbstractFileFilter {
2937

30-
/** The wildcards that will be used to match filenames */
31-
private String[] wildcards = null;
38+
/** The wildcards that will be used to match filenames. */
39+
private String[] wildcards;
3240

3341
/**
34-
* Construct a new wildcard filter for a single wildcard
42+
* Construct a new case-sensitive wildcard filter for a single wildcard.
3543
*
36-
* @param wildcard wildcard to match
44+
* @param wildcard the wildcard to match
3745
* @throws IllegalArgumentException if the pattern is null
3846
*/
3947
public WildcardFilter(String wildcard) {
4048
if (wildcard == null) {
41-
throw new java.lang.IllegalArgumentException();
49+
throw new IllegalArgumentException("The wildcard must not be null");
4250
}
43-
44-
wildcards = new String[] { wildcard };
51+
this.wildcards = new String[] { wildcard };
4552
}
4653

4754
/**
48-
* Construct a new wildcard filter for an array of wildcards
55+
* Construct a new case-sensitive wildcard filter for an array of wildcards.
4956
*
50-
* @param wildcards wildcards to match
57+
* @param wildcards the array of wildcards to match
5158
* @throws IllegalArgumentException if the pattern array is null
5259
*/
5360
public WildcardFilter(String[] wildcards) {
5461
if (wildcards == null) {
55-
throw new java.lang.IllegalArgumentException();
62+
throw new IllegalArgumentException("The wildcard array must not be null");
5663
}
57-
5864
this.wildcards = wildcards;
5965
}
6066

6167
/**
62-
* Construct a new wildcard filter for a list of wildcards
68+
* Construct a new case-sensitive wildcard filter for a list of wildcards.
6369
*
64-
* @param wildcards list of wildcards to match
70+
* @param wildcards the list of wildcards to match
6571
* @throws IllegalArgumentException if the pattern list is null
6672
* @throws ClassCastException if the list does not contain Strings
6773
*/
6874
public WildcardFilter(List wildcards) {
6975
if (wildcards == null) {
70-
throw new java.lang.IllegalArgumentException();
76+
throw new IllegalArgumentException("The wildcard list must not be null");
7177
}
72-
7378
this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
7479
}
7580

81+
//-----------------------------------------------------------------------
7682
/**
7783
* Checks to see if the filename matches one of the wildcards.
7884
*
79-
* @param dir the file directory
85+
* @param dir the file directory
8086
* @param name the filename
8187
* @return true if the filename matches one of the wildcards
8288
*/
8389
public boolean accept(File dir, String name) {
8490
if (dir != null && new File(dir, name).isDirectory()) {
8591
return false;
8692
}
87-
93+
8894
for (int i = 0; i < wildcards.length; i++) {
8995
if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
9096
return true;
9197
}
9298
}
93-
99+
94100
return false;
95101
}
96102

@@ -104,13 +110,13 @@ public boolean accept(File file) {
104110
if (file.isDirectory()) {
105111
return false;
106112
}
107-
113+
108114
for (int i = 0; i < wildcards.length; i++) {
109115
if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) {
110116
return true;
111117
}
112118
}
113-
119+
114120
return false;
115121
}
116122

0 commit comments

Comments
 (0)