001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.issue.ignore;
021    
022    import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer;
023    import org.sonar.batch.issue.ignore.pattern.IssuePattern;
024    import org.sonar.batch.issue.ignore.pattern.PatternMatcher;
025    
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    import org.sonar.api.issue.Issue;
029    import org.sonar.api.issue.batch.IssueFilter;
030    import org.sonar.api.issue.batch.IssueFilterChain;
031    
032    public class IgnoreIssuesFilter implements IssueFilter {
033    
034      private PatternMatcher patternMatcher;
035    
036      private static final Logger LOG = LoggerFactory.getLogger(IgnoreIssuesFilter.class);
037    
038      public IgnoreIssuesFilter(IssueExclusionPatternInitializer patternInitializer) {
039        this.patternMatcher = patternInitializer.getPatternMatcher();
040      }
041    
042      @Override
043      public boolean accept(Issue issue, IssueFilterChain chain) {
044        if (hasMatchFor(issue)) {
045          return false;
046        } else {
047          return chain.accept(issue);
048        }
049      }
050    
051      private boolean hasMatchFor(Issue issue) {
052        IssuePattern pattern = patternMatcher.getMatchingPattern(issue);
053        if (pattern != null) {
054          logExclusion(issue, pattern);
055          return true;
056        }
057        return false;
058      }
059    
060      private void logExclusion(Issue issue, IssuePattern pattern) {
061        LOG.debug("Issue {} ignored by exclusion pattern {}", issue, pattern);
062      }
063    }