001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 parent module. 054 * </p> 055 * <ul> 056 * <li> 057 * Property {@code checks} - Define a Regular Expression matched against the name 058 * of the check associated with an audit event. 059 * Type is {@code java.util.regex.Pattern}. 060 * Default value is {@code null}. 061 * </li> 062 * <li> 063 * Property {@code files} - Define a Regular Expression matched against the file 064 * name associated with an audit event. 065 * Type is {@code java.util.regex.Pattern}. 066 * Default value is {@code null}. 067 * </li> 068 * <li> 069 * Property {@code id} - Define a string matched against the ID of the check 070 * associated with an audit event. 071 * Type is {@code java.lang.String}. 072 * Default value is {@code null}. 073 * </li> 074 * <li> 075 * Property {@code message} - Define a Regular Expression matched against the message 076 * of the check associated with an audit event. 077 * Type is {@code java.util.regex.Pattern}. 078 * Default value is {@code null}. 079 * </li> 080 * <li> 081 * Property {@code query} - Define a string xpath query. 082 * Type is {@code java.lang.String}. 083 * Default value is {@code null}. 084 * </li> 085 * </ul> 086 * 087 * <p> 088 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 089 * </p> 090 * 091 * @since 8.18 092 */ 093public class SuppressionXpathSingleFilter extends AbstractAutomaticBean implements 094 TreeWalkerFilter { 095 /** 096 * XpathFilterElement instance. 097 */ 098 private XpathFilterElement xpathFilter; 099 /** 100 * Define a Regular Expression matched against the file name associated with an audit event. 101 */ 102 private Pattern files; 103 /** 104 * Define a Regular Expression matched against the name of the check associated 105 * with an audit event. 106 */ 107 private Pattern checks; 108 /** 109 * Define a Regular Expression matched against the message of the check 110 * associated with an audit event. 111 */ 112 private Pattern message; 113 /** 114 * Define a string matched against the ID of the check associated with an audit event. 115 */ 116 private String id; 117 /** 118 * Define a string xpath query. 119 */ 120 private String query; 121 122 /** 123 * Setter to define a Regular Expression matched against the file name 124 * associated with an audit event. 125 * 126 * @param files the name of the file 127 * @since 8.18 128 */ 129 public void setFiles(String files) { 130 if (files == null) { 131 this.files = null; 132 } 133 else { 134 this.files = Pattern.compile(files); 135 } 136 } 137 138 /** 139 * Setter to define a Regular Expression matched against the name of the check 140 * associated with an audit event. 141 * 142 * @param checks the name of the check 143 * @since 8.18 144 */ 145 public void setChecks(String checks) { 146 if (checks == null) { 147 this.checks = null; 148 } 149 else { 150 this.checks = Pattern.compile(checks); 151 } 152 } 153 154 /** 155 * Setter to define a Regular Expression matched against the message of 156 * the check associated with an audit event. 157 * 158 * @param message the message of the check 159 * @since 8.18 160 */ 161 public void setMessage(String message) { 162 if (message == null) { 163 this.message = null; 164 } 165 else { 166 this.message = Pattern.compile(message); 167 } 168 } 169 170 /** 171 * Setter to define a string matched against the ID of the check associated 172 * with an audit event. 173 * 174 * @param id the ID of the check 175 * @since 8.18 176 */ 177 public void setId(String id) { 178 this.id = id; 179 } 180 181 /** 182 * Setter to define a string xpath query. 183 * 184 * @param query the xpath query 185 * @since 8.18 186 */ 187 public void setQuery(String query) { 188 this.query = query; 189 } 190 191 @Override 192 protected void finishLocalSetup() { 193 xpathFilter = new XpathFilterElement(files, checks, message, id, query); 194 } 195 196 @Override 197 public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) { 198 return xpathFilter.accept(treeWalkerAuditEvent); 199 } 200 201}