2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.plugins.core.issue.ignore.pattern;
22 import com.google.common.collect.LinkedHashMultimap;
23 import com.google.common.collect.Multimap;
24 import org.sonar.api.issue.Issue;
26 import java.util.Collection;
27 import java.util.Iterator;
30 public class PatternMatcher {
32 private Multimap<String, IssuePattern> patternByComponent = LinkedHashMultimap.create();
34 public IssuePattern getMatchingPattern(Issue issue) {
35 IssuePattern matchingPattern = null;
36 Iterator<IssuePattern> patternIterator = getPatternsForComponent(issue.componentKey()).iterator();
37 while(matchingPattern == null && patternIterator.hasNext()) {
38 IssuePattern nextPattern = patternIterator.next();
39 if (nextPattern.match(issue)) {
40 matchingPattern = nextPattern;
43 return matchingPattern;
46 public Collection<IssuePattern> getPatternsForComponent(String componentKey) {
47 return patternByComponent.get(componentKey);
50 public void addPatternForComponent(String component, IssuePattern pattern) {
51 patternByComponent.put(component, pattern.forResource(component));
54 public void addPatternToExcludeResource(String resource) {
55 addPatternForComponent(resource, new IssuePattern(resource, "*").setCheckLines(false));
58 public void addPatternToExcludeLines(String resource, Set<LineRange> lineRanges) {
59 addPatternForComponent(resource, new IssuePattern(resource, "*", lineRanges).setCheckLines(true));