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