1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2024 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.filters; 21 22 import java.util.regex.Pattern; 23 24 import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean; 25 import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent; 26 import com.puppycrawl.tools.checkstyle.TreeWalkerFilter; 27 28 /** 29 * <div> 30 * Filter {@code SuppressionXpathSingleFilter} suppresses audit events for Checks 31 * violations in the specified file, class, checks, message, module id, and xpath. 32 * </div> 33 * 34 * <p> 35 * Rationale: To allow users to use suppressions configured in the same config as other modules. 36 * {@code SuppressionFilter} and {@code SuppressionXpathFilter} require a separate file. 37 * </p> 38 * 39 * <p> 40 * Advice: If checkstyle configuration is used for several projects, single suppressions 41 * on common files/folders is better to put in checkstyle configuration as common rule. 42 * All suppression that are for specific file names is better to keep in project 43 * specific config file. 44 * </p> 45 * 46 * <p> 47 * Attention: This filter only supports single suppression, and will need multiple 48 * instances if users wants to suppress multiple violations. 49 * </p> 50 * 51 * <p> 52 * {@code SuppressionXpathSingleFilter} can suppress Checks that have {@code Treewalker} as parent module. 53 * </p> 54 * <ul> 55 * <li> 56 * Property {@code checks} - Define a Regular Expression matched against the name 57 * of the check associated with an audit event. 58 * Type is {@code java.util.regex.Pattern}. 59 * Default value is {@code null}. 60 * </li> 61 * <li> 62 * Property {@code files} - Define a Regular Expression matched against the file 63 * name associated with an audit event. 64 * Type is {@code java.util.regex.Pattern}. 65 * Default value is {@code null}. 66 * </li> 67 * <li> 68 * Property {@code id} - Define a string matched against the ID of the check 69 * associated with an audit event. 70 * Type is {@code java.lang.String}. 71 * Default value is {@code null}. 72 * </li> 73 * <li> 74 * Property {@code message} - Define a Regular Expression matched against the message 75 * of the check associated with an audit event. 76 * Type is {@code java.util.regex.Pattern}. 77 * Default value is {@code null}. 78 * </li> 79 * <li> 80 * Property {@code query} - Define a string xpath query. 81 * Type is {@code java.lang.String}. 82 * Default value is {@code null}. 83 * </li> 84 * </ul> 85 * 86 * <p> 87 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 88 * </p> 89 * 90 * @since 8.18 91 */ 92 public class SuppressionXpathSingleFilter extends AbstractAutomaticBean implements 93 TreeWalkerFilter { 94 /** 95 * XpathFilterElement instance. 96 */ 97 private XpathFilterElement xpathFilter; 98 /** 99 * Define a Regular Expression matched against the file name associated with an audit event. 100 */ 101 private Pattern files; 102 /** 103 * Define a Regular Expression matched against the name of the check associated 104 * with an audit event. 105 */ 106 private Pattern checks; 107 /** 108 * Define a Regular Expression matched against the message of the check 109 * associated with an audit event. 110 */ 111 private Pattern message; 112 /** 113 * Define a string matched against the ID of the check associated with an audit event. 114 */ 115 private String id; 116 /** 117 * Define a string xpath query. 118 */ 119 private String query; 120 121 /** 122 * Setter to define a Regular Expression matched against the file name 123 * associated with an audit event. 124 * 125 * @param files the name of the file 126 * @since 8.18 127 */ 128 public void setFiles(String files) { 129 if (files == null) { 130 this.files = null; 131 } 132 else { 133 this.files = Pattern.compile(files); 134 } 135 } 136 137 /** 138 * Setter to define a Regular Expression matched against the name of the check 139 * associated with an audit event. 140 * 141 * @param checks the name of the check 142 * @since 8.18 143 */ 144 public void setChecks(String checks) { 145 if (checks == null) { 146 this.checks = null; 147 } 148 else { 149 this.checks = Pattern.compile(checks); 150 } 151 } 152 153 /** 154 * Setter to define a Regular Expression matched against the message of 155 * the check associated with an audit event. 156 * 157 * @param message the message of the check 158 * @since 8.18 159 */ 160 public void setMessage(String message) { 161 if (message == null) { 162 this.message = null; 163 } 164 else { 165 this.message = Pattern.compile(message); 166 } 167 } 168 169 /** 170 * Setter to define a string matched against the ID of the check associated 171 * with an audit event. 172 * 173 * @param id the ID of the check 174 * @since 8.18 175 */ 176 public void setId(String id) { 177 this.id = id; 178 } 179 180 /** 181 * Setter to define a string xpath query. 182 * 183 * @param query the xpath query 184 * @since 8.18 185 */ 186 public void setQuery(String query) { 187 this.query = query; 188 } 189 190 @Override 191 protected void finishLocalSetup() { 192 xpathFilter = new XpathFilterElement(files, checks, message, id, query); 193 } 194 195 @Override 196 public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) { 197 return xpathFilter.accept(treeWalkerAuditEvent); 198 } 199 200 }