001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 expression. 041 */ 042 /* package */ PkgImportRule(final boolean allow, final boolean localOnly, 043 final String pkgName, final boolean exactMatch, final boolean regExp) { 044 super(allow, localOnly, regExp); 045 this.pkgName = pkgName; 046 this.exactMatch = exactMatch; 047 } 048 049 /** 050 * Verifies whether a package name is used. 051 * 052 * @param forImport the import to check. 053 * @return a result {@link AccessResult} indicating whether it can be used. 054 */ 055 @Override 056 public AccessResult verifyImport(final String forImport) { 057 // First check that we actually match the package. 058 // Then check if matched and f we must be an exact match. 059 // In this case, the text after the first "." must not contain 060 // another "." as this indicates that it is not an exact match. 061 062 boolean pkgMatch; 063 064 if (isRegExp()) { 065 pkgMatch = forImport.matches(pkgName + "\\..*"); 066 067 if (pkgMatch && exactMatch) { 068 pkgMatch = !forImport.matches(pkgName + "\\..*\\..*"); 069 } 070 } 071 else { 072 pkgMatch = forImport.startsWith(pkgName + "."); 073 074 if (pkgMatch && exactMatch) { 075 pkgMatch = forImport.indexOf('.', 076 pkgName.length() + 1) == -1; 077 } 078 } 079 080 return calculateResult(pkgMatch); 081 } 082 083}