001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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    /** The name for the file. */
031    private final String name;
032    /** The regex pattern for exact matches - only not null if regex is true. */
033    private final Pattern patternForExactMatch;
034    /** If this file name represents a regular expression. */
035    private final boolean regex;
036
037    /**
038     * Construct a file node.
039     *
040     * @param parent the parent node.
041     * @param name the name of the file.
042     * @param regex flags interpretation of name as regex pattern.
043     */
044    /* package */ FileImportControl(PkgImportControl parent, String name, boolean regex) {
045        super(parent, MismatchStrategy.DELEGATE_TO_PARENT);
046        this.regex = regex;
047        this.name = name;
048        patternForExactMatch = createPatternForExactMatch(name);
049    }
050
051    /**
052     * Creates a Pattern from {@code expression}.
053     *
054     * @param expression a self-contained regular expression matching the full
055     *     file name exactly.
056     * @return a Pattern.
057     */
058    private static Pattern createPatternForExactMatch(String expression) {
059        return Pattern.compile(expression);
060    }
061
062    @Override
063    public AbstractImportControl locateFinest(String forPkg, String forFileName) {
064        AbstractImportControl finestMatch = null;
065        // Check if we are a match.
066        if (matchesExactly(forPkg, forFileName)) {
067            finestMatch = this;
068        }
069        return finestMatch;
070    }
071
072    @Override
073    protected boolean matchesExactly(String pkg, String fileName) {
074        final boolean result;
075        if (fileName == null) {
076            result = false;
077        }
078        else if (regex) {
079            result = patternForExactMatch.matcher(fileName).matches();
080        }
081        else {
082            result = name.equals(fileName);
083        }
084        return result;
085    }
086}