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.filters;
021
022import java.util.regex.Pattern;
023
024import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
025import com.puppycrawl.tools.checkstyle.api.AuditEvent;
026import com.puppycrawl.tools.checkstyle.api.Filter;
027
028/**
029 * <div>
030 * Filter {@code SuppressionSingleFilter} suppresses audit events for Checks violations in the
031 * specified file, class, checks, message, module id, lines, and columns.
032 * </div>
033 *
034 * <p>
035 * Rationale: To allow users to use suppressions configured in the same config as other modules.
036 * {@code SuppressionFilter} and {@code SuppressionXpathFilter} require a separate file.
037 * </p>
038 *
039 * <p>
040 * Advice: If checkstyle configuration is used for several projects, single suppressions on common
041 * files/folders is better to put in checkstyle configuration as common rule. All suppression that
042 * are for specific file names is better to keep in project specific config file.
043 * </p>
044 *
045 * <p>
046 * Attention: This filter only supports single suppression, and will need multiple instances if
047 * users wants to suppress multiple violations.
048 * </p>
049 *
050 * <p>
051 * Notes:
052 * {@code SuppressionSingleFilter} can suppress Checks that have {@code Treewalker} or
053 * {@code Checker} as parent module.
054 * </p>
055 *
056 * @since 8.23
057 */
058public class SuppressionSingleFilter extends AbstractAutomaticBean implements Filter {
059
060    /**
061     * SuppressFilterElement instance.
062     */
063    private SuppressFilterElement filter;
064    /**
065     * Define the RegExp for matching against the file name associated with an audit event.
066     */
067    private Pattern files;
068    /**
069     * Define the RegExp for matching against the name of the check associated with an audit event.
070     */
071    private Pattern checks;
072    /**
073     * Define the RegExp for matching against the message of the check associated with an audit
074     * event.
075     */
076    private Pattern message;
077    /**
078     * Specify a string matched against the ID of the check associated with an audit event.
079     */
080    private String id;
081    /**
082     * Specify a comma-separated list of values, where each value is an integer or a range of
083     * integers denoted by integer-integer.
084     */
085    private String lines;
086    /**
087     * Specify a comma-separated list of values, where each value is an integer or a range of
088     * integers denoted by integer-integer.
089     */
090    private String columns;
091
092    /**
093     * Setter to define the RegExp for matching against the file name associated with an audit
094     * event.
095     *
096     * @param files regular expression for filtered file names
097     * @since 8.23
098     */
099    public void setFiles(Pattern files) {
100        this.files = files;
101    }
102
103    /**
104     * Setter to define the RegExp for matching against the name of the check associated with an
105     * audit event.
106     * The pattern is matched against the fully qualified class name of the Check.
107     *
108     * @param checks the name of the check
109     * @since 8.23
110     */
111    public void setChecks(String checks) {
112        this.checks = Pattern.compile(checks);
113    }
114
115    /**
116     * Setter to define the RegExp for matching against the message of the check associated with
117     * an audit event.
118     *
119     * @param message the message of the check
120     * @since 8.23
121     */
122    public void setMessage(Pattern message) {
123        this.message = message;
124    }
125
126    /**
127     * Setter to specify a string matched against the ID of the check associated with an audit
128     * event.
129     *
130     * @param id the ID of the check
131     * @since 8.23
132     */
133    public void setId(String id) {
134        this.id = id;
135    }
136
137    /**
138     * Setter to specify a comma-separated list of values, where each value is an integer or a
139     * range of integers denoted by integer-integer.
140     *
141     * @param lines the lines of the check
142     * @since 8.23
143     */
144    public void setLines(String lines) {
145        this.lines = lines;
146    }
147
148    /**
149     * Setter to specify a comma-separated list of values, where each value is an integer or a
150     * range of integers denoted by integer-integer.
151     *
152     * @param columns the columns of the check
153     * @since 8.23
154     */
155    public void setColumns(String columns) {
156        this.columns = columns;
157    }
158
159    @Override
160    protected void finishLocalSetup() {
161        filter = new SuppressFilterElement(files, checks, message, id, lines, columns);
162    }
163
164    @Override
165    public boolean accept(AuditEvent event) {
166        return filter.accept(event);
167    }
168
169}