use of java.nio.file.PathMatcher in project OreSpawn by MinecraftModDevelopmentMods.
the class OS3APIImpl method loadConfigFiles.
public void loadConfigFiles() {
PathMatcher featuresFiles = FileSystems.getDefault().getPathMatcher("glob:**/features-*.json");
PathMatcher replacementsFiles = FileSystems.getDefault().getPathMatcher("glob:**/replacements-*.json");
PathMatcher jsonMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.json");
try (Stream<Path> stream = Files.walk(Constants.SYSCONF, 1)) {
stream.filter(featuresFiles::matches).map(Path::toFile).forEach(features::loadFeaturesFile);
} catch (IOException e) {
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading configs from " + Constants.SYSCONF.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
// have to do this twice or we have issues
try (Stream<Path> stream = Files.walk(Constants.SYSCONF, 1)) {
stream.filter(replacementsFiles::matches).forEach(replacements::loadFile);
} catch (IOException e) {
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading configs from " + Constants.SYSCONF.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
if (Constants.SYSCONF.resolve("presets-default.json").toFile().exists()) {
presets.load(Constants.SYSCONF.resolve("presets-default.json"));
}
try (Stream<Path> stream = Files.walk(Constants.CONFDIR, 1)) {
stream.filter(jsonMatcher::matches).forEach(conf -> {
try {
OreSpawnReader.tryReadFile(conf, this);
} catch (MissingVersionException | NotAProperConfigException | OldVersionException | UnknownVersionException e) {
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading config " + conf.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
});
} catch (IOException e) {
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading configs from " + Constants.CONFDIR.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
}
use of java.nio.file.PathMatcher in project goodies by sonatype.
the class TestDataRule method findFile.
/**
* Finds the file that matches the given path relative to the given root directory.
*
* @param rootDir root directory to search from
* @param path path to look up (supports globbed syntax)
* @param ignoreDirs directories to ignore
* @return matching file; {@code null} if no match was found
*/
private File findFile(final File rootDir, final String path, final Set<File> ignoreDirs) {
if (!isGlobbedPath(path)) {
final File file = file(rootDir, path);
return file.exists() ? file : null;
}
final File[] result = new File[1];
final PathMatcher matcher = asGlobbedMatcher(path);
final Path rootPath = rootDir.toPath();
try {
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) {
return ignoreDirs.contains(dir.toFile()) ? SKIP_SUBTREE : CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
if (matcher.matches(rootPath.relativize(file))) {
result[0] = file.toFile();
return TERMINATE;
}
return CONTINUE;
}
});
} catch (final IOException e) {
failed(e, description);
}
return result[0];
}
use of java.nio.file.PathMatcher in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method transformXMLMatchingPatternWithStylesheet.
@MCRCommand(syntax = "transform xml matching file name pattern {0} in derivate {1} with stylesheet {2}", help = "Finds all files in Derivate {1} which match the pattern {0} (the complete path with regex: or glob:*.xml syntax) and transforms them with stylesheet {2}")
public static void transformXMLMatchingPatternWithStylesheet(String pattern, String derivate, String stylesheet) throws IOException {
MCRXSLTransformer transformer = new MCRXSLTransformer(stylesheet);
MCRPath derivateRoot = MCRPath.getPath(derivate, "/");
PathMatcher matcher = derivateRoot.getFileSystem().getPathMatcher(pattern);
Files.walkFileTree(derivateRoot, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (matcher.matches(file)) {
LOGGER.info("The file {} matches the pattern {}", file, pattern);
MCRContent sourceContent = new MCRPathContent(file);
MCRContent resultContent = transformer.transform(sourceContent);
try {
Document source = sourceContent.asXML();
Document result = resultContent.asXML();
LOGGER.info("Transforming complete!");
if (!MCRXMLHelper.deepEqual(source, result)) {
LOGGER.info("Writing result..");
resultContent.sendTo(file, StandardCopyOption.REPLACE_EXISTING);
} else {
LOGGER.info("Result and Source is the same..");
}
} catch (JDOMException | SAXException e) {
throw new IOException("Error while processing file : " + file, e);
}
}
return FileVisitResult.CONTINUE;
}
});
}
use of java.nio.file.PathMatcher in project flink by apache.
the class GlobFilePathFilter method buildPatterns.
private ArrayList<PathMatcher> buildPatterns(List<String> patterns) {
FileSystem fileSystem = FileSystems.getDefault();
ArrayList<PathMatcher> matchers = new ArrayList<>(patterns.size());
for (String patternStr : patterns) {
matchers.add(fileSystem.getPathMatcher("glob:" + patternStr));
}
return matchers;
}
use of java.nio.file.PathMatcher in project flink by apache.
the class GlobFilePathFilter method filterPath.
@Override
public boolean filterPath(Path filePath) {
if (getIncludeMatchers().isEmpty() && getExcludeMatchers().isEmpty()) {
return false;
}
// compensate for the fact that Flink paths are slashed
final String path = filePath.hasWindowsDrive() ? filePath.getPath().substring(1) : filePath.getPath();
final java.nio.file.Path nioPath = Paths.get(path);
for (PathMatcher matcher : getIncludeMatchers()) {
if (matcher.matches(nioPath)) {
return shouldExclude(nioPath);
}
}
return true;
}
Aggregations