3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.scanner.issue.ignore.pattern;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.api.batch.ScannerSide;
28 import org.sonar.api.config.Configuration;
30 import static com.google.common.base.MoreObjects.firstNonNull;
33 public abstract class AbstractPatternInitializer {
34 private Configuration settings;
35 private List<IssuePattern> multicriteriaPatterns;
37 protected AbstractPatternInitializer(Configuration settings) {
38 this.settings = settings;
42 protected Configuration getSettings() {
46 public List<IssuePattern> getMulticriteriaPatterns() {
47 return multicriteriaPatterns;
50 public boolean hasConfiguredPatterns() {
51 return hasMulticriteriaPatterns();
54 public boolean hasMulticriteriaPatterns() {
55 return !multicriteriaPatterns.isEmpty();
59 protected final void initPatterns() {
60 // Patterns Multicriteria
61 multicriteriaPatterns = new ArrayList<>();
62 for (String id : settings.getStringArray(getMulticriteriaConfigurationKey())) {
63 String propPrefix = getMulticriteriaConfigurationKey() + "." + id + ".";
64 String resourceKeyPattern = settings.get(propPrefix + "resourceKey").orElse(null);
65 String ruleKeyPattern = settings.get(propPrefix + "ruleKey").orElse(null);
66 String lineRange = "*";
67 String[] fields = new String[] {resourceKeyPattern, ruleKeyPattern, lineRange};
68 PatternDecoder.checkRegularLineConstraints(StringUtils.join(fields, ","), fields);
69 Set<LineRange> rangeOfLines = PatternDecoder.decodeRangeOfLines(firstNonNull(lineRange, "*"));
70 IssuePattern pattern = new IssuePattern(firstNonNull(resourceKeyPattern, "*"), firstNonNull(ruleKeyPattern, "*"), rangeOfLines);
72 multicriteriaPatterns.add(pattern);
76 protected abstract String getMulticriteriaConfigurationKey();