001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.Objects;
023import java.util.regex.Pattern;
024
025import com.puppycrawl.tools.checkstyle.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
030
031/**
032 * <div>
033 * Checks specified tokens text for matching an illegal pattern.
034 * By default, no tokens are specified.
035 * </div>
036 *
037 * @since 3.2
038 */
039@StatelessCheck
040public class IllegalTokenTextCheck extends AbstractCheck {
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties" file.
044     */
045    public static final String MSG_KEY = "illegal.token.text";
046
047    /**
048     * Define the message which is used to notify about violations;
049     * if empty then the default message is used.
050     */
051    private String message = "";
052
053    /** The format string of the regexp. */
054    private String formatString = "^$";
055
056    /** Define the RegExp for illegal pattern. */
057    private Pattern format = Pattern.compile(formatString);
058
059    /** Control whether to ignore case when matching. */
060    private boolean ignoreCase;
061
062    /**
063     * Creates a new {@code IllegalTokenTextCheck} instance.
064     */
065    public IllegalTokenTextCheck() {
066        // no code by default
067    }
068
069    @Override
070    public int[] getDefaultTokens() {
071        return CommonUtil.EMPTY_INT_ARRAY;
072    }
073
074    @Override
075    public int[] getAcceptableTokens() {
076        return new int[] {
077            TokenTypes.NUM_DOUBLE,
078            TokenTypes.NUM_FLOAT,
079            TokenTypes.NUM_INT,
080            TokenTypes.NUM_LONG,
081            TokenTypes.IDENT,
082            TokenTypes.COMMENT_CONTENT,
083            TokenTypes.STRING_LITERAL,
084            TokenTypes.CHAR_LITERAL,
085            TokenTypes.TEXT_BLOCK_CONTENT,
086        };
087    }
088
089    @Override
090    public int[] getRequiredTokens() {
091        return CommonUtil.EMPTY_INT_ARRAY;
092    }
093
094    @Override
095    public boolean isCommentNodesRequired() {
096        return true;
097    }
098
099    @Override
100    public void visitToken(DetailAST ast) {
101        final String text = ast.getText();
102        if (format.matcher(text).find()) {
103            String customMessage = message;
104            if (customMessage.isEmpty()) {
105                customMessage = MSG_KEY;
106            }
107            log(
108                ast,
109                customMessage,
110                formatString);
111        }
112    }
113
114    /**
115     * Setter to define the message which is used to notify about violations;
116     * if empty then the default message is used.
117     *
118     * @param message custom message which should be used to report about violations.
119     * @since 3.2
120     */
121    public void setMessage(String message) {
122        this.message = Objects.requireNonNullElse(message, "");
123    }
124
125    /**
126     * Setter to define the RegExp for illegal pattern.
127     *
128     * @param format a {@code String} value
129     * @since 3.2
130     */
131    public void setFormat(String format) {
132        formatString = format;
133        updateRegexp();
134    }
135
136    /**
137     * Setter to control whether to ignore case when matching.
138     *
139     * @param caseInsensitive true if the match is case-insensitive.
140     * @since 3.2
141     */
142    public void setIgnoreCase(boolean caseInsensitive) {
143        ignoreCase = caseInsensitive;
144        updateRegexp();
145    }
146
147    /**
148     * Updates the {@link #format} based on the values from {@link #formatString} and
149     * {@link #ignoreCase}.
150     */
151    private void updateRegexp() {
152        final int compileFlags;
153        if (ignoreCase) {
154            compileFlags = Pattern.CASE_INSENSITIVE;
155        }
156        else {
157            compileFlags = 0;
158        }
159        format = CommonUtil.createPattern(formatString, compileFlags);
160    }
161
162}