use of java.nio.file.SimpleFileVisitor in project Anserini by castorini.
the class Collection method discover.
/**
* Used internally by implementations to walk a path and collect file segments.
*
* @param p path to walk
* @param skippedFilePrefix set of file prefixes to skip
* @param allowedFilePrefix set of file prefixes to allow
* @param skippedFileSuffix set of file suffixes to skip
* @param allowedFileSuffix set of file suffixes to allow
* @param skippedDir set of directories to skip
* @return result of walking the specified path according to the specified constraints
*/
protected List<Path> discover(Path p, Set<String> skippedFilePrefix, Set<String> allowedFilePrefix, Set<String> skippedFileSuffix, Set<String> allowedFileSuffix, Set<String> skippedDir) {
final List<Path> paths = new ArrayList<>();
FileVisitor<Path> fv = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path name = file.getFileName();
boolean shouldAdd = true;
if (name != null) {
String fileName = name.toString();
for (String s : skippedFileSuffix) {
if (fileName.endsWith(s)) {
shouldAdd = false;
break;
}
}
if (shouldAdd && !allowedFileSuffix.isEmpty()) {
shouldAdd = false;
for (String s : allowedFileSuffix) {
if (fileName.endsWith(s)) {
shouldAdd = true;
break;
}
}
}
if (shouldAdd) {
for (String s : skippedFilePrefix) {
if (fileName.startsWith(s)) {
shouldAdd = false;
break;
}
}
}
if (shouldAdd && !allowedFilePrefix.isEmpty()) {
shouldAdd = false;
for (String s : allowedFilePrefix) {
if (fileName.startsWith(s)) {
shouldAdd = true;
break;
}
}
}
}
if (shouldAdd) {
paths.add(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (skippedDir.contains(dir.getFileName().toString())) {
LOG.info("Skipping: " + dir);
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException ioe) {
LOG.error("Visiting failed for " + file.toString(), ioe);
return FileVisitResult.SKIP_SUBTREE;
}
};
try {
Files.walkFileTree(p, fv);
} catch (IOException e) {
LOG.error("IOException during file visiting", e);
}
return paths;
}
Aggregations