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+),?)*\\]";
35 public List<Pattern> decode(String patternsList) {
36 List<Pattern> patterns = Lists.newLinkedList();
37 String[] patternsLines = StringUtils.split(patternsList, "\n");
38 for (String patternLine : patternsLines) {
39 Pattern pattern = decodeLine(patternLine.trim());
40 if (pattern != null) {
41 patterns.add(pattern);
48 * Main method that decodes a line which defines a pattern
50 public Pattern decodeLine(String line) {
51 if (isBlankOrComment(line)) {
55 String[] fields = StringUtils.splitPreserveAllTokens(line, ';');
56 if (fields.length > THREE_FIELDS_PER_LINE) {
57 throw new SonarException("Invalid format. The following line has more than 3 fields separated by comma: " + line);
61 if (fields.length == THREE_FIELDS_PER_LINE) {
62 checkRegularLineConstraints(line, fields);
63 pattern = new Pattern(StringUtils.trim(fields[0]), StringUtils.trim(fields[1]));
64 decodeRangeOfLines(pattern, fields[2]);
65 } else if (fields.length == 2) {
66 checkDoubleRegexpLineConstraints(line, fields);
67 pattern = new Pattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]);
69 pattern = new Pattern().setAllFileRegexp(fields[0]);
75 private void checkRegularLineConstraints(String line, String[] fields) {
76 if (!isResource(fields[0])) {
77 throw new SonarException("Invalid format. The first field does not define a resource pattern: " + line);
79 if (!isRule(fields[1])) {
80 throw new SonarException("Invalid format. The second field does not define a rule pattern: " + line);
82 if (!isLinesRange(fields[2])) {
83 throw new SonarException("Invalid format. The third field does not define a range of lines: " + line);
87 private void checkDoubleRegexpLineConstraints(String line, String[] fields) {
88 if (!isRegexp(fields[0])) {
89 throw new SonarException("Invalid format. The first field does not define a regular expression: " + line);
91 if (!isRegexp(fields[1])) {
92 throw new SonarException("Invalid format. The second field does not define a regular expression: " + line);
96 public static void decodeRangeOfLines(Pattern pattern, String field) {
97 if (StringUtils.equals(field, "*")) {
98 pattern.setCheckLines(false);
100 pattern.setCheckLines(true);
101 String s = StringUtils.substringBetween(StringUtils.trim(field), "[", "]");
102 String[] parts = StringUtils.split(s, ',');
103 for (String part : parts) {
104 if (StringUtils.contains(part, '-')) {
105 String[] range = StringUtils.split(part, '-');
106 pattern.addLineRange(Integer.valueOf(range[0]), Integer.valueOf(range[1]));
108 pattern.addLine(Integer.valueOf(part));
115 boolean isLinesRange(String field) {
116 return StringUtils.equals(field, "*") || java.util.regex.Pattern.matches(LINE_RANGE_REGEXP, field);
120 boolean isBlankOrComment(String line) {
121 return StringUtils.isBlank(line) ^ StringUtils.startsWith(line, "#");
125 boolean isResource(String field) {
126 return StringUtils.isNotBlank(field);
130 boolean isRule(String field) {
131 return StringUtils.isNotBlank(field);
135 boolean isRegexp(String field) {
136 return StringUtils.isNotBlank(field);