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    package org.sonar.batch.issue.ignore.pattern;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import com.google.common.collect.Lists;
024    import org.apache.commons.lang.StringUtils;
025    import org.sonar.api.BatchExtension;
026    import org.sonar.api.config.Settings;
027    
028    import java.util.List;
029    
030    import static com.google.common.base.Objects.firstNonNull;
031    
032    public abstract class AbstractPatternInitializer implements BatchExtension {
033    
034      private Settings settings;
035    
036      private List<IssuePattern> multicriteriaPatterns;
037    
038      protected AbstractPatternInitializer(Settings settings) {
039        this.settings = settings;
040        initPatterns();
041      }
042    
043      protected Settings getSettings() {
044        return settings;
045      }
046    
047      public List<IssuePattern> getMulticriteriaPatterns() {
048        return multicriteriaPatterns;
049      }
050    
051      public boolean hasConfiguredPatterns() {
052        return hasMulticriteriaPatterns();
053      }
054    
055      public boolean hasMulticriteriaPatterns() {
056        return ! multicriteriaPatterns.isEmpty();
057      }
058    
059      public abstract void initializePatternsForPath(String relativePath, String componentKey);
060    
061      @VisibleForTesting
062      protected final void initPatterns() {
063        // Patterns Multicriteria
064        multicriteriaPatterns = Lists.newArrayList();
065        String patternConf = StringUtils.defaultIfBlank(settings.getString(getMulticriteriaConfigurationKey()), "");
066        for (String id : StringUtils.split(patternConf, ',')) {
067          String propPrefix = getMulticriteriaConfigurationKey() + "." + id + ".";
068          String resourceKeyPattern = settings.getString(propPrefix + "resourceKey");
069          String ruleKeyPattern = settings.getString(propPrefix + "ruleKey");
070          String lineRange = "*";
071          String[] fields = new String[] { resourceKeyPattern, ruleKeyPattern, lineRange };
072          PatternDecoder.checkRegularLineConstraints(StringUtils.join(fields, ","), fields);
073          IssuePattern pattern = new IssuePattern(firstNonNull(resourceKeyPattern, "*"), firstNonNull(ruleKeyPattern, "*"));
074          PatternDecoder.decodeRangeOfLines(pattern, firstNonNull(lineRange, "*"));
075          multicriteriaPatterns.add(pattern);
076        }
077      }
078    
079      protected abstract String getMulticriteriaConfigurationKey();
080    }