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.Collections;
23  import java.util.Set;
24  
25  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
26  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
27  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
28  import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
29  import com.puppycrawl.tools.checkstyle.api.Filter;
30  import com.puppycrawl.tools.checkstyle.api.FilterSet;
31  import com.puppycrawl.tools.checkstyle.utils.FilterUtil;
32  
33  /**
34   * <p>
35   * This filter accepts AuditEvents according to file, check, line, and
36   * column, as specified in a suppression file.
37   * </p>
38   */
39  public class SuppressionFilter extends AutomaticBean implements Filter, ExternalResourceHolder {
40  
41      /** Filename of suppression file. */
42      private String file;
43      /** Tells whether config file existence is optional. */
44      private boolean optional;
45      /** Set of individual suppresses. */
46      private FilterSet filters = new FilterSet();
47  
48      /**
49       * Sets name of the suppression file.
50       * @param fileName name of the suppressions file.
51       */
52      public void setFile(String fileName) {
53          file = fileName;
54      }
55  
56      /**
57       * Sets whether config file existence is optional.
58       * @param optional tells if config file existence is optional.
59       */
60      public void setOptional(boolean optional) {
61          this.optional = optional;
62      }
63  
64      @Override
65      public boolean accept(AuditEvent event) {
66          return filters.accept(event);
67      }
68  
69      @Override
70      protected void finishLocalSetup() throws CheckstyleException {
71          if (file != null) {
72              if (optional) {
73                  if (FilterUtil.isFileExists(file)) {
74                      filters = SuppressionsLoader.loadSuppressions(file);
75                  }
76                  else {
77                      filters = new FilterSet();
78                  }
79              }
80              else {
81                  filters = SuppressionsLoader.loadSuppressions(file);
82              }
83          }
84      }
85  
86      @Override
87      public Set<String> getExternalResourceLocations() {
88          return Collections.singleton(file);
89      }
90  
91  }