001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2021 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;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.PrintWriter;
025import java.nio.charset.StandardCharsets;
026import java.util.function.Consumer;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
031import com.puppycrawl.tools.checkstyle.api.DetailAST;
032import com.puppycrawl.tools.checkstyle.api.DetailNode;
033import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
034import com.puppycrawl.tools.checkstyle.api.TokenTypes;
035import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
036import picocli.CommandLine;
037import picocli.CommandLine.Command;
038import picocli.CommandLine.Option;
039import picocli.CommandLine.ParameterException;
040import picocli.CommandLine.Parameters;
041import picocli.CommandLine.ParseResult;
042
043/**
044 * This class is used internally in the build process to write a property file
045 * with short descriptions (the first sentences) of TokenTypes constants.
046 * Request: 724871
047 * For IDE plugins (like the eclipse plugin) it would be useful to have
048 * a programmatic access to the first sentence of the TokenType constants,
049 * so they can use them in their configuration gui.
050 *
051 * @noinspection UseOfSystemOutOrSystemErr, unused, ClassIndependentOfModule
052 */
053public final class JavadocPropertiesGenerator {
054
055    /**
056     * This regexp is used to extract the first sentence from the text.
057     * The end of the sentence is determined by the symbol "period", "exclamation mark" or
058     * "question mark", followed by a space or the end of the text.
059     */
060    private static final Pattern END_OF_SENTENCE_PATTERN = Pattern.compile(
061        "(([^.?!]|[.?!](?!\\s|$))*+[.?!])(\\s|$)");
062
063    /** Max width of the usage help message for this command. */
064    private static final int USAGE_HELP_WIDTH = 100;
065
066    /**
067     * Don't create instance of this class, use the {@link #main(String[])} method instead.
068     */
069    private JavadocPropertiesGenerator() {
070    }
071
072    /**
073     * TokenTypes.properties generator entry point.
074     *
075     * @param args the command line arguments
076     * @throws CheckstyleException if parser or lexer failed or if there is an IO problem
077     **/
078    public static void main(String... args) throws CheckstyleException {
079        final CliOptions cliOptions = new CliOptions();
080        final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH);
081        try {
082            final ParseResult parseResult = cmd.parseArgs(args);
083            if (parseResult.isUsageHelpRequested()) {
084                cmd.usage(System.out);
085            }
086            else {
087                writePropertiesFile(cliOptions);
088            }
089        }
090        catch (ParameterException ex) {
091            System.err.println(ex.getMessage());
092            ex.getCommandLine().usage(System.err);
093        }
094    }
095
096    /**
097     * Creates the .properties file from a .java file.
098     *
099     * @param options the user-specified options
100     * @throws CheckstyleException if a javadoc comment can not be parsed
101     */
102    private static void writePropertiesFile(CliOptions options) throws CheckstyleException {
103        try (PrintWriter writer = new PrintWriter(options.outputFile,
104                StandardCharsets.UTF_8.name())) {
105            final DetailAST top = JavaParser.parseFile(options.inputFile,
106                    JavaParser.Options.WITH_COMMENTS);
107            final DetailAST objBlock = getClassBody(top);
108            if (objBlock != null) {
109                iteratePublicStaticIntFields(objBlock, writer::println);
110            }
111        }
112        catch (IOException ex) {
113            throw new CheckstyleException("Failed to write javadoc properties of '"
114                    + options.inputFile + "' to '" + options.outputFile + "'", ex);
115        }
116    }
117
118    /**
119     * Walks over the type members and push the first javadoc sentence of every
120     * {@code public} {@code static} {@code int} field to the consumer.
121     *
122     * @param objBlock the OBJBLOCK of a class to iterate over its members
123     * @param consumer first javadoc sentence consumer
124     * @throws CheckstyleException if failed to parse a javadoc comment
125     */
126    private static void iteratePublicStaticIntFields(DetailAST objBlock, Consumer<String> consumer)
127            throws CheckstyleException {
128        for (DetailAST member = objBlock.getFirstChild(); member != null;
129                member = member.getNextSibling()) {
130            if (isPublicStaticFinalIntField(member)) {
131                final DetailAST modifiers = member.findFirstToken(TokenTypes.MODIFIERS);
132                final String firstJavadocSentence = getFirstJavadocSentence(modifiers);
133                if (firstJavadocSentence != null) {
134                    consumer.accept(getName(member) + "=" + firstJavadocSentence.trim());
135                }
136            }
137        }
138    }
139
140    /**
141     * Finds the class body of the first class in the DetailAST.
142     *
143     * @param top AST to find the class body
144     * @return OBJBLOCK token if found; {@code null} otherwise
145     */
146    private static DetailAST getClassBody(DetailAST top) {
147        DetailAST ast = top;
148        while (ast != null && ast.getType() != TokenTypes.CLASS_DEF) {
149            ast = ast.getNextSibling();
150        }
151        DetailAST objBlock = null;
152        if (ast != null) {
153            objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
154        }
155        return objBlock;
156    }
157
158    /**
159     * Checks that the DetailAST is a {@code public} {@code static} {@code final} {@code int} field.
160     *
161     * @param ast to process
162     * @return {@code true} if matches; {@code false} otherwise
163     */
164    private static boolean isPublicStaticFinalIntField(DetailAST ast) {
165        boolean result = ast.getType() == TokenTypes.VARIABLE_DEF;
166        if (result) {
167            final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
168            result = type.getFirstChild().getType() == TokenTypes.LITERAL_INT;
169            if (result) {
170                final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
171                result = modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
172                    && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) != null
173                    && modifiers.findFirstToken(TokenTypes.FINAL) != null;
174            }
175        }
176        return result;
177    }
178
179    /**
180     * Extracts the name of an ast.
181     *
182     * @param ast to extract the name
183     * @return the text content of the inner {@code TokenTypes.IDENT} node
184     */
185    private static String getName(DetailAST ast) {
186        return ast.findFirstToken(TokenTypes.IDENT).getText();
187    }
188
189    /**
190     * Extracts the first sentence as HTML formatted text from the comment of an DetailAST.
191     * The end of the sentence is determined by the symbol "period", "exclamation mark" or
192     * "question mark", followed by a space or the end of the text. Inline tags @code and @literal
193     * are converted to HTML code.
194     *
195     * @param ast to extract the first sentence
196     * @return the first sentence of the inner {@code TokenTypes.BLOCK_COMMENT_BEGIN} node
197     *      or {@code null} if the first sentence is absent or malformed (does not end with period)
198     * @throws CheckstyleException if a javadoc comment can not be parsed or an unsupported inline
199     *      tag found
200     */
201    private static String getFirstJavadocSentence(DetailAST ast) throws CheckstyleException {
202        String firstSentence = null;
203        for (DetailAST child = ast.getFirstChild(); child != null && firstSentence == null;
204                child = child.getNextSibling()) {
205            // If there is an annotation, the javadoc comment will be a child of it.
206            if (child.getType() == TokenTypes.ANNOTATION) {
207                firstSentence = getFirstJavadocSentence(child);
208            }
209            // Otherwise, the javadoc comment will be right here.
210            else if (child.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
211                    && JavadocUtil.isJavadocComment(child)) {
212                final DetailNode tree = DetailNodeTreeStringPrinter.parseJavadocAsDetailNode(child);
213                firstSentence = getFirstJavadocSentence(tree);
214            }
215        }
216        return firstSentence;
217    }
218
219    /**
220     * Extracts the first sentence as HTML formatted text from a DetailNode.
221     * The end of the sentence is determined by the symbol "period", "exclamation mark" or
222     * "question mark", followed by a space or the end of the text. Inline tags @code and @literal
223     * are converted to HTML code.
224     *
225     * @param tree to extract the first sentence
226     * @return the first sentence of the node or {@code null} if the first sentence is absent or
227     *      malformed (does not end with any of the end-of-sentence markers)
228     * @throws CheckstyleException if an unsupported inline tag found
229     */
230    private static String getFirstJavadocSentence(DetailNode tree) throws CheckstyleException {
231        String firstSentence = null;
232        final StringBuilder builder = new StringBuilder(128);
233        for (DetailNode node : tree.getChildren()) {
234            if (node.getType() == JavadocTokenTypes.TEXT) {
235                final Matcher matcher = END_OF_SENTENCE_PATTERN.matcher(node.getText());
236                if (matcher.find()) {
237                    // Commit the sentence if an end-of-sentence marker is found.
238                    firstSentence = builder.append(matcher.group(1)).toString();
239                    break;
240                }
241                // Otherwise append the whole line and look for an end-of-sentence marker
242                // on the next line.
243                builder.append(node.getText());
244            }
245            else if (node.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG) {
246                formatInlineCodeTag(builder, node);
247            }
248            else {
249                formatHtmlElement(builder, node);
250            }
251        }
252        return firstSentence;
253    }
254
255    /**
256     * Converts inline code tag into HTML form.
257     *
258     * @param builder to append
259     * @param inlineTag to format
260     * @throws CheckstyleException if the inline javadoc tag is not a literal nor a code tag
261     */
262    private static void formatInlineCodeTag(StringBuilder builder, DetailNode inlineTag)
263            throws CheckstyleException {
264        boolean wrapWithCodeTag = false;
265        for (DetailNode node : inlineTag.getChildren()) {
266            switch (node.getType()) {
267                case JavadocTokenTypes.CODE_LITERAL:
268                    wrapWithCodeTag = true;
269                    break;
270                // The text to append.
271                case JavadocTokenTypes.TEXT:
272                    if (wrapWithCodeTag) {
273                        builder.append("<code>").append(node.getText()).append("</code>");
274                    }
275                    else {
276                        builder.append(node.getText());
277                    }
278                    break;
279                // Empty content tags.
280                case JavadocTokenTypes.LITERAL_LITERAL:
281                case JavadocTokenTypes.JAVADOC_INLINE_TAG_START:
282                case JavadocTokenTypes.JAVADOC_INLINE_TAG_END:
283                case JavadocTokenTypes.WS:
284                    break;
285                default:
286                    throw new CheckstyleException("Unsupported inline tag "
287                        + JavadocUtil.getTokenName(node.getType()));
288            }
289        }
290    }
291
292    /**
293     * Concatenates the HTML text from AST of a JavadocTokenTypes.HTML_ELEMENT.
294     *
295     * @param builder to append
296     * @param node to format
297     */
298    private static void formatHtmlElement(StringBuilder builder, DetailNode node) {
299        switch (node.getType()) {
300            case JavadocTokenTypes.START:
301            case JavadocTokenTypes.HTML_TAG_NAME:
302            case JavadocTokenTypes.END:
303            case JavadocTokenTypes.TEXT:
304            case JavadocTokenTypes.SLASH:
305                builder.append(node.getText());
306                break;
307            default:
308                for (DetailNode child : node.getChildren()) {
309                    formatHtmlElement(builder, child);
310                }
311                break;
312        }
313    }
314
315    /**
316     * Helper class encapsulating the command line options and positional parameters.
317     */
318    @Command(name = "java com.puppycrawl.tools.checkstyle.JavadocPropertiesGenerator",
319            mixinStandardHelpOptions = true)
320    private static class CliOptions {
321
322        /**
323         * The command line option to specify the output file.
324         */
325        @Option(names = "--destfile", required = true, description = "The output file.")
326        private File outputFile;
327
328        /**
329         * The command line positional parameter to specify the input file.
330         */
331        @Parameters(index = "0", description = "The input file.")
332        private File inputFile;
333    }
334}