001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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 com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
025
026/**
027 * <p>
028 * Checks line wrapping with separators.
029 * The policy to verify is specified using the {@link WrapOption} class
030 * and defaults to {@link WrapOption#EOL}.
031 * </p>
032 * <p> By default the check will check the following separators:
033 *  {@link TokenTypes#DOT DOT},
034 *  {@link TokenTypes#COMMA COMMA},
035 * Other acceptable tokens are
036 *  {@link TokenTypes#SEMI SEMI},
037 *  {@link TokenTypes#ELLIPSIS ELLIPSIS},
038 *  {@link TokenTypes#AT AT},
039 *  {@link TokenTypes#LPAREN LPAREN},
040 *  {@link TokenTypes#RPAREN RPAREN},
041 *  {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR},
042 *  {@link TokenTypes#RBRACK RBRACK},
043 * </p>
044 * <p>
045 * Code example for comma and dot at the new line:
046 * </p>
047 * <pre>
048 * s
049 *    .isEmpty();
050 * foo(i
051 *    ,s);
052 * </pre>
053 *  <p>
054 * An example of how to configure the check is:
055 * </p>
056 * <pre>
057 * &lt;module name="SeparatorWrap"/&gt;
058 * </pre>
059 * <p>
060 * Code example for comma and dot at the previous line:
061 * </p>
062 * <pre>
063 * s.
064 *    isEmpty();
065 * foo(i,
066 *    s);
067 * </pre>
068 * <p> An example of how to configure the check for comma at the
069 * new line is:
070 * </p>
071 * <pre>
072 * &lt;module name="SeparatorWrap"&gt;
073 *     &lt;property name="tokens" value="COMMA"/&gt;
074 *     &lt;property name="option" value="nl"/&gt;
075 * &lt;/module&gt;
076 * </pre>
077 *
078 * @author maxvetrenko
079 */
080public class SeparatorWrapCheck
081    extends AbstractOptionCheck<WrapOption>
082{
083    /**
084     * Sets the comma wrap option to end of the line.
085     */
086    public SeparatorWrapCheck()
087    {
088        super(WrapOption.EOL, WrapOption.class);
089    }
090
091    @Override
092    public int[] getDefaultTokens()
093    {
094        return new int[] {
095            TokenTypes.DOT,
096            TokenTypes.COMMA,
097        };
098    }
099
100    @Override
101    public int[] getAcceptableTokens()
102    {
103        return new int[] {
104            TokenTypes.DOT,
105            TokenTypes.COMMA,
106            TokenTypes.SEMI,
107            TokenTypes.ELLIPSIS,
108            TokenTypes.AT,
109            TokenTypes.LPAREN,
110            TokenTypes.RPAREN,
111            TokenTypes.ARRAY_DECLARATOR,
112            TokenTypes.RBRACK,
113        };
114    }
115
116    @Override
117    public void visitToken(DetailAST ast)
118    {
119        // TODO: It is a copy/paste from OperatorWrapCheck.
120        //It should be fixed in another issue
121        final String text = ast.getText();
122        final int colNo = ast.getColumnNo();
123        final int lineNo = ast.getLineNo();
124        final String currentLine = getLines()[lineNo - 1];
125        final String substringAfterToken =
126                currentLine.substring(colNo + text.length()).trim();
127        final String substringBeforeToken =
128                currentLine.substring(0, colNo).trim();
129        final WrapOption wSp = getAbstractOption();
130
131        if (wSp == WrapOption.EOL
132                && (substringBeforeToken.length() == 0))
133        {
134            log(lineNo, colNo, "line.previous", text);
135        }
136        else if (wSp == WrapOption.NL
137                 && substringAfterToken.length() == 0)
138        {
139            log(lineNo, colNo, "line.new", text);
140        }
141    }
142}