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.resources; 021 022import java.util.List; 023import javax.annotation.CheckForNull; 024import org.apache.commons.lang.StringUtils; 025import org.apache.commons.lang.builder.ToStringBuilder; 026import org.sonar.api.batch.SensorContext; 027import org.sonar.api.batch.fs.FileSystem; 028import org.sonar.api.batch.fs.InputFile; 029import org.sonar.api.scan.filesystem.PathResolver; 030import org.sonar.api.utils.WildcardPattern; 031 032/** 033 * @since 1.10 034 * @deprecated since 5.6 replaced by {@link InputFile}. 035 */ 036@Deprecated 037public class File extends Resource { 038 039 public static final String SCOPE = Scopes.FILE; 040 041 private String filename; 042 private Language language; 043 private Directory parent; 044 private String qualifier = Qualifiers.FILE; 045 046 private final String relativePathFromSourceDir; 047 048 private File() { 049 // Used by factory method 050 this.relativePathFromSourceDir = null; 051 } 052 053 /** 054 * @deprecated since 4.2 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} 055 */ 056 @Deprecated 057 public File(String relativePathFromSourceDir) { 058 if (relativePathFromSourceDir == null) { 059 throw new IllegalArgumentException("File key is null"); 060 } 061 this.relativePathFromSourceDir = parseKey(relativePathFromSourceDir); 062 } 063 064 /** 065 * @deprecated since 4.2 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} 066 */ 067 @Deprecated 068 public File(String relativeDirectoryPathFromSourceDir, String filename) { 069 this.filename = StringUtils.trim(filename); 070 if (StringUtils.isBlank(relativeDirectoryPathFromSourceDir)) { 071 this.relativePathFromSourceDir = filename; 072 } else { 073 this.relativePathFromSourceDir = new StringBuilder().append(Directory.parseKey(relativeDirectoryPathFromSourceDir)).append(Directory.SEPARATOR).append(this.filename) 074 .toString(); 075 } 076 } 077 078 /** 079 * @deprecated since 4.2 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} 080 */ 081 @Deprecated 082 public File(Language language, String relativePathFromSourceDir) { 083 this(relativePathFromSourceDir); 084 this.language = language; 085 } 086 087 /** 088 * Creates a File from language, directory and filename 089 * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} 090 */ 091 @Deprecated 092 public File(Language language, String relativeDirectoryPathFromSourceDir, String filename) { 093 this(relativeDirectoryPathFromSourceDir, filename); 094 this.language = language; 095 } 096 097 /** 098 * Internal. 099 */ 100 public String relativePathFromSourceDir() { 101 return relativePathFromSourceDir; 102 } 103 104 /** 105 * {@inheritDoc} 106 * 107 * @see Resource#getParent() 108 */ 109 @Override 110 public Directory getParent() { 111 return parent; 112 } 113 114 private static String parseKey(String key) { 115 if (StringUtils.isBlank(key)) { 116 return null; 117 } 118 String normalizedKey = key; 119 normalizedKey = normalizedKey.replace('\\', '/'); 120 normalizedKey = StringUtils.trim(normalizedKey); 121 return normalizedKey; 122 } 123 124 /** 125 * {@inheritDoc} 126 * 127 * @see Resource#matchFilePattern(String) 128 */ 129 @Override 130 public boolean matchFilePattern(String antPattern) { 131 WildcardPattern matcher = WildcardPattern.create(antPattern, Directory.SEPARATOR); 132 return matcher.match(getKey()); 133 } 134 135 /** 136 * Creates a File from an io.file and a list of sources directories 137 * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)} 138 */ 139 @Deprecated 140 @CheckForNull 141 public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) { 142 PathResolver.RelativePath relativePath = new PathResolver().relativePath(sourceDirs, file); 143 if (relativePath != null) { 144 return new File(relativePath.path()); 145 } 146 return null; 147 } 148 149 /** 150 * Creates a {@link File} from an absolute {@link java.io.File} and a module. 151 * The returned {@link File} can be then passed for example to 152 * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}. 153 * @param file absolute path to a file 154 * @param module 155 * @return null if the file is not under module basedir. 156 * @deprecated since 4.5 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} 157 */ 158 @Deprecated 159 @CheckForNull 160 public static File fromIOFile(java.io.File file, Project module) { 161 String relativePathFromBasedir = new PathResolver().relativePath(module.getBaseDir(), file); 162 if (relativePathFromBasedir != null) { 163 return File.create(relativePathFromBasedir); 164 } 165 return null; 166 } 167 168 /** 169 * {@inheritDoc} 170 * 171 * @see Resource#getName() 172 */ 173 @Override 174 public String getName() { 175 return filename; 176 } 177 178 /** 179 * {@inheritDoc} 180 * 181 * @see Resource#getLongName() 182 */ 183 @Override 184 public String getLongName() { 185 return StringUtils.defaultIfBlank(getPath(), getKey()); 186 } 187 188 /** 189 * {@inheritDoc} 190 * 191 * @see Resource#getDescription() 192 */ 193 @Override 194 public String getDescription() { 195 return null; 196 } 197 198 /** 199 * {@inheritDoc} 200 * 201 * @see Resource#getLanguage() 202 */ 203 @Override 204 public Language getLanguage() { 205 return language; 206 } 207 208 /** 209 * Sets the language of the file 210 */ 211 public void setLanguage(Language language) { 212 this.language = language; 213 } 214 215 /** 216 * @return SCOPE_ENTITY 217 */ 218 @Override 219 public final String getScope() { 220 return SCOPE; 221 } 222 223 /** 224 * Returns the qualifier associated to this File. Should be QUALIFIER_FILE or QUALIFIER_UNIT_TEST_CLASS 225 */ 226 @Override 227 public String getQualifier() { 228 return qualifier; 229 } 230 231 public void setQualifier(String qualifier) { 232 this.qualifier = qualifier; 233 } 234 235 /** 236 * Internal use only. 237 * @deprecated since 5.1 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} 238 */ 239 @Deprecated 240 public static File create(String relativePathFromBasedir) { 241 File file = new File(); 242 String normalizedPath = normalize(relativePathFromBasedir); 243 file.setKey(normalizedPath); 244 file.setPath(normalizedPath); 245 String directoryPath; 246 if (normalizedPath != null && normalizedPath.contains(Directory.SEPARATOR)) { 247 directoryPath = StringUtils.substringBeforeLast(normalizedPath, Directory.SEPARATOR); 248 file.filename = StringUtils.substringAfterLast(normalizedPath, Directory.SEPARATOR); 249 } else { 250 directoryPath = Directory.SEPARATOR; 251 file.filename = normalizedPath; 252 } 253 file.parent = Directory.create(directoryPath); 254 return file; 255 } 256 257 /** 258 * Internal use only. 259 * @deprecated since 5.1 use {@link FileSystem#inputFile(org.sonar.api.batch.fs.FilePredicate)} 260 */ 261 @Deprecated 262 public static File create(String relativePathFromBasedir, Language language, boolean unitTest) { 263 File file = create(relativePathFromBasedir); 264 file.setLanguage(language); 265 if (unitTest) { 266 file.setQualifier(Qualifiers.UNIT_TEST_FILE); 267 } 268 return file; 269 } 270 271 @Override 272 public String toString() { 273 return new ToStringBuilder(this) 274 .append("key", getKey()) 275 .append("path", getPath()) 276 .append("filename", filename) 277 .append("language", language) 278 .toString(); 279 } 280}