Package java.nio.file

Examples of java.nio.file.PathMatcher


  /**
   * Asserts that the path matches the given syntax and pattern.
   */
  public PathSubject matches(String syntaxAndPattern) {
    PathMatcher matcher = getSubject().getFileSystem().getPathMatcher(syntaxAndPattern);
    if (!matcher.matches(getSubject())) {
      fail("matches", syntaxAndPattern);
    }
    return this;
  }
View Full Code Here


  /**
   * Asserts that the path does not match the given syntax and pattern.
   */
  public PathSubject doesNotMatch(String syntaxAndPattern) {
    PathMatcher matcher = getSubject().getFileSystem().getPathMatcher(syntaxAndPattern);
    if (matcher.matches(getSubject())) {
      fail("does not match", syntaxAndPattern);
    }
    return this;
  }
View Full Code Here

        log(env);
        return env;
    }
   
    public Copy substituting(final String cwd, final String src, final String dst) {
        return substituting(cwd, new PathMatcher() {
            @Override
            public boolean matches(Path path) {
                return path.endsWith(src);
            }
        }, dst);
View Full Code Here

    List<Path> paths = JuCollectionUtils.asList(ds);
    Assert.assertEquals(1, paths.size()); // Note that search is not recursive
    Assert.assertEquals(pFile1Txt, paths.get(0));
   
    // Use PathMatcher
    PathMatcher pm1 = FileSystems.getDefault().getPathMatcher("glob:*"); // Note that we need the glob prefix here
    Assert.assertFalse(pm1.matches(pFile1Txt)); // Note that * doesn't cross directory boundaries
    Assert.assertTrue(pm1.matches(pFile1Txt.getFileName()));

    // ** crosses directory boundaries
    Assert.assertTrue(FileSystems.getDefault().getPathMatcher("glob:**").matches(pFile1Txt));
   
    // Multi Matches
    Assert.assertTrue(FileSystems.getDefault().getPathMatcher("glob:**.{txt,sql}").matches(pFile1Txt));
    Assert.assertFalse(FileSystems.getDefault().getPathMatcher("glob:**.{html,xml}").matches(pFile1Txt));
   
    // Complex Matches
    PathMatcher pm2 = FileSystems.getDefault().getPathMatcher("glob:**/sub/sub[A-F]ile?.{txt,bak}");
    Assert.assertFalse(pm2.matches(pFile1Txt));
    Assert.assertTrue(pm2.matches(pSubFile1Txt));
  }
 
View Full Code Here

    }

    private static List<File> findFiles(File incPath, String searchArgument, boolean findDirectory) {
        ArrayList<File> files = new ArrayList<>();

        final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**");
         DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
            @Override
            public boolean accept(Path entry)  {
                return matcher.matches(entry.getFileName());
            }
        };

        if(starPattern.matcher(searchArgument).matches()) {
            try (DirectoryStream<Path> stream = findDirectory ?
View Full Code Here

        for (int i = 0; i < token; i+=2) {
            if (expression.regionMatches(tokens[i], "or", 0, tokens[i+1] - tokens[i])) {
                if (i == 0 || i > token - 2) {
                    throw new IllegalArgumentException("Bad syntax: " + expression);
                }
                PathMatcher left = doParse(expression.substring(tokens[0], tokens[i-1]));
                PathMatcher right = doParse(expression.substring(tokens[i+2], tokens[token-1]));
                return new OrMatcher(left, right);
            }
        }
        for (int i = 0; i < token; i+=2) {
            if (expression.regionMatches(tokens[i], "and", 0, tokens[i+1] - tokens[i])) {
                if (i == 0 || i > token - 2) {
                    throw new IllegalArgumentException("Bad syntax: " + expression);
                }
                PathMatcher left = doParse(expression.substring(tokens[0], tokens[i-1]));
                PathMatcher right = doParse(expression.substring(tokens[i+2], tokens[token-1]));
                return new AndMatcher(left, right);
            }
        }
        if (token > 2 && expression.regionMatches(tokens[0], "not", 0, tokens[1] - tokens[0])) {
            PathMatcher right = doParse(expression.substring(tokens[2], tokens[token-1]));
            return new NotMatcher(right);
        }
        if (!expression.matches("[a-z]+:.*")) {
            expression = "glob:" + expression;
        }
View Full Code Here

public class MatchersTest {

    @Test
    public void testLogical() {
        PathMatcher matcher = Logical.parse("log:(org/{apache,foo}/** or **) and(not **example**)");

        assertTrue(matcher.matches(Paths.get("org/foo/foo.java")));
        assertFalse(matcher.matches(Paths.get("org/foo/example.java")));
    }
View Full Code Here

        assertFalse(matcher.matches(Paths.get("org/foo/example.java")));
    }

    @Test
    public void testMavenGroupId() {
        PathMatcher matcher = Maven.parse("mvn:groupId:org.apache,org.foo");

        assertTrue(matcher.matches(Paths.get("org")));
        assertTrue(matcher.matches(Paths.get("org/apache")));
        assertTrue(matcher.matches(Paths.get("org/apache/foo.jar")));
        assertTrue(matcher.matches(Paths.get("org/foo")));
        assertTrue(matcher.matches(Paths.get("org/foo/foo/bar.jar")));
        assertFalse(matcher.matches(Paths.get("org/jboss")));
        assertFalse(matcher.matches(Paths.get("org/jboss/foo.jar")));
        assertFalse(matcher.matches(Paths.get("com/jboss/foo.jar")));
    }
View Full Code Here

        assertFalse(matcher.matches(Paths.get("com/jboss/foo.jar")));
    }

    @Test
    public void testComplete() {
        PathMatcher matcher = Matchers.parse("log:mvn:groupId:org.apache,org.foo and glob:**/*.jar and not **example**");

        assertFalse(matcher.matches(Paths.get("org")));
        assertFalse(matcher.matches(Paths.get("org/apache")));
        assertTrue(matcher.matches(Paths.get("org/apache/foo.jar")));
        assertFalse(matcher.matches(Paths.get("org/foo")));
        assertTrue(matcher.matches(Paths.get("org/foo/foo/bar.jar")));
        assertFalse(matcher.matches(Paths.get("org/foo/foo/bar.xml")));
        assertFalse(matcher.matches(Paths.get("org/foo/example/bar.jar")));
        assertFalse(matcher.matches(Paths.get("io/fabric8")));
        assertFalse(matcher.matches(Paths.get("io/fabric8/foo.jar")));
        assertFalse(matcher.matches(Paths.get("io/fabric8/foo.jar")));
    }
View Full Code Here

public class FilterBuilder {
   
    private FilterBuilder() {}
   
    public static DirectoryStream.Filter<Path> buildGlobFilter(String pattern) {
        final PathMatcher pathMatcher = getPathMatcher("glob:"+pattern);
        return new DirectoryStream.Filter<Path>() {
            @Override
            public boolean accept(Path entry) throws IOException {
                return pathMatcher.matches(entry);
            }
        };
    }
View Full Code Here

TOP

Related Classes of java.nio.file.PathMatcher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.