001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.io.OutputStream;
025import java.nio.charset.StandardCharsets;
026import java.nio.file.Files;
027import java.nio.file.Path;
028import java.nio.file.Paths;
029import java.util.ArrayList;
030import java.util.List;
031import java.util.stream.Collectors;
032import java.util.stream.Stream;
033
034import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
035import com.puppycrawl.tools.checkstyle.Checker;
036import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
037import com.puppycrawl.tools.checkstyle.MetadataGeneratorLogger;
038import com.puppycrawl.tools.checkstyle.TreeWalker;
039import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
040
041/** Class which handles all the metadata generation and writing calls. */
042public final class MetadataGeneratorUtil {
043
044    /** Stop instances being created. **/
045    private MetadataGeneratorUtil() {
046    }
047
048    /**
049     * Generate metadata from the module source files available in the input argument path.
050     *
051     * @param path arguments
052     * @param out OutputStream for error messages
053     * @param moduleFolders folders to check
054     * @throws IOException ioException
055     * @throws CheckstyleException checkstyleException
056     */
057    public static void generate(String path, OutputStream out, String... moduleFolders)
058            throws IOException, CheckstyleException {
059        JavadocMetadataScraper.resetModuleDetailsStore();
060
061        final Checker checker = new Checker();
062        checker.setModuleClassLoader(Checker.class.getClassLoader());
063        final DefaultConfiguration scraperCheckConfig =
064                        new DefaultConfiguration(JavadocMetadataScraper.class.getName());
065        final DefaultConfiguration defaultConfiguration =
066                new DefaultConfiguration("configuration");
067        final DefaultConfiguration treeWalkerConfig =
068                new DefaultConfiguration(TreeWalker.class.getName());
069        defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name());
070        defaultConfiguration.addChild(treeWalkerConfig);
071        treeWalkerConfig.addChild(scraperCheckConfig);
072        checker.configure(defaultConfiguration);
073
074        checker.addListener(new MetadataGeneratorLogger(out, OutputStreamOptions.NONE));
075
076        final List<File> moduleFiles = getTargetFiles(path, moduleFolders);
077
078        checker.process(moduleFiles);
079    }
080
081    /**
082     * Get files that represent modules.
083     *
084     * @param moduleFolders folders to check
085     * @param path          rootPath
086     * @return files for scrapping javadoc and generation of metadata files
087     * @throws IOException ioException
088     */
089    private static List<File> getTargetFiles(String path, String... moduleFolders)
090            throws IOException {
091        final List<File> validFiles = new ArrayList<>();
092        for (String folder : moduleFolders) {
093            try (Stream<Path> files = Files.walk(Paths.get(path + "/" + folder))) {
094                validFiles.addAll(
095                        files.map(Path::toFile)
096                        .filter(file -> {
097                            final String fileName = file.getName();
098                            return fileName.endsWith("SuppressWarningsHolder.java")
099                                    || fileName.endsWith("Check.java")
100                                    || fileName.endsWith("Filter.java");
101                        })
102                        .collect(Collectors.toUnmodifiableList()));
103            }
104        }
105
106        return validFiles;
107    }
108}