Search in sources :

Example 71 with PathMatcher

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());
    }
}
Also used : Path(java.nio.file.Path) MissingVersionException(com.mcmoddev.orespawn.api.exceptions.MissingVersionException) CrashReport(net.minecraft.crash.CrashReport) IOException(java.io.IOException) NotAProperConfigException(com.mcmoddev.orespawn.api.exceptions.NotAProperConfigException) PathMatcher(java.nio.file.PathMatcher) OldVersionException(com.mcmoddev.orespawn.api.exceptions.OldVersionException) UnknownVersionException(com.mcmoddev.orespawn.api.exceptions.UnknownVersionException)

Example 72 with PathMatcher

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];
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 73 with PathMatcher

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;
        }
    });
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) PathMatcher(java.nio.file.PathMatcher) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 74 with PathMatcher

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;
}
Also used : PathMatcher(java.nio.file.PathMatcher) FileSystem(java.nio.file.FileSystem) ArrayList(java.util.ArrayList)

Example 75 with PathMatcher

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;
}
Also used : PathMatcher(java.nio.file.PathMatcher)

Aggregations

PathMatcher (java.nio.file.PathMatcher)87 Path (java.nio.file.Path)40 Test (org.junit.Test)23 IOException (java.io.IOException)22 File (java.io.File)16 ArrayList (java.util.ArrayList)12 FileSystem (java.nio.file.FileSystem)11 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)10 FileVisitResult (java.nio.file.FileVisitResult)9 Files (java.nio.file.Files)5 Paths (java.nio.file.Paths)5 HashSet (java.util.HashSet)5 List (java.util.List)5 ProjectHandlerRegistry (org.eclipse.che.api.project.server.handlers.ProjectHandlerRegistry)5 ProjectImporterRegistry (org.eclipse.che.api.project.server.importer.ProjectImporterRegistry)5 ProjectTypeRegistry (org.eclipse.che.api.project.server.type.ProjectTypeRegistry)5 DefaultFileWatcherNotificationHandler (org.eclipse.che.api.vfs.impl.file.DefaultFileWatcherNotificationHandler)5 FileTreeWatcher (org.eclipse.che.api.vfs.impl.file.FileTreeWatcher)5 LocalVirtualFileSystemProvider (org.eclipse.che.api.vfs.impl.file.LocalVirtualFileSystemProvider)5 FSLuceneSearcherProvider (org.eclipse.che.api.vfs.search.impl.FSLuceneSearcherProvider)5