001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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        try {
079            option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
080        }
081        catch (IllegalArgumentException iae) {
082            throw new IllegalArgumentException("unable to parse " + optionStr, iae);
083        }
084    }
085
086    /**
087     * Process a token representing a left parentheses.
088     * @param ast the token representing a left parentheses
089     */
090    protected void processLeft(DetailAST ast) {
091        final String line = getLines()[ast.getLineNo() - 1];
092        final int after = ast.getColumnNo() + 1;
093        if (after < line.length()) {
094            if (option == PadOption.NOSPACE
095                && Character.isWhitespace(line.charAt(after))) {
096                log(ast.getLineNo(), after, MSG_WS_FOLLOWED, OPEN_PARENTHESIS);
097            }
098            else if (option == PadOption.SPACE
099                     && !Character.isWhitespace(line.charAt(after))
100                     && line.charAt(after) != CLOSE_PARENTHESIS) {
101                log(ast.getLineNo(), after, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS);
102            }
103        }
104    }
105
106    /**
107     * Process a token representing a right parentheses.
108     * @param ast the token representing a right parentheses
109     */
110    protected void processRight(DetailAST ast) {
111        final int before = ast.getColumnNo() - 1;
112        if (before >= 0) {
113            final String line = getLines()[ast.getLineNo() - 1];
114            if (option == PadOption.NOSPACE
115                && Character.isWhitespace(line.charAt(before))
116                && !CommonUtil.hasWhitespaceBefore(before, line)) {
117                log(ast.getLineNo(), before, MSG_WS_PRECEDED, CLOSE_PARENTHESIS);
118            }
119            else if (option == PadOption.SPACE
120                && !Character.isWhitespace(line.charAt(before))
121                && line.charAt(before) != OPEN_PARENTHESIS) {
122                log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS);
123            }
124        }
125    }
126
127}