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;
}
});
}
Aggregations