@Override public boolean matches(final Path path) { for (PathMatcher matcher : matchers) { if (matcher.matches(path)) { return true; } } return false; }
@Override public boolean apply(final String filename) { return matcher.matches(Paths.get(filename)); } };
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.java"); Path filename = ...; if (matcher.matches(filename)) { System.out.println(filename); }
private static boolean globMatches(PathMatcher matcher, String rule, File file) { if (matcher.matches(file.toPath())) { return true; } switch (rule) { case "*": case "*.*": return true; } return false; }
@Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (matcher.matches(file)) { builder.add(file.toString()); } return FileVisitResult.CONTINUE; } });
private boolean shouldExclude(java.nio.file.Path nioPath) { for (PathMatcher matcher : getExcludeMatchers()) { if (matcher.matches(nioPath)) { return true; } } return false; }
@Override public boolean matches(final Path path) { return matchers.stream() .anyMatch(it -> it.matches(path)); }
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/Users/username/workspace/*"); Files.walk(Paths.get(".")).forEach((path) -> { path = path.toAbsolutePath().normalize(); System.out.print("Path: " + path + " "); if (pathMatcher.matches(path)) { System.out.print("matched"); } System.out.println(); });
@Override public FileVisitResult visitFile(Path filePath, BasicFileAttributes attributes) throws IOException { if (pathMatcher.matches(filePath.getFileName())) { final Matcher matcher = pattern.matcher(read(filePath)); if (matcher.find()) { matchers.add(matcher); } } return FileVisitResult.CONTINUE; } });
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attributes) { Path relative = Paths.get(base.relativize(dir.toUri()).getPath()); if (excludeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) { return FileVisitResult.SKIP_SUBTREE; } if (isParentIncluded.getOrDefault(dir.getParent(), false) || includeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) { isParentIncluded.put(dir, true); return FileVisitResult.CONTINUE; } return FileVisitResult.CONTINUE; }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { Path relative = Paths.get(base.relativize(file.toUri()).getPath()); if (excludeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) { return FileVisitResult.CONTINUE; } if ((isParentIncluded.getOrDefault(file.getParent(), false) || includeMatcher.stream().anyMatch(pathMatcher -> pathMatcher.matches(relative))) && (file.toFile().getCanonicalPath().endsWith(".swift") && file.toFile().canRead())) { fileNames.add(file.toFile().getCanonicalPath()); } return FileVisitResult.CONTINUE; }
@Signature public static boolean match(String path, String pattern) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(pattern); return pathMatcher.matches(Paths.get(path)); }
@Override public FileVisitResult visitFile(Path filePath, BasicFileAttributes attributes) throws IOException { if (pathMatcher.matches(filePath.getFileName())) { insertLine(filePath, pattern, stringUpsert); } return FileVisitResult.CONTINUE; } });
@Override public List<SelectedField> getFields(String fieldGlobPattern) { if (fieldGlobPattern == null || fieldGlobPattern.isEmpty()) { return emptyList(); } computeValuesLazily(); List<String> targetNames = new ArrayList<>(); PathMatcher globMatcher = globMatcher(fieldGlobPattern); for (String flattenedField : flattenedFields) { Path path = Paths.get(flattenedField); if (globMatcher.matches(path)) { targetNames.add(flattenedField); } } return targetNames.stream() .map(this::getField) .collect(Collectors.toList()); }
@Signature(@Arg("pattern")) public Memory matches(Environment env, Memory... args) { FileSystem aDefault = FileSystems.getDefault(); PathMatcher pathMatcher = aDefault.getPathMatcher(args[0].toString()); return pathMatcher.matches(aDefault.getPath(file.getPath())) ? Memory.TRUE : Memory.FALSE; }
@Override public boolean contains(String fieldGlobPattern) { if (fieldGlobPattern == null || fieldGlobPattern.isEmpty()) { return false; } computeValuesLazily(); PathMatcher globMatcher = globMatcher(fieldGlobPattern); for (String flattenedField : flattenedFields) { Path path = Paths.get(flattenedField); if (globMatcher.matches(path)) { return true; } } return false; }
@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; }
@Test public void testPathMatcher() { Assert.assertFalse(format.getFileMatcher().matches(Paths.get("menu.json"))); Assert.assertFalse(format.getFileMatcher().matches(Paths.get("menu.prefab"))); Assert.assertTrue(format.getFileMatcher().matches(Paths.get("menu.lang"))); Assert.assertTrue(format.getFileMatcher().matches(Paths.get("menu_pl.lang"))); Assert.assertTrue(format.getFileMatcher().matches(Paths.get("menu_en-US-x-lvariant-POSIX.lang"))); }