Java源码示例:org.gradle.api.file.FileVisitor

示例1
JarSnapshot createSnapshot(byte[] hash, FileTree classes, final ClassFilesAnalyzer analyzer) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    classes.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            analyzer.visitFile(fileDetails);
            String className = fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", "");
            byte[] classHash = hasher.hash(fileDetails.getFile());
            hashes.put(className, classHash);
        }
    });

    return new JarSnapshot(new JarSnapshotData(hash, hashes, analyzer.getAnalysis()));
}
 
示例2
JarSnapshot createSnapshot(byte[] hash, FileTree classes, final ClassFilesAnalyzer analyzer) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    classes.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            analyzer.visitFile(fileDetails);
            String className = fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", "");
            byte[] classHash = hasher.hash(fileDetails.getFile());
            hashes.put(className, classHash);
        }
    });

    return new JarSnapshot(new JarSnapshotData(hash, hashes, analyzer.getAnalysis()));
}
 
示例3
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
示例4
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
示例5
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
示例6
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!file.canRead()) {
                throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file));
            }
            // else, might be a link which points to nothing, or has been removed while we're visiting, or ...
            throw new GradleException(String.format("Could not list contents of '%s'.", file));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
示例7
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
示例8
public void visit(FileVisitor visitor) {
    InputStream inputStream;
    try {
        inputStream = resource.read();
        assert inputStream != null;
    } catch (MissingResourceException e) {
        DeprecationLogger.nagUserOfDeprecatedBehaviour(
                String.format("The specified tar file %s does not exist and will be silently ignored", getDisplayName())
        );
        return;
    } catch (ResourceException e) {
        throw new InvalidUserDataException(String.format("Cannot expand %s.", getDisplayName()), e);
    }

    try {
        try {
            visitImpl(visitor, inputStream);
        } finally {
            inputStream.close();
        }
    } catch (Exception e) {
        String message = "Unable to expand " + getDisplayName() + "\n"
                + "  The tar might be corrupted or it is compressed in an unexpected way.\n"
                + "  By default the tar tree tries to guess the compression based on the file extension.\n"
                + "  If you need to specify the compression explicitly please refer to the DSL reference.";
        throw new GradleException(message, e);
    }
}
 
示例9
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
示例10
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
示例11
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!file.canRead()) {
                throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file));
            }
            // else, might be a link which points to nothing, or has been removed while we're visiting, or ...
            throw new GradleException(String.format("Could not list contents of '%s'.", file));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
示例12
JarSnapshot createSnapshot(FileTree archivedClasses) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    archivedClasses.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            hashes.put(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""), hasher.hash(fileDetails.getFile()));
        }
    });
    return new JarSnapshot(hashes);
}
 
示例13
private static Set<String> classesInJar(FileTree jarContents) {
    final Set<String> out = new HashSet<String>();
    jarContents.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}
        public void visitFile(FileVisitDetails fileDetails) {
            out.add(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""));
        }
    });
    return out;
}
 
示例14
@Override
public void visit(FileVisitor visitor) {
    if (!archiveFile.exists()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!archiveFile.isFile()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
    }

    AtomicBoolean stopFlag = new AtomicBoolean();

    try {
        IS archiveInputStream = inputStreamProvider.openFile(archiveFile);
        try {
            ArchiveEntry archiveEntry;

            while (!stopFlag.get() && (archiveEntry = archiveInputStream.getNextEntry()) != null) {
                ArchiveEntryFileTreeElement details = createDetails(chmod);
                details.archiveInputStream = archiveInputStream;
                details.archiveEntry = (E) archiveEntry;
                details.stopFlag = stopFlag;

                try {
                    if (archiveEntry.isDirectory()) {
                        visitor.visitDir(details);
                    }
                    else {
                        visitor.visitFile(details);
                    }
                } finally {
                    details.close();
                }
            }
        } finally {
            archiveInputStream.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
    }
}
 
示例15
@Override
public void visit(FileVisitor visitor) {
    if (!archiveFile.exists()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!archiveFile.isFile()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
    }

    AtomicBoolean stopFlag = new AtomicBoolean();

    try {
        IS archiveInputStream = inputStreamProvider.openFile(archiveFile);
        try {
            ArchiveEntry archiveEntry;

            while (!stopFlag.get() && (archiveEntry = archiveInputStream.getNextEntry()) != null) {
                ArchiveEntryFileTreeElement details = createDetails(chmod);
                details.archiveInputStream = archiveInputStream;
                details.archiveEntry = (E) archiveEntry;
                details.stopFlag = stopFlag;

                try {
                    if (archiveEntry.isDirectory()) {
                        visitor.visitDir(details);
                    }
                    else {
                        visitor.visitFile(details);
                    }
                } finally {
                    details.close();
                }
            }
        } finally {
            archiveInputStream.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
    }
}
 
示例16
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
示例17
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
示例18
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
示例19
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!file.canRead()) {
                throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file));
            }
            // else, might be a link which points to nothing, or has been removed while we're visiting, or ...
            throw new GradleException(String.format("Could not list contents of '%s'.", file));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
示例20
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
示例21
public void visit(FileVisitor visitor) {
    InputStream inputStream;
    try {
        inputStream = resource.read();
        assert inputStream != null;
    } catch (MissingResourceException e) {
        DeprecationLogger.nagUserOfDeprecatedBehaviour(
                String.format("The specified tar file %s does not exist and will be silently ignored", getDisplayName())
        );
        return;
    } catch (ResourceException e) {
        throw new InvalidUserDataException(String.format("Cannot expand %s.", getDisplayName()), e);
    }

    try {
        try {
            visitImpl(visitor, inputStream);
        } finally {
            inputStream.close();
        }
    } catch (Exception e) {
        String message = "Unable to expand " + getDisplayName() + "\n"
                + "  The tar might be corrupted or it is compressed in an unexpected way.\n"
                + "  By default the tar tree tries to guess the compression based on the file extension.\n"
                + "  If you need to specify the compression explicitly please refer to the DSL reference.";
        throw new GradleException(message, e);
    }
}
 
示例22
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
示例23
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
示例24
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!file.canRead()) {
                throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file));
            }
            // else, might be a link which points to nothing, or has been removed while we're visiting, or ...
            throw new GradleException(String.format("Could not list contents of '%s'.", file));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
示例25
JarSnapshot createSnapshot(FileTree archivedClasses) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    archivedClasses.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            hashes.put(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""), hasher.hash(fileDetails.getFile()));
        }
    });
    return new JarSnapshot(hashes);
}
 
示例26
private static Set<String> classesInJar(FileTree jarContents) {
    final Set<String> out = new HashSet<String>();
    jarContents.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}
        public void visitFile(FileVisitDetails fileDetails) {
            out.add(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""));
        }
    });
    return out;
}
 
示例27
public FileTree visit(FileVisitor visitor) {
    for (FileTree tree : getSourceCollections()) {
        tree.visit(visitor);
    }
    return this;
}
 
示例28
public FileTree visit(FileVisitor visitor) {
    tree.visit(visitor);
    return this;
}
 
示例29
public void visit(FileVisitor visitor) {
}
 
示例30
public Visit(FileVisitor visitor, AtomicBoolean stopFlag) {
    this.visitor = visitor;
    this.stopFlag = stopFlag;
}