001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.imports;
021
022/**
023 * Represents whether a package is allowed to be imported or not.
024 */
025class PkgImportRule extends AbstractImportRule {
026
027    /** Package to control access to. */
028    private final String pkgName;
029
030    /** Indicates if the package name must be an exact match. */
031    private final boolean exactMatch;
032
033    /**
034     * Constructs an instance.
035     *
036     * @param allow whether to allow access.
037     * @param localOnly whether the rule is to be applied locally only
038     * @param pkgName the package to apply the rule on.
039     * @param exactMatch whether the package name must match exactly.
040     * @param regExp whether the package name is to be interpreted as a regular
041     *        expression.
042     */
043    /* package */ PkgImportRule(final boolean allow, final boolean localOnly,
044        final String pkgName, final boolean exactMatch, final boolean regExp) {
045        super(allow, localOnly, regExp);
046        this.pkgName = pkgName;
047        this.exactMatch = exactMatch;
048    }
049
050    /**
051     * Verifies whether a package name is used.
052     *
053     * @param forImport the import to check.
054     * @return a result {@link AccessResult} indicating whether it can be used.
055     */
056    @Override
057    public AccessResult verifyImport(final String forImport) {
058        // First check that we actually match the package.
059        // Then check if matched and f we must be an exact match.
060        // In this case, the text after the first "." must not contain
061        // another "." as this indicates that it is not an exact match.
062
063        boolean pkgMatch;
064
065        if (isRegExp()) {
066            pkgMatch = forImport.matches(pkgName + "\\..*");
067
068            if (pkgMatch && exactMatch) {
069                pkgMatch = !forImport.matches(pkgName + "\\..*\\..*");
070            }
071        }
072        else {
073            pkgMatch = forImport.startsWith(pkgName + ".");
074
075            if (pkgMatch && exactMatch) {
076                pkgMatch = forImport.indexOf('.',
077                        pkgName.length() + 1) == -1;
078            }
079        }
080
081        return calculateResult(pkgMatch);
082    }
083
084}