Java 类java.nio.file.PathMatcher 实例源码

项目:tscfg-docgen    文件:PathMatcherConfigProvider.java   
@Override
public Config getConfig() throws IOException {
  PathMatcher pathMatcher;

  try {
    pathMatcher = FileSystems.getDefault().getPathMatcher(inputFilePattern);
  } catch (IllegalArgumentException e) {
    throw new IllegalArgumentException(
        "Invalid input file pattern: " + inputFilePattern);
  }

  try (Stream<Path> pathStream = Files.walk(baseDirectory)) {
    return pathStream
        .filter(p -> Files.isRegularFile(p) && pathMatcher.matches(p))
        .map(p -> ConfigFactory.parseFile(p.toFile()))
        .reduce(ConfigFactory.empty(), Config::withFallback)
        .resolve(
            ConfigResolveOptions.defaults()
                .setAllowUnresolved(true)
                .setUseSystemEnvironment(false)
        );
  }
}
项目:elasticsearch_my    文件:IngestGeoIpPlugin.java   
static Map<String, DatabaseReaderLazyLoader> loadDatabaseReaders(Path geoIpConfigDirectory, NodeCache cache) throws IOException {
    if (Files.exists(geoIpConfigDirectory) == false && Files.isDirectory(geoIpConfigDirectory)) {
        throw new IllegalStateException("the geoip directory [" + geoIpConfigDirectory  + "] containing databases doesn't exist");
    }

    Map<String, DatabaseReaderLazyLoader> databaseReaders = new HashMap<>();
    try (Stream<Path> databaseFiles = Files.list(geoIpConfigDirectory)) {
        PathMatcher pathMatcher = geoIpConfigDirectory.getFileSystem().getPathMatcher("glob:**.mmdb.gz");
        // Use iterator instead of forEach otherwise IOException needs to be caught twice...
        Iterator<Path> iterator = databaseFiles.iterator();
        while (iterator.hasNext()) {
            Path databasePath = iterator.next();
            if (Files.isRegularFile(databasePath) && pathMatcher.matches(databasePath)) {
                String databaseFileName = databasePath.getFileName().toString();
                DatabaseReaderLazyLoader holder = new DatabaseReaderLazyLoader(databaseFileName, () -> {
                    try (InputStream inputStream = new GZIPInputStream(Files.newInputStream(databasePath, StandardOpenOption.READ))) {
                        return new DatabaseReader.Builder(inputStream).withCache(cache).build();
                    }
                });
                databaseReaders.put(databaseFileName, holder);
            }
        }
    }
    return Collections.unmodifiableMap(databaseReaders);
}
项目:graphouse    文件:MetricTreeTest.java   
@Test
public void testGlobPath() {
    PathMatcher matcher = MetricTree.createPathMatcher("asdf[");
    assertNull(matcher);

    Multimap<String, String> pattern2Candidates = generate();
    for (Map.Entry<String, Collection<String>> pattern2CandidatesMap : pattern2Candidates.asMap().entrySet()) {
        String glob = pattern2CandidatesMap.getKey();
        matcher = MetricTree.createPathMatcher(glob);
        if (matcher == null) {
            System.out.println("Wrong pattern " + glob);
            continue;
        }
        for (String node : pattern2CandidatesMap.getValue()) {
            System.out.println(String.format("%40s\t%40s\t%s", glob, node, MetricTree.matches(matcher, node)));
        }
    }
}
项目:emufog    文件:SettingsReader.java   
/**
 * Reads in the given JSON file and parses the content.
 * Creates and returns a new settings object with it.
 *
 * @param path path to JSON file
 * @return settings object or null if impossible to read
 * @throws IllegalArgumentException if the given path is null
 * @throws FileNotFoundException    if the given path can not be found
 */
public static Settings read(Path path) throws IllegalArgumentException, FileNotFoundException {
    if (path == null) {
        throw new IllegalArgumentException("The given file path is not initialized.");
    }
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.json");
    if (!matcher.matches(path)) {
        throw new IllegalArgumentException("The file ending does not match .json.");
    }

    Settings settings = null;
    // parse JSON document to a java object
    JSONSettings json = new Gson().fromJson(new FileReader(path.toFile()), JSONSettings.class);

    if (json != null) {
        // create the actual settings object with the information of the read in objects
        settings = new Settings(json);
    }

    return settings;
}
项目:flume-release-1.7.0    文件:TaildirMatcher.java   
/**
 * Package accessible constructor. From configuration context it represents a single
 * <code>filegroup</code> and encapsulates the corresponding <code>filePattern</code>.
 * <code>filePattern</code> consists of two parts: first part has to be a valid path to an
 * existing parent directory, second part has to be a valid regex
 * {@link java.util.regex.Pattern} that match any non-hidden file names within parent directory
 * . A valid example for filePattern is <code>/dir0/dir1/.*</code> given
 * <code>/dir0/dir1</code> is an existing directory structure readable by the running user.
 * <p></p>
 * An instance of this class is created for each fileGroup
 *
 * @param fileGroup arbitrary name of the group given by the config
 * @param filePattern parent directory plus regex pattern. No wildcards are allowed in directory
 *                    name
 * @param cachePatternMatching default true, recommended in every setup especially with huge
 *                             parent directories. Don't set when local system clock is not used
 *                             for stamping mtime (eg: remote filesystems)
 * @see TaildirSourceConfigurationConstants
 */
TaildirMatcher(String fileGroup, String filePattern, boolean cachePatternMatching) {
  // store whatever came from configuration
  this.fileGroup = fileGroup;
  this.filePattern = filePattern;
  this.cachePatternMatching = cachePatternMatching;

  // calculate final members
  File f = new File(filePattern);
  this.parentDir = f.getParentFile();
  String regex = f.getName();
  final PathMatcher matcher = FS.getPathMatcher("regex:" + regex);
  this.fileFilter = new DirectoryStream.Filter<Path>() {
    @Override
    public boolean accept(Path entry) throws IOException {
      return matcher.matches(entry.getFileName()) && !Files.isDirectory(entry);
    }
  };

  // sanity check
  Preconditions.checkState(parentDir.exists(),
      "Directory does not exist: " + parentDir.getAbsolutePath());
}
项目:jpa2ddl    文件:FileResolver.java   
static List<String> listClassNamesInPackage(String packageName) throws Exception {
    List<String> classes = new ArrayList<>();
    Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageName.replace('.', File.separatorChar));
    if (!resources.hasMoreElements()) {
        throw new IllegalStateException("No package found: " + packageName);
    }
    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:*.class");
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        Files.walkFileTree(Paths.get(resource.toURI()), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(path.getFileName())) {
                    try {
                        String className = Paths.get(resource.toURI()).relativize(path).toString().replace(File.separatorChar, '.');
                        classes.add(packageName + '.' + className.substring(0, className.length() - 6));
                    } catch (URISyntaxException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return classes;
}
项目:vanno    文件:IOutils.java   
public static List<String> readFileWithPattern(File path) {
        PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + path.getAbsolutePath());
        List<String> result = new ArrayList<String>();

        String p = path.getAbsolutePath();
        int index = p.lastIndexOf(File.separator);
        if(index == -1) {
            throw new IllegalArgumentException("Cannot parse path:" + p + ", please use like this : /f/example/*.bgz ");
        }
        File dir = new File(p.substring(0, index));  
        File[] files = dir.listFiles();
        for (File file : files) {
            if(pathMatcher.matches(file.toPath())) {
                result.add(file.getAbsolutePath());
            } 
        }
//      if(result.size() > MAX_FILE) {
//          throw new IllegalArgumentException("Cannot handle files more than " + MAX_FILE + " at one time, please merge file first." );
//      }
        return result;
    }
项目:openjdk-jdk10    文件:JrtFileSystem.java   
@Override
public PathMatcher getPathMatcher(String syntaxAndInput) {
    int pos = syntaxAndInput.indexOf(':');
    if (pos <= 0 || pos == syntaxAndInput.length()) {
        throw new IllegalArgumentException("pos is " + pos);
    }
    String syntax = syntaxAndInput.substring(0, pos);
    String input = syntaxAndInput.substring(pos + 1);
    String expr;
    if (syntax.equalsIgnoreCase("glob")) {
        expr = JrtUtils.toRegexPattern(input);
    } else if (syntax.equalsIgnoreCase("regex")) {
        expr = input;
    } else {
            throw new UnsupportedOperationException("Syntax '" + syntax
                    + "' not recognized");
    }
    // return matcher
    final Pattern pattern = Pattern.compile(expr);
    return (Path path) -> pattern.matcher(path.toString()).matches();
}
项目:openjdk9    文件:JrtFileSystem.java   
@Override
public PathMatcher getPathMatcher(String syntaxAndInput) {
    int pos = syntaxAndInput.indexOf(':');
    if (pos <= 0 || pos == syntaxAndInput.length()) {
        throw new IllegalArgumentException();
    }
    String syntax = syntaxAndInput.substring(0, pos);
    String input = syntaxAndInput.substring(pos + 1);
    String expr;
    if (syntax.equalsIgnoreCase("glob")) {
        expr = JrtUtils.toRegexPattern(input);
    } else if (syntax.equalsIgnoreCase("regex")) {
        expr = input;
    } else {
            throw new UnsupportedOperationException("Syntax '" + syntax
                    + "' not recognized");
    }
    // return matcher
    final Pattern pattern = Pattern.compile(expr);
    return (Path path) -> pattern.matcher(path.toString()).matches();
}
项目:audiveris    文件:FileUtil.java   
/**
 * Opens a directory stream on provided 'dir' folder, filtering file names according
 * to the provided glob syntax.
 * <p>
 * See http://blog.eyallupu.com/2011/11/java-7-working-with-directories.html
 *
 * @param dir  the directory to read from
 * @param glob the glob matching
 * @return the opened DirectoryStream (remaining to be closed)
 * @throws IOException
 */
public static DirectoryStream newDirectoryStream (Path dir,
                                                  String glob)
        throws IOException
{
    // create a matcher and return a filter that uses it.
    final FileSystem fs = dir.getFileSystem();
    final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
    final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>()
    {
        @Override
        public boolean accept (Path entry)
        {
            return matcher.matches(entry.getFileName());
        }
    };

    return fs.provider().newDirectoryStream(dir, filter);
}
项目:filesystem    文件:AbstractFileSystem.java   
@Override
public PathMatcher getPathMatcher( String syntaxAndInput )
{
    int pos = syntaxAndInput.indexOf( ':' );
    if( pos <= 0 || pos == syntaxAndInput.length() ) {
        throw new IllegalArgumentException();
    }
    String syntax = syntaxAndInput.substring( 0, pos );
    String input = syntaxAndInput.substring( pos + 1 );
    String expr;
    if( syntax.equals( REGEX_SYNTAX ) ) {
        expr = input;
    }
    else {
        throw new UnsupportedOperationException( "Syntax '" + syntax + "' not recognized" );
    }

    // return matcher
    final Pattern pattern = Pattern.compile( expr );
    return path -> pattern.matcher( path.toString() ).matches();
}
项目:mycore    文件:MCRAbstractFileSystem.java   
@Override
public PathMatcher getPathMatcher(final String syntaxAndPattern) {
    final int pos = syntaxAndPattern.indexOf(':');
    if (pos <= 0 || pos == syntaxAndPattern.length()) {
        throw new IllegalArgumentException();
    }
    final String syntax = syntaxAndPattern.substring(0, pos);
    final String pattern = syntaxAndPattern.substring(pos + 1);
    switch (syntax) {
        case "glob":
            return new MCRGlobPathMatcher(pattern);
        case "regex":
            return new MCRPathMatcher(pattern);
        default:
            throw new UnsupportedOperationException("If the pattern syntax '" + syntax + "' is not known.");
    }
}
项目:nb-eslint    文件:ESLintIgnore.java   
public static ESLintIgnore get(FileObject fileObject) {
    if (fileObject.isFolder() && fileObject.getFileObject(".eslintignore") != null) {
        try {
            FileObject ignore = fileObject.getFileObject(".eslintignore");
            List<PathMatcher> pathMatchers = new ArrayList<>();
            final List<String> lines = ignore.asLines();
            for (String glob : lines) {
                glob = glob.endsWith("/") ? glob + "**/*" : glob;
                pathMatchers.add(FileSystems.getDefault().getPathMatcher("glob:" + glob));
            }
            return new ESLintIgnore(fileObject, pathMatchers);
        } catch (IOException iOException) {
            Logger.getLogger(ESLintIgnore.class.getName()).log(Level.WARNING, "Failed to read ignore file");
            return new ESLintIgnore(fileObject, Collections.EMPTY_LIST);
        }
    } else if (fileObject.isFolder()) {
        return new ESLintIgnore(fileObject, Collections.EMPTY_LIST);

    } else {
        throw new IllegalArgumentException("Not a folder " + fileObject);
    }
}
项目:java-mapollage    文件:FileVisitor.java   
public FileVisitor(PathMatcher pathMatcher, List<File> paths, File startDir, Operation operation) {
    mStartDir = startDir;
    mOperation = operation;
    mOperationListener = operation.getListener();
    mFiles = paths;
    mPathMatcher = pathMatcher;
    mExcludePatterns = StringUtils.split(operation.getExcludePattern(), "::");
    mDirToDesc = operation.getDirToDesc();

    final DescriptionMode mode = operation.getProfileDescription().getMode();
    mUseExternalDescription = mode == DescriptionMode.EXTERNAL;
    mExternalFileValue = operation.getProfileDescription().getExternalFileValue();
    if (mode == DescriptionMode.EXTERNAL) {
        try {
            File file = new File(startDir, mExternalFileValue);
            if (file.isFile()) {
                mDefaultDescProperties.load(new InputStreamReader(new FileInputStream(file), Charset.defaultCharset()));
            }
        } catch (IOException ex) {
            // nvm
        }
    }
}
项目:ASCIIGenome    文件:Utils.java   
/** Search for a glob pattern in a given directory and its sub directories
 * See http://javapapers.com/java/glob-with-java-nio/
 * */
private static List<Path> match(String glob, String location) throws IOException {

    final List<Path> globbed= new ArrayList<Path>();

    final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);

    Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            if (pathMatcher.matches(path)) {
                globbed.add(path);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc)
                throws IOException {
            return FileVisitResult.CONTINUE;
        }
    });
    return globbed;
}
项目:copybara    文件:FileUtil.java   
/**
 * Deletes the files that match the PathMatcher.
 *
 * <p> Note that this method doesn't delete the directories, only the files inside those
 * directories. This is fine for our use case since the majority of SCMs don't care about empty
 * directories.
 *
 * @throws IOException If it fails traversing or deleting the tree.
 */
public static int deleteFilesRecursively(Path path, final PathMatcher pathMatcher)
    throws IOException {
  final AtomicInteger counter = new AtomicInteger();
  Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      if (pathMatcher.matches(file)) {
        Files.delete(file);
        counter.incrementAndGet();
      }
      return FileVisitResult.CONTINUE;
    }
  });
  return counter.get();
}
项目:copybara    文件:WorkflowTest.java   
@Test
public void testDestinationFilesPassedToDestination_iterative() throws Exception {
  destinationFiles = "glob(['**'], exclude = ['foo', 'bar/**'])";
  origin.addSimpleChange(/*timestamp*/ 42);
  Workflow workflow = iterativeWorkflow(resolveHead());
  origin.addSimpleChange(/*timestamp*/ 4242);
  workflow.run(workdir, HEAD);

  assertThat(destination.processed).hasSize(1);

  PathMatcher matcher = destination.processed.get(0).getDestinationFiles()
      .relativeTo(workdir);
  assertThat(matcher.matches(workdir.resolve("foo"))).isFalse();
  assertThat(matcher.matches(workdir.resolve("foo/indir"))).isTrue();
  assertThat(matcher.matches(workdir.resolve("bar/indir"))).isFalse();
}
项目:arquillian-algeron    文件:PactBorkerContractsPublisher.java   
private void publishContractFiles(PactBrokerClient pactBrokerClient, Path contractsLocation, String version) throws IOException {
    final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.json");
    try (Stream<Path> stream = Files.walk(contractsLocation)) {
        stream
            .filter(matcher::matches)
            .peek(path -> logger.log(Level.INFO, String.format("Publishing to Pact Broker contract %s", path)))
            .forEach(path -> {
                final String result = (String) pactBrokerClient.uploadPactFile(path.toFile(), version);

                if (result.startsWith("FAILED!")) {
                    throw new IllegalStateException(String.format("Failed to publish %s contract with server failure %s", path, result));
                }

            });
    }

}
项目:mdw    文件:Test.java   
protected List<File> findCaseFiles() throws IOException {
    List<File> caseFiles = new ArrayList<>();
    List<PathMatcher> inMatchers = getIncludeMatchers();
    List<PathMatcher> exMatchers = getExcludeMatchers();
    Files.walkFileTree(Paths.get(getAssetRoot().getPath()),
            EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                    Path p = Paths.get(getAssetPath(path.toFile()));
                    if (matches(inMatchers, p) && !matches(exMatchers, p)) {
                        caseFiles.add(p.toFile());
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
    return caseFiles;
}
项目:maven-javassist    文件:AbstractProcessMojo.java   
List<CtClass> findClasses(
        final ClassPool classPool,
        final String classPathElement) {

    // TODO(ville): If we ever support finding classes in other ways extract this method into a strategy.
    getLog().debug(String.format("Searching classpath element: %s", classPathElement));
    final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.{class}");
    final Path basePath = Paths.get(classPathElement);
    final List<CtClass> ctClasses = new ArrayList<>();

    try {
        Files.walkFileTree(
                basePath,
                new ClasspathSimpleFileVisitor(matcher, ctClasses, classPool, basePath));
    } catch (final IOException e) {
        throw new CompletionException("Unable to resolve compile classpath elements", e);
    }

    return ctClasses;
}
项目:parsec    文件:FileUtils.java   
/**
 * Find files in path.
 *
 * @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;
    }
}
项目:flink    文件:GlobFilePathFilter.java   
@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;
}
项目:pipegen    文件:PathUtilities.java   
public static Stream<Path> getFileOrFiles(Path fileOrDirectory, String filterExpression) {
    File[] files;
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher(filterExpression);

    if(fileOrDirectory.getFileName().toString().contains("*"))
        return Lists.newArrayList(fileOrDirectory.toFile()
                .getParentFile()
                .listFiles((FileFilter)new WildcardFileFilter(
                        fileOrDirectory.getFileName().toString())))
                .stream()
                .map(File::toPath)
                .filter(p -> matcher.matches(p.getFileName()));
    else if(fileOrDirectory.toFile().isDirectory() && (files = fileOrDirectory.toFile().listFiles()) != null)
        return Lists.newArrayList(files)
                    .stream()
                    .map(File::toPath)
                    .filter(p -> p.toFile().isDirectory() || matcher.matches(p.getFileName()));
    else
        return Lists.newArrayList(new Path[] {fileOrDirectory}).stream();
}
项目:jerkar    文件:JkPathMatcherTest.java   
private void testDoMatchOk(String pathString) {

        Path path = Paths.get(pathString).normalize();
        System.out.println("---------------------- " + pathString + ":" + path);

        PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.txt");
        assertTrue(matcher.matches(path));
        assertTrue(pathString, JkPathMatcher.accept("**/*.txt").matches(path));

        matcher = FileSystems.getDefault().getPathMatcher("glob:foo/bar.txt");
        assertTrue(pathString , matcher.matches(path));
        assertTrue(pathString, JkPathMatcher.accept("foo/bar.txt").matches(path));

        assertTrue(JkPathMatcher.accept("foo/b?r.txt").matches(path));
        assertTrue(JkPathMatcher.accept("f*/bar.txt").matches(path));
        assertTrue(JkPathMatcher.accept("*/bar.txt").matches(path));
        assertTrue(JkPathMatcher.accept("**").matches(path));
    }
项目:protostuff-compiler    文件:AbstractGeneratorMojo.java   
List<String> findProtoFiles(final Path sourcePath) {
    List<String> protoFiles = new ArrayList<>();
    if (Files.exists(sourcePath) && Files.isDirectory(sourcePath)) {
        PathMatcher protoMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.proto");
        try {
            Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (protoMatcher.matches(file)) {
                        String protoFile = sourcePath.relativize(file).toString();
                        String normalizedPath = normalizeProtoPath(protoFile);
                        protoFiles.add(normalizedPath);
                    }
                    return super.visitFile(file, attrs);
                }
            });
        } catch (IOException e) {
            LOGGER.error("Can not build source files list", e);
        }
    }
    return protoFiles;
}
项目:statecharts    文件:VersionUpdateVisitor.java   
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes arg1) throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern);

    if (pathMatcher.matches(path.getFileName())) {
        searchList.add(path);
        String charset = Charset.defaultCharset().name();
        //System.out.println(charset);
        String content = new String(Files.readAllBytes(path), charset);
        content = content.replaceAll(this.searchPattern, this.replacePattern);
        Files.write(path, content.getBytes(charset));
        fileCount++;
    }
    return FileVisitResult.CONTINUE;
}
项目:roda-in    文件:SipPreviewCreator.java   
private Set<Path> searchMetadata(Path sipPath) {
  File dir = sipPath.toFile();
  if (!dir.isDirectory()) {
    dir = sipPath.getParent().toFile();
  }

  PathMatcher matcher = FileSystems.getDefault().getPathMatcher(Constants.MISC_GLOB + templateType);
  File[] foundFiles = dir.listFiles((dir1, name) -> matcher.matches(Paths.get(name)));

  Set<Path> result = new HashSet<>();
  if (foundFiles != null && foundFiles.length > 0) {
    for (File foundFile : foundFiles) {
      result.add(foundFile.toPath());
    }
  }
  return result;
}
项目:che    文件:LanguageServerFileWatcher.java   
private void onServerInitialized(
    LanguageServerLauncher launcher,
    LanguageServer server,
    ServerCapabilities capabilities,
    String projectPath) {
  LanguageServerDescription description = launcher.getDescription();
  FileSystem fileSystem = FileSystems.getDefault();
  for (String pattern : description.getFileWatchPatterns()) {
    PathMatcher matcher = fileSystem.getPathMatcher(pattern);
    int watcherId =
        watcherManager.registerByMatcher(
            matcher,
            s -> send(server, s, FileChangeType.Created),
            s -> send(server, s, FileChangeType.Changed),
            s -> send(server, s, FileChangeType.Deleted));

    watcherIds.add(watcherId);
  }
}
项目:che    文件:LanguageServerFileWatcherTest.java   
@Test
public void testRegisterFileWatcher() throws Exception {
  ArgumentCaptor<ServerInitializerObserver> argumentCaptor =
      ArgumentCaptor.forClass(ServerInitializerObserver.class);
  watcher = new LanguageServerFileWatcher(watcherManager, initializer);
  verify(initializer).addObserver(argumentCaptor.capture());
  ServerInitializerObserver value = argumentCaptor.getValue();

  LanguageServerDescription description =
      new LanguageServerDescription(
          "foo",
          Collections.singletonList("bar"),
          Collections.emptyList(),
          Collections.singletonList("glob:*.foo"));
  when(launcher.getDescription()).thenReturn(description);
  value.onServerInitialized(launcher, server, null, null);

  ArgumentCaptor<PathMatcher> pathMatcherCaptor = ArgumentCaptor.forClass(PathMatcher.class);
  verify(watcherManager).registerByMatcher(pathMatcherCaptor.capture(), any(), any(), any());
  assertTrue(pathMatcherCaptor.getValue().matches(new File("bar.foo").toPath()));
}
项目:che    文件:FileWatcherApiModule.java   
private void configureTreeWalker() {
  bind(FileTreeWalker.class).asEagerSingleton();

  newSetBinder(
      binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.directory.update"));
  newSetBinder(
      binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.directory.create"));
  newSetBinder(
      binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.directory.delete"));
  newSetBinder(
      binder(), new TypeLiteral<PathMatcher>() {}, Names.named("che.fs.directory.excludes"));
  newSetBinder(binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.file.update"));
  newSetBinder(binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.file.create"));
  newSetBinder(binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.file.delete"));
  newSetBinder(binder(), new TypeLiteral<PathMatcher>() {}, Names.named("che.fs.file.excludes"));
}
项目:che    文件:FileWatcherByPathMatcher.java   
int watch(
    PathMatcher matcher,
    Consumer<String> create,
    Consumer<String> modify,
    Consumer<String> delete) {
  LOG.debug("Watching matcher '{}'", matcher);
  int operationId = operationIdCounter.getAndIncrement();

  matchers.putIfAbsent(matcher, newConcurrentHashSet());
  matchers.get(matcher).add(operationId);

  operations.put(operationId, new Operation(create, modify, delete));

  LOG.debug("Registered matcher operation set with id '{}'", operationId);
  return operationId;
}
项目:che    文件:FileTreeWalker.java   
@Inject
public FileTreeWalker(
    @Named("che.user.workspaces.storage") File root,
    @Named("che.fs.directory.update") Set<Consumer<Path>> directoryUpdateConsumers,
    @Named("che.fs.directory.create") Set<Consumer<Path>> directoryCreateConsumers,
    @Named("che.fs.directory.delete") Set<Consumer<Path>> directoryDeleteConsumers,
    @Named("che.fs.directory.excludes") Set<PathMatcher> directoryExcludes,
    @Named("che.fs.file.update") Set<Consumer<Path>> fileUpdateConsumers,
    @Named("che.fs.file.create") Set<Consumer<Path>> fileCreateConsumers,
    @Named("che.fs.file.delete") Set<Consumer<Path>> fileDeleteConsumers,
    @Named("che.fs.file.excludes") Set<PathMatcher> fileExcludes) {
  this.root = root;

  this.directoryUpdateConsumers = directoryUpdateConsumers;
  this.directoryCreateConsumers = directoryCreateConsumers;
  this.directoryDeleteConsumers = directoryDeleteConsumers;

  this.fileUpdateConsumers = fileUpdateConsumers;
  this.fileCreateConsumers = fileCreateConsumers;
  this.fileDeleteConsumers = fileDeleteConsumers;

  this.directoryExcludes = directoryExcludes;
  this.fileExcludes = fileExcludes;
}
项目:che    文件:SearchApiModule.java   
@Override
protected void configure() {
  bind(Searcher.class).to(LuceneSearcher.class);

  Multibinder<PathMatcher> excludeMatcher =
      newSetBinder(binder(), PathMatcher.class, Names.named("vfs.index_filter_matcher"));
  excludeMatcher.addBinding().to(MediaTypesExcludeMatcher.class);
  excludeMatcher.addBinding().to(DotCheExcludeMatcher.class);
  excludeMatcher.addBinding().to(DotNumberSignExcludeMatcher.class);
  excludeMatcher.addBinding().to(HiddenItemPathMatcher.class);

  newSetBinder(binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.file.create"))
      .addBinding()
      .to(IndexedFileCreateConsumer.class);
  newSetBinder(binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.file.update"))
      .addBinding()
      .to(IndexedFileUpdateConsumer.class);
  newSetBinder(binder(), new TypeLiteral<Consumer<Path>>() {}, Names.named("che.fs.file.delete"))
      .addBinding()
      .to(IndexedFileDeleteConsumer.class);
}
项目:che    文件:LuceneSearcher.java   
@Inject
protected LuceneSearcher(
    @Named("vfs.index_filter_matcher") Set<PathMatcher> excludePatterns,
    @Named("vfs.local.fs_index_root_dir") File indexDirectory,
    @Named("che.user.workspaces.storage") File root,
    PathTransformer pathTransformer) {
  this.indexDirectory = indexDirectory;
  this.root = root;
  this.excludePatterns = excludePatterns;
  this.pathTransformer = pathTransformer;

  executor =
      newSingleThreadExecutor(
          new ThreadFactoryBuilder()
              .setDaemon(true)
              .setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance())
              .setNameFormat("LuceneSearcherInitThread")
              .build());
}
项目:todo-teamcity-plugin    文件:PathMatcherTests.java   
@Test
public void Test_GlobPatternMatcher_AbsolutePath_InRoot() {

    Path root = Paths.get("c:\\temp");
    Path examplePath = Paths.get("c:\\temp\\blabla.sql");

    String pattern = String.format(
            "glob:%1$s%2$s%3$s",
            root.normalize().toString().replace('\\', '/'),
            "/",
            "**/*.sql");
    PathMatcher testMatcher = FileSystems
            .getDefault()
            .getPathMatcher(pattern);

    Assert.assertFalse(testMatcher.matches(examplePath));
}
项目:todo-teamcity-plugin    文件:PathMatcherTests.java   
@Test
public void Test_GlobPatternMatcher_AbsolutePath_InFolderUnderRoot() {

    Path root = Paths.get("c:\\temp");
    Path examplePath = Paths.get("c:\\temp\\tra\\blabla.sql");

    String pattern = String.format(
            "glob:%1$s%2$s%3$s",
            root.normalize().toString().replace('\\', '/'),
            "/",
            "**/*.sql");
    PathMatcher testMatcher = FileSystems
            .getDefault()
            .getPathMatcher(pattern);

    Assert.assertTrue(testMatcher.matches(examplePath));
}
项目:ignite    文件:VisorIgfsProfilerTask.java   
/**
 * Parse all IGFS log files in specified log directory.
 *
 * @param logDir Folder were log files located.
 * @return List of line with aggregated information by files.
 */
private List<VisorIgfsProfilerEntry> parse(Path logDir, String igfsName) throws IOException {
    List<VisorIgfsProfilerEntry> parsedFiles = new ArrayList<>(512);

    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(logDir)) {
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:igfs-log-" + igfsName + "-*.csv");

        for (Path p : dirStream) {
            if (matcher.matches(p.getFileName())) {
                try {
                    parsedFiles.addAll(parseFile(p));
                }
                catch (NoSuchFileException ignored) {
                    // Files was deleted, skip it.
                }
                catch (Exception e) {
                    ignite.log().warning("Failed to parse IGFS profiler log file: " + p, e);
                }
            }
        }
    }

    return parsedFiles;
}
项目:CS-FileTransfer    文件:UsingMongoSavaFile.java   
private String findExcelFile() throws IOException {
    // use the matcher to find the file
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher(
            "glob:*.{xls,xlsx}");
    // find the return
    Path dir = Paths.get(syncDirectory);
    DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
    for (Path p : stream) {
        // here match the file name
        // not the path
        Path file = p.getFileName();
        // match
        if (matcher.matches(file)) {
            return p.toString();
        }
    }
    // return
    return null;
}
项目:datacollector    文件:SpoolDirSource.java   
private void validateInitialFileToProcess(List<ConfigIssue> issues) {
  if (conf.initialFileToProcess != null && !conf.initialFileToProcess.isEmpty()) {
    try {
      PathMatcher pathMatcher = DirectorySpooler.createPathMatcher(conf.filePattern, conf.pathMatcherMode);
      if (!pathMatcher.matches(new File(conf.initialFileToProcess).toPath().getFileName())) {
        issues.add(
            getContext().createConfigIssue(
                Groups.FILES.name(),
                SPOOLDIR_CONFIG_BEAN_PREFIX + "initialFileToProcess",
                Errors.SPOOLDIR_18,
                conf.initialFileToProcess,
                conf.filePattern
            )
        );
      }
    } catch (Exception ex) {
    }
  }
}
项目:ephemeralfs    文件:EphemeralFsFileSystem.java   
@Override
public PathMatcher getPathMatcher(String syntaxAndPattern) {
    if(syntaxAndPattern.startsWith("regex:")) {
        return regexPathMatcher(syntaxAndPattern.substring("regex:".length()));
    } else if(syntaxAndPattern.startsWith("glob:")) {
        String glob = syntaxAndPattern.substring("glob:".length());
        String regex = GlobUtil.globToRegex(glob, 
                settings.isPosix()
                );
        return regexPathMatcher(regex); 
    } else if(!Pattern.matches(".+:.+", syntaxAndPattern)) {
        throw new IllegalArgumentException("syntaxAndPattern must take the form syntax:patterbn, not" + syntaxAndPattern);
    } else {
        throw new UnsupportedOperationException("invlalid syntaxAndPattern:" + syntaxAndPattern);
    }
}