001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 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 * @since 1.10 030 */ 031 public class JavaFile extends AbstractResource<JavaPackage> { 032 033 private String packageName; 034 035 public JavaFile(String packageName, String className) { 036 this(packageName, className, false); 037 } 038 039 public JavaFile(String packageName, String className, boolean unitTest) { 040 super(Resource.SCOPE_ENTITY, (unitTest ? Resource.QUALIFIER_UNIT_TEST_CLASS : Resource.QUALIFIER_CLASS)); 041 this.packageName = StringUtils.isBlank(packageName) ? JavaPackage.DEFAULT_PACKAGE_NAME : packageName; 042 setLanguage(Java.INSTANCE); 043 setName(className); 044 setKey(new StringBuilder().append(this.packageName).append(".").append(getName()).toString()); 045 } 046 047 public JavaFile(String key) { 048 this(key, false); 049 } 050 051 public JavaFile(String key, boolean unitTest) { 052 super(Resource.SCOPE_ENTITY, (unitTest ? Resource.QUALIFIER_UNIT_TEST_CLASS : Resource.QUALIFIER_CLASS)); 053 setLanguage(Java.INSTANCE); 054 setKey(key); 055 if (getKey().contains(".")) { 056 setName(StringUtils.substringAfterLast(getKey(), ".")); 057 packageName = StringUtils.substringBeforeLast(getKey(), "."); 058 } else { 059 setName(getKey()); 060 setKey(JavaPackage.DEFAULT_PACKAGE_NAME.concat(".").concat(getKey())); 061 } 062 } 063 064 @Override 065 public JavaFile setKey(String key) { 066 if (key != null && key.indexOf('$') > 0) { 067 throw new IllegalArgumentException("Java inner classes are not supported yet : " + key); 068 } 069 return (JavaFile) super.setKey(key); 070 } 071 072 @Override 073 public JavaPackage getParent() { 074 return new JavaPackage(packageName); 075 } 076 077 public boolean matchFilePattern(String antPattern) { 078 String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, "."); 079 WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, "."); 080 return matcher.match(getKey()); 081 } 082 083 084 public static JavaFile fromIOFile(File file, List<File> sourceDirs, boolean unitTest) { 085 if (file == null || !StringUtils.endsWithIgnoreCase(file.getName(), ".java")) { 086 return null; 087 } 088 String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs); 089 if (relativePath != null) { 090 String pacname = null; 091 String classname = relativePath; 092 093 if (relativePath.indexOf('/') >= 0) { 094 pacname = StringUtils.substringBeforeLast(relativePath, "/"); 095 pacname = StringUtils.replace(pacname, "/", "."); 096 classname = StringUtils.substringAfterLast(relativePath, "/"); 097 } 098 classname = StringUtils.substringBeforeLast(classname, "."); 099 return new JavaFile(pacname, classname, unitTest); 100 } 101 return null; 102 } 103 104 public static JavaFile fromAbsolutePath(String path, List<File> sourceDirs, boolean unitTest) { 105 if (path == null) { 106 return null; 107 } 108 return fromIOFile(new File(path), sourceDirs, unitTest); 109 } 110 111 }