| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IllegalTokenTextCheck |
|
| 1.625;1.625 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.coding; | |
| 20 | ||
| 21 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 23 | import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; | |
| 24 | import java.util.Set; | |
| 25 | import java.util.regex.Pattern; | |
| 26 | ||
| 27 | /** | |
| 28 | * <p> | |
| 29 | * Checks for illegal token text. | |
| 30 | * </p> | |
| 31 | * <p> An example of how to configure the check to forbid String literals | |
| 32 | * containing <code>"a href"</code> is: | |
| 33 | * </p> | |
| 34 | * <pre> | |
| 35 | * <module name="IllegalTokenText"> | |
| 36 | * <property name="tokens" value="STRING_LITERAL"/> | |
| 37 | * <property name="format" value="a href"/> | |
| 38 | * </module> | |
| 39 | * </pre> | |
| 40 | * <p> An example of how to configure the check to forbid leading zeros in an | |
| 41 | * integer literal, other than zero and a hex literal is: | |
| 42 | * </p> | |
| 43 | * <pre> | |
| 44 | * <module name="IllegalTokenText"> | |
| 45 | * <property name="tokens" value="NUM_INT,NUM_LONG"/> | |
| 46 | * <property name="format" value="^0[^lx]"/> | |
| 47 | * <property name="ignoreCase" value="true"/> | |
| 48 | * </module> | |
| 49 | * </pre> | |
| 50 | * @author Rick Giles | |
| 51 | */ | |
| 52 | public class IllegalTokenTextCheck | |
| 53 | extends AbstractFormatCheck | |
| 54 | { | |
| 55 | /** | |
| 56 | * Custom message for report if illegal regexp found | |
| 57 | * ignored if empty. | |
| 58 | */ | |
| 59 | 2 | private String mMessage = ""; |
| 60 | ||
| 61 | /** | |
| 62 | * Instantiates a new instance. | |
| 63 | */ | |
| 64 | public IllegalTokenTextCheck() | |
| 65 | { | |
| 66 | 2 | super("$^"); // the empty language |
| 67 | 2 | } |
| 68 | ||
| 69 | @Override | |
| 70 | public void beginTree(DetailAST aRootAST) | |
| 71 | { | |
| 72 | 2 | } |
| 73 | ||
| 74 | @Override | |
| 75 | public int[] getDefaultTokens() | |
| 76 | { | |
| 77 | 0 | return new int[0]; |
| 78 | } | |
| 79 | ||
| 80 | @Override | |
| 81 | public int[] getAcceptableTokens() | |
| 82 | { | |
| 83 | // Any tokens set by property 'tokens' are acceptable | |
| 84 | 2 | final Set<String> tokenNames = getTokenNames(); |
| 85 | 2 | final int[] result = new int[tokenNames.size()]; |
| 86 | 2 | int i = 0; |
| 87 | 2 | for (final String name : tokenNames) { |
| 88 | 2 | result[i] = TokenTypes.getTokenId(name); |
| 89 | 2 | i++; |
| 90 | } | |
| 91 | 2 | return result; |
| 92 | } | |
| 93 | ||
| 94 | @Override | |
| 95 | public void visitToken(DetailAST aAST) | |
| 96 | { | |
| 97 | 4 | final String text = aAST.getText(); |
| 98 | 4 | if (getRegexp().matcher(text).find()) { |
| 99 | 3 | String message = getMessage(); |
| 100 | 3 | if ("".equals(message)) { |
| 101 | 3 | message = "illegal.token.text"; |
| 102 | } | |
| 103 | 3 | log( |
| 104 | aAST.getLineNo(), | |
| 105 | aAST.getColumnNo(), | |
| 106 | message, | |
| 107 | getFormat()); | |
| 108 | } | |
| 109 | 4 | } |
| 110 | ||
| 111 | /** | |
| 112 | * Setter for message property. | |
| 113 | * @param aMessage custom message which should be used | |
| 114 | * to report about violations. | |
| 115 | */ | |
| 116 | public void setMessage(String aMessage) | |
| 117 | { | |
| 118 | 0 | mMessage = (null == aMessage) ? "" : aMessage; |
| 119 | 0 | } |
| 120 | ||
| 121 | /** | |
| 122 | * Getter for message property. | |
| 123 | * @return custom message which should be used | |
| 124 | * to report about violations. | |
| 125 | */ | |
| 126 | public String getMessage() | |
| 127 | { | |
| 128 | 3 | return mMessage; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Set whether or not the match is case sensitive. | |
| 133 | * @param aCaseInsensitive true if the match is case insensitive. | |
| 134 | */ | |
| 135 | public void setIgnoreCase(boolean aCaseInsensitive) | |
| 136 | { | |
| 137 | 1 | if (aCaseInsensitive) { |
| 138 | 1 | setCompileFlags(Pattern.CASE_INSENSITIVE); |
| 139 | } | |
| 140 | 1 | } |
| 141 | } |