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:org.codice.ddf.configuration.migration.ImportMigrationExternalEntryImplTest.java

@Test
public void testRestoreWithFilterVerifyingFilterReceivingEntryPath() throws Exception {
    final PathMatcher filter = Mockito.mock(PathMatcher.class);

    when(filter.matches(entry.getPath())).thenReturn(false);

    entry.restore(false, filter);/*from  ww  w .  j  a  v a  2  s.  c  o  m*/

    verify(filter).matches(entry.getPath());
}

From source file:org.codice.ddf.configuration.migration.ImportMigrationPropertyReferencedEntryImpl.java

@Override
public boolean restore(boolean required, PathMatcher filter) {
    Validate.notNull(filter, "invalid null path filter");
    if (restored == null) {
        super.restored = false; // until proven otherwise
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Importing {}{} with path filter...", (required ? "required " : ""), toDebugString());
        }// www. j  a v a  2  s.com
        if (referenced.restore(required, filter)) {
            super.restored = true;
            // don't bother verifying if the filter didn't match!
            if (required || filter.matches(referenced.getPath())) {
                verifyPropertyAfterCompletionOnce();
            }
        }
    }
    return restored;
}

From source file:org.commonjava.test.compile.CompilerFixture.java

public List<File> scan(final File directory, final String pattern) throws IOException {
    final PathMatcher matcher = FileSystems.getDefault()
            .getPathMatcher("glob:" + directory.getCanonicalPath() + "/" + pattern);

    final List<File> sources = new ArrayList<>();
    Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {

        @Override// w w w. j  a v a 2s  .c  o  m
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                sources.add(file.toFile());
            }

            return FileVisitResult.CONTINUE;
        }

    });

    return sources;
}

From source file:org.eclipse.winery.repository.importing.CSARImporter.java

/**
 * Modifies given allFiles object to exclude all files given by the excl pattern
 * // w w  w .ja  va2s.  c  o  m
 * Semantics: Remove all files from the set, which match the given pattern
 */
private void handleExclude(Exclude excl, Path localRoot, Set<Path> allFiles) {
    PathMatcher pathMatcher = localRoot.getFileSystem().getPathMatcher("glob:" + excl.getPattern());
    Iterator<Path> it = allFiles.iterator();
    while (it.hasNext()) {
        Path curPath = it.next();
        if (pathMatcher.matches(curPath)) {
            it.remove();
        }
    }
}

From source file:org.eclipse.winery.repository.importing.CSARImporter.java

/**
 * Modifies given allFiles object to include all files given by the incl pattern
 * //w w  w  . j  a v a  2 s  .  c o  m
 * Semantics: Add all files from localRoot to allFiles matching the pattern
 */
private void handleInclude(final Include incl, final Path localRoot, final Set<Path> allFiles) {
    final PathMatcher pathMatcher = localRoot.getFileSystem().getPathMatcher("glob:" + incl.getPattern());
    try {
        Files.walkFileTree(localRoot, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path relFile = localRoot.relativize(file);
                if (pathMatcher.matches(relFile)) {
                    allFiles.add(file);
                }
                return CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(dir)) {
                    Set<Path> filesToAdd = CSARImporter.this.getAllFiles(dir);
                    allFiles.addAll(filesToAdd);
                    return SKIP_SUBTREE;
                } else {
                    return CONTINUE;
                }
            }
        });
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.jacoco.examples.ReportGenerator.java

private List<File> getFileNames(List<File> fileNames, Path dir) {

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path path : stream) {
            if (path.toFile().isDirectory()) {
                getFileNames(fileNames, path);
            } else {
                boolean match = false;
                for (String pattern : regexes) {
                    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
                    if (matcher.matches(path)) {
                        match = true;// w ww .  ja v a2s. c om
                        break;
                    }
                }
                if (!match) {
                    fileNames.add(path.toFile());
                }
            }
        }
    } catch (

    IOException e) {
        e.printStackTrace();
    }
    return fileNames;
}

From source file:org.openeos.tools.maven.plugins.GenerateEntitiesMojo.java

private Set<File> checkForLocalFiles(File file) throws IOException, URISyntaxException {
    final Set<File> result = new TreeSet<File>();
    final PathMatcher matcher = FileSystems.getDefault()
            .getPathMatcher("glob:" + file.getAbsolutePath() + "/" + searchpath);
    SimpleFileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {

        @Override/* w ww .ja  v a  2  s.  c om*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                result.add(file.toFile());
            }
            return FileVisitResult.CONTINUE;
        }

    };
    Files.walkFileTree(file.toPath(), fileVisitor);
    return result;
}

From source file:org.openeos.tools.maven.plugins.GenerateEntitiesMojo.java

private List<File> findLiquibaseFiles(Artifact artifact) throws IOException {

    if (artifact == null) {
        return Collections.emptyList();
    }/*from   w  w  w .  ja  v a  2  s .  c o m*/
    List<File> result = new ArrayList<File>();

    if (artifact.getType().equals("jar")) {
        File file = artifact.getFile();
        FileSystem fs = FileSystems.newFileSystem(Paths.get(file.getAbsolutePath()),
                this.getClass().getClassLoader());
        PathMatcher matcher = fs.getPathMatcher("glob:" + searchpath);
        JarFile jarFile = new JarFile(file);
        Enumeration<JarEntry> entries = jarFile.entries();
        TreeSet<JarEntry> setEntries = new TreeSet<JarEntry>(new Comparator<JarEntry>() {

            @Override
            public int compare(JarEntry o1, JarEntry o2) {
                return o1.getName().compareTo(o2.getName());
            }

        });
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (matcher.matches(fs.getPath(entry.getName()))) {
                setEntries.add(entry);
            }
        }
        for (JarEntry entry : setEntries) {
            File resultFile = File.createTempFile("generate-entities-maven", ".xml");
            FileOutputStream out = new FileOutputStream(resultFile);
            InputStream in = jarFile.getInputStream(entry);
            IOUtils.copy(in, out);
            in.close();
            out.close();
            result.add(resultFile);
        }
        jarFile.close();
    }
    return result;
}

From source file:org.wikidata.wdtk.testing.MockDirectoryManager.java

@Override
public List<String> getSubdirectories(String glob) throws IOException {
    List<String> result = new ArrayList<String>();
    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
    for (Path path : files.keySet()) {
        if (!this.directory.equals(path.getParent())) {
            continue;
        }/*from   w ww.j  a v a  2 s .c  o  m*/
        if (pathMatcher.matches(path.getFileName())) {
            result.add(path.getFileName().toString());
        }
    }
    return result;
}

From source file:org.zanata.client.commands.FileMappingRuleHandler.java

/**
 * Check whether the parsed rule is applicable to a source document.
 *
 * @param docNameWithExt/*w w  w  . jav  a2  s .  c  o  m*/
 *            source document name with extension
 * @return true if this parsed rule is applicable
 */
public boolean isApplicable(DocNameWithExt docNameWithExt) {
    if (Strings.isNullOrEmpty(mappingRule.getPattern())) {
        return true;
    }
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + mappingRule.getPattern());
    // this will help when docNameWithExt has just file name i.e.
    // test.odt whereas pattern is defined as **/*.odt
    File srcFile = new File(opts.getSrcDir(), docNameWithExt.getFullName());
    log.debug("trying to match pattern: {} to file: {}", mappingRule.getPattern(), srcFile.getPath());
    return matcher.matches(Paths.get(srcFile.getPath()));
}