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