001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.whitespace;
021
022import java.util.Locale;
023
024import org.apache.commons.beanutils.ConversionException;
025
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
030
031/**
032 * <p>
033 * Checks line wrapping with separators.
034 * The policy to verify is specified using the {@link WrapOption} class
035 * and defaults to {@link WrapOption#EOL}.
036 * </p>
037 * <p> By default the check will check the following separators:
038 *  {@link TokenTypes#DOT DOT},
039 *  {@link TokenTypes#COMMA COMMA},
040 * Other acceptable tokens are
041 *  {@link TokenTypes#SEMI SEMI},
042 *  {@link TokenTypes#ELLIPSIS ELLIPSIS},
043 *  {@link TokenTypes#AT AT},
044 *  {@link TokenTypes#LPAREN LPAREN},
045 *  {@link TokenTypes#RPAREN RPAREN},
046 *  {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR},
047 *  {@link TokenTypes#RBRACK RBRACK},
048 * </p>
049 * <p>
050 * Code example for comma and dot at the new line:
051 * </p>
052 * <pre>
053 * s
054 *    .isEmpty();
055 * foo(i
056 *    ,s);
057 * </pre>
058 *  <p>
059 * An example of how to configure the check is:
060 * </p>
061 * <pre>
062 * &lt;module name="SeparatorWrap"/&gt;
063 * </pre>
064 * <p>
065 * Code example for comma and dot at the previous line:
066 * </p>
067 * <pre>
068 * s.
069 *    isEmpty();
070 * foo(i,
071 *    s);
072 * </pre>
073 * <p> An example of how to configure the check for comma at the
074 * new line is:
075 * </p>
076 * <pre>
077 * &lt;module name="SeparatorWrap"&gt;
078 *     &lt;property name="tokens" value="COMMA"/&gt;
079 *     &lt;property name="option" value="nl"/&gt;
080 * &lt;/module&gt;
081 * </pre>
082 *
083 * @author maxvetrenko
084 */
085public class SeparatorWrapCheck
086    extends AbstractCheck {
087
088    /**
089     * A key is pointing to the warning message text in "messages.properties"
090     * file.
091     */
092    public static final String MSG_LINE_PREVIOUS = "line.previous";
093
094    /**
095     * A key is pointing to the warning message text in "messages.properties"
096     * file.
097     */
098    public static final String MSG_LINE_NEW = "line.new";
099
100    /** The policy to enforce. */
101    private WrapOption option = WrapOption.EOL;
102
103    /**
104     * Set the option to enforce.
105     * @param optionStr string to decode option from
106     * @throws ConversionException if unable to decode
107     */
108    public void setOption(String optionStr) {
109        try {
110            option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
111        }
112        catch (IllegalArgumentException iae) {
113            throw new ConversionException("unable to parse " + optionStr, iae);
114        }
115    }
116
117    @Override
118    public int[] getDefaultTokens() {
119        return new int[] {
120            TokenTypes.DOT,
121            TokenTypes.COMMA,
122        };
123    }
124
125    @Override
126    public int[] getAcceptableTokens() {
127        return new int[] {
128            TokenTypes.DOT,
129            TokenTypes.COMMA,
130            TokenTypes.SEMI,
131            TokenTypes.ELLIPSIS,
132            TokenTypes.AT,
133            TokenTypes.LPAREN,
134            TokenTypes.RPAREN,
135            TokenTypes.ARRAY_DECLARATOR,
136            TokenTypes.RBRACK,
137        };
138    }
139
140    @Override
141    public int[] getRequiredTokens() {
142        return CommonUtils.EMPTY_INT_ARRAY;
143    }
144
145    @Override
146    public void visitToken(DetailAST ast) {
147        final String text = ast.getText();
148        final int colNo = ast.getColumnNo();
149        final int lineNo = ast.getLineNo();
150        final String currentLine = getLines()[lineNo - 1];
151        final String substringAfterToken =
152                currentLine.substring(colNo + text.length()).trim();
153        final String substringBeforeToken =
154                currentLine.substring(0, colNo).trim();
155
156        if (option == WrapOption.EOL
157                && substringBeforeToken.isEmpty()) {
158            log(lineNo, colNo, MSG_LINE_PREVIOUS, text);
159        }
160        else if (option == WrapOption.NL
161                 && substringAfterToken.isEmpty()) {
162            log(lineNo, colNo, MSG_LINE_NEW, text);
163        }
164    }
165}