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 */
8585public 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 }
0 commit comments