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.TreeWalkerAuditEvent;
026import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
027
028/**
029 * <div>
030 * Filter {@code SuppressionXpathSingleFilter} suppresses audit events for Checks
031 * violations in the specified file, class, checks, message, module id, and xpath.
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
041 * on common files/folders is better to put in checkstyle configuration as common rule.
042 * All suppression that are for specific file names is better to keep in project
043 * specific config file.
044 * </p>
045 *
046 * <p>
047 * Attention: This filter only supports single suppression, and will need multiple
048 * instances if users wants to suppress multiple violations.
049 * </p>
050 *
051 * <p>
052 * Notes:
053 * {@code SuppressionXpathSingleFilter} can suppress Checks that have {@code Treewalker} as
054 * parent module.
055 * </p>
056 *
057 * @since 8.18
058 */
059public class SuppressionXpathSingleFilter extends AbstractAutomaticBean implements
060        TreeWalkerFilter {
061
062    /**
063     * XpathFilterElement instance.
064     */
065    private XpathFilterElement xpathFilter;
066    /**
067     * Define a Regular Expression matched against the file name associated with an audit event.
068     */
069    private Pattern files;
070    /**
071     * Define a Regular Expression matched against the name of the check associated
072     * with an audit event.
073     */
074    private Pattern checks;
075    /**
076     * Define a Regular Expression matched against the message of the check
077     * associated with an audit event.
078     */
079    private Pattern message;
080    /**
081     * Define a string matched against the ID of the check associated with an audit event.
082     */
083    private String id;
084    /**
085     * Define a string xpath query.
086     */
087    private String query;
088
089    /**
090     * Creates a new {@code SuppressionXpathSingleFilter} instance.
091     */
092    public SuppressionXpathSingleFilter() {
093        // no code by default
094    }
095
096    /**
097     * Setter to define a Regular Expression matched against the file name
098     * associated with an audit event.
099     *
100     * @param files the name of the file
101     * @since 8.18
102     */
103    public void setFiles(String files) {
104        if (files == null) {
105            this.files = null;
106        }
107        else {
108            this.files = Pattern.compile(files);
109        }
110    }
111
112    /**
113     * Setter to define a Regular Expression matched against the name of the check
114     * associated with an audit event.
115     * The pattern is matched against the fully qualified class name of the Check.
116     *
117     * @param checks the name of the check
118     * @since 8.18
119     */
120    public void setChecks(String checks) {
121        if (checks == null) {
122            this.checks = null;
123        }
124        else {
125            this.checks = Pattern.compile(checks);
126        }
127    }
128
129    /**
130     * Setter to define a Regular Expression matched against the message of
131     * the check associated with an audit event.
132     *
133     * @param message the message of the check
134     * @since 8.18
135     */
136    public void setMessage(String message) {
137        if (message == null) {
138            this.message = null;
139        }
140        else {
141            this.message = Pattern.compile(message);
142        }
143    }
144
145    /**
146     * Setter to define a string matched against the ID of the check associated with an audit event.
147     *
148     * @param id the ID of the check
149     * @since 8.18
150     */
151    public void setId(String id) {
152        this.id = id;
153    }
154
155    /**
156     * Setter to define a string xpath query.
157     *
158     * @param query the xpath query
159     * @since 8.18
160     */
161    public void setQuery(String query) {
162        this.query = query;
163    }
164
165    @Override
166    protected void finishLocalSetup() {
167        xpathFilter = new XpathFilterElement(files, checks, message, id, query);
168    }
169
170    @Override
171    public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
172        return xpathFilter.accept(treeWalkerAuditEvent);
173    }
174
175}