commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/io/src/test/org/apache/commons/io/find WildcardUtilsTest.java
Date Mon, 25 Oct 2004 22:55:28 GMT
bayard      2004/10/25 15:55:28

  Modified:    io/src/java/org/apache/commons/io/filefilter
                        WildcardFilter.java
               io/src/java/org/apache/commons/io/find FindingFilter.java
  Added:       io/src/java/org/apache/commons/io WildcardUtils.java
               io/src/test/org/apache/commons/io WildcardUtilsTest.java
  Removed:     io/src/java/org/apache/commons/io/find WildcardUtils.java
               io/src/test/org/apache/commons/io/find
                        WildcardUtilsTest.java
  Log:
  moved WildcardUtils up a directory as it is used in two sub-packages of IO
  
  Revision  Changes    Path
  1.1                  jakarta-commons/io/src/java/org/apache/commons/io/WildcardUtils.java
  
  Index: WildcardUtils.java
  ===================================================================
  /*
   * Copyright 2001-2004 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.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io;
  
  import java.util.ArrayList;
  
  /**
   * An implementation of Wildcard logic, as seen on command lines 
   * on UNIX and DOS.
   */
  public class WildcardUtils {
  
      /**
       * See if a particular piece of text, often a filename, 
       * matches to a specified wildcard. 
       */
      public static boolean match(String text, String wildcard) {
          String[] wcs = splitOnTokens(wildcard);
    
          int textIdx = 0;
          int wcsIdx = 0;
          boolean anyChars = false;
    
          // loop whilst tokens and text left to process
          while (wcsIdx < wcs.length && textIdx < text.length()) {
    
              // ? so move to next text char
              if (wcs[wcsIdx].equals("?")) {
                  textIdx++;
              } else
              if (!wcs[wcsIdx].equals("*")) {
                  // matching text token
                  if (anyChars) {
                      // any chars then try to locate text token
                      textIdx = text.indexOf(wcs[wcsIdx], textIdx);
    
                      if (textIdx == -1) {
                          // token not found
                          return false;
                      }
                  } else {
                      // matching from current position
                      if (!text.startsWith(wcs[wcsIdx], textIdx)) {
                          // couldnt match token
                          return false;
                      }
                  }
    
                  // matched text token, move text index to end of matched token
                  textIdx += wcs[wcsIdx].length();
              }
    
              // set any chars status
              anyChars = wcs[wcsIdx].equals("*");
    
              wcsIdx++;
          }
  
          // didnt match all wildcards
          if (wcsIdx < wcs.length) {
              // ok if one remaining and wildcard or empty
              if (wcsIdx + 1 != wcs.length || !(wcs[wcsIdx].equals("*") || wcs[wcsIdx].equals(""))
) {
                  return false;
              }
          }
    
          // ran out of text chars
          if (textIdx > text.length()) {
             return false;
          }
    
          // didnt match all text chars, only ok if any chars set
          if (textIdx < text.length() && !anyChars) {
              return false;
          }
    
          return true;
      }
  
      // 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 };
          }
  
          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(buffer.length() != 0) {
              list.add(buffer.toString());
          }
  
          return (String[]) list.toArray(new String[0]);
      }
          
  
  }
  
  
  
  1.3       +1 -1      jakarta-commons/io/src/java/org/apache/commons/io/filefilter/WildcardFilter.java
  
  Index: WildcardFilter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/filefilter/WildcardFilter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- WildcardFilter.java	25 Oct 2004 22:53:26 -0000	1.2
  +++ WildcardFilter.java	25 Oct 2004 22:55:28 -0000	1.3
  @@ -2,7 +2,7 @@
   
   import java.io.File;
   import java.util.List;
  -import org.apache.commons.io.find.WildcardUtils;
  +import org.apache.commons.io.WildcardUtils;
   
   /**
    * Filters files using supplied wildcard(s).
  
  
  
  1.3       +2 -0      jakarta-commons/io/src/java/org/apache/commons/io/find/FindingFilter.java
  
  Index: FindingFilter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/find/FindingFilter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FindingFilter.java	24 Oct 2004 00:06:34 -0000	1.2
  +++ FindingFilter.java	25 Oct 2004 22:55:28 -0000	1.3
  @@ -25,6 +25,8 @@
   import java.util.regex.Pattern;
   import java.util.regex.Matcher;
   
  +import org.apache.commons.io.WildcardUtils;
  +
   /**
    * This is where most of the find functionality occurs. Nearly every option 
    * to find is mapped to a FileFilter, which are then chained together inside 
  
  
  
  1.1                  jakarta-commons/io/src/test/org/apache/commons/io/WildcardUtilsTest.java
  
  Index: WildcardUtilsTest.java
  ===================================================================
  /*
   * Copyright 2001-2004 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.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.io;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  public class WildcardUtilsTest extends TestCase {
  
      public WildcardUtilsTest(String name) {
          super(name);
      }
  
      //-----------------------------------------------------------------------
      // To test: 
      //   WildcardUtils.match(String,String)
  
      public void testMatch() {
          assertTrue( WildcardUtils.match("Foo", "Foo") );
          assertTrue( WildcardUtils.match("", "") );
          assertTrue( WildcardUtils.match("Foo", "Fo*") );
          assertTrue( WildcardUtils.match("Foo", "Fo?") );
          assertTrue( WildcardUtils.match("Foo Bar and Catflap", "Fo*") );
          assertTrue( WildcardUtils.match("New Bookmarks", "N?w ?o?k??r?s") );
          assertFalse( WildcardUtils.match("Foo", "Bar") );
          assertTrue( WildcardUtils.match("Foo Bar Foo", "F*o Bar*") );
          assertTrue( WildcardUtils.match("Adobe Acrobat Installer", "Ad*er") );
          assertTrue( WildcardUtils.match("Foo", "*Foo") );
          assertTrue( WildcardUtils.match("Foo", "Foo*") );
      }
  
      public void testSplitOnTokens() {
          assertArrayEquals( new String[] { "Ad", "*", "er" }, WildcardUtils.splitOnTokens("Ad*er")
);
          assertArrayEquals( new String[] { "Ad", "?", "er" }, WildcardUtils.splitOnTokens("Ad?er")
);
          assertArrayEquals( new String[] { "Test", "*", "?", "One" }, WildcardUtils.splitOnTokens("Test*?One")
);
          assertArrayEquals( new String[] { "*", "*", "*", "*" }, WildcardUtils.splitOnTokens("****")
);
          assertArrayEquals( new String[] { "*", "?", "?", "*" }, WildcardUtils.splitOnTokens("*??*")
);
          assertArrayEquals( new String[] { "*", "?", "?", "*" }, WildcardUtils.splitOnTokens("*??*")
);
          assertArrayEquals( new String[] { "h", "?", "?", "*" }, WildcardUtils.splitOnTokens("h??*")
);
          assertArrayEquals( new String[] { "" }, WildcardUtils.splitOnTokens("") );
      }
  
      private void assertArrayEquals(Object[] a1, Object[] a2) {
          assertEquals(a1.length, a2.length);
          for(int i=0; i<a1.length; i++) {
              assertEquals(a1[i], a2[i]);
          }
      }
  
      private void assertMatch(String text, String wildcard, boolean expected) {
          assertEquals(text + " " + wildcard, expected, WildcardUtils.match(text, wildcard));
      }
  
      // A separate set of tests, added to this batch
      public void testMatch2() {
          assertMatch("log.txt", "log.txt", true);
          assertMatch("log.txt1", "log.txt", false);
  
          assertMatch("log.txt", "log.txt*", true);
          assertMatch("log.txt", "log.txt*1", false);
          assertMatch("log.txt", "*log.txt*", true);
  
          assertMatch("log.txt", "*.txt", true);
          assertMatch("txt.log", "*.txt", false);
          assertMatch("config.ini", "*.ini", true);
  
          assertMatch("config.txt.bak", "con*.txt", false);
  
          assertMatch("log.txt9", "*.txt?", true);
          assertMatch("log.txt", "*.txt?", false);
  
          assertMatch("progtestcase.java~5~", "*test*.java~*~", true);
          assertMatch("progtestcase.java;5~", "*test*.java~*~", false);
          assertMatch("progtestcase.java~5", "*test*.java~*~", false);
  
          assertMatch("log.txt", "log.*", true);
  
          assertMatch("log.txt", "log?*", true);
  
          assertMatch("log.txt12", "log.txt??", true);
      }
  
  }
  
  
  

---------------------------------------------------------------------
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