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 */
020 package org.sonar.api.resources;
021
022 import org.apache.commons.lang.StringUtils;
023
024 import javax.annotation.CheckForNull;
025 import javax.annotation.Nullable;
026
027 import java.io.Serializable;
028
029 /**
030 * The interface to implement to create a resource in Sonar
031 *
032 * @since 1.10
033 */
034 public 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 deprecatedKey;
133
134 private String path;
135
136 private String effectiveKey;
137
138 /**
139 * @return the resource key
140 */
141 public final String getKey() {
142 return key;
143 }
144
145 /**
146 * Internal use only
147 */
148 public void setKey(String s) {
149 this.key = s;
150 }
151
152 /**
153 * @since 5.0
154 */
155 public final String getUuid() {
156 return uuid;
157 }
158
159 /**
160 * Internal use only
161 */
162 public void setUuid(String s) {
163 this.uuid = s;
164 }
165
166 /**
167 * @return the resource deprecated key. Should not be used except to deal with backward compatibility.
168 * @since 4.2
169 */
170 public final String getDeprecatedKey() {
171 return deprecatedKey;
172 }
173
174 /**
175 * For internal use only
176 */
177 public void setDeprecatedKey(String s) {
178 this.deprecatedKey = s;
179 }
180
181 /**
182 * @return the resource name
183 */
184 public abstract String getName();
185
186 /**
187 * @return the resource long name
188 */
189 public abstract String getLongName();
190
191 /**
192 * @return the resource description
193 */
194 public abstract String getDescription();
195
196 /**
197 * @return the language of the resource. Only {@link File}s have a non null value.
198 */
199 @CheckForNull
200 public abstract Language getLanguage();
201
202 /**
203 * @return the scope
204 */
205 public abstract String getScope();
206
207 /**
208 * The qualifier tells the type of the resource. For example, it can be a File, a Class, a Project, a Unit Test...
209 *
210 * @return the qualifier
211 * @see org.sonar.api.resources.Qualifiers for the list of qualifiers
212 * @see org.sonar.api.resources.ResourceUtils to find out if a resource if a class, a unit test,... from its qualifier
213 */
214 public abstract String getQualifier();
215
216 /**
217 * The parent is used to build the resources tree, for example for relations between classes, packages and projects.
218 * <p>
219 * Return null if the parent is the project.
220 * </p>
221 */
222 public abstract Resource getParent();
223
224 /**
225 * Check resource against an Ant pattern, like mypackag?/*Foo.java. It's used for example to match resource exclusions.
226 *
227 * @param antPattern Ant-like pattern (with **, * and ?). It includes file suffixes.
228 * @return true if the resource matches the Ant pattern
229 */
230 public abstract boolean matchFilePattern(String antPattern);
231
232 public final Integer getId() {
233 return id;
234 }
235
236 /**
237 * Internal use only
238 */
239 public Resource setId(Integer id) {
240 this.id = id;
241 return this;
242 }
243
244 public String getPath() {
245 return path;
246 }
247
248 public Resource setPath(@Nullable String path) {
249 this.path = normalize(path);
250 return this;
251 }
252
253 @CheckForNull
254 protected static String normalize(@Nullable String path) {
255 if (StringUtils.isBlank(path)) {
256 return null;
257 }
258 String normalizedPath = path;
259 normalizedPath = normalizedPath.replace('\\', '/');
260 normalizedPath = StringUtils.trim(normalizedPath);
261 if (Directory.SEPARATOR.equals(normalizedPath)) {
262 return Directory.SEPARATOR;
263 }
264 normalizedPath = StringUtils.removeStart(normalizedPath, Directory.SEPARATOR);
265 normalizedPath = StringUtils.removeEnd(normalizedPath, Directory.SEPARATOR);
266 return normalizedPath;
267 }
268
269 public String getEffectiveKey() {
270 return effectiveKey;
271 }
272
273 /**
274 * Internal use only
275 */
276 public final Resource setEffectiveKey(String effectiveKey) {
277 this.effectiveKey = effectiveKey;
278 return this;
279 }
280
281 /**
282 * @deprecated since 2.6.
283 */
284 @Deprecated
285 public final boolean isExcluded() {
286 return false;
287 }
288
289 /**
290 * Internal use only
291 *
292 * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier.
293 */
294 @Deprecated
295 public final Resource setExcluded(boolean b) {
296 return this;
297 }
298
299 @Override
300 public boolean equals(Object o) {
301 if (this == o) {
302 return true;
303 }
304 if (o == null) {
305 return false;
306 }
307 if (getClass() != o.getClass()) {
308 return false;
309 }
310
311 Resource resource = (Resource) o;
312 if (key != null) {
313 return key.equals(resource.key);
314 } else {
315 return resource.key == null && deprecatedKey.equals(resource.deprecatedKey);
316 }
317 }
318
319 @Override
320 public int hashCode() {
321 return key != null ? key.hashCode() : deprecatedKey.hashCode();
322 }
323 }