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 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  /**
033   * Root views. Scope of views is Scopes.PROJECT
034   */
035  public static final String VIEW = "VW";
036
037  /**
038   * Sub-views, defined in root views. Scope of sub-views is Scopes.PROJECT
039   */
040  public static final String SUBVIEW = "SVW";
041
042  /**
043   * Library, for example a JAR dependency of Java projects.
044   * Scope of libraries is Scopes.PROJECT
045   * @deprecated since 5.2 No more design features
046   */
047  @Deprecated
048  public static final String LIBRARY = "LIB";
049
050  /**
051   * Single project or root of multi-modules projects
052   * Scope is Scopes.PROJECT
053   */
054  public static final String PROJECT = "TRK";
055
056  /**
057   * Module of a multi-modules project. It's sometimes called "sub-project".
058   * Scope of modules is Scopes.PROJECT
059   */
060  public static final String MODULE = "BRC";
061
062  public static final String DIRECTORY = "DIR";
063  public static final String FILE = "FIL";
064
065  // ugly, should be replaced by "natures"
066  public static final String UNIT_TEST_FILE = "UTS";
067
068  private Qualifiers() {
069    // only static methods
070  }
071
072  /**
073   * @param resource not nullable
074   */
075  public static boolean isView(final Resource resource, final boolean acceptSubViews) {
076    boolean isView = StringUtils.equals(VIEW, resource.getQualifier());
077    if (!isView && acceptSubViews) {
078      isView = StringUtils.equals(SUBVIEW, resource.getQualifier());
079    }
080
081    return isView;
082  }
083
084  /**
085   * @param resource not nullable
086   */
087  public static boolean isSubview(final Resource resource) {
088    return StringUtils.equals(SUBVIEW, resource.getScope());
089  }
090
091  /**
092   * @param resource not nullable
093   */
094  public static boolean isProject(final Resource resource, final boolean acceptModules) {
095    boolean isProject = StringUtils.equals(PROJECT, resource.getQualifier());
096    if (!isProject && acceptModules) {
097      isProject = StringUtils.equals(MODULE, resource.getQualifier());
098    }
099    return isProject;
100  }
101
102  /**
103   * @param resource not nullable
104   */
105  public static boolean isModule(final Resource resource) {
106    return StringUtils.equals(MODULE, resource.getQualifier());
107  }
108
109  /**
110   * @param resource not nullable
111   */
112  public static boolean isDirectory(final Resource resource) {
113    return StringUtils.equals(DIRECTORY, resource.getQualifier());
114  }
115
116  /**
117   * @param resource not nullable
118   */
119  public static boolean isFile(final Resource resource) {
120    return StringUtils.equals(FILE, resource.getQualifier());
121  }
122}