io.cucumber.core.resource.PathScanner Maven / Gradle / Ivy
The newest version!
package io.cucumber.core.resource;
import io.cucumber.core.logging.Logger;
import io.cucumber.core.logging.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.walkFileTree;
class PathScanner {
private static final Logger log = LoggerFactory.getLogger(PathScanner.class);
void findResourcesForUri(URI baseUri, Predicate filter, Function> consumer) {
try (CloseablePath closeablePath = open(baseUri)) {
Path baseDir = closeablePath.getPath();
findResourcesForPath(baseDir, filter, consumer);
} catch (FileSystemNotFoundException e) {
log.warn(e, () -> "Failed to find resources for '" + baseUri + "'");
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
private CloseablePath open(URI uri) throws IOException, URISyntaxException {
if (JarUriFileSystemService.supports(uri)) {
return JarUriFileSystemService.open(uri);
}
return CloseablePath.open(uri);
}
void findResourcesForPath(Path path, Predicate filter, Function> consumer) {
if (!exists(path)) {
throw new IllegalArgumentException("path must exist: " + path);
}
try {
walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
new ResourceFileVisitor(filter, consumer.apply(path)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static class ResourceFileVisitor extends SimpleFileVisitor {
private static final Logger logger = LoggerFactory.getLogger(ResourceFileVisitor.class);
private final Predicate resourceFileFilter;
private final Consumer resourceFileConsumer;
ResourceFileVisitor(Predicate resourceFileFilter, Consumer resourceFileConsumer) {
this.resourceFileFilter = resourceFileFilter;
this.resourceFileConsumer = resourceFileConsumer;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
if (resourceFileFilter.test(file)) {
resourceFileConsumer.accept(file);
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException e) {
logger.warn(e, () -> "IOException visiting file: " + file);
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) {
if (e != null) {
logger.warn(e, () -> "IOException visiting directory: " + dir);
}
return CONTINUE;
}
}
}