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.CommonUtil; 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 * <module name="IllegalTokenText"> 039 * <property name="tokens" value="STRING_LITERAL"/> 040 * <property name="format" value="a href"/> 041 * </module> 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 * <module name="IllegalTokenText"> 048 * <property name="tokens" value="NUM_INT,NUM_LONG"/> 049 * <property name="format" value="^0[^lx]"/> 050 * <property name="ignoreCase" value="true"/> 051 * </module> 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 CommonUtil.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 CommonUtil.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, 118 customMessage, 119 formatString); 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 formatString = 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 ignoreCase = caseInsensitive; 153 updateRegexp(); 154 } 155 156 /** 157 * Updates the {@link #format} based on the values from {@link #formatString} and 158 * {@link #ignoreCase}. 159 */ 160 private void updateRegexp() { 161 final int compileFlags; 162 if (ignoreCase) { 163 compileFlags = Pattern.CASE_INSENSITIVE; 164 } 165 else { 166 compileFlags = 0; 167 } 168 format = CommonUtil.createPattern(formatString, compileFlags); 169 } 170 171}