Skip to content

Commit 1d40fce

Browse files
committed
WildcardFilter (Bugzilla #31115)
Submitted by: Jason Anderson git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140604 13f79535-47bb-0310-9956-ffa450edef68
1 parent c7cb6d1 commit 1d40fce

2 files changed

Lines changed: 161 additions & 11 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import org.apache.commons.io.find.WildcardUtils;
6+
7+
8+
/**
9+
* Filters files using supplied wildcard(s).
10+
* <p/>
11+
* See org.apache.commons.io.find.WildcardUtils for wildcard matching rules
12+
* <p/>
13+
*
14+
* <p/>
15+
* e.g.
16+
* <pre>
17+
* File dir = new File(".");
18+
* FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
19+
* File[] files = dir.listFiles(fileFilter);
20+
* for (int i = 0; i < files.length; i++) {
21+
* System.out.println(files[i]);
22+
* }
23+
* </pre>
24+
*
25+
* @author Jason Anderson
26+
*/
27+
public class WildcardFilter extends AbstractFileFilter {
28+
/** The wildcards that will be used to match filenames */
29+
private String[] wildcards = null;
30+
31+
/**
32+
* Construct a new wildcard filter for a single wildcard
33+
*
34+
* @param wildcard wildcard to match
35+
* @throws IllegalArgumentException if the pattern is null
36+
*/
37+
public WildcardFilter(String wildcard) {
38+
if (wildcard == null) {
39+
throw new java.lang.IllegalArgumentException();
40+
}
41+
42+
wildcards = new String[] { wildcard };
43+
}
44+
45+
46+
/**
47+
* Construct a new wildcard filter for an array of wildcards
48+
*
49+
* @param wildcards wildcards to match
50+
* @throws IllegalArgumentException if the pattern array is null
51+
*/
52+
public WildcardFilter(String[] wildcards) {
53+
if (wildcards == null) {
54+
throw new java.lang.IllegalArgumentException();
55+
}
56+
57+
this.wildcards = wildcards;
58+
}
59+
60+
61+
/**
62+
* Construct a new wildcard filter for a list of wildcards
63+
*
64+
* @param wildcards list of wildcards to match
65+
* @throws IllegalArgumentException if the pattern list is null
66+
* @throws ClassCastException if the list does not contain Strings
67+
*/
68+
public WildcardFilter(List wildcards) {
69+
if (wildcards == null) {
70+
throw new java.lang.IllegalArgumentException();
71+
}
72+
73+
this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
74+
}
75+
76+
77+
/**
78+
* Checks to see if the filename matches one of the wildcards.
79+
*
80+
* @param dir the file directory
81+
* @param name the filename
82+
* @return true if the filename matches one of the wildcards
83+
*/
84+
public boolean accept(File dir, String name) {
85+
if (dir != null && new File(dir, name).isDirectory()) {
86+
return false;
87+
}
88+
89+
for (int i = 0; i < wildcards.length; i++) {
90+
if (WildcardUtils.match(name, wildcards[i])) {
91+
return true;
92+
}
93+
}
94+
95+
return false;
96+
}
97+
98+
99+
/**
100+
* Checks to see if the filename matches one of the wildcards.
101+
*
102+
* @param file the file to check
103+
* @return true if the filename matches one of the wildcards
104+
*/
105+
public boolean accept(File file) {
106+
if (file.isDirectory()) {
107+
return false;
108+
}
109+
110+
for (int i = 0; i < wildcards.length; i++) {
111+
if (WildcardUtils.match(file.getName(), wildcards[i])) {
112+
return true;
113+
}
114+
}
115+
116+
return false;
117+
}
118+
119+
}

src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2002-2004 The Apache Software Foundation.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,11 +29,11 @@ public class FileFilterTestCase extends TestCase {
2929
public FileFilterTestCase(String name) {
3030
super(name);
3131
}
32-
32+
3333
public static void main(String[] args) {
3434
TestRunner.run(suite());
3535
}
36-
36+
3737
public static TestSuite suite() {
3838
return new TestSuite(FileFilterTestCase.class);
3939
}
@@ -45,7 +45,7 @@ public void tearDown() {
4545
}
4646

4747
public void assertFiltering(IOFileFilter filter, File file, boolean expected) throws Exception {
48-
// Note. This only tests the (File, String) version if the parent of
48+
// Note. This only tests the (File, String) version if the parent of
4949
// the File passed in is not null
5050
assertTrue(
5151
"Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
@@ -71,11 +71,11 @@ public void testSuffix() throws Exception {
7171
assertFiltering(filter, new File("fred"), false);
7272
assertFiltering(filter, new File(".tes"), true);
7373
assertFiltering(filter, new File("fred.test"), true);
74-
74+
7575
filter = new SuffixFileFilter("est");
7676
assertFiltering(filter, new File("test"), true);
7777
assertFiltering(filter, new File("fred"), false);
78-
78+
7979
try {
8080
new SuffixFileFilter((String) null);
8181
fail();
@@ -108,18 +108,18 @@ public void testPrefix() throws Exception {
108108
assertFiltering(filter, new File("test"), false);
109109
assertFiltering(filter, new File("fo_o.test"), false);
110110
assertFiltering(filter, new File("abar.exe"), false);
111-
111+
112112
filter = new PrefixFileFilter("tes");
113113
assertFiltering(filter, new File("test"), true);
114114
assertFiltering(filter, new File("fred"), false);
115-
115+
116116
try {
117117
new PrefixFileFilter((String) null);
118118
fail();
119119
} catch (IllegalArgumentException ex) {
120120
}
121121
}
122-
122+
123123
public void testNameFilter() throws Exception {
124124
IOFileFilter filter = new NameFileFilter(new String[] { "foo", "bar" });
125125
assertFiltering(filter, new File("foo"), true);
@@ -197,4 +197,35 @@ public void testOr() throws Exception {
197197
}
198198
}
199199

200+
201+
public void testWildcard() throws Exception {
202+
IOFileFilter filter = new WildcardFilter("*.txt");
203+
assertFiltering(filter, new File("log.txt"), true);
204+
// assertFiltering(filter, new File("log.txt.bak"), false);
205+
206+
filter = new WildcardFilter("log?.txt");
207+
assertFiltering(filter, new File("log1.txt"), true);
208+
assertFiltering(filter, new File("log12.txt"), false);
209+
210+
filter = new WildcardFilter("open??.????04");
211+
assertFiltering(filter, new File("openAB.102504"), true);
212+
assertFiltering(filter, new File("openA.102504"), false);
213+
assertFiltering(filter, new File("openXY.123103"), false);
214+
// assertFiltering(filter, new File("openAB.102504.old"), false);
215+
216+
filter = new WildcardFilter(new String[] {"*.java", "*.class"});
217+
assertFiltering(filter, new File("Test.java"), true);
218+
assertFiltering(filter, new File("Test.class"), true);
219+
assertFiltering(filter, new File("Test.jsp"), false);
220+
221+
try {
222+
new WildcardFilter((String) null);
223+
fail();
224+
} catch (IllegalArgumentException ex) {
225+
// expected
226+
}
227+
}
228+
229+
200230
}
231+

0 commit comments

Comments
 (0)