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