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