1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.imports;
21  
22  /**
23   * Represents whether a package is allowed to be imported or not.
24   */
25  class PkgImportRule extends AbstractImportRule {
26  
27      /** Package to control access to. */
28      private final String pkgName;
29  
30      /** Indicates if the package name must be an exact match. */
31      private final boolean exactMatch;
32  
33      /**
34       * Constructs an instance.
35       * @param allow whether to allow access.
36       * @param localOnly whether the rule is to be applied locally only
37       * @param pkgName the package to apply the rule on.
38       * @param exactMatch whether the package name must match exactly.
39       * @param regExp whether the package name is to be interpreted as a regular
40       *        expression.
41       */
42      /* package */ PkgImportRule(final boolean allow, final boolean localOnly,
43          final String pkgName, final boolean exactMatch, final boolean regExp) {
44          super(allow, localOnly, regExp);
45          this.pkgName = pkgName;
46          this.exactMatch = exactMatch;
47      }
48  
49      /**
50       * Verifies whether a package name is used.
51       * @param forImport the import to check.
52       * @return a result {@link AccessResult} indicating whether it can be used.
53       */
54      @Override
55      public AccessResult verifyImport(final String forImport) {
56          // First check that we actually match the package.
57          // Then check if matched and f we must be an exact match.
58          // In this case, the text after the first "." must not contain
59          // another "." as this indicates that it is not an exact match.
60  
61          boolean pkgMatch;
62  
63          if (isRegExp()) {
64              pkgMatch = forImport.matches(pkgName + "\\..*");
65  
66              if (pkgMatch && exactMatch) {
67                  pkgMatch = !forImport.matches(pkgName + "\\..*\\..*");
68              }
69          }
70          else {
71              pkgMatch = forImport.startsWith(pkgName + ".");
72  
73              if (pkgMatch && exactMatch) {
74                  pkgMatch = forImport.indexOf('.',
75                          pkgName.length() + 1) == -1;
76              }
77          }
78  
79          return calculateResult(pkgMatch);
80      }
81  
82  }