commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Updated: (IO-66) [IO] FilenameFilter that uses regular expressions (upload)
Date Thu, 13 Jul 2006 03:14:33 GMT
     [ http://issues.apache.org/jira/browse/IO-66?page=all ]

Henri Yandell updated IO-66:
----------------------------

    Bugzilla Id:   (was: 32761)
    Fix Version: 1.3

Commonly requested. Would mean moving the JVM version to 1.3 to pick up the regexp, or adding
a dependency on ORO.

I think we've reached the point where the JVM we depend on can go to 1.3 (or even 1.4). Either
way this'll get done or WONTFIX'd for IO 1.3 so setting the fixVersion accordingly.

> [IO] FilenameFilter that uses regular expressions (upload)
> ----------------------------------------------------------
>
>          Key: IO-66
>          URL: http://issues.apache.org/jira/browse/IO-66
>      Project: Commons IO
>         Type: Improvement

>  Environment: Operating System: Windows XP
> Platform: All
>     Reporter: analogue
>     Priority: Minor
>      Fix For: 1.3

>
> I was in need of a FilenameFilter that observed case-sensetivity and couldn't
> find anything in the existing commons-io library to meet this requirement. So,
> I've thrown together RegexFileFilter and an accompanying unit test to fill the
> void. Please add/apply to commons-io as necessary.
> -sp
> I don't see a way to attach files so heres the cut-n-paste:
> ================================================================================
> /*
>  * Copyright 2002-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.filefilter;
> import java.io.File;
> import java.io.IOException;
> import org.apache.commons.io.filefilter.AbstractFileFilter;
> import org.apache.regexp.RE;
> import org.apache.regexp.RESyntaxException;
> /**
>  * Filters files based on a regular expression that is matched against the 
>  * entire filename (including drive, path, and name). Of particular note about
>  * this filter is the ability to express case-sensetivity. 
>  *  
>  * <p>
>  * For example, to retrieve and print all files that contain the string
>  * <code>"xyz"</code> in the current directory, the regular expression 
>  * <code>".*xyz.*"</code> can be used:
>  *
>  * <pre>
>  * File dir = new File(".");
>  * String[] files = dir.list(new RegexFileFilter(".*xyz.*", true));
>  * for (int i = 0; i &lt; files.length; i++) {
>  *     System.out.println(files[i]);
>  * }
>  * </pre>
>  * 
>  * To match using case-insensetivity, 
>  * <code>new RegexFileFilter(".*xyz.*", false)</code> would be appropriate.
>  * 
>  * @author Semir Patel
>  */
> public class RegexFileFilter extends AbstractFileFilter {
>     
>     //--------------------------------------------------------------------------
>     // Fields
>     //--------------------------------------------------------------------------
>     
>     /** 
>      * Regular expression matcher. 
>      */
>     private RE regExp;
>     
>     //--------------------------------------------------------------------------
>     // Constructors
>     //--------------------------------------------------------------------------
>     
>     /**
>      * Creates a file filter that applies a regular expression to the entire
>      * path + name of a file's absolute location.
>      * 
>      * @param regExp Regular expression to match.
>      * @param matchCase Set to true to match using case sensetivity, false 
>      *        otherwise.
>      * @throws RESyntaxException if the regular expression is invalid.
>      */
>     public RegexFileFilter(String regExp, boolean matchCase)
>         throws RESyntaxException {
>         
>         this.regExp = new RE(regExp, 
>             matchCase ? RE.MATCH_NORMAL: RE.MATCH_CASEINDEPENDENT);        
>     }
>     //--------------------------------------------------------------------------
>     // Overrides AbstractFileFilter
>     //--------------------------------------------------------------------------
>     
>     /**
>      * Accepts by matching the filename against a regular expression.
>      * 
>      * @throws RuntimeException if an IOException occurs.
>      * @see org.apache.commons.io.filefilter.AbstractFileFilter#accept(
>      *      java.io.File)
>      */
>     public boolean accept(File file) {
>         
>         try {
>             return regExp.match(file.getCanonicalPath());
>         }
>         catch (IOException e) {
>             
>             // TODO: Follow commons-io existing practice...
>             throw new RuntimeException(e);
>         }
>     }
> }
> ==========================================================================
> /*
>  * Copyright 2002-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.filefilter;
> import java.io.File;
> import junit.framework.TestCase;
> import junit.textui.TestRunner;
> import org.apache.commons.io.FileUtils;
> import org.apache.regexp.RESyntaxException;
> /**
>  * Unit test for {@link RegexFileFilterTest}.
>  * 
>  * @author Semir Patel
>  */
> public class RegexFileFilterTest extends TestCase {
>     
>     //--------------------------------------------------------------------------
>     // Constants
>     //--------------------------------------------------------------------------
>     /**
>      * Temporary file names that unit tests will use for verification.
>      */
>     private static final String[] FILENAMES = new String[] {
>         "EVENT.java",
>     };
>     //--------------------------------------------------------------------------
>     // Fields
>     //--------------------------------------------------------------------------
>     
>     /** 
>      * Test directory for filtering files. 
>      */
>     private File testDir;
>     
>     //--------------------------------------------------------------------------
>     // Main
>     //--------------------------------------------------------------------------
>     
>     /** 
>      * Entrypoint.
>      * 
>      * @param args None recognized.
>      */
>     public static void main(String[] args)
>     {
>         TestRunner.run(RegexFileFilterTest.class);
>     }
>     //--------------------------------------------------------------------------
>     // Overrides TestCase
>     //--------------------------------------------------------------------------
>     
>     /** 
>      * Create a temporary directory with files to use for testing.
>      */
>     protected void setUp() throws Exception {
>         
>         File tmpDir = new File(System.getProperty("java.io.tmpdir"));
>         testDir = File.createTempFile("temp", "", tmpDir);
>         testDir.delete();
>         
>         assertTrue(
>             "test dir creation failed for " + testDir.getCanonicalPath(), 
>             testDir.mkdirs()); 
>         
>         for (int i = 0; i < FILENAMES.length; i++)
>             FileUtils.writeStringToFile(
>                 new File(testDir, FILENAMES[i]), "testing" + i, "UTF-8");
>     }
>     
>     /**
>      * Clean up temporary directory.
>      */
>     protected void tearDown() throws Exception {
>         FileUtils.forceDelete(testDir);
>         super.tearDown();
>     }
>    
>     //--------------------------------------------------------------------------
>     // Case Insensetive 
>     //--------------------------------------------------------------------------
>     
>     public void testAccept_CaseInsensetive_NotFound() throws Exception {
>         IOFileFilter filter = new RegexFileFilter("bogus", false);
>         
>         String matches[] = testDir.list(filter);
>         assertEquals("No matches should have been found", 0, matches.length);
>     } 
>     
>     public void testAccept_CaseInsensetive_CaseMatch() throws Exception {
>         IOFileFilter filter = new RegexFileFilter("EVENT", false);
>         
>         String matches[] = testDir.list(filter);
>         assertEquals("One match should have been found", 1, matches.length);
>         assertEquals("One match should have been found", "EVENT.java", matches[0]);
>     } 
>     
>     public void testAccept_CaseInsensetive_CaseMismatch() throws Exception {
>         IOFileFilter filter = new RegexFileFilter("event", false);
>         
>         String matches[] = testDir.list(filter);
>         assertEquals("One match should have been found", 1, matches.length);
>         assertEquals("One match should have been found", "EVENT.java", matches[0]);
>     }
>     
>     //--------------------------------------------------------------------------
>     // Case Sensetive 
>     //--------------------------------------------------------------------------
>     
>     public void testAccept_CaseSensetive_NotFound() throws Exception {
>         IOFileFilter filter = new RegexFileFilter("bogus", true);
>         
>         String matches[] = testDir.list(filter);
>         assertEquals("No matches should have been found", 0, matches.length);
>     } 
>     
>     public void testAccept_CaseSensetive_ExactMatch() throws Exception {
>         IOFileFilter filter = new RegexFileFilter("EVENT", true);
>         
>         String matches[] = testDir.list(filter);
>         assertEquals("One match should have been found", 1, matches.length);
>         assertEquals("One match should have been found", "EVENT.java", matches[0]);
>     } 
>     
>     public void testAccept_CaseSensetive_CaseMismatch() throws Exception {
>         IOFileFilter filter = new RegexFileFilter("event", true);
>         
>         String matches[] = testDir.list(filter);
>         assertEquals("No matches should have been found", 0, matches.length);
>     } 
>     
>     //--------------------------------------------------------------------------
>     // Negative Unit Tests
>     //--------------------------------------------------------------------------
>     
>     /**
>      * Make sure constructor blows up on invalid regular expressions.
>      */
>     public void testConstructor_Invalid_RegExp() {
>         
>         try {
>             RegexFileFilter filter = new RegexFileFilter("*", true);
>             fail("Expected exception on invalid regular expression");
>         }
>         catch (RESyntaxException rese) {
>             // Success
>         }
>     }
> }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


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