Author: scolebourne
Date: Sun Jun 25 16:19:03 2006
New Revision: 417091
URL: http://svn.apache.org/viewvc?rev=417091&view=rev
Log:
Add methods to use IOCase case-sensitivity
Modified:
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
Modified:
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java?rev=417091&r1=417090&r2=417091&view=diff
==============================================================================
---
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
(original)
+++
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
Sun Jun 25 16:19:03 2006
@@ -901,9 +901,10 @@
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
+ * @see IOCase#SENSITIVE
*/
public static boolean equals(String filename1, String filename2) {
- return equals(filename1, filename2, false, false);
+ return equals(filename1, filename2, false, IOCase.SENSITIVE);
}
/**
@@ -915,9 +916,10 @@
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
+ * @see IOCase#SYSTEM
*/
public static boolean equalsOnSystem(String filename1, String filename2) {
- return equals(filename1, filename2, true, false);
+ return equals(filename1, filename2, false, IOCase.SYSTEM);
}
//-----------------------------------------------------------------------
@@ -930,9 +932,10 @@
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
+ * @see IOCase#SENSITIVE
*/
public static boolean equalsNormalized(String filename1, String filename2)
{
- return equals(filename1, filename2, false, true);
+ return equals(filename1, filename2, true, IOCase.SENSITIVE);
}
/**
@@ -946,41 +949,38 @@
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
+ * @see IOCase#SYSTEM
*/
public static boolean equalsNormalizedOnSystem(String filename1, String
filename2) {
- return equals(filename1, filename2, true, true);
+ return equals(filename1, filename2, true, IOCase.SYSTEM);
}
/**
- * Checks whether two filenames are equal after both have been normalized
- * and optionally using the case rules of the system.
- * <p>
- * Both filenames are first passed to [EMAIL PROTECTED]
#normalize(String)}.
+ * Checks whether two filenames are equal, optionally normalizing and
providing
+ * control over the case-sensitivity.
*
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
- * @param system whether to use the system (windows or unix)
* @param normalized whether to normalize the filenames
+ * @param caseSensitivity what case sensitivity rule to use, null means
case-sensitive
* @return true if the filenames are equal, null equals null
+ * @since Commons IO 1.3
*/
- private static boolean equals(
+ public static boolean equals(
String filename1, String filename2,
- boolean system, boolean normalized) {
- if (filename1 == filename2) {
- return true;
- }
+ boolean normalized, IOCase caseSensitivity) {
+
if (filename1 == null || filename2 == null) {
- return false;
+ return filename1 == filename2;
}
if (normalized) {
filename1 = normalize(filename1);
filename2 = normalize(filename2);
}
- if (system && isSystemWindows()) {
- return filename1.equalsIgnoreCase(filename2);
- } else {
- return filename1.equals(filename2);
+ if (caseSensitivity == null) {
+ caseSensitivity = IOCase.SENSITIVE;
}
+ return caseSensitivity.checkEquals(filename1, filename2);
}
//-----------------------------------------------------------------------
@@ -1068,7 +1068,7 @@
* The wildcard matcher uses the characters '?' and '*' to represent a
* single or multiple wildcard characters.
* This is the same as often found on Dos/Unix command lines.
- * The extension check is case-sensitive.
+ * The check is case-sensitive always.
* <pre>
* wildcardMatch("c.txt", "*.txt") --> true
* wildcardMatch("c.txt", "*.jpg") --> false
@@ -1080,9 +1080,10 @@
* @param filename the filename to match on
* @param wildcardMatcher the wildcard string to match against
* @return true if the filename matches the wilcard string
+ * @see IOCase#SENSITIVE
*/
public static boolean wildcardMatch(String filename, String
wildcardMatcher) {
- return wildcardMatch(filename, wildcardMatcher, false);
+ return wildcardMatch(filename, wildcardMatcher, IOCase.SENSITIVE);
}
/**
@@ -1104,32 +1105,34 @@
* @param filename the filename to match on
* @param wildcardMatcher the wildcard string to match against
* @return true if the filename matches the wilcard string
+ * @see IOCase#SYSTEM
*/
public static boolean wildcardMatchOnSystem(String filename, String
wildcardMatcher) {
- return wildcardMatch(filename, wildcardMatcher, true);
+ return wildcardMatch(filename, wildcardMatcher, IOCase.SYSTEM);
}
/**
- * Checks a filename to see if it matches the specified wildcard matcher.
+ * Checks a filename to see if it matches the specified wildcard matcher
+ * allowing control over case-sensitivity.
* <p>
* The wildcard matcher uses the characters '?' and '*' to represent a
* single or multiple wildcard characters.
*
* @param filename the filename to match on
* @param wildcardMatcher the wildcard string to match against
- * @param system whether to use the system (windows or unix)
+ * @param caseSensitivity what case sensitivity rule to use, null means
case-sensitive
* @return true if the filename matches the wilcard string
+ * @since Commons IO 1.3
*/
- private static boolean wildcardMatch(String filename, String
wildcardMatcher, boolean system) {
+ public static boolean wildcardMatch(String filename, String
wildcardMatcher, IOCase caseSensitivity) {
if (filename == null && wildcardMatcher == null) {
return true;
}
if (filename == null || wildcardMatcher == null) {
return false;
}
- if (system && isSystemWindows()) {
- filename = filename.toLowerCase();
- wildcardMatcher = wildcardMatcher.toLowerCase();
+ if (caseSensitivity == null) {
+ caseSensitivity = IOCase.SENSITIVE;
}
String[] wcs = splitOnTokens(wildcardMatcher);
boolean anyChars = false;
@@ -1176,7 +1179,7 @@
}
} else {
// matching from current position
- if (!filename.startsWith(wcs[wcsIdx], textIdx)) {
+ if (!caseSensitivity.checkRegionMatches(filename,
textIdx, wcs[wcsIdx])) {
// couldnt match token
break;
}
Modified:
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java?rev=417091&r1=417090&r2=417091&view=diff
==============================================================================
---
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
(original)
+++
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
Sun Jun 25 16:19:03 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 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.
@@ -781,6 +781,13 @@
assertEquals(WINDOWS,
FilenameUtils.equalsNormalizedOnSystem("file.txt", "FILE.TXT"));
assertEquals(true,
FilenameUtils.equalsNormalizedOnSystem("a\\b\\file.txt", "a/b/file.txt"));
assertEquals(false, FilenameUtils.equalsNormalizedOnSystem("a/b/",
"a/b"));
+ }
+
+ public void testEquals_fullControl() {
+ assertEquals(false, FilenameUtils.equals("file.txt", "FILE.TXT", true,
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.equals("file.txt", "FILE.TXT", true,
IOCase.INSENSITIVE));
+ assertEquals(WINDOWS, FilenameUtils.equals("file.txt", "FILE.TXT",
true, IOCase.SYSTEM));
+ assertEquals(false, FilenameUtils.equals("file.txt", "FILE.TXT", true,
null));
}
//-----------------------------------------------------------------------
Modified:
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java?rev=417091&r1=417090&r2=417091&view=diff
==============================================================================
---
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
(original)
+++
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
Sun Jun 25 16:19:03 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2004,2006 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.
@@ -65,6 +65,26 @@
assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo", "*Foo"));
assertEquals(true, FilenameUtils.wildcardMatchOnSystem("Foo", "Foo*"));
assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOO",
"Foo*"));
+ }
+
+ public void testMatchCaseSpecified() {
+ assertEquals(false, FilenameUtils.wildcardMatch(null, "Foo",
IOCase.SENSITIVE));
+ assertEquals(false, FilenameUtils.wildcardMatch("Foo", null,
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch(null, null,
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("", "",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo*",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Fo?",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar and Catflap",
"Fo*", IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("New Bookmarks", "N?w
?o?k??r?s", IOCase.SENSITIVE));
+ assertEquals(false, FilenameUtils.wildcardMatch("Foo", "Bar",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo Bar Foo", "F*o
Bar*", IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Adobe Acrobat
Installer", "Ad*er", IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo", "*Foo",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("Foo", "Foo*",
IOCase.SENSITIVE));
+ assertEquals(false, FilenameUtils.wildcardMatch("FOO", "Foo*",
IOCase.SENSITIVE));
+ assertEquals(true, FilenameUtils.wildcardMatch("FOO", "Foo*",
IOCase.INSENSITIVE));
+ assertEquals(WINDOWS, FilenameUtils.wildcardMatch("FOO", "Foo*",
IOCase.SYSTEM));
}
public void testSplitOnTokens() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]