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 java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import org.apache.commons.lang.StringUtils;
26 import org.sonar.api.config.Configuration;
27 import org.sonar.core.config.IssueExclusionProperties;
29 import static com.google.common.base.Strings.nullToEmpty;
31 public class IssueExclusionPatternInitializer extends AbstractPatternInitializer {
33 private List<BlockIssuePattern> blockPatterns;
34 private List<String> allFilePatterns;
36 public IssueExclusionPatternInitializer(Configuration settings) {
38 loadFileContentPatterns();
42 protected String getMulticriteriaConfigurationKey() {
43 return "sonar.issue.ignore" + ".multicriteria";
47 public boolean hasConfiguredPatterns() {
48 return hasFileContentPattern() || hasMulticriteriaPatterns();
51 private final void loadFileContentPatterns() {
53 blockPatterns = new ArrayList<>();
54 for (String id : getSettings().getStringArray(IssueExclusionProperties.PATTERNS_BLOCK_KEY)) {
55 String propPrefix = IssueExclusionProperties.PATTERNS_BLOCK_KEY + "." + id + ".";
56 String beginBlockRegexp = getSettings().get(propPrefix + IssueExclusionProperties.BEGIN_BLOCK_REGEXP).orElse(null);
57 String endBlockRegexp = getSettings().get(propPrefix + IssueExclusionProperties.END_BLOCK_REGEXP).orElse(null);
58 String[] fields = new String[] {beginBlockRegexp, endBlockRegexp};
59 PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields);
60 BlockIssuePattern pattern = new BlockIssuePattern(nullToEmpty(beginBlockRegexp), nullToEmpty(endBlockRegexp));
61 blockPatterns.add(pattern);
63 blockPatterns = Collections.unmodifiableList(blockPatterns);
66 allFilePatterns = new ArrayList<>();
67 for (String id : getSettings().getStringArray(IssueExclusionProperties.PATTERNS_ALLFILE_KEY)) {
68 String propPrefix = IssueExclusionProperties.PATTERNS_ALLFILE_KEY + "." + id + ".";
69 String allFileRegexp = getSettings().get(propPrefix + IssueExclusionProperties.FILE_REGEXP).orElse(null);
70 PatternDecoder.checkWholeFileRegexp(allFileRegexp);
71 allFilePatterns.add(nullToEmpty(allFileRegexp));
73 allFilePatterns = Collections.unmodifiableList(allFilePatterns);
76 public List<BlockIssuePattern> getBlockPatterns() {
80 public List<String> getAllFilePatterns() {
81 return allFilePatterns;
84 public boolean hasFileContentPattern() {
85 return !(blockPatterns.isEmpty() && allFilePatterns.isEmpty());