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 022import java.util.regex.Pattern; 023 024/** 025 * Represents an import rules for a specific file. Only the file name is 026 * considered and only files processed by TreeWalker. The file's 027 * extension is ignored. 028 */ 029class FileImportControl extends AbstractImportControl { 030 031 /** The name for the file. */ 032 private final String name; 033 /** The regex pattern for exact matches - only not null if regex is true. */ 034 private final Pattern patternForExactMatch; 035 /** If this file name represents a regular expression. */ 036 private final boolean regex; 037 038 /** 039 * Construct a file node. 040 * 041 * @param parent the parent node. 042 * @param name the name of the file. 043 * @param regex flags interpretation of name as regex pattern. 044 */ 045 /* package */ FileImportControl(PkgImportControl parent, String name, boolean regex) { 046 super(parent, MismatchStrategy.DELEGATE_TO_PARENT); 047 this.regex = regex; 048 this.name = name; 049 patternForExactMatch = createPatternForExactMatch(name); 050 } 051 052 /** 053 * Creates a Pattern from {@code expression}. 054 * 055 * @param expression a self-contained regular expression matching the full 056 * file name exactly. 057 * @return a Pattern. 058 */ 059 private static Pattern createPatternForExactMatch(String expression) { 060 return Pattern.compile(expression); 061 } 062 063 @Override 064 public AbstractImportControl locateFinest(String forPkg, String forFileName) { 065 AbstractImportControl finestMatch = null; 066 // Check if we are a match. 067 if (matchesExactly(forPkg, forFileName)) { 068 finestMatch = this; 069 } 070 return finestMatch; 071 } 072 073 @Override 074 protected boolean matchesExactly(String pkg, String fileName) { 075 final boolean result; 076 if (fileName == null) { 077 result = false; 078 } 079 else if (regex) { 080 result = patternForExactMatch.matcher(fileName).matches(); 081 } 082 else { 083 result = name.equals(fileName); 084 } 085 return result; 086 } 087 088}