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.api.utils.MessageException;
28 import org.sonar.core.config.IssueExclusionProperties;
30 import static com.google.common.base.Strings.nullToEmpty;
32 public class IssueExclusionPatternInitializer extends AbstractPatternInitializer {
34 public static final String CONFIG_KEY = IssueExclusionProperties.EXCLUSION_KEY_PREFIX + ".multicriteria";
35 private List<BlockIssuePattern> blockPatterns;
36 private List<String> allFilePatterns;
38 public IssueExclusionPatternInitializer(Configuration settings) {
40 loadFileContentPatterns();
44 protected String getMulticriteriaConfigurationKey() {
49 public boolean hasConfiguredPatterns() {
50 return hasFileContentPattern() || hasMulticriteriaPatterns();
53 private final void loadFileContentPatterns() {
55 blockPatterns = new ArrayList<>();
56 for (String id : getSettings().getStringArray(IssueExclusionProperties.PATTERNS_BLOCK_KEY)) {
57 String propPrefix = IssueExclusionProperties.PATTERNS_BLOCK_KEY + "." + id + ".";
58 String beginBlockRegexp = getSettings().get(propPrefix + IssueExclusionProperties.BEGIN_BLOCK_REGEXP).orElse(null);
59 if (StringUtils.isBlank(beginBlockRegexp)) {
60 throw MessageException.of("Issue exclusions are misconfigured. Start block regexp is mandatory for each entry of '" + IssueExclusionProperties.PATTERNS_BLOCK_KEY + "'");
62 String endBlockRegexp = getSettings().get(propPrefix + IssueExclusionProperties.END_BLOCK_REGEXP).orElse(null);
63 // As per configuration help, missing second field means: from start regexp to EOF
64 BlockIssuePattern pattern = new BlockIssuePattern(nullToEmpty(beginBlockRegexp), nullToEmpty(endBlockRegexp));
65 blockPatterns.add(pattern);
67 blockPatterns = Collections.unmodifiableList(blockPatterns);
70 allFilePatterns = new ArrayList<>();
71 for (String id : getSettings().getStringArray(IssueExclusionProperties.PATTERNS_ALLFILE_KEY)) {
72 String propPrefix = IssueExclusionProperties.PATTERNS_ALLFILE_KEY + "." + id + ".";
73 String allFileRegexp = getSettings().get(propPrefix + IssueExclusionProperties.FILE_REGEXP).orElse(null);
74 if (StringUtils.isBlank(allFileRegexp)) {
75 throw MessageException.of("Issue exclusions are misconfigured. Remove blank entries from '" + IssueExclusionProperties.PATTERNS_ALLFILE_KEY + "'");
77 allFilePatterns.add(nullToEmpty(allFileRegexp));
79 allFilePatterns = Collections.unmodifiableList(allFilePatterns);
82 public List<BlockIssuePattern> getBlockPatterns() {
86 public List<String> getAllFilePatterns() {
87 return allFilePatterns;
90 public boolean hasFileContentPattern() {
91 return !(blockPatterns.isEmpty() && allFilePatterns.isEmpty());