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 org.sonar.api.issue.batch.IssueFilterChain; 023 024import com.google.common.collect.Maps; 025import org.apache.commons.lang.StringUtils; 026 027import java.util.Map; 028import java.util.Set; 029 030/** 031 * Issue filter used to ignore issues created on lines commented with the tag "NOSONAR". 032 * <p/> 033 * Plugins, via {@link org.sonar.api.BatchExtension}s, must feed this filter by registering the 034 * lines that contain "NOSONAR". Note that filters are disabled for the issues reported by 035 * end-users from UI or web services. 036 * 037 * @since 3.6 038 */ 039public class NoSonarFilter implements org.sonar.api.issue.batch.IssueFilter { 040 041 private final Map<String, Set<Integer>> noSonarLinesByResource = Maps.newHashMap(); 042 043 public NoSonarFilter addComponent(String componentKey, Set<Integer> noSonarLines) { 044 noSonarLinesByResource.put(componentKey, noSonarLines); 045 return this; 046 } 047 048 @Override 049 public boolean accept(Issue issue, IssueFilterChain chain) { 050 boolean accepted = true; 051 if (issue.line() != null) { 052 Set<Integer> noSonarLines = noSonarLinesByResource.get(issue.componentKey()); 053 accepted = noSonarLines == null || !noSonarLines.contains(issue.line()); 054 if (!accepted && StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar")) { 055 accepted = true; 056 } 057 } 058 if (accepted) { 059 accepted = chain.accept(issue); 060 } 061 return accepted; 062 } 063}