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.
21 package org.sonar.plugins.core.issue.ignore.pattern;
23 import com.google.common.collect.Sets;
24 import org.apache.commons.lang.builder.ToStringBuilder;
25 import org.apache.commons.lang.builder.ToStringStyle;
26 import org.sonar.api.issue.Issue;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.utils.WildcardPattern;
32 public class IssuePattern {
34 private WildcardPattern resourcePattern;
35 private WildcardPattern rulePattern;
36 private Set<Integer> lines = Sets.newLinkedHashSet();
37 private Set<LineRange> lineRanges = Sets.newLinkedHashSet();
38 private String beginBlockRegexp;
39 private String endBlockRegexp;
40 private String allFileRegexp;
41 private boolean checkLines = true;
43 public IssuePattern() {
46 public IssuePattern(String resourcePattern, String rulePattern) {
47 this.resourcePattern = WildcardPattern.create(resourcePattern);
48 this.rulePattern = WildcardPattern.create(rulePattern);
51 public IssuePattern(String resourcePattern, String rulePattern, Set<LineRange> lineRanges) {
52 this(resourcePattern, rulePattern);
53 this.lineRanges = lineRanges;
56 public WildcardPattern getResourcePattern() {
57 return resourcePattern;
60 public WildcardPattern getRulePattern() {
64 public String getBeginBlockRegexp() {
65 return beginBlockRegexp;
68 public String getEndBlockRegexp() {
69 return endBlockRegexp;
72 public String getAllFileRegexp() {
76 IssuePattern addLineRange(int fromLineId, int toLineId) {
77 lineRanges.add(new LineRange(fromLineId, toLineId));
81 IssuePattern addLine(int lineId) {
86 boolean isCheckLines() {
90 IssuePattern setCheckLines(boolean b) {
95 IssuePattern setBeginBlockRegexp(String beginBlockRegexp) {
96 this.beginBlockRegexp = beginBlockRegexp;
100 IssuePattern setEndBlockRegexp(String endBlockRegexp) {
101 this.endBlockRegexp = endBlockRegexp;
105 IssuePattern setAllFileRegexp(String allFileRegexp) {
106 this.allFileRegexp = allFileRegexp;
110 Set<Integer> getAllLines() {
111 Set<Integer> allLines = Sets.newLinkedHashSet(lines);
112 for (LineRange lineRange : lineRanges) {
113 allLines.addAll(lineRange.toLines());
118 public boolean match(Issue issue) {
119 boolean match = matchResource(issue.componentKey())
120 && matchRule(issue.ruleKey());
122 Integer line = issue.line();
126 match = match && matchLine(line);
132 boolean matchLine(int lineId) {
133 if (lines.contains(lineId)) {
137 for (LineRange range : lineRanges) {
138 if (range.in(lineId)) {
146 boolean matchRule(RuleKey rule) {
151 String key = new StringBuilder().append(rule.repository()).append(':').append(rule.rule()).toString();
152 return rulePattern.match(key);
155 boolean matchResource(String resource) {
156 return resource != null && resourcePattern.match(resource);
159 public IssuePattern forResource(String resource) {
160 return new IssuePattern(resource, rulePattern.toString(), lineRanges).setCheckLines(isCheckLines());
164 public String toString() {
165 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);