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.scan.filesystem;
021    
022    import org.apache.commons.lang.ArrayUtils;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.BatchComponent;
026    import org.sonar.api.batch.fs.InputFile;
027    import org.sonar.api.batch.fs.internal.PathPattern;
028    import org.sonar.api.scan.filesystem.FileExclusions;
029    
030    public class ExclusionFilters implements BatchComponent {
031    
032      private static final Logger LOG = LoggerFactory.getLogger(ExclusionFilters.class);
033    
034      private final FileExclusions exclusionSettings;
035    
036      private PathPattern[] mainInclusions;
037      private PathPattern[] mainExclusions;
038      private PathPattern[] testInclusions;
039      private PathPattern[] testExclusions;
040    
041      public ExclusionFilters(FileExclusions exclusions) {
042        this.exclusionSettings = exclusions;
043      }
044    
045      public void prepare() {
046        mainInclusions = prepareMainInclusions();
047        mainExclusions = prepareMainExclusions();
048        testInclusions = prepareTestInclusions();
049        testExclusions = prepareTestExclusions();
050        log("Included sources: ", mainInclusions);
051        log("Excluded sources: ", mainExclusions);
052        log("Included tests: ", testInclusions);
053        log("Excluded tests: ", testExclusions);
054      }
055    
056      private void log(String title, PathPattern[] patterns) {
057        if (patterns.length > 0) {
058          LOG.info(title);
059          for (PathPattern pattern : patterns) {
060            LOG.info("  " + pattern);
061          }
062        }
063      }
064    
065      public boolean accept(InputFile inputFile, InputFile.Type type) {
066        PathPattern[] inclusionPatterns;
067        PathPattern[] exclusionPatterns;
068        if (InputFile.Type.MAIN == type) {
069          inclusionPatterns = mainInclusions;
070          exclusionPatterns = mainExclusions;
071        } else if (InputFile.Type.TEST == type) {
072          inclusionPatterns = testInclusions;
073          exclusionPatterns = testExclusions;
074        } else {
075          throw new IllegalArgumentException("Unknown file type: " + type);
076        }
077    
078        if (inclusionPatterns.length > 0) {
079          boolean matchInclusion = false;
080          for (PathPattern pattern : inclusionPatterns) {
081            matchInclusion |= pattern.match(inputFile);
082          }
083          if (!matchInclusion) {
084            return false;
085          }
086        }
087        if (exclusionPatterns.length > 0) {
088          for (PathPattern pattern : exclusionPatterns) {
089            if (pattern.match(inputFile)) {
090              return false;
091            }
092          }
093        }
094        return true;
095      }
096    
097      PathPattern[] prepareMainInclusions() {
098        if (exclusionSettings.sourceInclusions().length > 0) {
099          // User defined params
100          return PathPattern.create(exclusionSettings.sourceInclusions());
101        }
102        return new PathPattern[0];
103      }
104    
105      PathPattern[] prepareTestInclusions() {
106        return PathPattern.create(computeTestInclusions());
107      }
108    
109      private String[] computeTestInclusions() {
110        if (exclusionSettings.testInclusions().length > 0) {
111          // User defined params
112          return exclusionSettings.testInclusions();
113        }
114        return ArrayUtils.EMPTY_STRING_ARRAY;
115      }
116    
117      PathPattern[] prepareMainExclusions() {
118        String[] patterns = (String[]) ArrayUtils.addAll(
119          exclusionSettings.sourceExclusions(), computeTestInclusions());
120        return PathPattern.create(patterns);
121      }
122    
123      PathPattern[] prepareTestExclusions() {
124        return PathPattern.create(exclusionSettings.testExclusions());
125      }
126    }