bayard 2004/10/30 21:17:34
Modified: io/src/java/org/apache/commons/io FilenameUtils.java
io/src/java/org/apache/commons/io/filefilter
WildcardFilter.java
io/src/java/org/apache/commons/io/find FileFinder.java
FindingFilter.java
io/src/test/org/apache/commons/io PackageTestSuite.java
Added: io/src/test/org/apache/commons/io
FilenameUtilsWildcardTestCase.java
Removed: io/src/java/org/apache/commons/io WildcardUtils.java
io/src/test/org/apache/commons/io WildcardUtilsTest.java
Log:
merged WildcardUtils into FilenameUtils; thought the test case is still a separate
file
Revision Changes Path
1.25 +98 -1
jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java
Index: FilenameUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- FilenameUtils.java 30 Oct 2004 23:59:17 -0000 1.24
+++ FilenameUtils.java 31 Oct 2004 04:17:34 -0000 1.25
@@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Collection;
+import java.util.ArrayList;
/**
* Utility class that provides methods to manipulate filenames and filepaths.
@@ -551,5 +552,101 @@
String[] array = (String[]) extensions.toArray(new
String[extensions.size()]);
return isExtension(filename, array);
}
+
+ /**
+ * See if a particular piece of text, often a filename,
+ * matches to a specified wildcard, as seen on DOS/UNIX command lines.
+ */
+ public static boolean wildcardMatch(String text, String wildcard) {
+ String[] wcs = splitOnTokens(wildcard);
+
+ int textIdx = 0;
+ int wcsIdx = 0;
+ boolean anyChars = false;
+
+ // loop whilst tokens and text left to process
+ while (wcsIdx < wcs.length && textIdx < text.length()) {
+
+ // ? so move to next text char
+ if (wcs[wcsIdx].equals("?")) {
+ textIdx++;
+ } else
+ if (!wcs[wcsIdx].equals("*")) {
+ // matching text token
+ if (anyChars) {
+ // any chars then try to locate text token
+ textIdx = text.indexOf(wcs[wcsIdx], textIdx);
+
+ if (textIdx == -1) {
+ // token not found
+ return false;
+ }
+ } else {
+ // matching from current position
+ if (!text.startsWith(wcs[wcsIdx], textIdx)) {
+ // couldnt match token
+ return false;
+ }
+ }
+
+ // matched text token, move text index to end of matched token
+ textIdx += wcs[wcsIdx].length();
+ }
+
+ // set any chars status
+ anyChars = wcs[wcsIdx].equals("*");
+
+ wcsIdx++;
+ }
+
+ // didnt match all wildcards
+ if (wcsIdx < wcs.length) {
+ // ok if one remaining and wildcard or empty
+ if (wcsIdx + 1 != wcs.length || !(wcs[wcsIdx].equals("*") ||
wcs[wcsIdx].equals("")) ) {
+ return false;
+ }
+ }
+
+ // ran out of text chars
+ if (textIdx > text.length()) {
+ return false;
+ }
+
+ // didnt match all text chars, only ok if any chars set
+ if (textIdx < text.length() && !anyChars) {
+ return false;
+ }
+
+ return true;
+ }
+
+ // used by wildcardMatch
+ // package level so a unit test may run on this
+ static String[] splitOnTokens(String text) {
+ char[] array = text.toCharArray();
+ if(text.indexOf("?") == -1 && text.indexOf("*") == -1) {
+ return new String[] { text };
+ }
+
+ ArrayList list = new ArrayList();
+ StringBuffer buffer = new StringBuffer();
+ for(int i=0; i<array.length; i++) {
+ if(array[i] == '?' || array[i] == '*') {
+ if(buffer.length() != 0) {
+ list.add(buffer.toString());
+ buffer.setLength(0);
+ }
+ list.add(new String( new char[] { array[i] } ));
+ } else {
+ buffer.append(array[i]);
+ }
+ }
+ if(buffer.length() != 0) {
+ list.add(buffer.toString());
+ }
+
+ return (String[]) list.toArray(new String[0]);
+ }
+
}
1.5 +5 -5
jakarta-commons/io/src/java/org/apache/commons/io/filefilter/WildcardFilter.java
Index: WildcardFilter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/filefilter/WildcardFilter.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- WildcardFilter.java 25 Oct 2004 23:28:29 -0000 1.4
+++ WildcardFilter.java 31 Oct 2004 04:17:34 -0000 1.5
@@ -2,12 +2,12 @@
import java.io.File;
import java.util.List;
-import org.apache.commons.io.WildcardUtils;
+import org.apache.commons.io.FilenameUtils;
/**
* Filters files using supplied wildcard(s).
* <p/>
- * See org.apache.commons.io.find.WildcardUtils for wildcard matching rules
+ * See org.apache.commons.io.find.FilenameUtils.wildcardMatch() for wildcard
matching rules
* <p/>
*
* <p/>
@@ -86,7 +86,7 @@
}
for (int i = 0; i < wildcards.length; i++) {
- if (WildcardUtils.match(name, wildcards[i])) {
+ if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
return true;
}
}
@@ -106,7 +106,7 @@
}
for (int i = 0; i < wildcards.length; i++) {
- if (WildcardUtils.match(file.getName(), wildcards[i])) {
+ if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) {
return true;
}
}
1.4 +0 -1
jakarta-commons/io/src/java/org/apache/commons/io/find/FileFinder.java
Index: FileFinder.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/find/FileFinder.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FileFinder.java 29 Oct 2004 21:34:56 -0000 1.3
+++ FileFinder.java 31 Oct 2004 04:17:34 -0000 1.4
@@ -77,7 +77,6 @@
boolean depthFirst = toBoolean(options.get(Finder.DEPTH));
- // TODO: to implement
int maxDepth = toInt(options.get(Finder.MAXDEPTH));
int minDepth = toInt(options.get(Finder.MINDEPTH));
boolean ignoreHiddenDirs =
toBoolean(options.get(Finder.IGNORE_HIDDEN_DIRS));
1.6 +6 -5
jakarta-commons/io/src/java/org/apache/commons/io/find/FindingFilter.java
Index: FindingFilter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/find/FindingFilter.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- FindingFilter.java 29 Oct 2004 21:34:56 -0000 1.5
+++ FindingFilter.java 31 Oct 2004 04:17:34 -0000 1.6
@@ -25,7 +25,7 @@
import java.util.regex.Pattern;
import java.util.regex.Matcher;
-import org.apache.commons.io.WildcardUtils;
+import org.apache.commons.io.FilenameUtils;
/**
* This is where most of the find functionality occurs. Nearly every option
@@ -219,6 +219,7 @@
/**
* @todo needs to handle +5 for > 5 and -5 for < 5.
* @todo Also needs to handle k, m, g, as suffixes.
+ * @todo Switch from 512 byte block size to bytes
*/
class SizeFilter implements FileFilter {
private Object option;
@@ -251,9 +252,9 @@
}
public boolean accept(File file) {
if(this.ignoreCase) {
- return FindingFilter.invert( this.invert,
WildcardUtils.match(file.getName().toLowerCase(),
this.argument.toString().toLowerCase()) );
+ return FindingFilter.invert( this.invert,
FilenameUtils.wildcardMatch(file.getName().toLowerCase(),
this.argument.toString().toLowerCase()) );
} else {
- return FindingFilter.invert( this.invert,
WildcardUtils.match(file.getName(), this.argument.toString()) );
+ return FindingFilter.invert( this.invert,
FilenameUtils.wildcardMatch(file.getName(), this.argument.toString()) );
}
}
}
@@ -271,9 +272,9 @@
}
public boolean accept(File file) {
if(this.ignoreCase) {
- return FindingFilter.invert( this.invert,
WildcardUtils.match(file.getPath().toLowerCase(),
this.argument.toString().toLowerCase()) );
+ return FindingFilter.invert( this.invert,
FilenameUtils.wildcardMatch(file.getPath().toLowerCase(),
this.argument.toString().toLowerCase()) );
} else {
- return FindingFilter.invert( this.invert,
WildcardUtils.match(file.getPath(), this.argument.toString()) );
+ return FindingFilter.invert( this.invert,
FilenameUtils.wildcardMatch(file.getPath(), this.argument.toString()) );
}
}
}
1.2 +1 -1
jakarta-commons/io/src/test/org/apache/commons/io/PackageTestSuite.java
Index: PackageTestSuite.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/PackageTestSuite.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PackageTestSuite.java 25 Oct 2004 23:35:48 -0000 1.1
+++ PackageTestSuite.java 31 Oct 2004 04:17:34 -0000 1.2
@@ -41,6 +41,7 @@
suite.addTest(new TestSuite(DemuxTestCase.class));
suite.addTest(new TestSuite(EndianUtilsTest.class));
suite.addTest(new TestSuite(FilenameUtilsTestCase.class));
+ suite.addTest(new TestSuite(FilenameUtilsWildcardTestCase.class));
suite.addTest(new TestSuite(FileUtilsFileNewerTestCase.class));
suite.addTest(new TestSuite(FileUtilsListFilesTestCase.class));
suite.addTest(new TestSuite(FileUtilsTestCase.class));
@@ -49,7 +50,6 @@
suite.addTest(new TestSuite(IOUtilsCopyTestCase.class));
suite.addTest(new TestSuite(IOUtilsTestCase.class));
suite.addTest(new TestSuite(IOUtilsWriteTestCase.class));
- suite.addTest(new TestSuite(WildcardUtilsTest.class));
return suite;
}
}
1.1
jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
Index: FilenameUtilsWildcardTestCase.java
===================================================================
/*
* Copyright 2001-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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.io;
import junit.framework.TestCase;
public class FilenameUtilsWildcardTestCase extends TestCase {
public FilenameUtilsWildcardTestCase(String name) {
super(name);
}
//-----------------------------------------------------------------------
// Testing:
// FilenameUtils.wildcardMatch(String,String)
public void testMatch() {
assertTrue( FilenameUtils.wildcardMatch("Foo", "Foo") );
assertTrue( FilenameUtils.wildcardMatch("", "") );
assertTrue( FilenameUtils.wildcardMatch("Foo", "Fo*") );
assertTrue( FilenameUtils.wildcardMatch("Foo", "Fo?") );
assertTrue( FilenameUtils.wildcardMatch("Foo Bar and Catflap", "Fo*") );
assertTrue( FilenameUtils.wildcardMatch("New Bookmarks", "N?w ?o?k??r?s") );
assertFalse( FilenameUtils.wildcardMatch("Foo", "Bar") );
assertTrue( FilenameUtils.wildcardMatch("Foo Bar Foo", "F*o Bar*") );
assertTrue( FilenameUtils.wildcardMatch("Adobe Acrobat Installer", "Ad*er")
);
assertTrue( FilenameUtils.wildcardMatch("Foo", "*Foo") );
assertTrue( FilenameUtils.wildcardMatch("Foo", "Foo*") );
}
public void testSplitOnTokens() {
assertArrayEquals( new String[] { "Ad", "*", "er" },
FilenameUtils.splitOnTokens("Ad*er") );
assertArrayEquals( new String[] { "Ad", "?", "er" },
FilenameUtils.splitOnTokens("Ad?er") );
assertArrayEquals( new String[] { "Test", "*", "?", "One" },
FilenameUtils.splitOnTokens("Test*?One") );
assertArrayEquals( new String[] { "*", "*", "*", "*" },
FilenameUtils.splitOnTokens("****") );
assertArrayEquals( new String[] { "*", "?", "?", "*" },
FilenameUtils.splitOnTokens("*??*") );
assertArrayEquals( new String[] { "*", "?", "?", "*" },
FilenameUtils.splitOnTokens("*??*") );
assertArrayEquals( new String[] { "h", "?", "?", "*" },
FilenameUtils.splitOnTokens("h??*") );
assertArrayEquals( new String[] { "" }, FilenameUtils.splitOnTokens("") );
}
private void assertArrayEquals(Object[] a1, Object[] a2) {
assertEquals(a1.length, a2.length);
for(int i=0; i<a1.length; i++) {
assertEquals(a1[i], a2[i]);
}
}
private void assertMatch(String text, String wildcard, boolean expected) {
assertEquals(text + " " + wildcard, expected,
FilenameUtils.wildcardMatch(text, wildcard));
}
// A separate set of tests, added to this batch
public void testMatch2() {
assertMatch("log.txt", "log.txt", true);
assertMatch("log.txt1", "log.txt", false);
assertMatch("log.txt", "log.txt*", true);
assertMatch("log.txt", "log.txt*1", false);
assertMatch("log.txt", "*log.txt*", true);
assertMatch("log.txt", "*.txt", true);
assertMatch("txt.log", "*.txt", false);
assertMatch("config.ini", "*.ini", true);
assertMatch("config.txt.bak", "con*.txt", false);
assertMatch("log.txt9", "*.txt?", true);
assertMatch("log.txt", "*.txt?", false);
assertMatch("progtestcase.java~5~", "*test*.java~*~", true);
assertMatch("progtestcase.java;5~", "*test*.java~*~", false);
assertMatch("progtestcase.java~5", "*test*.java~*~", false);
assertMatch("log.txt", "log.*", true);
assertMatch("log.txt", "log?*", true);
assertMatch("log.txt12", "log.txt??", true);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]