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.config.Settings;
27 import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration;
29 import java.util.List;
31 import static com.google.common.base.Strings.nullToEmpty;
33 public class ExclusionPatternInitializer extends AbstractPatternInitializer {
35 private List<IssuePattern> blockPatterns;
36 private List<IssuePattern> allFilePatterns;
37 private PatternMatcher patternMatcher;
39 public ExclusionPatternInitializer(Settings settings) {
41 patternMatcher = new PatternMatcher();
42 loadFileContentPatterns();
46 protected String getMulticriteriaConfigurationKey() {
47 return IgnoreIssuesConfiguration.PATTERNS_MULTICRITERIA_EXCLUSION_KEY;
50 public PatternMatcher getPatternMatcher() {
51 return patternMatcher;
55 public void initializePatternsForPath(String relativePath, String componentKey) {
56 for (IssuePattern pattern: getMulticriteriaPatterns()) {
57 if (pattern.matchResource(relativePath)) {
58 getPatternMatcher().addPatternForComponent(componentKey, pattern);
64 public boolean hasConfiguredPatterns() {
65 return hasFileContentPattern() || hasMulticriteriaPatterns();
69 protected final void loadFileContentPatterns() {
71 blockPatterns = Lists.newArrayList();
72 String patternConf = StringUtils.defaultIfBlank(getSettings().getString(IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY), "");
73 for (String id : StringUtils.split(patternConf, ',')) {
74 String propPrefix = IgnoreIssuesConfiguration.PATTERNS_BLOCK_KEY + "." + id + ".";
75 String beginBlockRegexp = getSettings().getString(propPrefix + IgnoreIssuesConfiguration.BEGIN_BLOCK_REGEXP);
76 String endBlockRegexp = getSettings().getString(propPrefix + IgnoreIssuesConfiguration.END_BLOCK_REGEXP);
77 String[] fields = new String[] { beginBlockRegexp, endBlockRegexp };
78 PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields);
79 IssuePattern pattern = new IssuePattern().setBeginBlockRegexp(nullToEmpty(beginBlockRegexp)).setEndBlockRegexp(nullToEmpty(endBlockRegexp));
80 blockPatterns.add(pattern);
84 allFilePatterns = Lists.newArrayList();
85 patternConf = StringUtils.defaultIfBlank(getSettings().getString(IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY), "");
86 for (String id : StringUtils.split(patternConf, ',')) {
87 String propPrefix = IgnoreIssuesConfiguration.PATTERNS_ALLFILE_KEY + "." + id + ".";
88 String allFileRegexp = getSettings().getString(propPrefix + IgnoreIssuesConfiguration.FILE_REGEXP);
89 PatternDecoder.checkWholeFileRegexp(allFileRegexp);
90 IssuePattern pattern = new IssuePattern().setAllFileRegexp(nullToEmpty(allFileRegexp));
91 allFilePatterns.add(pattern);
95 public List<IssuePattern> getBlockPatterns() {
99 public List<IssuePattern> getAllFilePatterns() {
100 return allFilePatterns;
103 public boolean hasFileContentPattern() {
104 return ! (blockPatterns.isEmpty() && allFilePatterns.isEmpty());