[ https://issues.apache.org/jira/browse/IO-66?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Niall Pemberton resolved IO-66. ------------------------------- Resolution: Duplicate The regular expression filter implementation from IO-74 has been implemented - see: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/RegexFilter.java?view=log > [IO] FilenameFilter that uses regular expressions (upload) > ---------------------------------------------------------- > > Key: IO-66 > URL: https://issues.apache.org/jira/browse/IO-66 > Project: Commons IO > Issue Type: Improvement > Components: Filters > Environment: Operating System: Windows XP > Platform: All > Reporter: analogue > Priority: Minor > Fix For: 1.4 > > > 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 < 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 [EMAIL PROTECTED] 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. - You can reply to this email to add a comment to the issue online.