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