All Downloads are FREE. Search and download functionalities are using the official Maven repository.

queryless.plugin.source.PathResolver Maven / Gradle / Ivy

The newest version!
/*
 * ==============================LICENSE_START=============================
 * Queryless (query constants generation)
 * ========================================================================
 * Copyright (C) 2018 - 2019 Pavel Jakovlev
 * ========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===============================LICENSE_END==============================
 */
package queryless.plugin.source;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public final class PathResolver {

    public static Set resolve(final Set sources, final Path resourcesPath) {
        return sources.stream()
                .map(source -> resolveFiles(resourcesPath, source))
                .flatMap(Collection::stream)
                .collect(Collectors.toSet());
    }

    private static Set resolveFiles(final Path resourcesPath, final String source) {
        final Set files = new HashSet<>();

        try {
            final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + source);

            Files.walkFileTree(resourcesPath, new SimpleFileVisitor() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    final Path relative = resourcesPath.relativize(file);
                    if (matcher.matches(relative)) {
                        files.add(file);
                    }
                    return FileVisitResult.CONTINUE;
                }

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

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return files;
    }

    private PathResolver() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy