001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.checks.javadoc;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import com.puppycrawl.tools.checkstyle.api.DetailNode;
026import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
029
030/**
031 * <p>
032 * Checks the indentation of the continuation lines in at-clauses.
033 * </p>
034 * <p>
035 * Default configuration:
036 * </p>
037 * <pre>
038 * &lt;module name=&quot;JavadocTagContinuationIndentation&quot;&gt;
039 *     &lt;property name=&quot;offset&quot; value=&quot;4&quot;/&gt;
040 * &lt;/module&gt;
041 * </pre>
042 *
043 * @author max
044 *
045 */
046public class JavadocTagContinuationIndentationCheck extends AbstractJavadocCheck {
047
048    /**
049     * A key is pointing to the warning message text in "messages.properties"
050     * file.
051     */
052    public static final String MSG_KEY = "tag.continuation.indent";
053
054    /** Default tag continuation indentation. */
055    private static final int DEFAULT_INDENTATION = 4;
056
057    /**
058     * How many spaces to use for new indentation level.
059     */
060    private int offset = DEFAULT_INDENTATION;
061
062    /**
063     * Sets custom indentation level.
064     * @param offset custom value.
065     */
066    public void setOffset(int offset) {
067        this.offset = offset;
068    }
069
070    @Override
071    public int[] getDefaultJavadocTokens() {
072        return new int[] {JavadocTokenTypes.DESCRIPTION };
073    }
074
075    @Override
076    public int[] getAcceptableTokens() {
077        return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN };
078    }
079
080    @Override
081    public int[] getRequiredTokens() {
082        return getAcceptableTokens();
083    }
084
085    @Override
086    public void visitJavadocToken(DetailNode ast) {
087        if (isInlineDescription(ast)) {
088            return;
089        }
090        final List<DetailNode> textNodes = getAllNewlineNodes(ast);
091        for (DetailNode newlineNode : textNodes) {
092            final DetailNode textNode = JavadocUtils.getNextSibling(JavadocUtils
093                    .getNextSibling(newlineNode));
094            if (textNode != null && textNode.getType() == JavadocTokenTypes.TEXT
095                    && textNode.getChildren().length > 1) {
096                final DetailNode whitespace = JavadocUtils.getFirstChild(textNode);
097                if (whitespace.getText().length() - 1 < offset) {
098                    log(textNode.getLineNumber(), MSG_KEY, offset);
099                }
100            }
101        }
102    }
103
104    /**
105     * Finds and collects all NEWLINE nodes inside DESCRIPTION node.
106     * @param descriptionNode DESCRIPTION node.
107     * @return List with NEWLINE nodes.
108     */
109    private static List<DetailNode> getAllNewlineNodes(DetailNode descriptionNode) {
110        final List<DetailNode> textNodes = new ArrayList<>();
111        DetailNode node = JavadocUtils.getFirstChild(descriptionNode);
112        while (JavadocUtils.getNextSibling(node) != null) {
113            if (node.getType() == JavadocTokenTypes.NEWLINE) {
114                textNodes.add(node);
115            }
116            node = JavadocUtils.getNextSibling(node);
117        }
118        return textNodes;
119    }
120
121    /**
122     * Checks, if description node is a description of in-line tag.
123     * @param description DESCRIPTION node.
124     * @return true, if description node is a description of in-line tag.
125     */
126    private static boolean isInlineDescription(DetailNode description) {
127        DetailNode inlineTag = description.getParent();
128        while (inlineTag != null) {
129            if (inlineTag.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG) {
130                return true;
131            }
132            inlineTag = inlineTag.getParent();
133        }
134        return false;
135    }
136}