001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.Arrays;
030import java.util.List;
031import java.util.stream.Collectors;
032import java.util.stream.Stream;
033
034import com.puppycrawl.tools.checkstyle.Checker;
035import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
036import com.puppycrawl.tools.checkstyle.TreeWalker;
037import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
038import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
039
040/** Class which handles all the metadata generation and writing calls. */
041public final class MetadataGeneratorUtil {
042
043    private MetadataGeneratorUtil() {
044    }
045
046    /**
047     * Generate metadata from the module source files available in the input argument path.
048     *
049     * @param args arguments
050     * @throws IOException ioException
051     * @throws CheckstyleException checkstyleException
052     */
053    public static void generate(String... args) throws IOException, CheckstyleException {
054        final Checker checker = new Checker();
055        checker.setModuleClassLoader(Checker.class.getClassLoader());
056        final DefaultConfiguration scraperCheckConfig =
057                        new DefaultConfiguration(JavadocMetadataScraper.class.getName());
058        final DefaultConfiguration defaultConfiguration = new DefaultConfiguration("configuration");
059        final DefaultConfiguration treeWalkerConfig =
060                new DefaultConfiguration(TreeWalker.class.getName());
061        defaultConfiguration.addAttribute("charset", StandardCharsets.UTF_8.name());
062        defaultConfiguration.addChild(treeWalkerConfig);
063        treeWalkerConfig.addChild(scraperCheckConfig);
064        checker.configure(defaultConfiguration);
065        dumpMetadata(checker, args[0]);
066    }
067
068    /**
069     * Process files using the checker passed and write to corresponding XML files.
070     *
071     * @param checker checker
072     * @param path rootPath
073     * @throws CheckstyleException checkstyleException
074     * @throws IOException ioException
075     */
076    private static void dumpMetadata(Checker checker, String path) throws CheckstyleException,
077            IOException {
078        final List<File> validFiles = new ArrayList<>();
079        if (path.endsWith(".java")) {
080            validFiles.add(new File(path));
081        }
082        else {
083            final List<String> moduleFolders = Arrays.asList("checks", "filters", "filefilters");
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
099        checker.process(validFiles);
100    }
101
102    /**
103     * Return all token types present in checkstyle.
104     *
105     * @return list of token type names
106     */
107    public static List<String> fetchAllTokens() {
108        return Arrays.stream(TokenUtil.getAllTokenIds())
109                .mapToObj(TokenUtil::getTokenName)
110                .collect(Collectors.toList());
111    }
112}