Search in sources :

Example 11 with SimpleFileVisitor

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;
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

Path (java.nio.file.Path)11 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)10 IOException (java.io.IOException)8 SourcePath (com.facebook.buck.rules.SourcePath)3 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)2 ExecutionContext (com.facebook.buck.step.ExecutionContext)2 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)2 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)2 InputStream (java.io.InputStream)2 FileVisitResult (java.nio.file.FileVisitResult)2 TargetCpuType (com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Pair (com.facebook.buck.model.Pair)1 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 Step (com.facebook.buck.step.Step)1 CopyStep (com.facebook.buck.step.fs.CopyStep)1 MkdirStep (com.facebook.buck.step.fs.MkdirStep)1 XzStep (com.facebook.buck.step.fs.XzStep)1