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