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.scanner;
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Sets;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.sonar.api.BatchExtension;
30 import org.sonar.plugins.core.issue.ignore.pattern.ExclusionPatternInitializer;
31 import org.sonar.plugins.core.issue.ignore.pattern.IssuePattern;
32 import org.sonar.plugins.core.issue.ignore.pattern.LineRange;
35 import java.io.IOException;
36 import java.nio.charset.Charset;
37 import java.util.List;
40 public class RegexpScanner implements BatchExtension {
42 private static final Logger LOG = LoggerFactory.getLogger(RegexpScanner.class);
44 private ExclusionPatternInitializer exclusionPatternInitializer;
45 private List<java.util.regex.Pattern> allFilePatterns;
46 private List<DoubleRegexpMatcher> blockMatchers;
48 // fields to be reset at every new scan
49 private DoubleRegexpMatcher currentMatcher;
50 private int fileLength;
51 private List<LineExclusion> lineExclusions;
52 private LineExclusion currentLineExclusion;
54 public RegexpScanner(ExclusionPatternInitializer patternsInitializer) {
55 this.exclusionPatternInitializer = patternsInitializer;
57 lineExclusions = Lists.newArrayList();
58 allFilePatterns = Lists.newArrayList();
59 blockMatchers = Lists.newArrayList();
61 for (IssuePattern pattern : patternsInitializer.getAllFilePatterns()) {
62 allFilePatterns.add(java.util.regex.Pattern.compile(pattern.getAllFileRegexp()));
64 for (IssuePattern pattern : patternsInitializer.getBlockPatterns()) {
65 blockMatchers.add(new DoubleRegexpMatcher(
66 java.util.regex.Pattern.compile(pattern.getBeginBlockRegexp()),
67 java.util.regex.Pattern.compile(pattern.getEndBlockRegexp())));
74 currentMatcher = null;
76 lineExclusions.clear();
77 currentLineExclusion = null;
80 public void scan(String resource, File file, Charset sourcesEncoding) throws IOException {
81 LOG.debug("Scanning {}", resource);
84 List<String> lines = FileUtils.readLines(file, sourcesEncoding.name());
86 for (String line : lines) {
88 if (line.trim().length() == 0) {
92 // first check the single regexp patterns that can be used to totally exclude a file
93 for (java.util.regex.Pattern pattern : allFilePatterns) {
94 if (pattern.matcher(line).find()) {
95 exclusionPatternInitializer.getPatternMatcher().addPatternToExcludeResource(resource);
96 // nothing more to do on this file
97 LOG.debug("- Exclusion pattern '{}': every violation in this file will be ignored.", pattern);
102 // then check the double regexps if we're still here
103 checkDoubleRegexps(line, lineIndex);
106 if (currentMatcher != null && !currentMatcher.hasSecondPattern()) {
107 // this will happen when there is a start block regexp but no end block regexp
108 endExclusion(lineIndex + 1);
111 // now create the new line-based pattern for this file if there are exclusions
112 fileLength = lineIndex;
113 if (!lineExclusions.isEmpty()) {
114 Set<LineRange> lineRanges = convertLineExclusionsToLineRanges();
115 LOG.debug("- Line exclusions found: {}", lineRanges);
116 exclusionPatternInitializer.getPatternMatcher().addPatternToExcludeLines(resource, lineRanges);
120 private Set<LineRange> convertLineExclusionsToLineRanges() {
121 Set<LineRange> lineRanges = Sets.newHashSet();
122 for (LineExclusion lineExclusion : lineExclusions) {
123 lineRanges.add(lineExclusion.toLineRange());
128 private void checkDoubleRegexps(String line, int lineIndex) {
129 if (currentMatcher == null) {
130 for (DoubleRegexpMatcher matcher : blockMatchers) {
131 if (matcher.matchesFirstPattern(line)) {
132 startExclusion(lineIndex);
133 currentMatcher = matcher;
138 if (currentMatcher.matchesSecondPattern(line)) {
139 endExclusion(lineIndex);
140 currentMatcher = null;
145 private void startExclusion(int lineIndex) {
146 currentLineExclusion = new LineExclusion(lineIndex);
147 lineExclusions.add(currentLineExclusion);
150 private void endExclusion(int lineIndex) {
151 currentLineExclusion.setEnd(lineIndex);
152 currentLineExclusion = null;
155 private class LineExclusion {
160 LineExclusion(int start) {
165 void setEnd(int end) {
169 public LineRange toLineRange() {
170 return new LineRange(start, end == -1 ? fileLength : end);
175 private static class DoubleRegexpMatcher {
177 private java.util.regex.Pattern firstPattern;
178 private java.util.regex.Pattern secondPattern;
180 DoubleRegexpMatcher(java.util.regex.Pattern firstPattern, java.util.regex.Pattern secondPattern) {
181 this.firstPattern = firstPattern;
182 this.secondPattern = secondPattern;
185 boolean matchesFirstPattern(String line) {
186 return firstPattern.matcher(line).find();
189 boolean matchesSecondPattern(String line) {
190 return hasSecondPattern() && secondPattern.matcher(line).find();
193 boolean hasSecondPattern() {
194 return StringUtils.isNotEmpty(secondPattern.toString());