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.coding;
021
022import java.util.regex.Pattern;
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.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
029
030/**
031 * <p>
032 * Checks for illegal token text.
033 * </p>
034 * <p> An example of how to configure the check to forbid String literals
035 * containing {@code "a href"} is:
036 * </p>
037 * <pre>
038 * &lt;module name="IllegalTokenText"&gt;
039 *     &lt;property name="tokens" value="STRING_LITERAL"/&gt;
040 *     &lt;property name="format" value="a href"/&gt;
041 * &lt;/module&gt;
042 * </pre>
043 * <p> An example of how to configure the check to forbid leading zeros in an
044 * integer literal, other than zero and a hex literal is:
045 * </p>
046 * <pre>
047 * &lt;module name="IllegalTokenText"&gt;
048 *     &lt;property name="tokens" value="NUM_INT,NUM_LONG"/&gt;
049 *     &lt;property name="format" value="^0[^lx]"/&gt;
050 *     &lt;property name="ignoreCase" value="true"/&gt;
051 * &lt;/module&gt;
052 * </pre>
053 */
054@StatelessCheck
055public class IllegalTokenTextCheck
056    extends AbstractCheck {
057
058    /**
059     * A key is pointing to the warning message text in "messages.properties"
060     * file.
061     */
062    public static final String MSG_KEY = "illegal.token.text";
063
064    /**
065     * Custom message for report if illegal regexp found
066     * ignored if empty.
067     */
068    private String message = "";
069
070    /** The format string of the regexp. */
071    private String formatString = "$^";
072
073    /** The regexp to match against. */
074    private Pattern format = Pattern.compile(formatString);
075
076    /** {@code true} if the casing should be ignored. */
077    private boolean ignoreCase;
078
079    @Override
080    public int[] getDefaultTokens() {
081        return CommonUtils.EMPTY_INT_ARRAY;
082    }
083
084    @Override
085    public int[] getAcceptableTokens() {
086        return new int[] {
087            TokenTypes.NUM_DOUBLE,
088            TokenTypes.NUM_FLOAT,
089            TokenTypes.NUM_INT,
090            TokenTypes.NUM_LONG,
091            TokenTypes.IDENT,
092            TokenTypes.COMMENT_CONTENT,
093            TokenTypes.STRING_LITERAL,
094            TokenTypes.CHAR_LITERAL,
095        };
096    }
097
098    @Override
099    public int[] getRequiredTokens() {
100        return CommonUtils.EMPTY_INT_ARRAY;
101    }
102
103    @Override
104    public boolean isCommentNodesRequired() {
105        return true;
106    }
107
108    @Override
109    public void visitToken(DetailAST ast) {
110        final String text = ast.getText();
111        if (format.matcher(text).find()) {
112            String customMessage = message;
113            if (customMessage.isEmpty()) {
114                customMessage = MSG_KEY;
115            }
116            log(
117                ast.getLineNo(),
118                ast.getColumnNo(),
119                customMessage,
120                formatString);
121        }
122    }
123
124    /**
125     * Setter for message property.
126     * @param message custom message which should be used
127     *                 to report about violations.
128     */
129    public void setMessage(String message) {
130        if (message == null) {
131            this.message = "";
132        }
133        else {
134            this.message = message;
135        }
136    }
137
138    /**
139     * Set the format to the specified regular expression.
140     * @param format a {@code String} value
141     * @throws org.apache.commons.beanutils.ConversionException unable to parse format
142     */
143    public void setFormat(String format) {
144        formatString = format;
145        updateRegexp();
146    }
147
148    /**
149     * Set whether or not the match is case sensitive.
150     * @param caseInsensitive true if the match is case insensitive.
151     * @noinspection BooleanParameter
152     */
153    public void setIgnoreCase(boolean caseInsensitive) {
154        ignoreCase = caseInsensitive;
155        updateRegexp();
156    }
157
158    /**
159     * Updates the {@link #format} based on the values from {@link #formatString} and
160     * {@link #ignoreCase}.
161     */
162    private void updateRegexp() {
163        final int compileFlags;
164        if (ignoreCase) {
165            compileFlags = Pattern.CASE_INSENSITIVE;
166        }
167        else {
168            compileFlags = 0;
169        }
170        format = CommonUtils.createPattern(formatString, compileFlags);
171    }
172
173}