Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/resources/ResourceUrl.java

raw
1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.android.ide.common.resources;
17
18 import static com.android.SdkConstants.ANDROID_NS_NAME;
19 import static com.android.SdkConstants.ATTR_REF_PREFIX;
20 import static com.android.SdkConstants.PREFIX_RESOURCE_REF;
21 import static com.android.SdkConstants.PREFIX_THEME_REF;
22 import static com.android.SdkConstants.RESOURCE_CLZ_ATTR;
23 import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_EMPTY;
24 import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_NULL;
25 import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_UNDEFINED;
26
27 import com.android.annotations.NonNull;
28 import com.android.annotations.Nullable;
29 import com.android.ide.common.rendering.api.ResourceValue;
30 import com.android.resources.ResourceType;
31
32 /**
33 * A {@linkplain ResourceUrl} represents a parsed resource url such as {@code @string/foo} or
34 * {@code ?android:attr/bar}
35 */
36 public class ResourceUrl {
37 /** Type of resource */
38 @NonNull public final ResourceType type;
39
40 /** Name of resource */
41 @NonNull public final String name;
42
43 /** The namespace, or null if it's in the project namespace */
44 @Nullable public final String namespace;
45
46 /** If true, the resource is in the android: framework */
47 public final boolean framework;
48
49 /** Whether an id resource is of the form {@code @+id} rather than just {@code @id} */
50 public final boolean create;
51
52 /** Whether this is a theme resource reference */
53 public final boolean theme;
54
55 private ResourceUrl(@NonNull ResourceType type, @NonNull String name,
56 @Nullable String namespace, boolean framework,
57 boolean create, boolean theme) {
58 this.type = type;
59 this.name = name;
60 this.framework = framework;
61 this.namespace = namespace;
62 this.create = create;
63 this.theme = theme;
64 }
65
66 /**
67 * Creates a new resource URL. Normally constructed via {@link #parse(String)}.
68 *
69 * @param type the resource type
70 * @param name the name
71 * @param framework whether it's a framework resource
72 * @param create if it's an id resource, whether it's of the form {@code @+id}
73 */
74 public static ResourceUrl create(@NonNull ResourceType type, @NonNull String name,
75 boolean framework, boolean create) {
76 return new ResourceUrl(type, name, framework ? ANDROID_NS_NAME : null, framework, create, false);
77 }
78
79 public static ResourceUrl create(@NonNull ResourceValue value) {
80 return create(value.getResourceType(), value.getName(), value.isFramework(), false);
81 }
82
83 /**
84 * Return the resource type of the given url, and the resource name
85 *
86 * @param url the resource url to be parsed
87 * @return a pair of the resource type and the resource name
88 */
89 @Nullable
90 public static ResourceUrl parse(@NonNull String url) {
91 return parse(url, false);
92 }
93
94 /**
95 * Return the resource type of the given url, and the resource name.
96 *
97 * @param url the resource url to be parsed
98 * @param forceFramework force the returned value to be a framework resource.
99 * @return a pair of the resource type and the resource name
100 */
101 @Nullable
102 public static ResourceUrl parse(@NonNull String url, boolean forceFramework) {
103 boolean isTheme = false;
104 // Handle theme references
105 if (url.startsWith(PREFIX_THEME_REF)) {
106 isTheme = true;
107 String remainder = url.substring(PREFIX_THEME_REF.length());
108 if (url.startsWith(ATTR_REF_PREFIX)) {
109 url = PREFIX_RESOURCE_REF + url.substring(PREFIX_THEME_REF.length());
110 } else {
111 int colon = url.indexOf(':');
112 if (colon != -1) {
113 // Convert from ?android:progressBarStyleBig to ?android:attr/progressBarStyleBig
114 if (remainder.indexOf('/', colon) == -1) {
115 remainder = remainder.substring(0, colon) + RESOURCE_CLZ_ATTR + '/'
116 + remainder.substring(colon);
117 }
118 url = PREFIX_RESOURCE_REF + remainder;
119 } else {
120 int slash = url.indexOf('/');
121 if (slash == -1) {
122 url = PREFIX_RESOURCE_REF + RESOURCE_CLZ_ATTR + '/' + remainder;
123 }
124 }
125 }
126 }
127
128 if (!url.startsWith(PREFIX_RESOURCE_REF) || isNullOrEmpty(url)) {
129 return null;
130 }
131
132 int typeEnd = url.indexOf('/', 1);
133 if (typeEnd == -1) {
134 return null;
135 }
136 int nameBegin = typeEnd + 1;
137
138 // Skip @ and @+
139 boolean create = url.startsWith("@+");
140 int typeBegin = create ? 2 : 1;
141
142 int colon = url.lastIndexOf(':', typeEnd);
143 boolean framework = forceFramework;
144 String namespace = forceFramework ? ANDROID_NS_NAME : null;
145 if (colon != -1) {
146 if (url.startsWith(ANDROID_NS_NAME, typeBegin)) {
147 framework = true;
148 namespace = ANDROID_NS_NAME;
149 } else {
150 namespace = url.substring(typeBegin, colon);
151 }
152 typeBegin = colon + 1;
153 }
154 String typeName = url.substring(typeBegin, typeEnd);
155 ResourceType type = ResourceType.getEnum(typeName);
156 if (type == null) {
157 return null;
158 }
159 String name = url.substring(nameBegin);
160 if (name.isEmpty()) {
161 return null;
162 }
163 return new ResourceUrl(type, name, namespace, framework, create, isTheme);
164 }
165
166 /** Returns if the resource url is @null, @empty or @undefined. */
167 public static boolean isNullOrEmpty(@NonNull String url) {
168 return url.equals(REFERENCE_NULL) || url.equals(REFERENCE_EMPTY) ||
169 url.equals(REFERENCE_UNDEFINED);
170 }
171
172 /**
173 * Checks whether this resource has a valid name. Used when parsing data that isn't
174 * necessarily known to be a valid resource; for example, "?attr/hello world"
175 */
176 public boolean hasValidName() {
177 // Make sure it looks like a resource name; if not, it could just be a string
178 // which starts with a ?, etc.
179 if (name.isEmpty()) {
180 return false;
181 }
182
183 if (!Character.isJavaIdentifierStart(name.charAt(0))) {
184 return false;
185 }
186 for (int i = 1, n = name.length(); i < n; i++) {
187 char c = name.charAt(i);
188 if (!Character.isJavaIdentifierPart(c) && c != '.') {
189 return false;
190 }
191 }
192
193 return true;
194 }
195
196 @Override
197 public String toString() {
198 StringBuilder sb = new StringBuilder();
199 sb.append(theme ? PREFIX_THEME_REF : PREFIX_RESOURCE_REF);
200 if (create) {
201 sb.append('+');
202 }
203 if (framework) {
204 sb.append(ANDROID_NS_NAME);
205 sb.append(':');
206 }
207 sb.append(type.getName());
208 sb.append('/');
209 sb.append(name);
210 return sb.toString();
211 }
212
213 @SuppressWarnings("RedundantIfStatement")
214 @Override
215 public boolean equals(Object o) {
216 if (this == o) {
217 return true;
218 }
219 if (o == null || getClass() != o.getClass()) {
220 return false;
221 }
222
223 ResourceUrl that = (ResourceUrl) o;
224
225 if (create != that.create) {
226 return false;
227 }
228 if (framework != that.framework) {
229 return false;
230 }
231 if (!name.equals(that.name)) {
232 return false;
233 }
234 if (type != that.type) {
235 return false;
236 }
237
238 return true;
239 }
240
241 @Override
242 public int hashCode() {
243 int result = type.hashCode();
244 result = 31 * result + name.hashCode();
245 result = 31 * result + (framework ? 1 : 0);
246 result = 31 * result + (create ? 1 : 0);
247 return result;
248 }
249 }