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