commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r584278 - in /commons/proper/io/trunk: ./ src/java/org/apache/commons/io/filefilter/ src/test/org/apache/commons/io/filefilter/
Date Fri, 12 Oct 2007 21:31:07 GMT
Author: niallp
Date: Fri Oct 12 14:30:52 2007
New Revision: 584278

URL: http://svn.apache.org/viewvc?rev=584278&view=rev
Log:
IO-74 Refactor RegexFilter - add ability to construct instances with either pattern flags
or case sensitivity. Also simplify by removing the array/list of String patterns constructors
since adding all variants with flags/case sensitivity would be cumbersome and also the logic
is limited to "OR" and some users may want "AND" - AND/OR logic can be achieved by combining
RegexFilter instances using the AndFileFilter and OrFileFilter implementations.

Modified:
    commons/proper/io/trunk/RELEASE-NOTES.txt
    commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java
    commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/RegexFilter.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=584278&r1=584277&r2=584278&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Fri Oct 12 14:30:52 2007
@@ -38,6 +38,8 @@
 - SuffixFileFilter [IO-126]
   - Add faciltiy to specify case sensitivty on suffix matching
 
+- RegexFilter [IO-74]
+  - Add new regular expression file filter implementation
 
 Feedback
 --------

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java?rev=584278&r1=584277&r2=584278&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java
Fri Oct 12 14:30:52 2007
@@ -20,6 +20,7 @@
 import java.io.FileFilter;
 import java.io.FilenameFilter;
 import java.util.Date;
+import org.apache.commons.io.IOCase;
 
 /**
  * Useful utilities for working with file filters. It provides access to all
@@ -278,6 +279,43 @@
         IOFileFilter minimumFilter = new SizeFileFilter(minSizeInclusive, true);
         IOFileFilter maximumFilter = new SizeFileFilter(maxSizeInclusive + 1L, false);
         return new AndFileFilter(minimumFilter, maximumFilter);
+    }
+
+    /**
+     * Returns a filter that uses a regular expression to filter file names.
+     *
+     * @param pattern the regular expression pattern
+     * @return an appropriately configured IOFileFilter
+     * @since Commons IO 1.4
+     */
+    public static IOFileFilter regex(String pattern) {
+        return new RegexFilter(pattern);
+    }
+
+    /**
+     * Returns a filter that uses a regular expression to filter file names with
+     * the specified case sensitivity.
+     *
+     * @param pattern the regular expression pattern
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @return an appropriately configured IOFileFilter
+     * @since Commons IO 1.4
+     */
+    public static IOFileFilter regex(String pattern, IOCase caseSensitivity) {
+        return new RegexFilter(pattern, caseSensitivity);
+    }
+
+    /**
+     * Returns a filter that uses a regular expression to filter file names with
+     * the specified flags.
+     *
+     * @param pattern the regular expression pattern
+     * @param flags pattern flags (e.g. {@link java.util.regex.Pattern#CASE_INSENSITIVE})
+     * @return an appropriately configured IOFileFilter
+     * @since Commons IO 1.4
+     */
+    public static IOFileFilter regex(String pattern, int flags) {
+        return new RegexFilter(pattern, flags);
     }
 
     //-----------------------------------------------------------------------

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/RegexFilter.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/RegexFilter.java?rev=584278&r1=584277&r2=584278&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/RegexFilter.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/RegexFilter.java Fri
Oct 12 14:30:52 2007
@@ -17,9 +17,8 @@
 package org.apache.commons.io.filefilter;
 
 import java.io.File;
-import java.util.Iterator;
-import java.util.List;
 import java.util.regex.Pattern;
+import org.apache.commons.io.IOCase;
 
 /**
  * Filters files using supplied regular expression(s).
@@ -44,11 +43,11 @@
  */
 public class RegexFilter extends AbstractFileFilter {
 
-    /** The regular expression patterns that will be used to match filenames */
-    private Pattern[] patterns = null;
+    /** The regular expression pattern that will be used to match filenames */
+    private Pattern pattern;
 
     /**
-     * Construct a new regular expression filter for a single regular expression
+     * Construct a new regular expression filter.
      *
      * @param pattern regular string expression to match
      * @throws IllegalArgumentException if the pattern is null
@@ -58,80 +57,53 @@
             throw new java.lang.IllegalArgumentException();
         }
 
-        patterns = new Pattern[] { Pattern.compile(pattern) };
+        this.pattern = Pattern.compile(pattern);
     }
 
     /**
-     * Construct a new regular expression filter for a single regular expression
+     * Construct a new regular expression filter with the specified flags case sensitivity.
      *
-     * @param pattern regular expression to match
+     * @param pattern regular string expression to match
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
      * @throws IllegalArgumentException if the pattern is null
      */
-    public RegexFilter(Pattern pattern) {
+    public RegexFilter(String pattern, IOCase caseSensitivity) {
         if (pattern == null) {
             throw new java.lang.IllegalArgumentException();
         }
-
-        patterns = new Pattern[] { pattern };
-    }
-
-    /**
-     * Construct a new regular expression filter for an array of regular expressions
-     *
-     * @param patterns regular expression strings to match
-     * @throws IllegalArgumentException if the pattern array is null
-     */
-    public RegexFilter(String[] patterns) {
-        if (patterns == null) {
-            throw new java.lang.IllegalArgumentException();
-        }
-
-        this.patterns = new Pattern[patterns.length];
-        for (int i = 0; i < patterns.length; i++) {
-            this.patterns[i] = Pattern.compile(patterns[i]);
+        int flags = 0;
+        if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
+            flags = Pattern.CASE_INSENSITIVE;
         }
+        this.pattern = Pattern.compile(pattern, flags);
     }
 
     /**
-     * Construct a new regular expression filter for an array of regular expressions
+     * Construct a new regular expression filter with the specified flags.
      *
-     * @param patterns regular expression to match
-     * @throws IllegalArgumentException if the pattern array is null
+     * @param pattern regular string expression to match
+     * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
+     * @throws IllegalArgumentException if the pattern is null
      */
-    public RegexFilter(Pattern[] patterns) {
-        if (patterns == null) {
+    public RegexFilter(String pattern, int flags) {
+        if (pattern == null) {
             throw new java.lang.IllegalArgumentException();
         }
-
-        this.patterns = patterns;
+        this.pattern = Pattern.compile(pattern, flags);
     }
 
     /**
-     * Construct a new regular expression filter for a list of regular expressions
+     * Construct a new regular expression filter for a compiled regular expression
      *
-     * @param patterns list of regular expressions (either list of type
-     *                 <code>java.lang.String</code> or of type
-     *                 <code>java.util.regex.Pattern</code>) to match
-     * @throws IllegalArgumentException if the pattern list is null
-     * @throws ClassCastException if the list does not contain Strings
+     * @param pattern regular expression to match
+     * @throws IllegalArgumentException if the pattern is null
      */
-    public RegexFilter(List patterns) {
-        if (patterns == null) {
+    public RegexFilter(Pattern pattern) {
+        if (pattern == null) {
             throw new java.lang.IllegalArgumentException();
         }
 
-        if (patterns.size() == 0)
-            return;
-
-        this.patterns = new Pattern[patterns.size()];
-
-        int i = 0;
-        for (Iterator it = patterns.iterator(); it.hasNext(); i++) {
-            Object pattern = it.next();
-            this.patterns[i] = (pattern instanceof Pattern)
-                ? (Pattern)pattern
-                : Pattern.compile(pattern.toString());
-        }
+        this.pattern = pattern;
     }
 
     /**
@@ -142,37 +114,7 @@
      * @return true if the filename matches one of the regular expressions
      */
     public boolean accept(File dir, String name) {
-        if (dir != null && new File(dir, name).isDirectory()) {
-            return false;
-        }
-
-        for (int i = 0; i < patterns.length; i++) {
-            if (patterns[i].matcher(name).matches()) {
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    /**
-     * Checks to see if the filename matches one of the regular expressions.
-     *
-     * @param file the file to check
-     * @return true if the filename matches one of the regular expressions
-     */
-    public boolean accept(File file) {
-        if (file.isDirectory()) {
-            return false;
-        }
-
-        for (int i = 0; i < patterns.length; i++) {
-            if (patterns[i].matcher(file.getName()).matches()) {
-                return true;
-            }
-        }
-
-        return false;
+        return (pattern.matcher(name).matches());
     }
 
 }

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java?rev=584278&r1=584277&r2=584278&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
(original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
Fri Oct 12 14:30:52 2007
@@ -823,12 +823,12 @@
     }
 
     public void testRegex() throws Exception {
-        IOFileFilter filter = new RegexFilter("^.*[tT]est(-\\d+)?\\.java$");
+        IOFileFilter filter = FileFilterUtils.regex("^.*[tT]est(-\\d+)?\\.java$");
         assertFiltering(filter, new File("Test.java"), true);
         assertFiltering(filter, new File("test-10.java"), true);
         assertFiltering(filter, new File("test-.java"), false);
 
-        filter = new RegexFilter("^[Tt]est.java$");
+        filter = FileFilterUtils.regex("^[Tt]est.java$");
         assertFiltering(filter, new File("Test.java"), true);
         assertFiltering(filter, new File("test.java"), true);
         assertFiltering(filter, new File("tEST.java"), false);
@@ -838,57 +838,15 @@
         assertFiltering(filter, new File("test.java"), true);
         assertFiltering(filter, new File("tEST.java"), true);
 
-        filter = new RegexFilter(new String[] { "^Test.java$", "^test.java$" });
-        assertFiltering(filter, new File("Test.java"), true);
-        assertFiltering(filter, new File("test.java"), true);
-        assertFiltering(filter, new File("tEST.java"), false);
-
-        filter = new RegexFilter(new Pattern[] {
-            Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE), Pattern.compile("^test2.java$")
}
-        );
-        assertFiltering(filter, new File("Test.java"), true);
-        assertFiltering(filter, new File("test.java"), true);
-        assertFiltering(filter, new File("tEST.java"), true);
-        assertFiltering(filter, new File("test2.java"), true);
-        assertFiltering(filter, new File("Test2.java"), false);
-
-        List filterList = Arrays.asList(new String[] { "^Test.java$", "^test.java$" });
-        filter = new RegexFilter(filterList);
-        assertFiltering(filter, new File("Test.java"), true);
-        assertFiltering(filter, new File("test.java"), true);
-        assertFiltering(filter, new File("tEST.java"), false);
-
-        filterList = Arrays.asList(new Pattern[] {
-            Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE), Pattern.compile("^test2.java$")
}
-        );
-        filter = new RegexFilter(filterList);
+        filter = FileFilterUtils.regex("^test.java$", Pattern.CASE_INSENSITIVE);
         assertFiltering(filter, new File("Test.java"), true);
         assertFiltering(filter, new File("test.java"), true);
         assertFiltering(filter, new File("tEST.java"), true);
-        assertFiltering(filter, new File("test2.java"), true);
-        assertFiltering(filter, new File("Test2.java"), false);
 
-        filterList = new ArrayList();
-        filterList.add(Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE));
-        filterList.add("^test2.java$");
-        filter = new RegexFilter(filterList);
+        filter = FileFilterUtils.regex("^test.java$", IOCase.INSENSITIVE);
         assertFiltering(filter, new File("Test.java"), true);
         assertFiltering(filter, new File("test.java"), true);
         assertFiltering(filter, new File("tEST.java"), true);
-        assertFiltering(filter, new File("test2.java"), true);
-        assertFiltering(filter, new File("Test2.java"), false);
-
-        assertTrue(filter.accept(new File("Test.java")));
-        assertTrue(filter.accept(new File("test.java")));
-        assertTrue(filter.accept(new File("tEST.java")));
-        assertTrue(filter.accept(new File("test2.java")));
-        assertFalse(filter.accept(new File("Test2.java")));
-
-        assertTrue(filter.accept(new File("Test.java").getParentFile(), new File("Test.java").getName()));
-        assertTrue(filter.accept(new File("test.java").getParentFile(), new File("test.java").getName()));
-        assertTrue(filter.accept(new File("tEST.java").getParentFile(), new File("tEST.java").getName()));
-        assertTrue(filter.accept(new File("test2.java").getParentFile(), new File("test2.java").getName()));
-        assertFalse(filter.accept(new File("Test2.java").getParentFile(), new File("Test2.java").getName()));
     }
 
     //-----------------------------------------------------------------------



Mime
View raw message