Coverage Report - com.puppycrawl.tools.checkstyle.filters.SuppressionXpathFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
SuppressionXpathFilter
100%
28/28
100%
16/16
2.429
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 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.HashSet;
 24  
 import java.util.Objects;
 25  
 import java.util.Set;
 26  
 
 27  
 import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
 28  
 import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
 29  
 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
 30  
 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
 31  
 import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
 32  
 import com.puppycrawl.tools.checkstyle.utils.FilterUtils;
 33  
 
 34  
 /**
 35  
  * This filter accepts TreeWalkerAuditEvents according to file, check and xpath query,
 36  
  * as specified in a suppression file.
 37  
  *
 38  
  * @author Timur Tibeyev.
 39  
  * @noinspection NonFinalFieldReferenceInEquals, NonFinalFieldReferencedInHashCode
 40  
  */
 41  15
 public class SuppressionXpathFilter extends AutomaticBean implements
 42  
         TreeWalkerFilter, ExternalResourceHolder {
 43  
 
 44  
     /** Filename of suppression file. */
 45  
     private String file;
 46  
     /** Tells whether config file existence is optional. */
 47  
     private boolean optional;
 48  
     /** Set of individual xpath suppresses. */
 49  15
     private Set<TreeWalkerFilter> filters = new HashSet<>();
 50  
 
 51  
     /**
 52  
      * Sets name of the suppression file.
 53  
      * @param fileName name of the suppressions file.
 54  
      */
 55  
     public void setFile(String fileName) {
 56  12
         file = fileName;
 57  12
     }
 58  
 
 59  
     /**
 60  
      * Sets whether config file existence is optional.
 61  
      * @param optional tells if config file existence is optional.
 62  
      */
 63  
     public void setOptional(boolean optional) {
 64  10
         this.optional = optional;
 65  10
     }
 66  
 
 67  
     @Override
 68  
     public boolean equals(Object obj) {
 69  99
         if (this == obj) {
 70  2
             return true;
 71  
         }
 72  97
         if (obj == null || getClass() != obj.getClass()) {
 73  5
             return false;
 74  
         }
 75  92
         final SuppressionXpathFilter suppressionXpathFilter = (SuppressionXpathFilter) obj;
 76  92
         return Objects.equals(filters, suppressionXpathFilter.filters);
 77  
     }
 78  
 
 79  
     @Override
 80  
     public int hashCode() {
 81  35
         return Objects.hash(filters);
 82  
     }
 83  
 
 84  
     @Override
 85  
     public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
 86  6
         boolean result = true;
 87  6
         for (TreeWalkerFilter filter : filters) {
 88  2
             if (!filter.accept(treeWalkerAuditEvent)) {
 89  1
                 result = false;
 90  1
                 break;
 91  
             }
 92  1
         }
 93  6
         return result;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public Set<String> getExternalResourceLocations() {
 98  3
         return Collections.singleton(file);
 99  
     }
 100  
 
 101  
     @Override
 102  
     protected void finishLocalSetup() throws CheckstyleException {
 103  13
         if (file != null) {
 104  11
             if (optional) {
 105  3
                 if (FilterUtils.isFileExists(file)) {
 106  2
                     filters = SuppressionsLoader.loadXpathSuppressions(file);
 107  
                 }
 108  
                 else {
 109  1
                     filters = new HashSet<>();
 110  
                 }
 111  
             }
 112  
             else {
 113  8
                 filters = SuppressionsLoader.loadXpathSuppressions(file);
 114  
             }
 115  
         }
 116  11
     }
 117  
 }