001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.api.AuditEvent;
025import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
026import com.puppycrawl.tools.checkstyle.api.Filter;
027
028/**
029 * <p>
030 * Filter {@code SuppressionSingleFilter} suppresses audit events for Checks violations in the
031 * specified file, class, checks, message, module id, lines, and columns.
032 * </p>
033 * <p>
034 * Rationale: To allow users use suppressions configured in the same config with other modules.
035 * SuppressionFilter and SuppressionXpathFilter are require separate file.
036 * </p>
037 * <p>
038 * Advice: If checkstyle configuration is used for several projects, single suppressions on common
039 * files/folders is better to put in checkstyle configuration as common rule. All suppression that
040 * are for specific file names is better to keep in project specific config file.
041 * </p>
042 * <p>
043 * Attention: This filter only supports single suppression, and will need multiple instances if
044 * users wants to suppress multiple violations.
045 * </p>
046 * <ul>
047 * <li>
048 * Property {@code files} - Define the RegExp for matching against the file name associated with
049 * an audit event. Default value is {@code null}.
050 * </li>
051 * <li>
052 * Property {@code checks} - Define the RegExp for matching against the name of the check
053 * associated with an audit event. Default value is {@code null}.
054 * </li>
055 * <li>
056 * Property {@code message} - Define the RegExp for matching against the message of the check
057 * associated with an audit event. Default value is {@code null}.
058 * </li>
059 * <li>
060 * Property {@code id} - Specify a string matched against the ID of the check associated with
061 * an audit event. Default value is {@code null}.
062 * </li>
063 * <li>
064 * Property {@code lines} - Specify a comma-separated list of values, where each value is an
065 * integer or a range of integers denoted by integer-integer. Default value is {@code null}.
066 * </li>
067 * <li>
068 * Property {@code columns} - Specify a comma-separated list of values, where each value is an
069 * integer or a range of integers denoted by integer-integer. Default value is {@code null}.
070 * </li>
071 * </ul>
072 * <p>
073 * The following suppressions directs a {@code SuppressionSingleFilter} to reject
074 * {@code JavadocStyleCheck} violations for lines 82 and 108 to 122 of file
075 * {@code AbstractComplexityCheck.java}, and
076 * {@code MagicNumberCheck} violations for line 221 of file
077 * {@code JavadocStyleCheck.java}, and {@code 'Missing a Javadoc comment'} violations for all lines
078 * and files:
079 * </p>
080 * <pre>
081 * &lt;module name="SuppressionSingleFilter"&gt;
082 *   &lt;property name="checks" value="JavadocStyleCheck"/&gt;
083 *   &lt;property name="files" value="AbstractComplexityCheck.java"/&gt;
084 *   &lt;property name="lines" value="82,108-122"/&gt;
085 * &lt;/module&gt;
086 * &lt;module name="SuppressionSingleFilter"&gt;
087 *   &lt;property name="checks" value="MagicNumberCheck"/&gt;
088 *   &lt;property name="files" value="JavadocStyleCheck.java"/&gt;
089 *   &lt;property name="lines" value="221"/&gt;
090 * &lt;/module&gt;
091 * &lt;module name="SuppressionSingleFilter"&gt;
092 *   &lt;property name="message" value="Missing a Javadoc comment"/&gt;
093 * &lt;/module&gt;
094 * </pre>
095 * <p>
096 * Suppress check by <a href="https://checkstyle.org/config.html#Id">module id</a> when config
097 * have two instances on the same check:
098 * </p>
099 * <pre>
100 * &lt;module name="SuppressionSingleFilter"&gt;
101 *   &lt;property name="id" value="stringEqual"/&gt;
102 *   &lt;property name="files" value="SomeTestCode.java"/&gt;
103 * &lt;/module&gt;
104 * </pre>
105 * <p>
106 * Suppress all checks for hidden files and folders:
107 * </p>
108 * <pre>
109 * &lt;module name="SuppressionSingleFilter"&gt;
110 *   &lt;property name="files" value="[/\\]\..+"/&gt;
111 *   &lt;property name="checks" value=".*"/&gt;
112 * &lt;/module&gt;
113 * </pre>
114 * <p>
115 * Suppress all checks for Maven-generated code:
116 * </p>
117 * <pre>
118 * &lt;module name="SuppressionSingleFilter"&gt;
119 *   &lt;property name="files" value="[/\\]target[/\\]"/&gt;
120 *   &lt;property name="checks" value=".*"/&gt;
121 * &lt;/module&gt;
122 * </pre>
123 * <p>
124 * Suppress all checks for archives, classes and other binary files:
125 * </p>
126 * <pre>
127 * &lt;module name="SuppressionSingleFilter"&gt;
128 *   &lt;property name="files" value=".+\.(?:jar|zip|war|class|tar|bin)$"/&gt;
129 *   &lt;property name="checks" value=".*"/&gt;
130 * &lt;/module&gt;
131 * </pre>
132 * <p>
133 * Suppress all checks for image files:
134 * </p>
135 * <pre>
136 * &lt;module name="SuppressionSingleFilter"&gt;
137 *   &lt;property name="files" value=".+\.(?:png|gif|jpg|jpeg)$"/&gt;
138 *   &lt;property name="checks" value=".*"/&gt;
139 * &lt;/module&gt;
140 * </pre>
141 * <p>
142 * Suppress all checks for non-java files:
143 * </p>
144 * <pre>
145 * &lt;module name="SuppressionSingleFilter"&gt;
146 *   &lt;property name="files"
147 *     value=".+\.(?:txt|xml|csv|sh|thrift|html|sql|eot|ttf|woff|css|png)$"/&gt;
148 *   &lt;property name="checks" value=".*"/&gt;
149 * &lt;/module&gt;
150 * </pre>
151 * <p>
152 * Suppress all checks in generated sources:
153 * </p>
154 * <pre>
155 * &lt;module name="SuppressionSingleFilter"&gt;
156 *   &lt;property name="files" value="com[\\/]mycompany[\\/]app[\\/]gen[\\/]"/&gt;
157 *   &lt;property name="checks" value=".*"/&gt;
158 * &lt;/module&gt;
159 * </pre>
160 * <p>
161 * Suppress FileLength check on integration tests in certain folder:
162 * </p>
163 * <pre>
164 * &lt;module name="SuppressionSingleFilter"&gt;
165 *   &lt;property name="files" value="com[\\/]mycompany[\\/]app[\\/].*IT.java"/&gt;
166 *   &lt;property name="checks" value="FileLength"/&gt;
167 * &lt;/module&gt;
168 * </pre>
169 * <p>
170 * Suppress naming violations on variable named 'log' in all files:
171 * </p>
172 * <pre>
173 * &lt;module name="SuppressionSingleFilter"&gt;
174 *   &lt;property name="message" value="Name 'log' must match pattern"/&gt;
175 * &lt;/module&gt;
176 * </pre>
177 * @since 8.23
178 */
179public class SuppressionSingleFilter extends AutomaticBean implements Filter {
180
181    /**
182     * SuppressFilterElement instance.
183     */
184    private SuppressFilterElement filter;
185    /**
186     * Define the RegExp for matching against the file name associated with an audit event.
187     */
188    private Pattern files;
189    /**
190     * Define the RegExp for matching against the name of the check associated with an audit event.
191     */
192    private Pattern checks;
193    /**
194     * Define the RegExp for matching against the message of the check associated with an audit
195     * event.
196     */
197    private Pattern message;
198    /**
199     * Specify a string matched against the ID of the check associated with an audit event.
200     */
201    private String id;
202    /**
203     * Specify a comma-separated list of values, where each value is an integer or a range of
204     * integers denoted by integer-integer.
205     */
206    private String lines;
207    /**
208     * Specify a comma-separated list of values, where each value is an integer or a range of
209     * integers denoted by integer-integer.
210     */
211    private String columns;
212
213    /**
214     * Setter to define the RegExp for matching against the file name associated with an audit
215     * event.
216     * @param files regular expression for filtered file names
217     */
218    public void setFiles(Pattern files) {
219        this.files = files;
220    }
221
222    /**
223     * Setter to define the RegExp for matching against the name of the check associated with an
224     * audit event.
225     * @param checks the name of the check
226     */
227    public void setChecks(String checks) {
228        this.checks = Pattern.compile(checks);
229    }
230
231    /**
232     * Setter to define the RegExp for matching against the message of the check associated with
233     * an audit event.
234     * @param message the message of the check
235     */
236    public void setMessage(Pattern message) {
237        this.message = message;
238    }
239
240    /**
241     * Setter to specify a string matched against the ID of the check associated with an audit
242     * event.
243     * @param id the ID of the check
244     */
245    public void setId(String id) {
246        this.id = id;
247    }
248
249    /**
250     * Setter to specify a comma-separated list of values, where each value is an integer or a
251     * range of integers denoted by integer-integer.
252     * @param lines the lines of the check
253     */
254    public void setLines(String lines) {
255        this.lines = lines;
256    }
257
258    /**
259     * Setter to specify a comma-separated list of values, where each value is an integer or a
260     * range of integers denoted by integer-integer.
261     * @param columns the columns of the check
262     */
263    public void setColumns(String columns) {
264        this.columns = columns;
265    }
266
267    @Override
268    protected void finishLocalSetup() {
269        filter = new SuppressFilterElement(files, checks, message, id, lines, columns);
270    }
271
272    @Override
273    public boolean accept(AuditEvent event) {
274        return filter.accept(event);
275    }
276
277}