Skip to content

Commit 07751ea

Browse files
author
Stephen Colebourne
committed
Add methods to use IOCase case-sensitivity
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@417091 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9af996d commit 07751ea

3 files changed

Lines changed: 61 additions & 31 deletions

File tree

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -901,9 +901,10 @@ public static String removeExtension(String filename) {
901901
* @param filename1 the first filename to query, may be null
902902
* @param filename2 the second filename to query, may be null
903903
* @return true if the filenames are equal, null equals null
904+
* @see IOCase#SENSITIVE
904905
*/
905906
public static boolean equals(String filename1, String filename2) {
906-
return equals(filename1, filename2, false, false);
907+
return equals(filename1, filename2, false, IOCase.SENSITIVE);
907908
}
908909

909910
/**
@@ -915,9 +916,10 @@ public static boolean equals(String filename1, String filename2) {
915916
* @param filename1 the first filename to query, may be null
916917
* @param filename2 the second filename to query, may be null
917918
* @return true if the filenames are equal, null equals null
919+
* @see IOCase#SYSTEM
918920
*/
919921
public static boolean equalsOnSystem(String filename1, String filename2) {
920-
return equals(filename1, filename2, true, false);
922+
return equals(filename1, filename2, false, IOCase.SYSTEM);
921923
}
922924

923925
//-----------------------------------------------------------------------
@@ -930,9 +932,10 @@ public static boolean equalsOnSystem(String filename1, String filename2) {
930932
* @param filename1 the first filename to query, may be null
931933
* @param filename2 the second filename to query, may be null
932934
* @return true if the filenames are equal, null equals null
935+
* @see IOCase#SENSITIVE
933936
*/
934937
public static boolean equalsNormalized(String filename1, String filename2) {
935-
return equals(filename1, filename2, false, true);
938+
return equals(filename1, filename2, true, IOCase.SENSITIVE);
936939
}
937940

938941
/**
@@ -946,41 +949,38 @@ public static boolean equalsNormalized(String filename1, String filename2) {
946949
* @param filename1 the first filename to query, may be null
947950
* @param filename2 the second filename to query, may be null
948951
* @return true if the filenames are equal, null equals null
952+
* @see IOCase#SYSTEM
949953
*/
950954
public static boolean equalsNormalizedOnSystem(String filename1, String filename2) {
951-
return equals(filename1, filename2, true, true);
955+
return equals(filename1, filename2, true, IOCase.SYSTEM);
952956
}
953957

954958
/**
955-
* Checks whether two filenames are equal after both have been normalized
956-
* and optionally using the case rules of the system.
957-
* <p>
958-
* Both filenames are first passed to {@link #normalize(String)}.
959+
* Checks whether two filenames are equal, optionally normalizing and providing
960+
* control over the case-sensitivity.
959961
*
960962
* @param filename1 the first filename to query, may be null
961963
* @param filename2 the second filename to query, may be null
962-
* @param system whether to use the system (windows or unix)
963964
* @param normalized whether to normalize the filenames
965+
* @param caseSensitivity what case sensitivity rule to use, null means case-sensitive
964966
* @return true if the filenames are equal, null equals null
967+
* @since Commons IO 1.3
965968
*/
966-
private static boolean equals(
969+
public static boolean equals(
967970
String filename1, String filename2,
968-
boolean system, boolean normalized) {
969-
if (filename1 == filename2) {
970-
return true;
971-
}
971+
boolean normalized, IOCase caseSensitivity) {
972+
972973
if (filename1 == null || filename2 == null) {
973-
return false;
974+
return filename1 == filename2;
974975
}
975976
if (normalized) {
976977
filename1 = normalize(filename1);
977978
filename2 = normalize(filename2);
978979
}
979-
if (system && isSystemWindows()) {
980-
return filename1.equalsIgnoreCase(filename2);
981-
} else {
982-
return filename1.equals(filename2);
980+
if (caseSensitivity == null) {
981+
caseSensitivity = IOCase.SENSITIVE;
983982
}
983+
return caseSensitivity.checkEquals(filename1, filename2);
984984
}
985985

986986
//-----------------------------------------------------------------------
@@ -1068,7 +1068,7 @@ public static boolean isExtension(String filename, Collection extensions) {
10681068
* The wildcard matcher uses the characters '?' and '*' to represent a
10691069
* single or multiple wildcard characters.
10701070
* This is the same as often found on Dos/Unix command lines.
1071-
* The extension check is case-sensitive.
1071+
* The check is case-sensitive always.
10721072
* <pre>
10731073
* wildcardMatch("c.txt", "*.txt") --> true
10741074
* wildcardMatch("c.txt", "*.jpg") --> false
@@ -1080,9 +1080,10 @@ public static boolean isExtension(String filename, Collection extensions) {
10801080
* @param filename the filename to match on
10811081
* @param wildcardMatcher the wildcard string to match against
10821082
* @return true if the filename matches the wilcard string
1083+
* @see IOCase#SENSITIVE
10831084
*/
10841085
public static boolean wildcardMatch(String filename, String wildcardMatcher) {
1085-
return wildcardMatch(filename, wildcardMatcher, false);
1086+
return wildcardMatch(filename, wildcardMatcher, IOCase.SENSITIVE);
10861087
}
10871088

10881089
/**
@@ -1104,32 +1105,34 @@ public static boolean wildcardMatch(String filename, String wildcardMatcher) {
11041105
* @param filename the filename to match on
11051106
* @param wildcardMatcher the wildcard string to match against
11061107
* @return true if the filename matches the wilcard string
1108+
* @see IOCase#SYSTEM
11071109
*/
11081110
public static boolean wildcardMatchOnSystem(String filename, String wildcardMatcher) {
1109-
return wildcardMatch(filename, wildcardMatcher, true);
1111+
return wildcardMatch(filename, wildcardMatcher, IOCase.SYSTEM);
11101112
}
11111113

11121114
/**
1113-
* Checks a filename to see if it matches the specified wildcard matcher.
1115+
* Checks a filename to see if it matches the specified wildcard matcher
1116+
* allowing control over case-sensitivity.
11141117
* <p>
11151118
* The wildcard matcher uses the characters '?' and '*' to represent a
11161119
* single or multiple wildcard characters.
11171120
*
11181121
* @param filename the filename to match on
11191122
* @param wildcardMatcher the wildcard string to match against
1120-
* @param system whether to use the system (windows or unix)
1123+
* @param caseSensitivity what case sensitivity rule to use, null means case-sensitive
11211124
* @return true if the filename matches the wilcard string
1125+
* @since Commons IO 1.3
11221126
*/
1123-
private static boolean wildcardMatch(String filename, String wildcardMatcher, boolean system) {
1127+
public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity) {
11241128
if (filename == null && wildcardMatcher == null) {
11251129
return true;
11261130
}
11271131
if (filename == null || wildcardMatcher == null) {
11281132
return false;
11291133
}
1130-
if (system && isSystemWindows()) {
1131-
filename = filename.toLowerCase();
1132-
wildcardMatcher = wildcardMatcher.toLowerCase();
1134+
if (caseSensitivity == null) {
1135+
caseSensitivity = IOCase.SENSITIVE;
11331136
}
11341137
String[] wcs = splitOnTokens(wildcardMatcher);
11351138
boolean anyChars = false;
@@ -1176,7 +1179,7 @@ private static boolean wildcardMatch(String filename, String wildcardMatcher, bo
11761179
}
11771180
} else {
11781181
// matching from current position
1179-
if (!filename.startsWith(wcs[wcsIdx], textIdx)) {
1182+
if (!caseSensitivity.checkRegionMatches(filename, textIdx, wcs[wcsIdx])) {
11801183
// couldnt match token
11811184
break;
11821185
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 1999-2005 The Apache Software Foundation.
2+
* Copyright 1999-2006 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -783,6 +783,13 @@ public void testEqualsNormalizedOnSystem() {
783783
assertEquals(false, FilenameUtils.equalsNormalizedOnSystem("a/b/", "a/b"));
784784
}
785785

786+
public void testEquals_fullControl() {
787+
assertEquals(false, FilenameUtils.equals("file.txt", "FILE.TXT", true, IOCase.SENSITIVE));
788+
assertEquals(true, FilenameUtils.equals("file.txt", "FILE.TXT", true, IOCase.INSENSITIVE));
789+
assertEquals(WINDOWS, FilenameUtils.equals("file.txt", "FILE.TXT", true, IOCase.SYSTEM));
790+
assertEquals(false, FilenameUtils.equals("file.txt", "FILE.TXT", true, null));
791+
}
792+
786793
//-----------------------------------------------------------------------
787794
public void testIsExtension() {
788795
assertEquals(false, FilenameUtils.isExtension(null, (String) null));

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2001-2004 The Apache Software Foundation.
2+
* Copyright 2001-2004,2006 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,6 +67,26 @@ public void testMatchOnSystem() {
6767
assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOO", "Foo*"));
6868
}
6969

70+
public void testMatchCaseSpecified() {
71+
assertEquals(false, FilenameUtils.wildcardMatch(null, "Foo", IOCase.SENSITIVE));
72+
assertEquals(false, FilenameUtils.wildcardMatch("Foo", null, IOCase.SENSITIVE));
73+
assertEquals(true, FilenameUtils.wildcardMatch(null, null, IOCase.SENSITIVE));
74+
assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo", IOCase.SENSITIVE));
75+
assertEquals(true, FilenameUtils.wildcardMatch("", "", IOCase.SENSITIVE));
76+
assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo*", IOCase.SENSITIVE));
77+
assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo?", IOCase.SENSITIVE));
78+
assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar and Catflap", "Fo*", IOCase.SENSITIVE));
79+
assertEquals(true, FilenameUtils.wildcardMatch("New Bookmarks", "N?w ?o?k??r?s", IOCase.SENSITIVE));
80+
assertEquals(false, FilenameUtils.wildcardMatch("Foo", "Bar", IOCase.SENSITIVE));
81+
assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar Foo", "F*o Bar*", IOCase.SENSITIVE));
82+
assertEquals(true, FilenameUtils.wildcardMatch("Adobe Acrobat Installer", "Ad*er", IOCase.SENSITIVE));
83+
assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo", IOCase.SENSITIVE));
84+
assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*", IOCase.SENSITIVE));
85+
assertEquals(false, FilenameUtils.wildcardMatch("FOO", "Foo*", IOCase.SENSITIVE));
86+
assertEquals(true, FilenameUtils.wildcardMatch("FOO", "Foo*", IOCase.INSENSITIVE));
87+
assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO", "Foo*", IOCase.SYSTEM));
88+
}
89+
7090
public void testSplitOnTokens() {
7191
assertArrayEquals( new String[] { "Ad", "*", "er" }, FilenameUtils.splitOnTokens("Ad*er") );
7292
assertArrayEquals( new String[] { "Ad", "?", "er" }, FilenameUtils.splitOnTokens("Ad?er") );

0 commit comments

Comments
 (0)