01: ////////////////////////////////////////////////////////////////////////////////
02: // checkstyle: Checks Java source code for adherence to a set of rules.
03: // Copyright (C) 2001-2007 Oliver Burn
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
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:
20: package com.puppycrawl.tools.checkstyle.checks.naming;
21:
22: import com.puppycrawl.tools.checkstyle.api.DetailAST;
23: import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24: import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck;
25:
26: /**
27: * Abstract class for checking that names conform to a specified format.
28: *
29: * @author Rick Giles
30: * @version 1.0
31: */
32: public abstract class AbstractNameCheck extends AbstractFormatCheck {
33: /**
34: * Creates a new <code>AbstractNameCheck</code> instance.
35: * @param aFormat format to check with
36: */
37: public AbstractNameCheck(String aFormat) {
38: super (aFormat);
39: }
40:
41: /**
42: * Decides whether the name of an AST should be checked against
43: * the format regexp.
44: * @param aAST the AST to check.
45: * @return true if the IDENT subnode of aAST should be checked against
46: * the format regexp.
47: */
48: protected boolean mustCheckName(DetailAST aAST) {
49: return true;
50: }
51:
52: /** {@inheritDoc} */
53: public void visitToken(DetailAST aAST) {
54: if (mustCheckName(aAST)) {
55: final DetailAST nameAST = aAST
56: .findFirstToken(TokenTypes.IDENT);
57: if (!getRegexp().matcher(nameAST.getText()).find()) {
58: log(nameAST.getLineNo(), nameAST.getColumnNo(),
59: "name.invalidPattern", nameAST.getText(),
60: getFormat());
61: }
62: }
63: }
64: }
|