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 com.google.common.collect.ObjectArrays;
023import org.apache.commons.lang.StringUtils;
024import org.sonar.api.batch.ScannerSide;
025import org.sonar.api.CoreProperties;
026import org.sonar.api.config.Settings;
027
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * Configuration of file inclusions and exclusions.
033 * <p>Plugins must not extend nor instantiate this class. An instance is injected at
034 * runtime.
035 *
036 * @since 3.5
037 */
038@ScannerSide
039public class FileExclusions {
040  private final Settings settings;
041
042  public FileExclusions(Settings settings) {
043    this.settings = settings;
044  }
045
046  public String[] sourceInclusions() {
047    return inclusions(CoreProperties.PROJECT_INCLUSIONS_PROPERTY);
048  }
049
050  public String[] testInclusions() {
051    return inclusions(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY);
052  }
053
054  private String[] inclusions(String propertyKey) {
055    String[] patterns = sanitize(settings.getStringArray(propertyKey));
056    List<String> list = new ArrayList<>();
057    for (String pattern : patterns) {
058      if (!"**/*".equals(pattern) && !"file:**/*".equals(pattern)) {
059        list.add(pattern);
060      }
061    }
062    return list.toArray(new String[list.size()]);
063  }
064
065  public String[] sourceExclusions() {
066    return exclusions(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
067  }
068
069  public String[] testExclusions() {
070    return exclusions(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY);
071  }
072
073  private String[] exclusions(String globalExclusionsProperty, String exclusionsProperty) {
074    String[] globalExclusions = settings.getStringArray(globalExclusionsProperty);
075    String[] exclusions = settings.getStringArray(exclusionsProperty);
076    return sanitize(ObjectArrays.concat(globalExclusions, exclusions, String.class));
077  }
078
079  private static String[] sanitize(String[] patterns) {
080    for (int i = 0; i < patterns.length; i++) {
081      patterns[i] = StringUtils.trim(patterns[i]);
082    }
083    return patterns;
084  }
085}