KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > IllegalTokenTextCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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 java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26 import com.puppycrawl.tools.checkstyle.checks.GenericIllegalRegexpCheck;
27
28 /**
29  * <p>
30  * Checks for illegal token text.
31  * </p>
32  * <p> An example of how to configure the check to forbid String literals
33  * containing <code>"a href"</code> is:
34  * </p>
35  * <pre>
36  * &lt;module name="IllegalTokenText"&gt;
37  * &lt;property name="tokens" value="STRING_LITERAL"/&gt;
38  * &lt;property name="format" value="a href"/&gt;
39  * &lt;/module&gt;
40  * </pre>
41  * <p> An example of how to configure the check to forbid leading zeros in an
42  * integer literal, other than zero and a hex literal is:
43  * </p>
44  * <pre>
45  * &lt;module name="IllegalTokenText"&gt;
46  * &lt;property name="tokens" value="NUM_INT,NUM_LONG"/&gt;
47  * &lt;property name="format" value="^0[^lx]"/&gt;
48  * &lt;property name="ignoreCase" value="true"/&gt;
49  * &lt;/module&gt;
50  * </pre>
51  * @author Rick Giles
52  */

53 public class IllegalTokenTextCheck
54     extends GenericIllegalRegexpCheck
55 {
56     /** {@inheritDoc} */
57     public void beginTree(DetailAST aRootAST)
58     {
59     }
60
61     /** {@inheritDoc} */
62     public int[] getAcceptableTokens()
63     {
64         // Any tokens set by property 'tokens' are acceptable
65
final Set JavaDoc tokenNames = getTokenNames();
66         final int[] result = new int[tokenNames.size()];
67         int i = 0;
68         final Iterator JavaDoc it = tokenNames.iterator();
69         while (it.hasNext()) {
70             final String JavaDoc name = (String JavaDoc) it.next();
71             result[i] = TokenTypes.getTokenId(name);
72             i++;
73         }
74         return result;
75     }
76     /**
77      * {@inheritDoc}
78      */

79     public void visitToken(DetailAST aAST)
80     {
81         final String JavaDoc text = aAST.getText();
82         if (getRegexp().matcher(text).find()) {
83             String JavaDoc message = getMessage();
84             if ("".equals(message)) {
85                 message = "illegal.token.text";
86             }
87             log(
88                 aAST.getLineNo(),
89                 aAST.getColumnNo(),
90                 message,
91                 getFormat());
92         }
93     }
94 }
95
Popular Tags