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 */
020 package org.sonar.api.resources;
021
022 import 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 */
030 public 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 */
050 public static final String LIBRARY = "LIB";
051
052 /**
053 * Single project or root of multi-modules projects
054 * Scope is Scopes.PROJECT
055 */
056 public static final String PROJECT = "TRK";
057
058 /**
059 * Module of a multi-modules project. It's sometimes called "sub-project".
060 * Scope of modules is Scopes.PROJECT
061 */
062 public static final String MODULE = "BRC";
063
064 /**
065 * @deprecated since 4.2 now packages are considered as regular directories. Use {@link #DIRECTORY} instead.
066 */
067 @Deprecated
068 public static final String PACKAGE = "PAC";
069 public static final String DIRECTORY = "DIR";
070 public static final String FILE = "FIL";
071 /**
072 * @deprecated since 4.2 now java files are considered as regular files. Use {@link #FILE} instead.
073 */
074 @Deprecated
075 public static final String CLASS = "CLA";
076 /**
077 * @deprecated since 4.2 resources under FILE level will no more be supported.
078 */
079 @Deprecated
080 public static final String PARAGRAPH = "PAR";
081 /**
082 * @deprecated since 4.2 resources under FILE level will no more be supported.
083 */
084 @Deprecated
085 public static final String METHOD = "MET";
086 /**
087 * @deprecated since 4.2 resources under FILE level will no more be supported.
088 */
089 @Deprecated
090 public static final String FIELD = "FLD";
091
092 // ugly, should be replaced by "natures"
093 public static final String UNIT_TEST_FILE = "UTS";
094
095 /**
096 * @param resource not nullable
097 */
098 public static boolean isView(final Resource resource, final boolean acceptSubViews) {
099 boolean isView = StringUtils.equals(VIEW, resource.getQualifier());
100 if (!isView && acceptSubViews) {
101 isView = StringUtils.equals(SUBVIEW, resource.getQualifier());
102 }
103
104 return isView;
105 }
106
107 /**
108 * @param resource not nullable
109 */
110 public static boolean isSubview(final Resource resource) {
111 return StringUtils.equals(SUBVIEW, resource.getScope());
112 }
113
114 /**
115 * @param resource not nullable
116 */
117 public static boolean isProject(final Resource resource, final boolean acceptModules) {
118 boolean isProject = StringUtils.equals(PROJECT, resource.getQualifier());
119 if (!isProject && acceptModules) {
120 isProject = StringUtils.equals(MODULE, resource.getQualifier());
121 }
122 return isProject;
123 }
124
125 /**
126 * @param resource not nullable
127 */
128 public static boolean isModule(final Resource resource) {
129 return StringUtils.equals(MODULE, resource.getQualifier());
130 }
131
132 /**
133 * @param resource not nullable
134 */
135 public static boolean isDirectory(final Resource resource) {
136 return StringUtils.equals(DIRECTORY, resource.getQualifier());
137 }
138
139 /**
140 * @deprecated since 4.2 Package is now a directory. Use {@link #isDirectory(Resource)}
141 */
142 @Deprecated
143 public static boolean isPackage(final Resource resource) {
144 return StringUtils.equals(PACKAGE, resource.getQualifier()) || isDirectory(resource);
145 }
146
147 /**
148 * @param resource not nullable
149 */
150 public static boolean isFile(final Resource resource) {
151 return StringUtils.equals(FILE, resource.getQualifier());
152 }
153
154 /**
155 * @param resource not nullable
156 * @deprecated since 4.2 CLA qualifier is deprecated
157 */
158 @Deprecated
159 public static boolean isClass(final Resource resource) {
160 return StringUtils.equals(CLASS, resource.getQualifier()) || isFile(resource);
161 }
162
163 /**
164 * @param resource not nullable
165 */
166 public static boolean isMethod(final Resource resource) {
167 return StringUtils.equals(METHOD, resource.getQualifier());
168 }
169
170 /**
171 * @param resource not nullable
172 */
173 public static boolean isParagraph(final Resource resource) {
174 return StringUtils.equals(PARAGRAPH, resource.getQualifier());
175 }
176 }