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