![JAR search and dependency download from the Maven repository](/logo.png)
org.sonar.plugins.switchoffviolations.pattern.PatternDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-switch-off-violations-plugin Show documentation
Show all versions of sonar-switch-off-violations-plugin Show documentation
Filter out chosen violations from analysis
The newest version!
/*
* Sonar Switch Off Violations Plugin
* Copyright (C) 2011 SonarSource
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.switchoffviolations.pattern;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.SonarException;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class PatternDecoder {
static final String LINE_RANGE_REGEXP = "\\[((\\d+|\\d+-\\d+),?)*\\]";
public List decode(String patternsList) {
List patterns = Lists.newLinkedList();
String[] patternsLines = StringUtils.split(patternsList, "\n");
for (String patternLine : patternsLines) {
Pattern pattern = decodeLine(patternLine.trim());
if (pattern != null) {
patterns.add(pattern);
}
}
return patterns;
}
public List decode(File file) {
try {
List lines = FileUtils.readLines(file);
List patterns = Lists.newLinkedList();
for (String line : lines) {
Pattern pattern = decodeLine(line);
if (pattern != null) {
patterns.add(pattern);
}
}
return patterns;
} catch (IOException e) {
throw new SonarException("Fail to load the file: " + file.getAbsolutePath(), e);
}
}
/**
* Main method that decodes a line which defines a pattern
*/
public Pattern decodeLine(String line) {
if (isBlankOrComment(line)) {
return null;
}
String[] fields = StringUtils.splitPreserveAllTokens(line, ';');
if (fields.length > 3) {
throw new SonarException("Unvalid format. The following line has more than 3 fields separated by comma: " + line);
}
Pattern pattern = null;
if (fields.length == 3) {
checkRegularLineConstraints(line, fields);
pattern = new Pattern(StringUtils.trim(fields[0]), StringUtils.trim(fields[1]));
decodeRangeOfLines(pattern, fields[2]);
} else if (fields.length == 2) {
checkDoubleRegexpLineConstraints(line, fields);
pattern = new Pattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]);
} else {
pattern = new Pattern().setAllFileRegexp(fields[0]);
}
return pattern;
}
private void checkRegularLineConstraints(String line, String[] fields) {
if (!isResource(fields[0])) {
throw new SonarException("Unvalid format. The first field does not define a resource pattern: " + line);
}
if (!isRule(fields[1])) {
throw new SonarException("Unvalid format. The second field does not define a rule pattern: " + line);
}
if (!isLinesRange(fields[2])) {
throw new SonarException("Unvalid format. The third field does not define a range of lines: " + line);
}
}
private void checkDoubleRegexpLineConstraints(String line, String[] fields) {
if (!isRegexp(fields[0])) {
throw new SonarException("Unvalid format. The first field does not define a regular expression: " + line);
}
if (!isRegexp(fields[1])) {
throw new SonarException("Unvalid format. The second field does not define a regular expression: " + line);
}
}
public static void decodeRangeOfLines(Pattern pattern, String field) {
if (StringUtils.equals(field, "*")) {
pattern.setCheckLines(false);
} else {
pattern.setCheckLines(true);
String s = StringUtils.substringBetween(StringUtils.trim(field), "[", "]");
String[] parts = StringUtils.split(s, ',');
for (String part : parts) {
if (StringUtils.contains(part, '-')) {
String[] range = StringUtils.split(part, '-');
pattern.addLineRange(Integer.valueOf(range[0]), Integer.valueOf(range[1]));
} else {
pattern.addLine(Integer.valueOf(part));
}
}
}
}
@VisibleForTesting
boolean isLinesRange(String field) {
return StringUtils.equals(field, "*") || java.util.regex.Pattern.matches(LINE_RANGE_REGEXP, field);
}
@VisibleForTesting
boolean isBlankOrComment(String line) {
return StringUtils.isBlank(line) || StringUtils.startsWith(line, "#");
}
@VisibleForTesting
boolean isResource(String field) {
return StringUtils.isNotBlank(field);
}
@VisibleForTesting
boolean isRule(String field) {
return StringUtils.isNotBlank(field);
}
@VisibleForTesting
boolean isRegexp(String field) {
return StringUtils.isNotBlank(field);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy