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 */
020package org.sonar.api.resources;
021
022import org.apache.commons.lang.StringUtils;
023
024/**
025 * The qualifier determines the exact type of a resource.
026 * Plugins can define their own qualifiers.
027 *
028 * @since 2.6
029 */
030public final class Qualifiers {
031
032  private Qualifiers() {
033    // only static methods
034  }
035
036  /**
037   * Root views. Scope of views is Scopes.PROJECT
038   */
039  public static final String VIEW = "VW";
040
041  /**
042   * Sub-views, defined in root views. Scope of sub-views is Scopes.PROJECT
043   */
044  public static final String SUBVIEW = "SVW";
045
046  /**
047   * Library, for example a JAR dependency of Java projects.
048   * Scope of libraries is Scopes.PROJECT
049   * @deprecated since 5.2 No more design features
050   */
051  @Deprecated
052  public static final String LIBRARY = "LIB";
053
054  /**
055   * Single project or root of multi-modules projects
056   * Scope is Scopes.PROJECT
057   */
058  public static final String PROJECT = "TRK";
059
060  /**
061   * Module of a multi-modules project. It's sometimes called "sub-project".
062   * Scope of modules is Scopes.PROJECT
063   */
064  public static final String MODULE = "BRC";
065
066  /**
067   * @deprecated since 4.2 now packages are considered as regular directories. Use {@link #DIRECTORY} instead.
068   */
069  @Deprecated
070  public static final String PACKAGE = "PAC";
071  public static final String DIRECTORY = "DIR";
072  public static final String FILE = "FIL";
073  /**
074   * @deprecated since 4.2 now java files are considered as regular files. Use {@link #FILE} instead.
075   */
076  @Deprecated
077  public static final String CLASS = "CLA";
078  /**
079   * @deprecated since 4.2 resources under FILE level will no more be supported.
080   */
081  @Deprecated
082  public static final String PARAGRAPH = "PAR";
083  /**
084   * @deprecated since 4.2 resources under FILE level will no more be supported.
085   */
086  @Deprecated
087  public static final String METHOD = "MET";
088  /**
089   * @deprecated since 4.2 resources under FILE level will no more be supported.
090   */
091  @Deprecated
092  public static final String FIELD = "FLD";
093
094  // ugly, should be replaced by "natures"
095  public static final String UNIT_TEST_FILE = "UTS";
096
097  /**
098   * @param resource not nullable
099   */
100  public static boolean isView(final Resource resource, final boolean acceptSubViews) {
101    boolean isView = StringUtils.equals(VIEW, resource.getQualifier());
102    if (!isView && acceptSubViews) {
103      isView = StringUtils.equals(SUBVIEW, resource.getQualifier());
104    }
105
106    return isView;
107  }
108
109  /**
110   * @param resource not nullable
111   */
112  public static boolean isSubview(final Resource resource) {
113    return StringUtils.equals(SUBVIEW, resource.getScope());
114  }
115
116  /**
117   * @param resource not nullable
118   */
119  public static boolean isProject(final Resource resource, final boolean acceptModules) {
120    boolean isProject = StringUtils.equals(PROJECT, resource.getQualifier());
121    if (!isProject && acceptModules) {
122      isProject = StringUtils.equals(MODULE, resource.getQualifier());
123    }
124    return isProject;
125  }
126
127  /**
128   * @param resource not nullable
129   */
130  public static boolean isModule(final Resource resource) {
131    return StringUtils.equals(MODULE, resource.getQualifier());
132  }
133
134  /**
135   * @param resource not nullable
136   */
137  public static boolean isDirectory(final Resource resource) {
138    return StringUtils.equals(DIRECTORY, resource.getQualifier());
139  }
140
141  /**
142   * @deprecated since 4.2 Package is now a directory. Use {@link #isDirectory(Resource)}
143   */
144  @Deprecated
145  public static boolean isPackage(final Resource resource) {
146    return StringUtils.equals(PACKAGE, resource.getQualifier()) || isDirectory(resource);
147  }
148
149  /**
150   * @param resource not nullable
151   */
152  public static boolean isFile(final Resource resource) {
153    return StringUtils.equals(FILE, resource.getQualifier());
154  }
155
156  /**
157   * @param resource not nullable
158   * @deprecated since 4.2 CLA qualifier is deprecated
159   */
160  @Deprecated
161  public static boolean isClass(final Resource resource) {
162    return StringUtils.equals(CLASS, resource.getQualifier()) || isFile(resource);
163  }
164
165  /**
166   * @param resource not nullable
167   */
168  public static boolean isMethod(final Resource resource) {
169    return StringUtils.equals(METHOD, resource.getQualifier());
170  }
171
172  /**
173   * @param resource not nullable
174   */
175  public static boolean isParagraph(final Resource resource) {
176    return StringUtils.equals(PARAGRAPH, resource.getQualifier());
177  }
178}