001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact 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; 021 022import java.io.File; 023import java.nio.charset.Charset; 024import java.util.SortedSet; 025import javax.annotation.CheckForNull; 026import org.sonar.api.batch.BatchSide; 027 028/** 029 * The {@link FileSystem} manages all the source files to be analyzed. 030 * <p> 031 * This is not an extension point so it must not be implemented by plugins. It must be injected as a 032 * constructor parameter : 033 * <pre> 034 * public class MySensor implements Sensor { 035 * private final FileSystem fs; 036 * 037 * public MySensor(FileSystem fs) { 038 * this.fs = fs; 039 * } 040 * } 041 * </pre> 042 * 043 * <h2>How to use in unit tests</h2> 044 * The unit tests needing an instance of FileSystem can use the implementation 045 * {@link org.sonar.api.batch.fs.internal.DefaultFileSystem} and the related {@link org.sonar.api.batch.fs.internal.DefaultInputFile}, 046 * for example : 047 * <pre> 048 * DefaultFileSystem fs = new DefaultFileSystem(); 049 * fs.add(new DefaultInputFile("myprojectKey", "src/foo/bar.php")); 050 * </pre> 051 * 052 * @since 4.2 053 */ 054@BatchSide 055public interface FileSystem { 056 057 /** 058 * Absolute base directory of module 059 */ 060 File baseDir(); 061 062 /** 063 * Default encoding of input files. If it's not defined, then 064 * the platform default encoding is returned 065 */ 066 Charset encoding(); 067 068 /** 069 * Absolute work directory. It can be used to 070 * store third-party analysis reports. 071 * <br> 072 * The work directory can be located outside {@link #baseDir()}. 073 */ 074 File workDir(); 075 076 /** 077 * Factory of {@link FilePredicate} 078 */ 079 FilePredicates predicates(); 080 081 /** 082 * Returns the single element matching the predicate. If more than one elements match 083 * the predicate, then {@link IllegalArgumentException} is thrown. Returns {@code null} 084 * if no files match. 085 * 086 * <p> 087 * How to use : 088 * <pre> 089 * InputFile file = fs.inputFile(fs.predicates().hasRelativePath("src/Foo.php")); 090 * </pre> 091 * 092 * @see #predicates() 093 */ 094 @CheckForNull 095 InputFile inputFile(FilePredicate predicate); 096 097 /** 098 * Returns {@link InputDir} matching the current {@link File}. 099 * @return null if directory is not indexed. 100 * @throws IllegalArgumentException is File is null or not a directory. 101 * 102 * @since 4.5 103 */ 104 @CheckForNull 105 InputDir inputDir(File dir); 106 107 /** 108 * Input files matching the given attributes. Return all the files if the parameter 109 * <code>attributes</code> is empty. 110 * <p> 111 * <b>Important</b> - result is an {@link java.lang.Iterable} to benefit from streaming and decreasing 112 * memory consumption. It should be iterated only once, else copy it into a list : 113 * {@code com.google.common.collect.Lists.newArrayList(inputFiles(predicate))} 114 * <p> 115 * How to use : 116 * <pre> 117 * {@code 118 * FilePredicates p = fs.predicates(); 119 * Iterable<InputFile> files = fs.inputFiles(p.and(p.hasLanguage("java"), p.hasType(InputFile.Type.MAIN))); 120 * } 121 * </pre> 122 * 123 * @see #predicates() 124 */ 125 Iterable<InputFile> inputFiles(FilePredicate predicate); 126 127 /** 128 * Returns true if at least one {@link org.sonar.api.batch.fs.InputFile} matches 129 * the given predicate. This method can be faster than checking if {@link #inputFiles(org.sonar.api.batch.fs.FilePredicate)} 130 * has elements. 131 * @see #predicates() 132 */ 133 boolean hasFiles(FilePredicate predicate); 134 135 /** 136 * Files matching the given predicate. 137 * @see #predicates() 138 */ 139 Iterable<File> files(FilePredicate predicate); 140 141 /** 142 * Languages detected in all files, whatever their type (main or test) 143 */ 144 SortedSet<String> languages(); 145 146 /** 147 * Utility method mainly used to resolve location of reports. 148 * @return file in canonical form from specified path. Path can be absolute or relative to project basedir. 149 * For example resolvePath("pom.xml") or resolvePath("src/main/java") 150 * @since 5.0 151 */ 152 File resolvePath(String path); 153 154 /** 155 * Interface of the underlying file index. 156 */ 157 interface Index { 158 Iterable<InputFile> inputFiles(); 159 160 @CheckForNull 161 InputFile inputFile(String relativePath); 162 163 @CheckForNull 164 InputDir inputDir(String relativePath); 165 } 166}