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.Collections; 023import java.util.HashSet; 024import java.util.Objects; 025import java.util.Set; 026 027import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent; 028import com.puppycrawl.tools.checkstyle.TreeWalkerFilter; 029import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 030import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 031import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder; 032import com.puppycrawl.tools.checkstyle.utils.FilterUtil; 033 034/** 035 * This filter accepts TreeWalkerAuditEvents according to file, check and xpath query, 036 * as specified in a suppression file. 037 * 038 * @noinspection NonFinalFieldReferenceInEquals, NonFinalFieldReferencedInHashCode 039 */ 040public class SuppressionXpathFilter extends AutomaticBean implements 041 TreeWalkerFilter, ExternalResourceHolder { 042 043 /** Filename of suppression file. */ 044 private String file; 045 /** Tells whether config file existence is optional. */ 046 private boolean optional; 047 /** Set of individual xpath suppresses. */ 048 private Set<TreeWalkerFilter> filters = new HashSet<>(); 049 050 /** 051 * Sets name of the suppression file. 052 * @param fileName name of the suppressions file. 053 */ 054 public void setFile(String fileName) { 055 file = fileName; 056 } 057 058 /** 059 * Sets whether config file existence is optional. 060 * @param optional tells if config file existence is optional. 061 */ 062 public void setOptional(boolean optional) { 063 this.optional = optional; 064 } 065 066 @Override 067 public boolean equals(Object obj) { 068 if (this == obj) { 069 return true; 070 } 071 if (obj == null || getClass() != obj.getClass()) { 072 return false; 073 } 074 final SuppressionXpathFilter suppressionXpathFilter = (SuppressionXpathFilter) obj; 075 return Objects.equals(filters, suppressionXpathFilter.filters); 076 } 077 078 @Override 079 public int hashCode() { 080 return Objects.hash(filters); 081 } 082 083 @Override 084 public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) { 085 boolean result = true; 086 for (TreeWalkerFilter filter : filters) { 087 if (!filter.accept(treeWalkerAuditEvent)) { 088 result = false; 089 break; 090 } 091 } 092 return result; 093 } 094 095 @Override 096 public Set<String> getExternalResourceLocations() { 097 return Collections.singleton(file); 098 } 099 100 @Override 101 protected void finishLocalSetup() throws CheckstyleException { 102 if (file != null) { 103 if (optional) { 104 if (FilterUtil.isFileExists(file)) { 105 filters = SuppressionsLoader.loadXpathSuppressions(file); 106 } 107 else { 108 filters = new HashSet<>(); 109 } 110 } 111 else { 112 filters = SuppressionsLoader.loadXpathSuppressions(file); 113 } 114 } 115 } 116 117}