001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020
021 package org.sonar.batch.issue.ignore.pattern;
022
023 import com.google.common.annotations.VisibleForTesting;
024 import com.google.common.collect.Lists;
025 import org.apache.commons.lang.StringUtils;
026 import org.sonar.api.config.Settings;
027 import org.sonar.core.config.IssueExclusionProperties;
028
029 import java.util.List;
030
031 import static com.google.common.base.Strings.nullToEmpty;
032
033 public class IssueExclusionPatternInitializer extends AbstractPatternInitializer {
034
035 private List<IssuePattern> blockPatterns;
036 private List<IssuePattern> allFilePatterns;
037 private PatternMatcher patternMatcher;
038
039 public IssueExclusionPatternInitializer(Settings settings) {
040 super(settings);
041 patternMatcher = new PatternMatcher();
042 loadFileContentPatterns();
043 }
044
045 @Override
046 protected String getMulticriteriaConfigurationKey() {
047 return "sonar.issue.ignore" + ".multicriteria";
048 }
049
050 public PatternMatcher getPatternMatcher() {
051 return patternMatcher;
052 }
053
054 @Override
055 public void initializePatternsForPath(String relativePath, String componentKey) {
056 for (IssuePattern pattern : getMulticriteriaPatterns()) {
057 if (pattern.matchResource(relativePath)) {
058 getPatternMatcher().addPatternForComponent(componentKey, pattern);
059 }
060 }
061 }
062
063 @Override
064 public boolean hasConfiguredPatterns() {
065 return hasFileContentPattern() || hasMulticriteriaPatterns();
066 }
067
068 @VisibleForTesting
069 protected final void loadFileContentPatterns() {
070 // Patterns Block
071 blockPatterns = Lists.newArrayList();
072 String patternConf = StringUtils.defaultIfBlank(getSettings().getString(IssueExclusionProperties.PATTERNS_BLOCK_KEY), "");
073 for (String id : StringUtils.split(patternConf, ',')) {
074 String propPrefix = IssueExclusionProperties.PATTERNS_BLOCK_KEY + "." + id + ".";
075 String beginBlockRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.BEGIN_BLOCK_REGEXP);
076 String endBlockRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.END_BLOCK_REGEXP);
077 String[] fields = new String[]{beginBlockRegexp, endBlockRegexp};
078 PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields);
079 IssuePattern pattern = new IssuePattern().setBeginBlockRegexp(nullToEmpty(beginBlockRegexp)).setEndBlockRegexp(nullToEmpty(endBlockRegexp));
080 blockPatterns.add(pattern);
081 }
082
083 // Patterns All File
084 allFilePatterns = Lists.newArrayList();
085 patternConf = StringUtils.defaultIfBlank(getSettings().getString(IssueExclusionProperties.PATTERNS_ALLFILE_KEY), "");
086 for (String id : StringUtils.split(patternConf, ',')) {
087 String propPrefix = IssueExclusionProperties.PATTERNS_ALLFILE_KEY + "." + id + ".";
088 String allFileRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.FILE_REGEXP);
089 PatternDecoder.checkWholeFileRegexp(allFileRegexp);
090 IssuePattern pattern = new IssuePattern().setAllFileRegexp(nullToEmpty(allFileRegexp));
091 allFilePatterns.add(pattern);
092 }
093 }
094
095 public List<IssuePattern> getBlockPatterns() {
096 return blockPatterns;
097 }
098
099 public List<IssuePattern> getAllFilePatterns() {
100 return allFilePatterns;
101 }
102
103 public boolean hasFileContentPattern() {
104 return !(blockPatterns.isEmpty() && allFilePatterns.isEmpty());
105 }
106 }