bayard 2004/09/21 19:04:21
Modified: io/src/test/org/apache/commons/io/filefilter
FileFilterTestCase.java
Added: io/src/java/org/apache/commons/io/filefilter
WildcardFilter.java
Log:
WildcardFilter (Bugzilla #31115)
Submitted by: Jason Anderson
Revision Changes Path
1.1
jakarta-commons/io/src/java/org/apache/commons/io/filefilter/WildcardFilter.java
Index: WildcardFilter.java
===================================================================
package org.apache.commons.io.filefilter;
import java.io.File;
import java.util.List;
import org.apache.commons.io.find.WildcardUtils;
/**
* Filters files using supplied wildcard(s).
* <p/>
* See org.apache.commons.io.find.WildcardUtils for wildcard matching rules
* <p/>
*
* <p/>
* e.g.
* <pre>
* File dir = new File(".");
* FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
* File[] files = dir.listFiles(fileFilter);
* for (int i = 0; i < files.length; i++) {
* System.out.println(files[i]);
* }
* </pre>
*
* @author Jason Anderson
*/
public class WildcardFilter extends AbstractFileFilter {
/** The wildcards that will be used to match filenames */
private String[] wildcards = null;
/**
* Construct a new wildcard filter for a single wildcard
*
* @param wildcard wildcard to match
* @throws IllegalArgumentException if the pattern is null
*/
public WildcardFilter(String wildcard) {
if (wildcard == null) {
throw new java.lang.IllegalArgumentException();
}
wildcards = new String[] { wildcard };
}
/**
* Construct a new wildcard filter for an array of wildcards
*
* @param wildcards wildcards to match
* @throws IllegalArgumentException if the pattern array is null
*/
public WildcardFilter(String[] wildcards) {
if (wildcards == null) {
throw new java.lang.IllegalArgumentException();
}
this.wildcards = wildcards;
}
/**
* Construct a new wildcard filter for a list of wildcards
*
* @param wildcards list of wildcards to match
* @throws IllegalArgumentException if the pattern list is null
* @throws ClassCastException if the list does not contain Strings
*/
public WildcardFilter(List wildcards) {
if (wildcards == null) {
throw new java.lang.IllegalArgumentException();
}
this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
}
/**
* Checks to see if the filename matches one of the wildcards.
*
* @param dir the file directory
* @param name the filename
* @return true if the filename matches one of the wildcards
*/
public boolean accept(File dir, String name) {
if (dir != null && new File(dir, name).isDirectory()) {
return false;
}
for (int i = 0; i < wildcards.length; i++) {
if (WildcardUtils.match(name, wildcards[i])) {
return true;
}
}
return false;
}
/**
* Checks to see if the filename matches one of the wildcards.
*
* @param file the file to check
* @return true if the filename matches one of the wildcards
*/
public boolean accept(File file) {
if (file.isDirectory()) {
return false;
}
for (int i = 0; i < wildcards.length; i++) {
if (WildcardUtils.match(file.getName(), wildcards[i])) {
return true;
}
}
return false;
}
}
1.15 +42 -11
jakarta-commons/io/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
Index: FileFilterTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- FileFilterTestCase.java 23 Feb 2004 05:02:25 -0000 1.14
+++ FileFilterTestCase.java 22 Sep 2004 02:04:21 -0000 1.15
@@ -1,12 +1,12 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,11 +29,11 @@
public FileFilterTestCase(String name) {
super(name);
}
-
+
public static void main(String[] args) {
TestRunner.run(suite());
}
-
+
public static TestSuite suite() {
return new TestSuite(FileFilterTestCase.class);
}
@@ -45,7 +45,7 @@
}
public void assertFiltering(IOFileFilter filter, File file, boolean expected)
throws Exception {
- // Note. This only tests the (File, String) version if the parent of
+ // Note. This only tests the (File, String) version if the parent of
// the File passed in is not null
assertTrue(
"Filter(File) " + filter.getClass().getName() + " not " + expected + "
for " + file,
@@ -71,11 +71,11 @@
assertFiltering(filter, new File("fred"), false);
assertFiltering(filter, new File(".tes"), true);
assertFiltering(filter, new File("fred.test"), true);
-
+
filter = new SuffixFileFilter("est");
assertFiltering(filter, new File("test"), true);
assertFiltering(filter, new File("fred"), false);
-
+
try {
new SuffixFileFilter((String) null);
fail();
@@ -108,18 +108,18 @@
assertFiltering(filter, new File("test"), false);
assertFiltering(filter, new File("fo_o.test"), false);
assertFiltering(filter, new File("abar.exe"), false);
-
+
filter = new PrefixFileFilter("tes");
assertFiltering(filter, new File("test"), true);
assertFiltering(filter, new File("fred"), false);
-
+
try {
new PrefixFileFilter((String) null);
fail();
} catch (IllegalArgumentException ex) {
}
}
-
+
public void testNameFilter() throws Exception {
IOFileFilter filter = new NameFileFilter(new String[] { "foo", "bar" });
assertFiltering(filter, new File("foo"), true);
@@ -197,4 +197,35 @@
}
}
+
+ public void testWildcard() throws Exception {
+ IOFileFilter filter = new WildcardFilter("*.txt");
+ assertFiltering(filter, new File("log.txt"), true);
+// assertFiltering(filter, new File("log.txt.bak"), false);
+
+ filter = new WildcardFilter("log?.txt");
+ assertFiltering(filter, new File("log1.txt"), true);
+ assertFiltering(filter, new File("log12.txt"), false);
+
+ filter = new WildcardFilter("open??.????04");
+ assertFiltering(filter, new File("openAB.102504"), true);
+ assertFiltering(filter, new File("openA.102504"), false);
+ assertFiltering(filter, new File("openXY.123103"), false);
+// assertFiltering(filter, new File("openAB.102504.old"), false);
+
+ filter = new WildcardFilter(new String[] {"*.java", "*.class"});
+ assertFiltering(filter, new File("Test.java"), true);
+ assertFiltering(filter, new File("Test.class"), true);
+ assertFiltering(filter, new File("Test.jsp"), false);
+
+ try {
+ new WildcardFilter((String) null);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+
}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]