added support for path level regex matching

This commit is contained in:
Jonathan Shook 2020-04-07 18:07:57 -05:00
parent 59ee5355c4
commit a42ba634d7
3 changed files with 71 additions and 9 deletions

View File

@ -245,10 +245,11 @@ public class NBIO implements NBPathsAPI.Facets {
if (!extension.startsWith(".")) { if (!extension.startsWith(".")) {
extension = "." + extension; extension = "." + extension;
} }
String pattern = name.endsWith(extension) ? name : name + Pattern.quote(extension); String pattern = name.endsWith(extension) ? name : name + Pattern.quote(extension);
RegexPathFilter filter = new RegexPathFilter(pattern); RegexPathFilter filter = new RegexPathFilter(pattern);
NBIOWalker.walk(path, capture, filter); NBIOWalker.walkFullPath(path, capture, filter);
} }
} }
@ -267,6 +268,10 @@ public class NBIO implements NBPathsAPI.Facets {
private final Pattern regex; private final Pattern regex;
public RegexPathFilter(String pattern) { public RegexPathFilter(String pattern) {
if (!pattern.startsWith("^")) {
pattern = ".*" + pattern;
}
this.regex = Pattern.compile(pattern); this.regex = Pattern.compile(pattern);
} }

View File

@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream; import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider; import java.nio.file.spi.FileSystemProvider;
@ -12,9 +13,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class NBIOWalker { public class NBIOWalker {
private final static Logger logger = LogManager.getLogger(NBIOWalker.class); private final static Logger logger = LogManager.getLogger(NBIOWalker.class);
public static void walk(Path p, PathVisitor v) { public static void walk(Path p, PathVisitor v) {
walk(p,v,NBIOWalker.WALK_ALL); walkShortPath(p, v, NBIOWalker.WALK_ALL);
} }
public static List<Path> findAll(Path p) { public static List<Path> findAll(Path p) {
@ -24,24 +26,60 @@ public class NBIOWalker {
} }
public static void walk(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) { /**
* This walks the directory structure starting at the path specified. The path visitor is invoked for every
* directory, and every non-directory which matches the filter.
* This form uses only the filename component in Paths to be matched by the filter, and the short name is also
* what is returned by the filter.
*
* @param p The path to search
* @param v The visitor to accumulate or operate on matched paths and all directories
* @param filter The Path filter to determine whether a path is included
*/
public static void walkShortPath(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) {
walk(null, p, v, filter, false);
}
/**
* This walks the directory structure starting at the path specified. The path visitor is invoked for every
* directory, and every non-directory which matches the filter.
* This form uses only the full path from the initial search path root in all Paths to be matched by
* the filter, and this form of a Path component is also returned in all Paths seen by the visitor.
*
* @param p The path to search
* @param v The visitor to accumulate or operate on matched paths and all directories
* @param filter The Path filter to determine whether a path is included
*/
public static void walkFullPath(Path p, PathVisitor v, DirectoryStream.Filter<Path> filter) {
walk(null, p, v, filter, true);
}
public static void walk(Path root, Path p, PathVisitor v, DirectoryStream.Filter<Path> filter, boolean fullpath) {
try { try {
FileSystemProvider provider = p.getFileSystem().provider(); FileSystemProvider provider = p.getFileSystem().provider();
DirectoryStream<Path> paths = provider.newDirectoryStream(p, (Path r) -> true); DirectoryStream<Path> paths = provider.newDirectoryStream(p, (Path r) -> true);
List<Path> pathlist = new ArrayList<>(); List<Path> pathlist = new ArrayList<>();
for (Path path : paths) { for (Path path : paths) {
pathlist.add(path); pathlist.add(path);
} }
for (Path path : pathlist) { for (Path path : pathlist) {
if (fullpath && root != null) {
path = root.resolve(path);
}
if (path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class).isDirectory()) { if (path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class).isDirectory()) {
v.preVisitDir(path); v.preVisitDir(path);
walk(path, v, filter); walk(root, path, v, filter, fullpath);
v.postVisitDir(path); v.postVisitDir(path);
} else if (filter.accept(path)) { } else if (filter.accept(path)) {
v.preVisitFile(path); v.preVisitFile(path);
v.visit(path); v.visit(path);
v.postVisitFile(path); v.postVisitFile(path);
} }
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -50,10 +88,18 @@ public class NBIOWalker {
public interface PathVisitor { public interface PathVisitor {
void visit(Path p); void visit(Path p);
default void preVisitFile(Path path) {}
default void postVisitFile(Path path) {} default void preVisitFile(Path path) {
default void preVisitDir(Path path) {} }
default void postVisitDir(Path path) {}
default void postVisitFile(Path path) {
}
default void preVisitDir(Path path) {
}
default void postVisitDir(Path path) {
}
} }
public static DirectoryStream.Filter<Path> WALK_ALL = entry -> true; public static DirectoryStream.Filter<Path> WALK_ALL = entry -> true;

View File

@ -98,4 +98,15 @@ public class NBIOTest {
assertThat(list).hasSize(3); assertThat(list).hasSize(3);
} }
@Test
public void testPathSearchForSuffix() {
List<Content<?>> list = NBIO.classpath()
.prefix("nesteddir1")
.regex()
.name("nesteddir2/testdata12")
.extension("txt")
.list();
assertThat(list).hasSize(1);
}
} }