001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.TreeWalkerAuditEvent; 025import com.puppycrawl.tools.checkstyle.TreeWalkerFilter; 026import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 027 028/** 029 * Filter {@code SuppressionXpathSingleFilter} suppresses audit events for 030 * Checks violations in the specified file, class, checks, message, module id, 031 * and xpath. 032 * Attention: This filter only supports single suppression, and will need 033 * multiple instances if users wants to suppress multiple violations. 034 */ 035public class SuppressionXpathSingleFilter extends AutomaticBean implements 036 TreeWalkerFilter { 037 /** 038 * XpathFilterElement instance. 039 */ 040 private XpathFilterElement xpathFilter; 041 /** 042 * The pattern for file names. 043 */ 044 private Pattern files; 045 /** 046 * The pattern for check class names. 047 */ 048 private Pattern checks; 049 /** 050 * The pattern for message names. 051 */ 052 private Pattern message; 053 /** 054 * Module id of filter. 055 */ 056 private String id; 057 /** 058 * Xpath query. 059 */ 060 private String query; 061 062 /** 063 * Set the regular expression to specify names of files to suppress. 064 * @param files the name of the file 065 */ 066 public void setFiles(String files) { 067 if (files == null) { 068 this.files = null; 069 } 070 else { 071 this.files = Pattern.compile(files); 072 } 073 } 074 075 /** 076 * Set the regular expression to specify the name of the check to suppress. 077 * @param checks the name of the check 078 */ 079 public void setChecks(String checks) { 080 if (checks == null) { 081 this.checks = null; 082 } 083 else { 084 this.checks = Pattern.compile(checks); 085 } 086 } 087 088 /** 089 * Set the regular expression to specify the message of the check to suppress. 090 * @param message the message of the check 091 */ 092 public void setMessage(String message) { 093 if (message == null) { 094 this.message = null; 095 } 096 else { 097 this.message = Pattern.compile(message); 098 } 099 } 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}