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