001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 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.utils.CommonUtils; 027import com.puppycrawl.tools.checkstyle.utils.TokenUtils; 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 * <module name="IllegalTokenText"> 038 * <property name="tokens" value="STRING_LITERAL"/> 039 * <property name="format" value="a href"/> 040 * </module> 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 * <module name="IllegalTokenText"> 047 * <property name="tokens" value="NUM_INT,NUM_LONG"/> 048 * <property name="format" value="^0[^lx]"/> 049 * <property name="ignoreCase" value="true"/> 050 * </module> 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 TokenUtils.getAllTokenIds(); 086 } 087 088 @Override 089 public int[] getRequiredTokens() { 090 return CommonUtils.EMPTY_INT_ARRAY; 091 } 092 093 @Override 094 public boolean isCommentNodesRequired() { 095 return true; 096 } 097 098 @Override 099 public void visitToken(DetailAST ast) { 100 final String text = ast.getText(); 101 if (regexp.matcher(text).find()) { 102 String customMessage = message; 103 if (customMessage.isEmpty()) { 104 customMessage = MSG_KEY; 105 } 106 log( 107 ast.getLineNo(), 108 ast.getColumnNo(), 109 customMessage, 110 format); 111 } 112 } 113 114 /** 115 * Setter for message property. 116 * @param message custom message which should be used 117 * to report about violations. 118 */ 119 public void setMessage(String message) { 120 if (message == null) { 121 this.message = ""; 122 } 123 else { 124 this.message = message; 125 } 126 } 127 128 /** 129 * Set the format to the specified regular expression. 130 * @param format a {@code String} value 131 * @throws org.apache.commons.beanutils.ConversionException unable to parse format 132 */ 133 public void setFormat(String format) { 134 this.format = format; 135 updateRegexp(); 136 } 137 138 /** 139 * Set whether or not the match is case sensitive. 140 * @param caseInsensitive true if the match is case insensitive. 141 */ 142 public void setIgnoreCase(boolean caseInsensitive) { 143 if (caseInsensitive) { 144 compileFlags = Pattern.CASE_INSENSITIVE; 145 } 146 else { 147 compileFlags = 0; 148 } 149 150 updateRegexp(); 151 } 152 153 /** 154 * Updates the {@link #regexp} based on the values from {@link #format} and 155 * {@link #compileFlags}. 156 */ 157 private void updateRegexp() { 158 regexp = CommonUtils.createPattern(format, compileFlags); 159 } 160}