Search in sources :

Example 21 with FileVisitOption

use of java.nio.file.FileVisitOption in project ceylon-compiler by ceylon.

the class JavacPathFileManager method list.

private void list(Path path, String packageName, final Set<Kind> kinds, boolean recurse, final ListBuffer<JavaFileObject> results) throws IOException {
    if (!Files.exists(path))
        return;
    final Path pathDir;
    if (isDirectory(path))
        pathDir = path;
    else {
        FileSystem fs = getFileSystem(path);
        if (fs == null)
            return;
        pathDir = fs.getRootDirectories().iterator().next();
    }
    String sep = path.getFileSystem().getSeparator();
    Path packageDir = packageName.isEmpty() ? pathDir : pathDir.resolve(packageName.replace(".", sep));
    if (!Files.exists(packageDir))
        return;
    /* Alternate impl of list, superceded by use of Files.walkFileTree */
    // Deque<Path> queue = new LinkedList<Path>();
    // queue.add(packageDir);
    // 
    // Path dir;
    // while ((dir = queue.poll()) != null) {
    // DirectoryStream<Path> ds = dir.newDirectoryStream();
    // try {
    // for (Path p: ds) {
    // String name = p.getFileName().toString();
    // if (isDirectory(p)) {
    // if (recurse && SourceVersion.isIdentifier(name)) {
    // queue.add(p);
    // }
    // } else {
    // if (kinds.contains(getKind(name))) {
    // JavaFileObject fe =
    // PathFileObject.createDirectoryPathFileObject(this, p, pathDir);
    // results.append(fe);
    // }
    // }
    // }
    // } finally {
    // ds.close();
    // }
    // }
    int maxDepth = (recurse ? Integer.MAX_VALUE : 1);
    Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS);
    Files.walkFileTree(packageDir, opts, maxDepth, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            Path name = dir.getFileName();
            if (// JSR 292?
            name == null || SourceVersion.isIdentifier(name.toString()))
                return FileVisitResult.CONTINUE;
            else
                return FileVisitResult.SKIP_SUBTREE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            if (attrs.isRegularFile() && kinds.contains(getKind(file.getFileName().toString()))) {
                JavaFileObject fe = PathFileObject.createDirectoryPathFileObject(JavacPathFileManager.this, file, pathDir);
                results.append(fe);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) JavaFileObject(javax.tools.JavaFileObject) FileVisitOption(java.nio.file.FileVisitOption) FileSystem(java.nio.file.FileSystem) FileVisitResult(java.nio.file.FileVisitResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileVisitOption (java.nio.file.FileVisitOption)21 Path (java.nio.file.Path)18 IOException (java.io.IOException)12 FileVisitResult (java.nio.file.FileVisitResult)6 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 File (java.io.File)5 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)4 Test (org.junit.Test)4 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 FileSystem (java.nio.file.FileSystem)2 PathMatcher (java.nio.file.PathMatcher)2 ArrayList (java.util.ArrayList)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2