001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 */
020package org.sonar.api.issue;
021
022import org.sonar.api.scan.issue.filter.FilterableIssue;
023
024import org.sonar.api.scan.issue.filter.IssueFilter;
025import com.google.common.collect.Maps;
026import org.apache.commons.lang.StringUtils;
027import org.sonar.api.batch.ScannerSide;
028import org.sonar.api.batch.fs.InputFile;
029import org.sonar.api.batch.fs.internal.DefaultInputFile;
030import org.sonar.api.scan.issue.filter.IssueFilterChain;
031
032import java.util.Map;
033import java.util.Set;
034
035/**
036 * Issue filter used to ignore issues created on lines commented with the tag "NOSONAR".
037 * <br>
038 * Plugins, via {@link ScannerSide}s, must feed this filter by registering the
039 * lines that contain "NOSONAR". Note that filters are disabled for the issues reported by
040 * end-users from UI or web services.
041 *
042 * @since 3.6
043 */
044public class NoSonarFilter implements IssueFilter {
045
046  private final Map<String, Set<Integer>> noSonarLinesByResource = Maps.newHashMap();
047
048  /**
049   * @deprecated since 5.0 use {@link #noSonarInFile(InputFile, Set)}
050   */
051  @Deprecated
052  public NoSonarFilter addComponent(String componentKey, Set<Integer> noSonarLines) {
053    noSonarLinesByResource.put(componentKey, noSonarLines);
054    return this;
055  }
056
057  /**
058   * Register lines in a file that contains the NOSONAR flag.
059   * @param inputFile
060   * @param noSonarLines Line number starts at 1 in a file
061   * @since 5.0
062   */
063  public NoSonarFilter noSonarInFile(InputFile inputFile, Set<Integer> noSonarLines) {
064    noSonarLinesByResource.put(((DefaultInputFile) inputFile).key(), noSonarLines);
065    return this;
066  }
067
068  @Override
069  public boolean accept(FilterableIssue issue, IssueFilterChain chain) {
070    boolean accepted = true;
071    if (issue.line() != null) {
072      Set<Integer> noSonarLines = noSonarLinesByResource.get(issue.componentKey());
073      accepted = noSonarLines == null || !noSonarLines.contains(issue.line());
074      if (!accepted && StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar")) {
075        accepted = true;
076      }
077    }
078    if (accepted) {
079      accepted = chain.accept(issue);
080    }
081    return accepted;
082  }
083}