001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2022 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.meta;
021
022import java.io.File;
023import java.io.IOException;
024import java.nio.charset.StandardCharsets;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.nio.file.Paths;
028import java.util.ArrayList;
029import java.util.List;
030import java.util.stream.Collectors;
031import java.util.stream.Stream;
032
033import com.puppycrawl.tools.checkstyle.Checker;
034import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
035import com.puppycrawl.tools.checkstyle.TreeWalker;
036import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
037
038/** Class which handles all the metadata generation and writing calls. */
039public final class MetadataGeneratorUtil {
040
041    /** Stop instances being created. **/
042    private MetadataGeneratorUtil() {
043    }
044
045    /**
046     * Generate metadata from the module source files available in the input argument path.
047     *
048     * @param path arguments
049     * @param moduleFolders folders to check
050     * @throws IOException ioException
051     * @throws CheckstyleException checkstyleException
052     */
053    public static void generate(String path, String... moduleFolders)
054            throws IOException, CheckstyleException {
055        JavadocMetadataScraper.resetModuleDetailsStore();
056
057        final Checker checker = new Checker();
058        checker.setModuleClassLoader(Checker.class.getClassLoader());
059        final DefaultConfiguration scraperCheckConfig =
060                        new DefaultConfiguration(JavadocMetadataScraper.class.getName());
061        final DefaultConfiguration defaultConfiguration = new DefaultConfiguration("configuration");
062        final DefaultConfiguration treeWalkerConfig =
063                new DefaultConfiguration(TreeWalker.class.getName());
064        defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name());
065        defaultConfiguration.addChild(treeWalkerConfig);
066        treeWalkerConfig.addChild(scraperCheckConfig);
067        checker.configure(defaultConfiguration);
068        dumpMetadata(checker, path, moduleFolders);
069    }
070
071    /**
072     * Process files using the checker passed and write to corresponding XML files.
073     *
074     * @param moduleFolders folders to check
075     * @param checker checker
076     * @param path rootPath
077     * @throws CheckstyleException checkstyleException
078     * @throws IOException ioException
079     */
080    private static void dumpMetadata(Checker checker, String path, String... moduleFolders)
081            throws CheckstyleException,
082            IOException {
083        final List<File> validFiles = new ArrayList<>();
084        for (String folder : moduleFolders) {
085            try (Stream<Path> files = Files.walk(Paths.get(path
086                    + "/" + folder))) {
087                validFiles.addAll(
088                        files.map(Path::toFile)
089                        .filter(file -> {
090                            return file.getName().endsWith("SuppressWarningsHolder.java")
091                                    || file.getName().endsWith("Check.java")
092                                    || file.getName().endsWith("Filter.java");
093                        })
094                        .collect(Collectors.toList()));
095            }
096        }
097
098        checker.process(validFiles);
099    }
100}