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