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.checks;
021
022import com.google.common.collect.Maps;
023import org.apache.commons.lang.StringUtils;
024import org.sonar.api.batch.SonarIndex;
025import org.sonar.api.issue.Issue;
026import org.sonar.api.issue.batch.IssueFilterChain;
027import org.sonar.api.resources.Resource;
028
029import java.util.Map;
030import java.util.Set;
031
032/**
033 * @since 2.1
034 * @deprecated in 3.6. Replaced by {@link org.sonar.api.issue.NoSonarFilter}
035 */
036@Deprecated
037public class NoSonarFilter implements org.sonar.api.issue.batch.IssueFilter {
038
039  private final Map<String, Set<Integer>> noSonarLinesByKey = Maps.newHashMap();
040  private SonarIndex sonarIndex;
041
042  public NoSonarFilter(SonarIndex sonarIndex) {
043    this.sonarIndex = sonarIndex;
044  }
045
046  public void addResource(Resource model, Set<Integer> noSonarLines) {
047    if (model != null && noSonarLines != null) {
048      // Reload resource to handle backward compatibility of resource keys
049      Resource resource = sonarIndex.getResource(model);
050      if (resource != null) {
051        noSonarLinesByKey.put(resource.getEffectiveKey(), noSonarLines);
052      }
053    }
054  }
055
056  @Override
057  public boolean accept(Issue issue, IssueFilterChain chain) {
058    boolean accepted = true;
059    if (issue.line() != null) {
060      Set<Integer> noSonarLines = noSonarLinesByKey.get(issue.componentKey());
061      accepted = noSonarLines == null || !noSonarLines.contains(issue.line());
062      if (!accepted && StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar")) {
063        accepted = true;
064      }
065    }
066    if (accepted) {
067      accepted = chain.accept(issue);
068    }
069    return accepted;
070  }
071}