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