001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 java.io.Serializable;
023import javax.annotation.CheckForNull;
024import javax.annotation.Nullable;
025import org.apache.commons.lang.StringUtils;
026import org.sonar.api.batch.fs.InputComponent;
027
028/**
029 * @since 1.10
030 * @deprecated since 5.6 replaced by {@link InputComponent}
031 */
032@Deprecated
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;
126
127  private String key;
128
129  private String uuid;
130
131  private String path;
132
133  private String effectiveKey;
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   * @since 5.0
151   */
152  public final String getUuid() {
153    return uuid;
154  }
155
156  /**
157   * Internal use only
158   */
159  public void setUuid(String s) {
160    this.uuid = s;
161  }
162
163  /**
164   * @return the resource name
165   */
166  public abstract String getName();
167
168  /**
169   * @return the resource long name
170   */
171  public abstract String getLongName();
172
173  /**
174   * @return the resource description
175   */
176  public abstract String getDescription();
177
178  /**
179   * @return the language of the resource. Only {@link File}s may have a non null value.
180   * @deprecated since 5.1 use {@link #language()}
181   */
182  @Deprecated
183  @CheckForNull
184  public abstract Language getLanguage();
185
186  /**
187   * @return the language of the resource. Only {@link File}s may have a non null value.
188   */
189  @CheckForNull
190  public String language() {
191    Language l = getLanguage();
192    return l != null ? l.getKey() : null;
193  }
194
195  /**
196   * @return the scope
197   */
198  public abstract String getScope();
199
200  /**
201   * The qualifier tells the type of the resource. For example, it can be a File, a Class, a Project, a Unit Test...
202   *
203   * @return the qualifier
204   * @see org.sonar.api.resources.Qualifiers for the list of qualifiers
205   * @see org.sonar.api.resources.ResourceUtils to find out if a resource if a class, a unit test,... from its qualifier
206   */
207  public abstract String getQualifier();
208
209  /**
210   * The parent is used to build the resources tree, for example for relations between files, directories and projects.
211   * <p>
212   * Return null if the parent is the current project (or module in case of multi-module).
213   * 
214   */
215  @CheckForNull
216  public abstract Resource getParent();
217
218  /**
219   * Check resource against an Ant pattern, like mypackag?/*Foo.java. It's used for example to match resource exclusions.
220   *
221   * @param antPattern Ant-like pattern (with **, * and ?). It includes file suffixes.
222   * @return true if the resource matches the Ant pattern
223   */
224  public abstract boolean matchFilePattern(String antPattern);
225
226  public final Integer getId() {
227    return id;
228  }
229
230  /**
231   * Internal use only
232   */
233  public Resource setId(Integer id) {
234    this.id = id;
235    return this;
236  }
237
238  public String getPath() {
239    return path;
240  }
241
242  public Resource setPath(@Nullable String path) {
243    this.path = normalize(path);
244    return this;
245  }
246
247  @CheckForNull
248  protected static String normalize(@Nullable String path) {
249    if (StringUtils.isBlank(path)) {
250      return null;
251    }
252    String normalizedPath = path;
253    normalizedPath = normalizedPath.replace('\\', '/');
254    normalizedPath = StringUtils.trim(normalizedPath);
255    if (Directory.SEPARATOR.equals(normalizedPath)) {
256      return Directory.SEPARATOR;
257    }
258    normalizedPath = StringUtils.removeStart(normalizedPath, Directory.SEPARATOR);
259    normalizedPath = StringUtils.removeEnd(normalizedPath, Directory.SEPARATOR);
260    return normalizedPath;
261  }
262
263  public String getEffectiveKey() {
264    return effectiveKey;
265  }
266
267  /**
268   * Internal use only
269   */
270  public final Resource setEffectiveKey(String effectiveKey) {
271    this.effectiveKey = effectiveKey;
272    return this;
273  }
274
275  /**
276   * @deprecated since 2.6.
277   */
278  @Deprecated
279  public final boolean isExcluded() {
280    return false;
281  }
282
283  /**
284   * Internal use only
285   *
286   * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier.
287   */
288  @Deprecated
289  public final Resource setExcluded(boolean b) {
290    return this;
291  }
292
293  @Override
294  public boolean equals(Object o) {
295    if (this == o) {
296      return true;
297    }
298    if (o == null) {
299      return false;
300    }
301    if (getClass() != o.getClass()) {
302      return false;
303    }
304
305    Resource resource = (Resource) o;
306    return key.equals(resource.key);
307  }
308
309  @Override
310  public int hashCode() {
311    // For File and Directory using deprecatedKey, key can be null
312    return key != null ? key.hashCode() : super.hashCode();
313  }
314}