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.annotations.VisibleForTesting;
24 import com.google.common.collect.Lists;
25 import org.apache.commons.lang.StringUtils;
26 import org.sonar.api.utils.SonarException;
28 import java.util.List;
30 public class PatternDecoder {
32 private static final int THREE_FIELDS_PER_LINE = 3;
33 private static final String LINE_RANGE_REGEXP = "\\[((\\d+|\\d+-\\d+),?)*\\]";
34 private static final String CONFIG_FORMAT_ERROR_PREFIX = "Exclusions > Issues : Invalid format. ";
36 public List<IssuePattern> decode(String patternsList) {
37 List<IssuePattern> patterns = Lists.newLinkedList();
38 String[] patternsLines = StringUtils.split(patternsList, "\n");
39 for (String patternLine : patternsLines) {
40 IssuePattern pattern = decodeLine(patternLine.trim());
41 if (pattern != null) {
42 patterns.add(pattern);
49 * Main method that decodes a line which defines a pattern
51 public IssuePattern decodeLine(String line) {
52 if (isBlankOrComment(line)) {
56 String[] fields = StringUtils.splitPreserveAllTokens(line, ';');
57 if (fields.length > THREE_FIELDS_PER_LINE) {
58 throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The following line has more than 3 fields separated by comma: " + line);
62 if (fields.length == THREE_FIELDS_PER_LINE) {
63 checkRegularLineConstraints(line, fields);
64 pattern = new IssuePattern(StringUtils.trim(fields[0]), StringUtils.trim(fields[1]));
65 decodeRangeOfLines(pattern, fields[2]);
66 } else if (fields.length == 2) {
67 checkDoubleRegexpLineConstraints(line, fields);
68 pattern = new IssuePattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]);
70 checkWholeFileRegexp(fields[0]);
71 pattern = new IssuePattern().setAllFileRegexp(fields[0]);
77 static void checkRegularLineConstraints(String line, String[] fields) {
78 if (!isResource(fields[0])) {
79 throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The first field does not define a resource pattern: " + line);
81 if (!isRule(fields[1])) {
82 throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The second field does not define a rule pattern: " + line);
84 if (!isLinesRange(fields[2])) {
85 throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The third field does not define a range of lines: " + line);
89 static void checkDoubleRegexpLineConstraints(String line, String[] fields) {
90 if (!isRegexp(fields[0])) {
91 throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The first field does not define a regular expression: " + line);
93 // As per configuration help, missing second field means: from start regexp to EOF
96 static void checkWholeFileRegexp(String regexp) {
97 if (!isRegexp(regexp)) {
98 throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The field does not define a regular expression: " + regexp);
102 public static void decodeRangeOfLines(IssuePattern pattern, String field) {
103 if (StringUtils.equals(field, "*")) {
104 pattern.setCheckLines(false);
106 pattern.setCheckLines(true);
107 String s = StringUtils.substringBetween(StringUtils.trim(field), "[", "]");
108 String[] parts = StringUtils.split(s, ',');
109 for (String part : parts) {
110 if (StringUtils.contains(part, '-')) {
111 String[] range = StringUtils.split(part, '-');
112 pattern.addLineRange(Integer.valueOf(range[0]), Integer.valueOf(range[1]));
114 pattern.addLine(Integer.valueOf(part));
121 static boolean isLinesRange(String field) {
122 return StringUtils.equals(field, "*") || java.util.regex.Pattern.matches(LINE_RANGE_REGEXP, field);
126 static boolean isBlankOrComment(String line) {
127 return StringUtils.isBlank(line) ^ StringUtils.startsWith(line, "#");
131 static boolean isResource(String field) {
132 return StringUtils.isNotBlank(field);
136 static boolean isRule(String field) {
137 return StringUtils.isNotBlank(field);
141 static boolean isRegexp(String field) {
142 return StringUtils.isNotBlank(field);