001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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.naming;
021
022import java.util.regex.Pattern;
023
024import com.puppycrawl.tools.checkstyle.api.Check;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.TokenTypes;
027import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
028
029/**
030 * Abstract class for checking that names conform to a specified format.
031 *
032 * @author Rick Giles
033 */
034public abstract class AbstractNameCheck
035    extends Check {
036    /**
037     * Message key for invalid pattern error.
038     */
039    public static final String MSG_INVALID_PATTERN = "name.invalidPattern";
040
041    /** The format string of the regexp. */
042    private String format;
043
044    /** The regexp to match against. */
045    private Pattern regexp;
046
047    /**
048     * Creates a new {@code AbstractNameCheck} instance.
049     * @param format format to check with
050     */
051    protected AbstractNameCheck(String format) {
052        setFormat(format);
053    }
054
055    /**
056     * Set the format to the specified regular expression.
057     * @param format a {@code String} value
058     * @throws org.apache.commons.beanutils.ConversionException unable to parse format
059     */
060    public final void setFormat(String format) {
061        this.format = format;
062        regexp = CommonUtils.createPattern(format);
063    }
064
065    @Override
066    public void visitToken(DetailAST ast) {
067        if (mustCheckName(ast)) {
068            final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT);
069            if (!regexp.matcher(nameAST.getText()).find()) {
070                log(nameAST.getLineNo(),
071                    nameAST.getColumnNo(),
072                    MSG_INVALID_PATTERN,
073                    nameAST.getText(),
074                    format);
075            }
076        }
077    }
078
079    /**
080     * Decides whether the name of an AST should be checked against
081     * the format regexp.
082     * @param ast the AST to check.
083     * @return true if the IDENT subnode of ast should be checked against
084     *     the format regexp.
085     */
086    protected abstract boolean mustCheckName(DetailAST ast);
087}