Example usage for java.nio.file PathMatcher matches

List of usage examples for java.nio.file PathMatcher matches

Introduction

In this page you can find the example usage for java.nio.file PathMatcher matches.

Prototype

boolean matches(Path path);

Source Link

Document

Tells if given path matches this matcher's pattern.

Usage

From source file:ch.bender.evacuate.Runner.java

/**
 * isExcluded//  w  w  w. ja v a2s.c om
 * <p>
 * @param aPath
 * @return
 */
private boolean isExcluded(Path aPath) {
    for (PathMatcher matcher : myExclusionMatchers) {
        if (matcher.matches(aPath)) {
            return true;
        }
    }

    return false;
}

From source file:com.streamsets.pipeline.stage.origin.spooldir.SpoolDirSource.java

private void validateInitialFileToProcess(List<ConfigIssue> issues) {
    if (conf.initialFileToProcess != null && !conf.initialFileToProcess.isEmpty()) {
        try {/* w  w w  .  j  a v a2 s  .c om*/
            PathMatcher pathMatcher = DirectorySpooler.createPathMatcher(conf.filePattern);
            if (!pathMatcher.matches(new File(conf.initialFileToProcess).toPath())) {
                issues.add(getContext().createConfigIssue(Groups.FILES.name(),
                        SPOOLDIR_CONFIG_BEAN_PREFIX + "initialFileToProcess", Errors.SPOOLDIR_18,
                        conf.initialFileToProcess, conf.filePattern));
            }
        } catch (Exception ex) {
        }
    }
}

From source file:ch.cyberduck.cli.GlobTransferItemFinder.java

@Override
public Set<TransferItem> find(final CommandLine input, final TerminalAction action, final Path remote)
        throws AccessDeniedException {
    if (input.getOptionValues(action.name()).length == 2) {
        final String path = input.getOptionValues(action.name())[1];
        // This only applies to a shell where the glob is not already expanded into multiple arguments
        if (StringUtils.containsAny(path, '*', '?')) {
            final Local directory = LocalFactory.get(FilenameUtils.getFullPath(path));
            if (directory.isDirectory()) {
                final PathMatcher matcher = FileSystems.getDefault()
                        .getPathMatcher(String.format("glob:%s", FilenameUtils.getName(path)));
                final Set<TransferItem> items = new HashSet<TransferItem>();
                for (Local file : directory.list(new NullFilter<String>() {
                    @Override//from w  w  w  .  java  2 s .  co m
                    public boolean accept(final String file) {
                        try {
                            return matcher.matches(Paths.get(file));
                        } catch (InvalidPathException e) {
                            log.warn(String.format("Failure obtaining path for file %s", file));
                        }
                        return false;
                    }
                })) {
                    items.add(new TransferItem(new Path(remote, file.getName(), EnumSet.of(Path.Type.file)),
                            file));
                }
                return items;
            }
        }
    }
    return new SingleTransferItemFinder().find(input, action, remote);
}

From source file:com.yahoo.parsec.gradle.utils.FileUtils.java

/**
 * Find files in path./*www.  j  ava2 s .c  o m*/
 *
 * @param path    find path
 * @param pattern find patttern
 * @return a set of path
 * @throws IOException IOException
 */
public Set<Path> findFiles(String path, String pattern) throws IOException {
    try {
        Set<Path> paths = new HashSet<>();
        PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(pattern);

        Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) {
                if (pathMatcher.matches(filePath.getFileName())) {
                    paths.add(filePath);
                }
                return FileVisitResult.CONTINUE;
            }
        });

        return paths;
    } catch (IOException e) {
        throw e;
    }
}

From source file:com.hygenics.parser.ExistanceChecker.java

public ArrayList<Boolean> checkFilesExist(List<String> fileMatchers, List<String> directories, boolean exists) {
    ArrayList<PathMatcher> matchers = new ArrayList<PathMatcher>();
    for (String glob : fileMatchers) {
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
        matchers.add(matcher);/*from w  w w  .  ja  va 2  s .co m*/
    }

    ArrayList<Boolean> matches = new ArrayList<Boolean>();
    for (String dir : directories) {

        File dirFile = new File(dir);
        if (dirFile.exists()) {
            File[] files = dirFile.listFiles();
            for (int i = 0; i < files.length; i++) {
                for (PathMatcher matcher : matchers) {
                    matches.add(matcher.matches(files[i].toPath()) == exists);
                }
            }
        } else {
            log.warn("Continuing. Does Not Exist: %s".format(dir));
        }
    }
    return matches;
}

From source file:com.datafibers.kafka.connect.FileGenericSourceTask.java

/**
 * Looks for files that meet the glob criteria. If any found they will be added to the list of
 * files to be processed/*from   ww w  .j a  va  2s  .  c om*/
 */
private void findMatch() {
    final PathMatcher globMatcher = FileSystems.getDefault().getPathMatcher("glob:".concat(glob));

    try {
        Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) throws IOException {
                if (globMatcher.matches(path)) {
                    if (!processedPaths.contains(path)) {
                        inProgressPaths.add(path);
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:fr.ortolang.diffusion.runtime.engine.task.ImportReferentialEntityTask.java

@Override
public void executeTask(DelegateExecution execution) throws RuntimeEngineTaskException {
    checkParameters(execution);/*  w  w w .jav a 2  s .com*/
    String referentialPathParam = execution.getVariable(REFERENTIAL_PATH_PARAM_NAME, String.class);
    report = new StringBuilder();

    File referentialPathFile = new File(referentialPathParam);
    if (referentialPathFile.exists()) {

        final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.{json}");
        final Path referentialPath = Paths.get(referentialPathParam);
        try {
            Files.walkFileTree(referentialPath, new FileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path filepath, BasicFileAttributes attrs) throws IOException {
                    Path filename = filepath.getFileName();
                    if (filename != null && matcher.matches(filename)) {

                        File jsonFile = filepath.toFile();
                        String content = StreamUtils.getContent(jsonFile);
                        if (content == null) {
                            //                           LOGGER.log(Level.SEVERE, "Referential entity content is empty for file " + jsonFile);
                            report.append(" - referential entity content is empty for file ").append(jsonFile)
                                    .append("\r\n");
                            partial = true;
                            return FileVisitResult.CONTINUE;
                        }

                        String type = extractField(content, "type");
                        if (type == null) {
                            //                           LOGGER.log(Level.SEVERE, "Referential entity type unknown for file " + jsonFile);
                            report.append(" - referential entity type unknown for file ").append(jsonFile)
                                    .append("\r\n");
                            partial = true;
                            return FileVisitResult.CONTINUE;
                        }

                        String name = jsonFile.getName().substring(0, jsonFile.getName().length() - 5);
                        try {
                            boolean exist = exists(name);

                            if (!exist) {
                                createReferentialEntity(name, type, content);
                                report.append(" + referential entity created : ").append(name).append("\r\n");
                            } else {
                                updateReferentialEntity(name, type, content);
                                report.append(" + referential entity updated : ").append(name).append("\r\n");
                            }
                        } catch (RuntimeEngineTaskException e) {
                            //                           LOGGER.log(Level.SEVERE, "  unable to import referential entity ("+type+") named "+name, e);
                            report.append(" - unable to import referential entity '").append(name)
                                    .append("' : ").append(e.getMessage()).append("\r\n");
                            partial = true;
                            return FileVisitResult.CONTINUE;
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

            });
        } catch (Exception e) {
            //            LOGGER.log(Level.SEVERE, "  unable to import referential : " + referentialPathFile, e);
            report.append("Enable to import referential ").append(referentialPathFile).append(" caused by : ")
                    .append(e.getMessage()).append("\r\n");
            partial = true;
        }

    } else {
        //         LOGGER.log(Level.SEVERE, "Referential folder doesn't exists : " + referentialPathFile);
        report.append("Referential folder doesn't exists at ").append(referentialPathFile).append("\r\n");
        partial = true;
    }

    if (partial) {
        throwRuntimeEngineEvent(RuntimeEngineEvent.createProcessLogEvent(execution.getProcessBusinessKey(),
                "Some entities has not been imported (see trace for detail)"));
    } else {
        throwRuntimeEngineEvent(RuntimeEngineEvent.createProcessLogEvent(execution.getProcessBusinessKey(),
                "All entities imported succesfully"));
    }
    throwRuntimeEngineEvent(RuntimeEngineEvent.createProcessTraceEvent(execution.getProcessBusinessKey(),
            "Report: \r\n" + report.toString(), null));
}

From source file:ca.weblite.jdeploy.JDeploy.java

public File findWarFile() {
    if (getWar(null) != null) {
        File warFile = new File(getWar(null));
        if (!warFile.exists() && warFile.getParentFile().exists()) {
            // Jar file might be a glob
            PathMatcher matcher = warFile.getParentFile().toPath().getFileSystem()
                    .getPathMatcher(warFile.getName());
            for (File f : warFile.getParentFile().listFiles()) {
                if (matcher.matches(f.toPath())) {
                    warFile = f;/* w  ww.  ja v a 2s  .  com*/
                    break;
                }
            }

            if (!warFile.exists()) {
                return null;
            }

        }
        return warFile;
    }
    return null;
}

From source file:ca.weblite.jdeploy.JDeploy.java

private void findCandidates(File root, PathMatcher matcher, List<File> matches) {
    if (".".equals(root.getName()) && root.getParentFile() != null) {
        root = root.getParentFile();/*w  w  w . j  a v  a 2s.  com*/
    }
    //System.out.println("Checking "+root+" for "+matcher);
    if (matcher.matches(root.toPath())) {
        matches.add(root);
    }
    if (root.isDirectory()) {
        if (root.getName().startsWith(".")
                || excludedDirectoriesForJarAndWarSearches.contains(root.getName())) {
            return;
        }
        for (File f : root.listFiles()) {
            findCandidates(f, matcher, matches);
        }
    }
}

From source file:ca.weblite.jdeploy.JDeploy.java

public File findJarFile() {
    if (getJar(null) != null) {
        File jarFile = new File(getJar(null));
        if (!jarFile.exists() && jarFile.getParentFile().exists()) {
            // Jar file might be a glob
            try {
                PathMatcher matcher = jarFile.getParentFile().toPath().getFileSystem()
                        .getPathMatcher(jarFile.getName());
                for (File f : jarFile.getParentFile().listFiles()) {
                    if (matcher.matches(f.toPath())) {
                        jarFile = f;/*  w w  w .  j ava  2 s .  com*/
                        break;
                    }
                }
            } catch (IllegalArgumentException ex) {
                // just eat this
            }

            if (!jarFile.exists()) {
                return null;
            }

        }
        return jarFile;
    }
    return null;
}