|
| 1 | +/* |
| 2 | + * Copyright 2013 Christoph Böhme |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.b3e.mf.extra.source; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.FileVisitOption; |
| 20 | +import java.nio.file.FileVisitResult; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.nio.file.SimpleFileVisitor; |
| 25 | +import java.nio.file.attribute.BasicFileAttributes; |
| 26 | +import java.util.EnumSet; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import org.culturegraph.mf.exceptions.MetafactureException; |
| 30 | +import org.culturegraph.mf.framework.DefaultObjectPipe; |
| 31 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 32 | +import org.culturegraph.mf.framework.annotations.Description; |
| 33 | +import org.culturegraph.mf.framework.annotations.In; |
| 34 | +import org.culturegraph.mf.framework.annotations.Out; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | + |
| 38 | +/** |
| 39 | + * Walks through a file tree tree and emits the name of |
| 40 | + * each file found. |
| 41 | + * |
| 42 | + * @author Christoph Böhme |
| 43 | + * |
| 44 | + */ |
| 45 | +@Description("Walks through a file tree and emits the name of each file found.") |
| 46 | +@In(String.class) |
| 47 | +@Out(String.class) |
| 48 | +public final class FileTreeWalker extends |
| 49 | + DefaultObjectPipe<String, ObjectReceiver<String>> { |
| 50 | + |
| 51 | + private static final Logger LOG = LoggerFactory.getLogger(FileTreeWalker.class); |
| 52 | + |
| 53 | + private final Visitor visitor = new Visitor(); |
| 54 | + private final Set<FileVisitOption> visitOptions = EnumSet.noneOf(FileVisitOption.class); |
| 55 | + |
| 56 | + private int maxDepth = Integer.MAX_VALUE; |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns whether symbolic links are followed. |
| 60 | + * |
| 61 | + * @return true if symbolic links are followed |
| 62 | + */ |
| 63 | + public boolean isFollowingLinks() { |
| 64 | + return visitOptions.contains(FileVisitOption.FOLLOW_LINKS); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Configures whether to follow symbolic links or not |
| 69 | + * |
| 70 | + * @param follow if true symbolic links are followed |
| 71 | + */ |
| 72 | + public void setFollowLinks(final boolean follow) { |
| 73 | + if (follow) { |
| 74 | + visitOptions.add(FileVisitOption.FOLLOW_LINKS); |
| 75 | + } else { |
| 76 | + visitOptions.remove(FileVisitOption.FOLLOW_LINKS); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the maximum depth to which the walker will descend |
| 82 | + * in the directory hierarchy. |
| 83 | + * |
| 84 | + * @return max visitation depth |
| 85 | + */ |
| 86 | + public int getMaxDepth() { |
| 87 | + return maxDepth; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Sets the maximum depth to which the walker should descend |
| 92 | + * in the directory hierarchy. |
| 93 | + * |
| 94 | + * @param maxDepth sets the visitation depth. 0 means only |
| 95 | + * visiting the start node. Integer.MAX_VALUE |
| 96 | + * means descend as deep as possible. |
| 97 | + */ |
| 98 | + public void setMaxDepth(final int maxDepth) { |
| 99 | + this.maxDepth = maxDepth; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void process(final String directory) { |
| 104 | + try { |
| 105 | + Files.walkFileTree(Paths.get(directory), visitOptions, maxDepth, visitor); |
| 106 | + } catch (IOException e) { |
| 107 | + throw new MetafactureException(e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Visitor implementation |
| 113 | + */ |
| 114 | + private class Visitor extends SimpleFileVisitor<Path> { |
| 115 | + |
| 116 | + @Override |
| 117 | + public FileVisitResult visitFile(final Path file, |
| 118 | + final BasicFileAttributes attrs) { |
| 119 | + if (attrs.isRegularFile()) { |
| 120 | + getReceiver().process(file.toAbsolutePath().toString()); |
| 121 | + } |
| 122 | + return FileVisitResult.CONTINUE; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public FileVisitResult visitFileFailed(final Path file, final IOException exc) { |
| 127 | + LOG.warn("Failed visiting directory/file '{}': {}", file.toAbsolutePath().toString(), exc.toString()); |
| 128 | + return FileVisitResult.CONTINUE; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) { |
| 133 | + if (exc != null) { |
| 134 | + LOG.warn("Aborted directory visit '{}': {}", dir.toAbsolutePath().toString(), exc.toString()); |
| 135 | + } |
| 136 | + return FileVisitResult.CONTINUE; |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments