001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 org.apache.commons.lang.StringUtils; 023 024import javax.annotation.CheckForNull; 025import javax.annotation.Nullable; 026 027import java.io.Serializable; 028 029/** 030 * The interface to implement to create a resource in Sonar 031 * 032 * @since 1.10 033 */ 034public abstract class Resource implements Serializable { 035 036 /** 037 * @deprecated since 2.6. Use Scopes.PROJECT. 038 */ 039 @Deprecated 040 public static final String SCOPE_SET = Scopes.PROJECT; 041 042 /** 043 * @deprecated since 2.6. Use Scopes.DIRECTORY. 044 */ 045 @Deprecated 046 public static final String SCOPE_SPACE = Scopes.DIRECTORY; 047 048 /** 049 * @deprecated since 2.6. Use Scopes.FILE. 050 */ 051 @Deprecated 052 public static final String SCOPE_ENTITY = Scopes.FILE; 053 054 /** 055 * @deprecated since 2.6. Use Qualifiers.VIEW. 056 */ 057 @Deprecated 058 public static final String QUALIFIER_VIEW = Qualifiers.VIEW; 059 060 /** 061 * @deprecated since 2.6. Use Qualifiers.SUBVIEW. 062 */ 063 @Deprecated 064 public static final String QUALIFIER_SUBVIEW = Qualifiers.SUBVIEW; 065 066 /** 067 * @deprecated since 2.6. Use Qualifiers.LIBRARY. 068 */ 069 @Deprecated 070 public static final String QUALIFIER_LIB = Qualifiers.LIBRARY; 071 072 /** 073 * @deprecated since 2.6. Use Qualifiers.PROJECT. 074 */ 075 @Deprecated 076 public static final String QUALIFIER_PROJECT = Qualifiers.PROJECT; 077 078 /** 079 * @deprecated since 2.6. Use Qualifiers.MODULE. 080 */ 081 @Deprecated 082 public static final String QUALIFIER_MODULE = Qualifiers.MODULE; 083 084 /** 085 * @deprecated since 2.6. Use Qualifiers.PACKAGE. 086 */ 087 @Deprecated 088 public static final String QUALIFIER_PACKAGE = Qualifiers.PACKAGE; 089 090 /** 091 * @deprecated since 2.6. Use Qualifiers.DIRECTORY. 092 */ 093 @Deprecated 094 public static final String QUALIFIER_DIRECTORY = Qualifiers.DIRECTORY; 095 096 /** 097 * @deprecated since 2.6. Use Qualifiers.FILE. 098 */ 099 @Deprecated 100 public static final String QUALIFIER_FILE = Qualifiers.FILE; 101 102 /** 103 * @deprecated since 2.6. Use Qualifiers.CLASS. 104 */ 105 @Deprecated 106 public static final String QUALIFIER_CLASS = Qualifiers.CLASS; 107 108 /** 109 * @deprecated since 2.6. Use Qualifiers.FIELD. 110 */ 111 @Deprecated 112 public static final String QUALIFIER_FIELD = Qualifiers.FIELD; 113 114 /** 115 * @deprecated since 2.6. Use Qualifiers.METHOD. 116 */ 117 @Deprecated 118 public static final String QUALIFIER_METHOD = Qualifiers.METHOD; 119 120 /** 121 * @deprecated since 2.6. Use Qualifiers.UNIT_TEST_FILE. 122 */ 123 @Deprecated 124 public static final String QUALIFIER_UNIT_TEST_CLASS = Qualifiers.UNIT_TEST_FILE; 125 126 private Integer id; 127 128 private String key; 129 130 private String uuid; 131 132 private String path; 133 134 private String effectiveKey; 135 136 /** 137 * @return the resource key 138 */ 139 public final String getKey() { 140 return key; 141 } 142 143 /** 144 * Internal use only 145 */ 146 public void setKey(String s) { 147 this.key = s; 148 } 149 150 /** 151 * @since 5.0 152 */ 153 public final String getUuid() { 154 return uuid; 155 } 156 157 /** 158 * Internal use only 159 */ 160 public void setUuid(String s) { 161 this.uuid = s; 162 } 163 164 /** 165 * @return the resource name 166 */ 167 public abstract String getName(); 168 169 /** 170 * @return the resource long name 171 */ 172 public abstract String getLongName(); 173 174 /** 175 * @return the resource description 176 */ 177 public abstract String getDescription(); 178 179 /** 180 * @return the language of the resource. Only {@link File}s may have a non null value. 181 * @deprecated since 5.1 use {@link #language()} 182 */ 183 @Deprecated 184 @CheckForNull 185 public abstract Language getLanguage(); 186 187 /** 188 * @return the language of the resource. Only {@link File}s may have a non null value. 189 */ 190 @CheckForNull 191 public String language() { 192 Language l = getLanguage(); 193 return l != null ? l.getKey() : null; 194 } 195 196 /** 197 * @return the scope 198 */ 199 public abstract String getScope(); 200 201 /** 202 * The qualifier tells the type of the resource. For example, it can be a File, a Class, a Project, a Unit Test... 203 * 204 * @return the qualifier 205 * @see org.sonar.api.resources.Qualifiers for the list of qualifiers 206 * @see org.sonar.api.resources.ResourceUtils to find out if a resource if a class, a unit test,... from its qualifier 207 */ 208 public abstract String getQualifier(); 209 210 /** 211 * The parent is used to build the resources tree, for example for relations between files, directories and projects. 212 * <p> 213 * Return null if the parent is the current project (or module in case of multi-module). 214 * </p> 215 */ 216 @CheckForNull 217 public abstract Resource getParent(); 218 219 /** 220 * Check resource against an Ant pattern, like mypackag?/*Foo.java. It's used for example to match resource exclusions. 221 * 222 * @param antPattern Ant-like pattern (with **, * and ?). It includes file suffixes. 223 * @return true if the resource matches the Ant pattern 224 */ 225 public abstract boolean matchFilePattern(String antPattern); 226 227 public final Integer getId() { 228 return id; 229 } 230 231 /** 232 * Internal use only 233 */ 234 public Resource setId(Integer id) { 235 this.id = id; 236 return this; 237 } 238 239 public String getPath() { 240 return path; 241 } 242 243 public Resource setPath(@Nullable String path) { 244 this.path = normalize(path); 245 return this; 246 } 247 248 @CheckForNull 249 protected static String normalize(@Nullable String path) { 250 if (StringUtils.isBlank(path)) { 251 return null; 252 } 253 String normalizedPath = path; 254 normalizedPath = normalizedPath.replace('\\', '/'); 255 normalizedPath = StringUtils.trim(normalizedPath); 256 if (Directory.SEPARATOR.equals(normalizedPath)) { 257 return Directory.SEPARATOR; 258 } 259 normalizedPath = StringUtils.removeStart(normalizedPath, Directory.SEPARATOR); 260 normalizedPath = StringUtils.removeEnd(normalizedPath, Directory.SEPARATOR); 261 return normalizedPath; 262 } 263 264 public String getEffectiveKey() { 265 return effectiveKey; 266 } 267 268 /** 269 * Internal use only 270 */ 271 public final Resource setEffectiveKey(String effectiveKey) { 272 this.effectiveKey = effectiveKey; 273 return this; 274 } 275 276 /** 277 * @deprecated since 2.6. 278 */ 279 @Deprecated 280 public final boolean isExcluded() { 281 return false; 282 } 283 284 /** 285 * Internal use only 286 * 287 * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier. 288 */ 289 @Deprecated 290 public final Resource setExcluded(boolean b) { 291 return this; 292 } 293 294 @Override 295 public boolean equals(Object o) { 296 if (this == o) { 297 return true; 298 } 299 if (o == null) { 300 return false; 301 } 302 if (getClass() != o.getClass()) { 303 return false; 304 } 305 306 Resource resource = (Resource) o; 307 return key.equals(resource.key); 308 } 309 310 @Override 311 public int hashCode() { 312 // For File and Directory using deprecatedKey, key can be null 313 return key != null ? key.hashCode() : super.hashCode(); 314 } 315}