001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 */
020package org.sonar.api.scan.filesystem;
021
022import java.util.Arrays;
023import java.util.stream.Stream;
024import org.apache.commons.lang.StringUtils;
025import org.sonar.api.CoreProperties;
026import org.sonar.api.batch.ScannerSide;
027import org.sonar.api.config.Configuration;
028
029/**
030 * Configuration of file inclusions and exclusions.
031 * <p>Plugins must not extend nor instantiate this class. An instance is injected at
032 * runtime.
033 *
034 * @since 3.5
035 */
036@ScannerSide
037public class FileExclusions {
038  private final Configuration settings;
039
040  public FileExclusions(Configuration settings) {
041    this.settings = settings;
042  }
043
044  public String[] sourceInclusions() {
045    return inclusions(CoreProperties.PROJECT_INCLUSIONS_PROPERTY);
046  }
047
048  public String[] testInclusions() {
049    return inclusions(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY);
050  }
051
052  private String[] inclusions(String propertyKey) {
053    return Arrays.stream(settings.getStringArray(propertyKey))
054      .map(StringUtils::trim)
055      .filter(s -> !"**/*".equals(s))
056      .filter(s -> !"file:**/*".equals(s))
057      .toArray(String[]::new);
058  }
059
060  public String[] sourceExclusions() {
061    return exclusions(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
062  }
063
064  public String[] testExclusions() {
065    return exclusions(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY);
066  }
067
068  private String[] exclusions(String globalExclusionsProperty, String exclusionsProperty) {
069    String[] globalExclusions = settings.getStringArray(globalExclusionsProperty);
070    String[] exclusions = settings.getStringArray(exclusionsProperty);
071    return Stream.concat(Arrays.stream(globalExclusions), Arrays.stream(exclusions))
072      .map(StringUtils::trim)
073      .toArray(String[]::new);
074  }
075}