001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.naming;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.FullIdent;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck;
025
026/**
027 * <p>
028 * Checks that package names conform to a format specified
029 * by the format property. The format is a
030 * {@link java.util.regex.Pattern regular expression}
031 * and defaults to
032 * <strong>^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9]*)*$</strong>.
033 * </p>
034 * <p>
035 * The default format has been chosen to match the requirements in the
036 * <a
037 * href="http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#40169">
038 * Java Language specification</a> and the Sun coding conventions.
039 * However both underscores and uppercase letters are rather uncommon,
040 * so most projects should probably use
041 * <strong>^[a-z]+(\.[a-z][a-z0-9]*)*$</strong>.
042 * </p>
043 * <p>
044 * An example of how to configure the check is:
045 * </p>
046 * <pre>
047 * &lt;module name="PackageName"/&gt;
048 * </pre>
049 * <p>
050 * An example of how to configure the check for package names that begin with
051 * <code>com.puppycrawl.tools.checkstyle</code> is:
052 * </p>
053 * <pre>
054 * &lt;module name="PackageName"&gt;
055 *    &lt;property name="format"
056 *              value="^com\.puppycrawl\.tools\.checkstyle(\\.[a-zA-Z_][a-zA-Z_0-9]*)*$"/&gt;
057 * &lt;/module&gt;
058 * </pre>
059 *
060 * @author Oliver Burn
061 * @version 1.0
062 */
063public class PackageNameCheck
064    extends AbstractFormatCheck
065{
066    /**
067     * Creates a new <code>PackageNameCheck</code> instance.
068     */
069    public PackageNameCheck()
070    {
071        // Uppercase letters seem rather uncommon, but they're allowed in
072        // http://java.sun.com/docs/books/jls/
073        //   second_edition/html/packages.doc.html#40169
074        super("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
075    }
076
077    @Override
078    public int[] getDefaultTokens()
079    {
080        return new int[] {TokenTypes.PACKAGE_DEF};
081    }
082
083    @Override
084    public void visitToken(DetailAST ast)
085    {
086        final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
087        final FullIdent full = FullIdent.createFullIdent(nameAST);
088        if (!getRegexp().matcher(full.getText()).find()) {
089            log(full.getLineNo(),
090                full.getColumnNo(),
091                "name.invalidPattern",
092                full.getText(),
093                getFormat());
094        }
095    }
096}