001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.resources; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.sonar.api.utils.WildcardPattern; 024 025 import java.io.File; 026 import java.util.List; 027 028 /** 029 * A class that represents a Java class. This class can either be a Test class or source class 030 * 031 * @since 1.10 032 */ 033 public class JavaFile extends Resource<JavaPackage> { 034 035 private String filename; 036 private String longName; 037 private String packageKey; 038 private boolean unitTest; 039 private JavaPackage parent; 040 041 /** 042 * Creates a JavaFile that is not a class of test based on package and file names 043 */ 044 public JavaFile(String packageName, String className) { 045 this(packageName, className, false); 046 } 047 048 /** 049 * Creates a JavaFile that can be of any type based on package and file names 050 * 051 * @param unitTest whether it is a unit test file or a source file 052 */ 053 public JavaFile(String packageKey, String className, boolean unitTest) { 054 if (className == null) { 055 throw new IllegalArgumentException("Java filename can not be null"); 056 } 057 this.filename = StringUtils.trim(className); 058 String key; 059 if (StringUtils.isBlank(packageKey)) { 060 this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; 061 this.longName = this.filename; 062 key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); 063 } else { 064 this.packageKey = packageKey.trim(); 065 key = new StringBuilder().append(this.packageKey).append(".").append(this.filename).toString(); 066 this.longName = key; 067 } 068 setKey(key); 069 this.unitTest = unitTest; 070 } 071 072 /** 073 * Creates a source file from its key 074 */ 075 public JavaFile(String key) { 076 this(key, false); 077 } 078 079 /** 080 * Creates any JavaFile from its key 081 * 082 * @param unitTest whether it is a unit test file or a source file 083 */ 084 public JavaFile(String key, boolean unitTest) { 085 if (key == null) { 086 throw new IllegalArgumentException("Java filename can not be null"); 087 } 088 String realKey = StringUtils.trim(key); 089 this.unitTest = unitTest; 090 091 if (realKey.contains(".")) { 092 this.filename = StringUtils.substringAfterLast(realKey, "."); 093 this.packageKey = StringUtils.substringBeforeLast(realKey, "."); 094 this.longName = realKey; 095 096 } else { 097 this.filename = realKey; 098 this.longName = realKey; 099 this.packageKey = JavaPackage.DEFAULT_PACKAGE_NAME; 100 realKey = new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(realKey).toString(); 101 } 102 setKey(realKey); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 public JavaPackage getParent() { 109 if (parent == null) { 110 parent = new JavaPackage(packageKey); 111 } 112 return parent; 113 114 } 115 116 /** 117 * @return null 118 */ 119 public String getDescription() { 120 return null; 121 } 122 123 /** 124 * @return Java 125 */ 126 public Language getLanguage() { 127 return Java.INSTANCE; 128 } 129 130 /** 131 * {@inheritDoc} 132 */ 133 public String getName() { 134 return filename; 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 public String getLongName() { 141 return longName; 142 } 143 144 /** 145 * @return SCOPE_ENTITY 146 */ 147 public String getScope() { 148 return Scopes.FILE; 149 } 150 151 /** 152 * @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_CLASS depending whether it is a unit test class 153 */ 154 public String getQualifier() { 155 return unitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.CLASS; 156 } 157 158 /** 159 * @return whether the JavaFile is a unit test class or not 160 */ 161 public boolean isUnitTest() { 162 return unitTest; 163 } 164 165 /** 166 * {@inheritDoc} 167 */ 168 public boolean matchFilePattern(String antPattern) { 169 if (unitTest) { 170 return false; 171 } 172 String fileKey = getKey(); 173 if (!fileKey.endsWith(".java")) { 174 fileKey += ".java"; 175 } 176 if (StringUtils.substringAfterLast(antPattern, "/").indexOf(".") < 0) { 177 antPattern += ".*"; 178 } 179 WildcardPattern matcher = WildcardPattern.create(antPattern, "."); 180 return matcher.match(fileKey); 181 } 182 183 public static JavaFile fromRelativePath(String relativePath, boolean unitTest) { 184 if (relativePath != null) { 185 String pacname = null; 186 String classname = relativePath; 187 188 if (relativePath.indexOf('/') >= 0) { 189 pacname = StringUtils.substringBeforeLast(relativePath, "/"); 190 pacname = StringUtils.replace(pacname, "/", "."); 191 classname = StringUtils.substringAfterLast(relativePath, "/"); 192 } 193 classname = StringUtils.substringBeforeLast(classname, "."); 194 return new JavaFile(pacname, classname, unitTest); 195 } 196 return null; 197 } 198 199 /** 200 * Creates a JavaFile from a file in the source directories 201 * 202 * @return the JavaFile created if exists, null otherwise 203 */ 204 public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) { 205 if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) { 206 return null; 207 } 208 String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs); 209 return fromRelativePath(relativePath, unitTest); 210 } 211 212 /** 213 * Shortcut to fromIOFile with an abolute path 214 */ 215 public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) { 216 if (path == null) { 217 return null; 218 } 219 return fromIOFile(new File(path), sourceDirs, unitTest); 220 } 221 222 @Override 223 public String toString() { 224 return getKey(); 225 } 226 227 }