commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/io/src/test/org/apache/commons/io FilenameUtilsWildcardTestCase.java
Date Fri, 10 Dec 2004 22:36:57 GMT
scolebourne    2004/12/10 14:36:57

  Modified:    io/src/java/org/apache/commons/io FilenameUtils.java
               io/src/test/org/apache/commons/io
                        FilenameUtilsWildcardTestCase.java
  Log:
  Convert wildcard method to match based on OS case sensitivity
  
  Revision  Changes    Path
  1.32      +33 -20    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.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- FilenameUtils.java	4 Dec 2004 19:28:40 -0000	1.31
  +++ FilenameUtils.java	10 Dec 2004 22:36:56 -0000	1.32
  @@ -824,6 +824,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 on Unix and case insensitive on Windows.
        * <pre>
        * wildcardMatch("c.txt", "*.txt")      --> true
        * wildcardMatch("c.txt", "*.jpg")      --> false
  @@ -837,24 +838,38 @@
        * @return true if the filename matches the wilcard string
        */
       public static boolean wildcardMatch(String filename, String wildcardMatcher) {
  +        if (filename == null && wildcardMatcher == null) {
  +            return true;
  +        }
  +        if (filename == null || wildcardMatcher == null) {
  +            return false;
  +        }
  +        if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) {
  +            filename = filename.toLowerCase();
  +            wildcardMatcher = wildcardMatcher.toLowerCase();
  +        }
           String[] wcs = splitOnTokens(wildcardMatcher);
  -  
  +        boolean anyChars = false;
           int textIdx = 0;
           int wcsIdx = 0;
  -        boolean anyChars = false;
     
           // loop whilst tokens and text left to process
           while (wcsIdx < wcs.length && textIdx < filename.length()) {
     
  -            // ? so move to next text char
               if (wcs[wcsIdx].equals("?")) {
  +                // ? so move to next text char
                   textIdx++;
  -            } else if (!wcs[wcsIdx].equals("*")) {
  +                anyChars = false;
  +                
  +            } else if (wcs[wcsIdx].equals("*")) {
  +                // set any chars status
  +                anyChars = true;
  +                
  +            } else {
                   // matching text token
                   if (anyChars) {
                       // any chars then try to locate text token
                       textIdx = filename.indexOf(wcs[wcsIdx], textIdx);
  -  
                       if (textIdx == -1) {
                           // token not found
                           return false;
  @@ -869,11 +884,9 @@
     
                   // matched text token, move text index to end of matched token
                   textIdx += wcs[wcsIdx].length();
  +                anyChars = false;
               }
     
  -            // set any chars status
  -            anyChars = wcs[wcsIdx].equals("*");
  -  
               wcsIdx++;
           }
   
  @@ -901,24 +914,24 @@
       // 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 };
           }
   
  +        char[] array = text.toCharArray();
           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 (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());
           }
  
  
  
  1.2       +8 -0      jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java
  
  Index: FilenameUtilsWildcardTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsWildcardTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FilenameUtilsWildcardTestCase.java	31 Oct 2004 04:17:34 -0000	1.1
  +++ FilenameUtilsWildcardTestCase.java	10 Dec 2004 22:36:57 -0000	1.2
  @@ -15,10 +15,14 @@
    */
   package org.apache.commons.io;
   
  +import java.io.File;
  +
   import junit.framework.TestCase;
   
   public class FilenameUtilsWildcardTestCase extends TestCase {
   
  +    private static final boolean WINDOWS = (File.separatorChar == '\\');
  +
       public FilenameUtilsWildcardTestCase(String name) {
           super(name);
       }
  @@ -28,6 +32,9 @@
       //   FilenameUtils.wildcardMatch(String,String)
   
       public void testMatch() {
  +        assertEquals(false, FilenameUtils.wildcardMatch(null, "Foo") );
  +        assertEquals(false, FilenameUtils.wildcardMatch("Foo", null) );
  +        assertEquals(true, FilenameUtils.wildcardMatch(null, null) );
           assertTrue( FilenameUtils.wildcardMatch("Foo", "Foo") );
           assertTrue( FilenameUtils.wildcardMatch("", "") );
           assertTrue( FilenameUtils.wildcardMatch("Foo", "Fo*") );
  @@ -39,6 +46,7 @@
           assertTrue( FilenameUtils.wildcardMatch("Adobe Acrobat Installer", "Ad*er") );
           assertTrue( FilenameUtils.wildcardMatch("Foo", "*Foo") );
           assertTrue( FilenameUtils.wildcardMatch("Foo", "Foo*") );
  +        assertEquals(WINDOWS,  FilenameUtils.wildcardMatch("FOO", "Foo*") );
       }
   
       public void testSplitOnTokens() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message