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