You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2004/12/18 01:09:13 UTC
DO NOT REPLY [Bug 32761] New: -
[IO] FilenameFilter that uses regular expressions (upload)
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=32761>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=32761
Summary: [IO] FilenameFilter that uses regular expressions
(upload)
Product: Commons
Version: unspecified
Platform: All
OS/Version: Windows XP
Status: NEW
Severity: enhancement
Priority: P2
Component: IO
AssignedTo: commons-dev@jakarta.apache.org
ReportedBy: analogue@yahoo.com
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 {@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
}
}
}
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org