View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  import java.util.regex.Pattern;
23  
24  /**
25   * Represents an import rules for a specific file. Only the file name is
26   * considered and only files processed by TreeWalker. The file's
27   * extension is ignored.
28   */
29  class FileImportControl extends AbstractImportControl {
30  
31      /** The name for the file. */
32      private final String name;
33      /** The regex pattern for exact matches - only not null if regex is true. */
34      private final Pattern patternForExactMatch;
35      /** If this file name represents a regular expression. */
36      private final boolean regex;
37  
38      /**
39       * Construct a file node.
40       *
41       * @param parent the parent node.
42       * @param name the name of the file.
43       * @param regex flags interpretation of name as regex pattern.
44       */
45      /* package */ FileImportControl(PkgImportControl parent, String name, boolean regex) {
46          super(parent, MismatchStrategy.DELEGATE_TO_PARENT);
47          this.regex = regex;
48          this.name = name;
49          patternForExactMatch = createPatternForExactMatch(name);
50      }
51  
52      /**
53       * Creates a Pattern from {@code expression}.
54       *
55       * @param expression a self-contained regular expression matching the full
56       *     file name exactly.
57       * @return a Pattern.
58       */
59      private static Pattern createPatternForExactMatch(String expression) {
60          return Pattern.compile(expression);
61      }
62  
63      @Override
64      public AbstractImportControl locateFinest(String forPkg, String forFileName) {
65          AbstractImportControl finestMatch = null;
66          // Check if we are a match.
67          if (matchesExactly(forPkg, forFileName)) {
68              finestMatch = this;
69          }
70          return finestMatch;
71      }
72  
73      @Override
74      protected boolean matchesExactly(String pkg, String fileName) {
75          final boolean result;
76          if (fileName == null) {
77              result = false;
78          }
79          else if (regex) {
80              result = patternForExactMatch.matcher(fileName).matches();
81          }
82          else {
83              result = name.equals(fileName);
84          }
85          return result;
86      }
87  
88  }