001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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.DIRECTORY.
085   */
086  @Deprecated
087  public static final String QUALIFIER_DIRECTORY = Qualifiers.DIRECTORY;
088
089  /**
090   * @deprecated since 2.6. Use Qualifiers.FILE.
091   */
092  @Deprecated
093  public static final String QUALIFIER_FILE = Qualifiers.FILE;
094
095  private Integer id;
096
097  private String key;
098
099  private String uuid;
100
101  private String path;
102
103  private String effectiveKey;
104
105  /**
106   * @return the resource key
107   */
108  public final String getKey() {
109    return key;
110  }
111
112  /**
113   * Internal use only
114   */
115  public void setKey(String s) {
116    this.key = s;
117  }
118
119  /**
120   * @since 5.0
121   */
122  public final String getUuid() {
123    return uuid;
124  }
125
126  /**
127   * Internal use only
128   */
129  public void setUuid(String s) {
130    this.uuid = s;
131  }
132
133  /**
134   * @return the resource name
135   */
136  public abstract String getName();
137
138  /**
139   * @return the resource long name
140   */
141  public abstract String getLongName();
142
143  /**
144   * @return the resource description
145   */
146  public abstract String getDescription();
147
148  /**
149   * @return the language of the resource. Only {@link File}s may have a non null value.
150   * @deprecated since 5.1 use {@link #language()}
151   */
152  @Deprecated
153  @CheckForNull
154  public abstract Language getLanguage();
155
156  /**
157   * @return the language of the resource. Only {@link File}s may have a non null value.
158   */
159  @CheckForNull
160  public String language() {
161    Language l = getLanguage();
162    return l != null ? l.getKey() : null;
163  }
164
165  /**
166   * @return the scope
167   */
168  public abstract String getScope();
169
170  /**
171   * The qualifier tells the type of the resource. For example, it can be a File, a Class, a Project, a Unit Test...
172   *
173   * @return the qualifier
174   * @see org.sonar.api.resources.Qualifiers for the list of qualifiers
175   * @see org.sonar.api.resources.ResourceUtils to find out if a resource if a class, a unit test,... from its qualifier
176   */
177  public abstract String getQualifier();
178
179  /**
180   * The parent is used to build the resources tree, for example for relations between files, directories and projects.
181   * <p>
182   * Return null if the parent is the current project (or module in case of multi-module).
183   * 
184   */
185  @CheckForNull
186  public abstract Resource getParent();
187
188  /**
189   * Check resource against an Ant pattern, like mypackag?/*Foo.java. It's used for example to match resource exclusions.
190   *
191   * @param antPattern Ant-like pattern (with **, * and ?). It includes file suffixes.
192   * @return true if the resource matches the Ant pattern
193   */
194  public abstract boolean matchFilePattern(String antPattern);
195
196  public final Integer getId() {
197    return id;
198  }
199
200  /**
201   * Internal use only
202   */
203  public Resource setId(Integer id) {
204    this.id = id;
205    return this;
206  }
207
208  public String getPath() {
209    return path;
210  }
211
212  public Resource setPath(@Nullable String path) {
213    this.path = normalize(path);
214    return this;
215  }
216
217  @CheckForNull
218  protected static String normalize(@Nullable String path) {
219    if (StringUtils.isBlank(path)) {
220      return null;
221    }
222    String normalizedPath = path;
223    normalizedPath = normalizedPath.replace('\\', '/');
224    normalizedPath = StringUtils.trim(normalizedPath);
225    if (Directory.SEPARATOR.equals(normalizedPath)) {
226      return Directory.SEPARATOR;
227    }
228    normalizedPath = StringUtils.removeStart(normalizedPath, Directory.SEPARATOR);
229    normalizedPath = StringUtils.removeEnd(normalizedPath, Directory.SEPARATOR);
230    return normalizedPath;
231  }
232
233  public String getEffectiveKey() {
234    return effectiveKey;
235  }
236
237  /**
238   * Internal use only
239   */
240  public final Resource setEffectiveKey(String effectiveKey) {
241    this.effectiveKey = effectiveKey;
242    return this;
243  }
244
245  /**
246   * @deprecated since 2.6.
247   */
248  @Deprecated
249  public final boolean isExcluded() {
250    return false;
251  }
252
253  /**
254   * Internal use only
255   *
256   * @deprecated since 2.6 should use SensorContext#isExcluded(resource). It will make inheritance of Resource easier.
257   */
258  @Deprecated
259  public final Resource setExcluded(boolean b) {
260    return this;
261  }
262
263  @Override
264  public boolean equals(Object o) {
265    if (this == o) {
266      return true;
267    }
268    if (o == null) {
269      return false;
270    }
271    if (getClass() != o.getClass()) {
272      return false;
273    }
274
275    Resource resource = (Resource) o;
276    return key.equals(resource.key);
277  }
278
279  @Override
280  public int hashCode() {
281    // For File and Directory using deprecatedKey, key can be null
282    return key != null ? key.hashCode() : super.hashCode();
283  }
284}