3 * Copyright (C) 2009-2017 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 org.apache.commons.lang.StringUtils;
23 import org.sonar.api.config.Settings;
24 import org.sonar.core.config.IssueExclusionProperties;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
30 import static com.google.common.base.Strings.nullToEmpty;
32 public class IssueExclusionPatternInitializer extends AbstractPatternInitializer {
34 private List<BlockIssuePattern> blockPatterns;
35 private List<String> allFilePatterns;
37 public IssueExclusionPatternInitializer(Settings settings) {
39 loadFileContentPatterns();
43 protected String getMulticriteriaConfigurationKey() {
44 return "sonar.issue.ignore" + ".multicriteria";
48 public boolean hasConfiguredPatterns() {
49 return hasFileContentPattern() || hasMulticriteriaPatterns();
52 private final void loadFileContentPatterns() {
54 blockPatterns = new ArrayList<>();
55 String patternConf = StringUtils.defaultIfBlank(getSettings().getString(IssueExclusionProperties.PATTERNS_BLOCK_KEY), "");
56 for (String id : StringUtils.split(patternConf, ',')) {
57 String propPrefix = IssueExclusionProperties.PATTERNS_BLOCK_KEY + "." + id + ".";
58 String beginBlockRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.BEGIN_BLOCK_REGEXP);
59 String endBlockRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.END_BLOCK_REGEXP);
60 String[] fields = new String[] {beginBlockRegexp, endBlockRegexp};
61 PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields);
62 BlockIssuePattern pattern = new BlockIssuePattern(nullToEmpty(beginBlockRegexp), nullToEmpty(endBlockRegexp));
63 blockPatterns.add(pattern);
65 blockPatterns = Collections.unmodifiableList(blockPatterns);
68 allFilePatterns = new ArrayList<>();
69 patternConf = StringUtils.defaultIfBlank(getSettings().getString(IssueExclusionProperties.PATTERNS_ALLFILE_KEY), "");
70 for (String id : StringUtils.split(patternConf, ',')) {
71 String propPrefix = IssueExclusionProperties.PATTERNS_ALLFILE_KEY + "." + id + ".";
72 String allFileRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.FILE_REGEXP);
73 PatternDecoder.checkWholeFileRegexp(allFileRegexp);
74 allFilePatterns.add(nullToEmpty(allFileRegexp));
76 allFilePatterns = Collections.unmodifiableList(allFilePatterns);
79 public List<BlockIssuePattern> getBlockPatterns() {
83 public List<String> getAllFilePatterns() {
84 return allFilePatterns;
87 public boolean hasFileContentPattern() {
88 return !(blockPatterns.isEmpty() && allFilePatterns.isEmpty());