1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 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.TreeWalkerAuditEvent; 25 import com.puppycrawl.tools.checkstyle.TreeWalkerFilter; 26 import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 27 28 /** 29 * Filter {@code SuppressionXpathSingleFilter} suppresses audit events for 30 * Checks violations in the specified file, class, checks, message, module id, 31 * and xpath. 32 * Attention: This filter only supports single suppression, and will need 33 * multiple instances if users wants to suppress multiple violations. 34 */ 35 public class SuppressionXpathSingleFilter extends AutomaticBean implements 36 TreeWalkerFilter { 37 /** 38 * XpathFilterElement instance. 39 */ 40 private XpathFilterElement xpathFilter; 41 /** 42 * The pattern for file names. 43 */ 44 private Pattern files; 45 /** 46 * The pattern for check class names. 47 */ 48 private Pattern checks; 49 /** 50 * The pattern for message names. 51 */ 52 private Pattern message; 53 /** 54 * Module id of filter. 55 */ 56 private String id; 57 /** 58 * Xpath query. 59 */ 60 private String query; 61 62 /** 63 * Set the regular expression to specify names of files to suppress. 64 * @param files the name of the file 65 */ 66 public void setFiles(String files) { 67 if (files == null) { 68 this.files = null; 69 } 70 else { 71 this.files = Pattern.compile(files); 72 } 73 } 74 75 /** 76 * Set the regular expression to specify the name of the check to suppress. 77 * @param checks the name of the check 78 */ 79 public void setChecks(String checks) { 80 if (checks == null) { 81 this.checks = null; 82 } 83 else { 84 this.checks = Pattern.compile(checks); 85 } 86 } 87 88 /** 89 * Set the regular expression to specify the message of the check to suppress. 90 * @param message the message of the check 91 */ 92 public void setMessage(String message) { 93 if (message == null) { 94 this.message = null; 95 } 96 else { 97 this.message = Pattern.compile(message); 98 } 99 } 100 101 /** 102 * Set the ID of the check to suppress. 103 * @param id the ID of the check 104 */ 105 public void setId(String id) { 106 this.id = id; 107 } 108 109 /** 110 * Set the xpath query. 111 * @param query the xpath query 112 */ 113 public void setQuery(String query) { 114 this.query = query; 115 } 116 117 @Override 118 protected void finishLocalSetup() { 119 xpathFilter = new XpathFilterElement(files, checks, message, id, query); 120 } 121 122 @Override 123 public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) { 124 return xpathFilter.accept(treeWalkerAuditEvent); 125 } 126 127 }