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.batch.fs.internal;
021
022import org.sonar.api.batch.fs.FilePredicate;
023import org.sonar.api.batch.fs.FilePredicates;
024import org.sonar.api.batch.fs.InputFile;
025
026import java.io.File;
027import java.nio.file.Path;
028import java.util.ArrayList;
029import java.util.Arrays;
030import java.util.Collection;
031import java.util.List;
032
033/**
034 * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
035 *
036 * @since 4.2
037 */
038public class DefaultFilePredicates implements FilePredicates {
039
040  private final Path baseDir;
041
042  /**
043   * Client code should use {@link org.sonar.api.batch.fs.FileSystem#predicates()} to get an instance
044   */
045  DefaultFilePredicates(Path baseDir) {
046    this.baseDir = baseDir;
047  }
048
049  /**
050   * Returns a predicate that always evaluates to true
051   */
052  @Override
053  public FilePredicate all() {
054    return TruePredicate.TRUE;
055  }
056
057  /**
058   * Returns a predicate that always evaluates to false
059   */
060  @Override
061  public FilePredicate none() {
062    return FalsePredicate.FALSE;
063  }
064
065  @Override
066  public FilePredicate hasAbsolutePath(String s) {
067    return new AbsolutePathPredicate(s, baseDir);
068  }
069
070  /**
071   * non-normalized path and Windows-style path are supported
072   */
073  @Override
074  public FilePredicate hasRelativePath(String s) {
075    return new RelativePathPredicate(s);
076  }
077
078  @Override
079  public FilePredicate hasFilename(String s) {
080    return new FilenamePredicate(s);
081  }
082
083  @Override public FilePredicate hasExtension(String s) {
084    return new FileExtensionPredicate(s);
085  }
086
087  @Override
088  public FilePredicate matchesPathPattern(String inclusionPattern) {
089    return new PathPatternPredicate(PathPattern.create(inclusionPattern));
090  }
091
092  @Override
093  public FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
094    if (inclusionPatterns.length == 0) {
095      return TruePredicate.TRUE;
096    }
097    FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
098    for (int i = 0; i < inclusionPatterns.length; i++) {
099      predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
100    }
101    return or(predicates);
102  }
103
104  @Override
105  public FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
106    return not(matchesPathPattern(exclusionPattern));
107  }
108
109  @Override
110  public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
111    if (exclusionPatterns.length == 0) {
112      return TruePredicate.TRUE;
113    }
114    return not(matchesPathPatterns(exclusionPatterns));
115  }
116
117  @Override
118  public FilePredicate hasPath(String s) {
119    File file = new File(s);
120    if (file.isAbsolute()) {
121      return hasAbsolutePath(s);
122    }
123    return hasRelativePath(s);
124  }
125
126  @Override
127  public FilePredicate is(File ioFile) {
128    if (ioFile.isAbsolute()) {
129      return hasAbsolutePath(ioFile.getAbsolutePath());
130    }
131    return hasRelativePath(ioFile.getPath());
132  }
133
134  @Override
135  public FilePredicate hasLanguage(String language) {
136    return new LanguagePredicate(language);
137  }
138
139  @Override
140  public FilePredicate hasLanguages(Collection<String> languages) {
141    List<FilePredicate> list = new ArrayList<>();
142    for (String language : languages) {
143      list.add(hasLanguage(language));
144    }
145    return or(list);
146  }
147
148  @Override
149  public FilePredicate hasLanguages(String... languages) {
150    List<FilePredicate> list = new ArrayList<>();
151    for (String language : languages) {
152      list.add(hasLanguage(language));
153    }
154    return or(list);
155  }
156
157  @Override
158  public FilePredicate hasType(InputFile.Type type) {
159    return new TypePredicate(type);
160  }
161
162  @Override
163  public FilePredicate not(FilePredicate p) {
164    return new NotPredicate(p);
165  }
166
167  @Override
168  public FilePredicate or(Collection<FilePredicate> or) {
169    return OrPredicate.create(or);
170  }
171
172  @Override
173  public FilePredicate or(FilePredicate... or) {
174    return OrPredicate.create(Arrays.asList(or));
175  }
176
177  @Override
178  public FilePredicate or(FilePredicate first, FilePredicate second) {
179    return OrPredicate.create(Arrays.asList(first, second));
180  }
181
182  @Override
183  public FilePredicate and(Collection<FilePredicate> and) {
184    return AndPredicate.create(and);
185  }
186
187  @Override
188  public FilePredicate and(FilePredicate... and) {
189    return AndPredicate.create(Arrays.asList(and));
190  }
191
192  @Override
193  public FilePredicate and(FilePredicate first, FilePredicate second) {
194    return AndPredicate.create(Arrays.asList(first, second));
195  }
196}