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.
20 package org.sonar.plugins.core.issue.ignore.pattern;
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.collect.Lists;
24 import org.apache.commons.lang.StringUtils;
25 import org.sonar.api.BatchExtension;
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.Objects.firstNonNull;
33 public abstract class AbstractPatternInitializer implements BatchExtension {
35 private Settings settings;
37 private List<IssuePattern> multicriteriaPatterns;
39 protected AbstractPatternInitializer(Settings settings) {
40 this.settings = settings;
44 protected Settings getSettings() {
48 public List<IssuePattern> getMulticriteriaPatterns() {
49 return multicriteriaPatterns;
52 public boolean hasConfiguredPatterns() {
53 return hasMulticriteriaPatterns();
56 public boolean hasMulticriteriaPatterns() {
57 return ! multicriteriaPatterns.isEmpty();
60 public abstract void initializePatternsForPath(String relativePath, String componentKey);
63 protected final void initPatterns() {
64 // Patterns Multicriteria
65 multicriteriaPatterns = Lists.newArrayList();
66 String patternConf = StringUtils.defaultIfBlank(settings.getString(getMulticriteriaConfigurationKey()), "");
67 for (String id : StringUtils.split(patternConf, ',')) {
68 String propPrefix = getMulticriteriaConfigurationKey() + "." + id + ".";
69 String resourceKeyPattern = settings.getString(propPrefix + IgnoreIssuesConfiguration.RESOURCE_KEY);
70 String ruleKeyPattern = settings.getString(propPrefix + IgnoreIssuesConfiguration.RULE_KEY);
71 String lineRange = "*";
72 String[] fields = new String[] { resourceKeyPattern, ruleKeyPattern, lineRange };
73 PatternDecoder.checkRegularLineConstraints(StringUtils.join(fields, ","), fields);
74 IssuePattern pattern = new IssuePattern(firstNonNull(resourceKeyPattern, "*"), firstNonNull(ruleKeyPattern, "*"));
75 PatternDecoder.decodeRangeOfLines(pattern, firstNonNull(lineRange, "*"));
76 multicriteriaPatterns.add(pattern);
80 protected abstract String getMulticriteriaConfigurationKey();