001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2022 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.filefilters;
021
022import java.util.regex.Pattern;
023
024import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
025import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
026
027/**
028 * <p>
029 * File filter {@code BeforeExecutionExclusionFileFilter} decides which files should be
030 * excluded from being processed by the utility.
031 * </p>
032 *
033 * <p>
034 * By default Checkstyle includes all files and sub-directories in a directory to be processed and
035 * checked for violations. Users could have files that are in these sub-directories that shouldn't
036 * be processed with their checkstyle configuration for various reasons, one of which is a valid
037 * Java file that won't pass Checkstyle's parser. When Checkstyle tries to parse a Java file and
038 * fails, it will throw an {@code Exception} and halt parsing any more files for violations.
039 * An example of a valid Java file Checkstyle can't parse is JDK 9's {@code module-info.java}.
040 * This file filter will exclude these problem files from being parsed, allowing the rest of the
041 * files to run normal and be validated.
042 * </p>
043 *
044 * <p>
045 * <b>Note:</b> When a file is excluded from the utility, it is excluded from all Checks and no
046 * testing for violations will be performed on them.
047 * </p>
048 * <ul>
049 * <li>
050 * Property {@code fileNamePattern} - Define regular expression to match the file name against.
051 * Type is {@code java.util.regex.Pattern}.
052 * Default value is {@code null}.</li>
053 * </ul>
054 *
055 * <p>
056 * To configure the filter to exclude all 'module-info.java' files:
057 * </p>
058 *
059 * <pre>
060 * &lt;module name=&quot;BeforeExecutionExclusionFileFilter&quot;&gt;
061 *   &lt;property name=&quot;fileNamePattern&quot; value=&quot;module\-info\.java$&quot;/&gt;
062 * &lt;/module&gt;
063 * </pre>
064 * <p>
065 * To configure the filter to run only on required files for example that ends with "Remote"
066 * or end with "Client" in names or named as "Remote.java" or "Client.java"
067 * use <a href="https://www.regular-expressions.info/lookaround.html">negative lookahead</a>:
068 * </p>
069 *
070 * <pre>
071 * &lt;module name=&quot;BeforeExecutionExclusionFileFilter&quot;&gt;
072 *   &lt;property name=&quot;fileNamePattern&quot;
073 *  value=&quot;^(?!.*(Remote\.java|Client\.java|[\\/]Remote\.java|[\\/]Client\.java)).*$&quot;/&gt;
074 * &lt;/module&gt;
075 * </pre>
076 * <p>
077 * To configure the filter to exclude all Test folder files:
078 * </p>
079 *
080 * <pre>
081 * &lt;module name=&quot;BeforeExecutionExclusionFileFilter&quot;&gt;
082 *   &lt;property name=&quot;fileNamePattern&quot;
083 *     value=&quot;.*[\\/]src[\\/]test[\\/].*$&quot;/&gt;
084 * &lt;/module&gt;
085 * </pre>
086 * <p>
087 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
088 * </p>
089 *
090 * @since 7.2
091 */
092public final class BeforeExecutionExclusionFileFilter extends AutomaticBean
093        implements BeforeExecutionFileFilter {
094
095    /** Define regular expression to match the file name against. */
096    private Pattern fileNamePattern;
097
098    /**
099     * Setter to define regular expression to match the file name against.
100     *
101     * @param fileNamePattern regular expression of the excluded file.
102     */
103    public void setFileNamePattern(Pattern fileNamePattern) {
104        this.fileNamePattern = fileNamePattern;
105    }
106
107    @Override
108    protected void finishLocalSetup() {
109        // No code by default
110    }
111
112    @Override
113    public boolean accept(String uri) {
114        return fileNamePattern == null || !fileNamePattern.matcher(uri).find();
115    }
116
117}