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