001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.AbstractCheck;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.FullIdent;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028
029/**
030 * <p>
031 * Checks that package names conform to a format specified
032 * by the format property. The format is a
033 * {@link Pattern regular expression}
034 * and defaults to
035 * <strong>^[a-z]+(\.[a-zA-Z_][a-zA-Z_0-9_]*)*$</strong>.
036 * </p>
037 * <p>
038 * The default format has been chosen to match the requirements in the
039 * <a
040 * href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html">
041 * Java Language specification</a> and the Sun coding conventions.
042 * However both underscores and uppercase letters are rather uncommon,
043 * so most projects should probably use
044 * <strong>^[a-z]+(\.[a-z][a-z0-9]*)*$</strong>.
045 * </p>
046 * <p>
047 * An example of how to configure the check is:
048 * </p>
049 * <pre>
050 * &lt;module name="PackageName"/&gt;
051 * </pre>
052 * <p>
053 * An example of how to configure the check for package names that begin with
054 * {@code com.puppycrawl.tools.checkstyle} is:
055 * </p>
056 * <pre>
057 * &lt;module name="PackageName"&gt;
058 *    &lt;property name="format"
059 *              value="^com\.puppycrawl\.tools\.checkstyle(\.[a-zA-Z_][a-zA-Z_0-9]*)*$"/&gt;
060 * &lt;/module&gt;
061 * </pre>
062 *
063 * @author Oliver Burn
064 */
065public class PackageNameCheck
066    extends AbstractCheck {
067    /**
068     * A key is pointing to the warning message text in "messages.properties"
069     * file.
070     */
071    public static final String MSG_KEY = "name.invalidPattern";
072
073    /** The regexp to match against. */
074    // Uppercase letters seem rather uncommon, but they're allowed in
075    // http://docs.oracle.com/javase/specs/
076    //  second_edition/html/packages.doc.html#40169
077    private Pattern format = Pattern.compile("^[a-z]+(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$");
078
079    /**
080     * Set the format for the specified regular expression.
081     * @param pattern the new pattern
082     */
083    public void setFormat(Pattern pattern) {
084        format = pattern;
085    }
086
087    @Override
088    public int[] getDefaultTokens() {
089        return getAcceptableTokens();
090    }
091
092    @Override
093    public int[] getAcceptableTokens() {
094        return new int[] {TokenTypes.PACKAGE_DEF};
095    }
096
097    @Override
098    public int[] getRequiredTokens() {
099        return getAcceptableTokens();
100    }
101
102    @Override
103    public void visitToken(DetailAST ast) {
104        final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
105        final FullIdent full = FullIdent.createFullIdent(nameAST);
106        if (!format.matcher(full.getText()).find()) {
107            log(full.getLineNo(),
108                full.getColumnNo(),
109                MSG_KEY,
110                full.getText(),
111                format.pattern());
112        }
113    }
114}