Skip to content

Commit 84b251b

Browse files
author
Stephen Colebourne
committed
Convert wildcard method to match based on OS case sensitivity
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140647 13f79535-47bb-0310-9956-ffa450edef68
1 parent a09ebfc commit 84b251b

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

src/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
* @author Martin Cooper
8080
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
8181
* @author Stephen Colebourne
82-
* @version $Id: FilenameUtils.java,v 1.31 2004/12/04 19:28:40 scolebourne Exp $
82+
* @version $Id: FilenameUtils.java,v 1.32 2004/12/10 22:36:56 scolebourne Exp $
8383
* @since Commons IO 1.1
8484
*/
8585
public class FilenameUtils {
@@ -824,6 +824,7 @@ public static boolean isExtension(String filename, Collection extensions) {
824824
* The wildcard matcher uses the characters '?' and '*' to represent a
825825
* single or multiple wildcard characters.
826826
* This is the same as often found on Dos/Unix command lines.
827+
* The extension check is case sensitive on Unix and case insensitive on Windows.
827828
* <pre>
828829
* wildcardMatch("c.txt", "*.txt") --> true
829830
* wildcardMatch("c.txt", "*.jpg") --> false
@@ -837,24 +838,38 @@ public static boolean isExtension(String filename, Collection extensions) {
837838
* @return true if the filename matches the wilcard string
838839
*/
839840
public static boolean wildcardMatch(String filename, String wildcardMatcher) {
841+
if (filename == null && wildcardMatcher == null) {
842+
return true;
843+
}
844+
if (filename == null || wildcardMatcher == null) {
845+
return false;
846+
}
847+
if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) {
848+
filename = filename.toLowerCase();
849+
wildcardMatcher = wildcardMatcher.toLowerCase();
850+
}
840851
String[] wcs = splitOnTokens(wildcardMatcher);
841-
852+
boolean anyChars = false;
842853
int textIdx = 0;
843854
int wcsIdx = 0;
844-
boolean anyChars = false;
845855

846856
// loop whilst tokens and text left to process
847857
while (wcsIdx < wcs.length && textIdx < filename.length()) {
848858

849-
// ? so move to next text char
850859
if (wcs[wcsIdx].equals("?")) {
860+
// ? so move to next text char
851861
textIdx++;
852-
} else if (!wcs[wcsIdx].equals("*")) {
862+
anyChars = false;
863+
864+
} else if (wcs[wcsIdx].equals("*")) {
865+
// set any chars status
866+
anyChars = true;
867+
868+
} else {
853869
// matching text token
854870
if (anyChars) {
855871
// any chars then try to locate text token
856872
textIdx = filename.indexOf(wcs[wcsIdx], textIdx);
857-
858873
if (textIdx == -1) {
859874
// token not found
860875
return false;
@@ -869,11 +884,9 @@ public static boolean wildcardMatch(String filename, String wildcardMatcher) {
869884

870885
// matched text token, move text index to end of matched token
871886
textIdx += wcs[wcsIdx].length();
887+
anyChars = false;
872888
}
873889

874-
// set any chars status
875-
anyChars = wcs[wcsIdx].equals("*");
876-
877890
wcsIdx++;
878891
}
879892

@@ -901,24 +914,24 @@ public static boolean wildcardMatch(String filename, String wildcardMatcher) {
901914
// used by wildcardMatch
902915
// package level so a unit test may run on this
903916
static String[] splitOnTokens(String text) {
904-
char[] array = text.toCharArray();
905917
if (text.indexOf("?") == -1 && text.indexOf("*") == -1) {
906918
return new String[] { text };
907919
}
908920

921+
char[] array = text.toCharArray();
909922
ArrayList list = new ArrayList();
910923
StringBuffer buffer = new StringBuffer();
911924
for (int i = 0; i < array.length; i++) {
912-
if(array[i] == '?' || array[i] == '*') {
913-
if(buffer.length() != 0) {
914-
list.add(buffer.toString());
915-
buffer.setLength(0);
916-
}
917-
list.add(new String( new char[] { array[i] } ));
918-
} else {
919-
buffer.append(array[i]);
920-
}
921-
}
925+
if (array[i] == '?' || array[i] == '*') {
926+
if (buffer.length() != 0) {
927+
list.add(buffer.toString());
928+
buffer.setLength(0);
929+
}
930+
list.add(new String(new char[] { array[i] }));
931+
} else {
932+
buffer.append(array[i]);
933+
}
934+
}
922935
if (buffer.length() != 0) {
923936
list.add(buffer.toString());
924937
}

src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616
package org.apache.commons.io;
1717

18+
import java.io.File;
19+
1820
import junit.framework.TestCase;
1921

2022
public class FilenameUtilsWildcardTestCase extends TestCase {
2123

24+
private static final boolean WINDOWS = (File.separatorChar == '\\');
25+
2226
public FilenameUtilsWildcardTestCase(String name) {
2327
super(name);
2428
}
@@ -28,6 +32,9 @@ public FilenameUtilsWildcardTestCase(String name) {
2832
// FilenameUtils.wildcardMatch(String,String)
2933

3034
public void testMatch() {
35+
assertEquals(false, FilenameUtils.wildcardMatch(null, "Foo") );
36+
assertEquals(false, FilenameUtils.wildcardMatch("Foo", null) );
37+
assertEquals(true, FilenameUtils.wildcardMatch(null, null) );
3138
assertTrue( FilenameUtils.wildcardMatch("Foo", "Foo") );
3239
assertTrue( FilenameUtils.wildcardMatch("", "") );
3340
assertTrue( FilenameUtils.wildcardMatch("Foo", "Fo*") );
@@ -39,6 +46,7 @@ public void testMatch() {
3946
assertTrue( FilenameUtils.wildcardMatch("Adobe Acrobat Installer", "Ad*er") );
4047
assertTrue( FilenameUtils.wildcardMatch("Foo", "*Foo") );
4148
assertTrue( FilenameUtils.wildcardMatch("Foo", "Foo*") );
49+
assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO", "Foo*") );
4250
}
4351

4452
public void testSplitOnTokens() {

0 commit comments

Comments
 (0)