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.apache.commons.lang.builder.ToStringBuilder; 024 import org.sonar.api.utils.WildcardPattern; 025 026 import java.util.List; 027 028 /** 029 * This class is an implementation of a resource of type FILE 030 * @since 1.10 031 */ 032 public class File extends Resource<Directory> { 033 034 private String directoryKey; 035 private String filename; 036 private Language language; 037 private Directory parent; 038 039 /** 040 * File in project. Key is the path relative to project source directories. It is not the absolute path 041 * and it does not include the path to source directories. Example : <code>new File("org/sonar/foo.sql")</code>. The 042 * absolute path may be c:/myproject/src/main/sql/org/sonar/foo.sql. Project root is c:/myproject and source dir 043 * is src/main/sql. 044 */ 045 public File(String key) { 046 if (key == null) { 047 throw new IllegalArgumentException("File key is null"); 048 } 049 String realKey = parseKey(key); 050 if (realKey.indexOf(Directory.SEPARATOR) >= 0) { 051 this.directoryKey = Directory.parseKey(StringUtils.substringBeforeLast(key, Directory.SEPARATOR)); 052 this.filename = StringUtils.substringAfterLast(realKey, Directory.SEPARATOR); 053 realKey = new StringBuilder().append(this.directoryKey).append(Directory.SEPARATOR).append(filename).toString(); 054 055 } else { 056 this.filename = key; 057 } 058 setKey(realKey); 059 } 060 061 /** 062 * Creates a file from its containing directory and name 063 */ 064 public File(String directory, String filename) { 065 this.filename = StringUtils.trim(filename); 066 if (StringUtils.isBlank(directory)) { 067 setKey(filename); 068 069 } else { 070 this.directoryKey = Directory.parseKey(directory); 071 setKey(new StringBuilder().append(directoryKey).append(Directory.SEPARATOR).append(this.filename).toString()); 072 } 073 } 074 075 /** 076 * Creates a File from its language and its key 077 */ 078 public File(Language language, String key) { 079 this(key); 080 this.language = language; 081 } 082 083 /** 084 * Creates a File from language, directory and filename 085 */ 086 public File(Language language, String directory, String filename) { 087 this(directory, filename); 088 this.language = language; 089 } 090 091 /** 092 * {@inheritDoc} 093 * 094 * @see Resource#getParent() 095 */ 096 public Directory getParent() { 097 if (parent == null) { 098 parent = new Directory(directoryKey); 099 } 100 return parent; 101 } 102 103 private static String parseKey(String key) { 104 if (StringUtils.isBlank(key)) { 105 return null; 106 } 107 108 key = key.replace('\\', '/'); 109 key = StringUtils.trim(key); 110 return key; 111 } 112 113 /** 114 * {@inheritDoc} 115 * 116 * @see Resource#matchFilePattern(String) 117 */ 118 public boolean matchFilePattern(String antPattern) { 119 WildcardPattern matcher = WildcardPattern.create(antPattern, "/"); 120 return matcher.match(getKey()); 121 } 122 123 /** 124 * Creates a File from an io.file and a list of sources directories 125 */ 126 public static File fromIOFile(java.io.File file, List<java.io.File> sourceDirs) { 127 String relativePath = DefaultProjectFileSystem.getRelativePath(file, sourceDirs); 128 if (relativePath != null) { 129 return new File(relativePath); 130 } 131 return null; 132 } 133 134 /** 135 * Creates a File from its name and a project 136 */ 137 public static File fromIOFile(java.io.File file, Project project) { 138 return fromIOFile(file, project.getFileSystem().getSourceDirs()); 139 } 140 141 /** 142 * {@inheritDoc} 143 * 144 * @see Resource#getName() 145 */ 146 public String getName() { 147 return filename; 148 } 149 150 /** 151 * {@inheritDoc} 152 * 153 * @see Resource#getLongName() 154 */ 155 public String getLongName() { 156 return getKey(); 157 } 158 159 /** 160 * {@inheritDoc} 161 * 162 * @see Resource#getDescription() 163 */ 164 public String getDescription() { 165 return null; 166 } 167 168 /** 169 * {@inheritDoc} 170 * 171 * @see Resource#getLanguage() 172 */ 173 public Language getLanguage() { 174 return language; 175 } 176 177 /** 178 * Sets the language of the file 179 */ 180 public void setLanguage(Language language) { 181 this.language = language; 182 } 183 184 /** 185 * @return SCOPE_ENTITY 186 */ 187 public String getScope() { 188 return Resource.SCOPE_ENTITY; 189 } 190 191 /** 192 * @return QUALIFIER_FILE 193 */ 194 public String getQualifier() { 195 return Resource.QUALIFIER_FILE; 196 } 197 198 @Override 199 public String toString() { 200 return new ToStringBuilder(this) 201 .append("key", getKey()) 202 .append("dir", directoryKey) 203 .append("filename", filename) 204 .append("language", language) 205 .toString(); 206 } 207 }