RegexFileFilter uses the path and file name instead of just the
file name.
Add RegexFileFilter.RegexFileFilter(Pattern, Function<Path, String>).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -47,10 +47,17 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<!-- The release date is the date RC is cut -->
<release version="2.10.0" date="2021-MM-DD" description="Java 8 required.">
+ <!-- FIX -->
+ <action issue="IO-733" dev="ggregory" type="fix" due-to="Jim Sellers, Gary Gregory">
+ RegexFileFilter uses the path and file name instead of just the file name.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add RegexFileFilter.toString().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add RegexFileFilter.RegexFileFilter(Pattern, Function&lt;Path&gt;, String>)
+ </action>
</release>
<release version="2.9.0" date="2021-05-22" description="Java 8 required.">
<!-- FIX -->

MODIFY
src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
src/main/java/org/apache/commons/io/filefilter/RegexFileFilter.java
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.function.Function;
import java.util.regex.Pattern;
import org.apache.commons.io.IOCase;
/**
* Compiles the given pattern source.
*
- * @param pattern the source pattern
+ * @param pattern the source pattern.
* @param flags the compilation flags.
* @return a new Pattern.
*/
@@ -97,20 +98,36 @@ private static int toFlags(final IOCase caseSensitivity) {
return flags;
}
- /** The regular expression pattern that will be used to match file names */
+ /** The regular expression pattern that will be used to match file names. */
private final Pattern pattern;
+ /** How convert a path to a string. */
+ private final Function<Path, String> pathToString;
+
/**
* Constructs a new regular expression filter for a compiled regular expression
*
- * @param pattern regular expression to match
- * @throws IllegalArgumentException if the pattern is null
+ * @param pattern regular expression to match.
+ * @throws IllegalArgumentException if the pattern is null.
*/
public RegexFileFilter(final Pattern pattern) {
+ this(pattern, p -> p.getFileName().toString());
+ }
+
+ /**
+ * Constructs a new regular expression filter for a compiled regular expression
+ *
+ * @param pattern regular expression to match.
+ * @param pathToString How convert a path to a string.
+ * @throws IllegalArgumentException if the pattern is null.
+ * @since 2.10.0
+ */
+ public RegexFileFilter(final Pattern pattern, final Function<Path, String> pathToString) {
if (pattern == null) {
throw new IllegalArgumentException("Pattern is missing");
}
this.pattern = pattern;
+ this.pathToString = pathToString;
}
/**
@@ -166,7 +183,7 @@ public boolean accept(final File dir, final String name) {
*/
@Override
public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
- return toFileVisitResult(pattern.matcher(path.getFileName().toString()).matches(), path);
+ return toFileVisitResult(pattern.matcher(pathToString.apply(path)).matches(), path);
}
/**

MODIFY
src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
src/test/java/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java
import java.io.File;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.regex.Pattern;
import org.apache.commons.io.IOCase;
@@ -143,4 +144,15 @@ public void testRegexEdgeCases() {
}
}
+ /**
+ * Tests https://issues.apache.org/jira/browse/IO-733.
+ */
+ @Test
+ public void testRegexFileNameOnly() {
+ final Path path = Paths.get("folder", "Foo.java");
+ final String patternStr = "Foo.*";
+ assertFiltering(new RegexFileFilter(patternStr), path, true);
+ assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), Path::toString), path, false);
+ }
+
}

[IO-733] RegexFileFilter uses the path and file name instead of just the
file name.

Add RegexFileFilter.RegexFileFilter(Pattern, Function<Path, String>).