001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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 com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
028
029/**
030 * <p>Abstract class for checking the padding of parentheses. That is whether a
031 * space is required after a left parenthesis and before a right parenthesis,
032 * or such spaces are forbidden.
033 * </p>
034 */
035@StatelessCheck
036public abstract class AbstractParenPadCheck
037    extends AbstractCheck {
038
039    /**
040     * A key is pointing to the warning message text in "messages.properties"
041     * file.
042     */
043    public static final String MSG_WS_FOLLOWED = "ws.followed";
044
045    /**
046     * A key is pointing to the warning message text in "messages.properties"
047     * file.
048     */
049    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
050
051    /**
052     * A key is pointing to the warning message text in "messages.properties"
053     * file.
054     */
055    public static final String MSG_WS_PRECEDED = "ws.preceded";
056
057    /**
058     * A key is pointing to the warning message text in "messages.properties"
059     * file.
060     */
061    public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
062
063    /** Open parenthesis literal. */
064    private static final char OPEN_PARENTHESIS = '(';
065
066    /** Close parenthesis literal. */
067    private static final char CLOSE_PARENTHESIS = ')';
068
069    /** The policy to enforce. */
070    private PadOption option = PadOption.NOSPACE;
071
072    /**
073     * Set the option to enforce.
074     * @param optionStr string to decode option from
075     * @throws IllegalArgumentException if unable to decode
076     */
077    public void setOption(String optionStr) {
078        option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
079    }
080
081    /**
082     * Process a token representing a left parentheses.
083     * @param ast the token representing a left parentheses
084     */
085    protected void processLeft(DetailAST ast) {
086        final String line = getLines()[ast.getLineNo() - 1];
087        final int after = ast.getColumnNo() + 1;
088        if (after < line.length()) {
089            if (option == PadOption.NOSPACE
090                && Character.isWhitespace(line.charAt(after))) {
091                log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS);
092            }
093            else if (option == PadOption.SPACE
094                     && !Character.isWhitespace(line.charAt(after))
095                     && line.charAt(after) != CLOSE_PARENTHESIS) {
096                log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS);
097            }
098        }
099    }
100
101    /**
102     * Process a token representing a right parentheses.
103     * @param ast the token representing a right parentheses
104     */
105    protected void processRight(DetailAST ast) {
106        final int before = ast.getColumnNo() - 1;
107        if (before >= 0) {
108            final String line = getLines()[ast.getLineNo() - 1];
109            if (option == PadOption.NOSPACE
110                && Character.isWhitespace(line.charAt(before))
111                && !CommonUtil.hasWhitespaceBefore(before, line)) {
112                log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS);
113            }
114            else if (option == PadOption.SPACE
115                && !Character.isWhitespace(line.charAt(before))
116                && line.charAt(before) != OPEN_PARENTHESIS) {
117                log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS);
118            }
119        }
120    }
121
122}