blob: 2bf38e417cba1ee11c9a6b27542ffa4af894c13b [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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
17#include "Resource.h"
18#include "ResourceTypeExtensions.h"
19#include "ResourceValues.h"
20#include "Util.h"
21
22#include <androidfw/ResourceTypes.h>
23#include <limits>
24
25namespace aapt {
26
27bool Value::isItem() const {
28 return false;
29}
30
31bool Value::isWeak() const {
32 return false;
33}
34
35bool Item::isItem() const {
36 return true;
37}
38
39RawString::RawString(const StringPool::Ref& ref) : value(ref) {
40}
41
Adam Lesinski769de982015-04-10 19:43:55 -070042RawString* RawString::clone(StringPool* newPool) const {
43 return new RawString(newPool->makeRef(*value));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044}
45
46bool RawString::flatten(android::Res_value& outValue) const {
47 outValue.dataType = ExtendedTypes::TYPE_RAW_STRING;
48 outValue.data = static_cast<uint32_t>(value.getIndex());
49 return true;
50}
51
52void RawString::print(std::ostream& out) const {
53 out << "(raw string) " << *value;
54}
55
56Reference::Reference() : referenceType(Reference::Type::kResource) {
57}
58
59Reference::Reference(const ResourceNameRef& n, Type t) :
60 name(n.toResourceName()), referenceType(t) {
61}
62
63Reference::Reference(const ResourceId& i, Type type) : id(i), referenceType(type) {
64}
65
66bool Reference::flatten(android::Res_value& outValue) const {
67 outValue.dataType = (referenceType == Reference::Type::kResource)
68 ? android::Res_value::TYPE_REFERENCE
69 : android::Res_value::TYPE_ATTRIBUTE;
70 outValue.data = id.id;
71 return true;
72}
73
Adam Lesinski769de982015-04-10 19:43:55 -070074Reference* Reference::clone(StringPool* /*newPool*/) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075 Reference* ref = new Reference();
76 ref->referenceType = referenceType;
77 ref->name = name;
78 ref->id = id;
79 return ref;
80}
81
82void Reference::print(std::ostream& out) const {
83 out << "(reference) ";
84 if (referenceType == Reference::Type::kResource) {
85 out << "@";
86 } else {
87 out << "?";
88 }
89
90 if (name.isValid()) {
91 out << name;
92 }
93
94 if (id.isValid() || Res_INTERNALID(id.id)) {
95 out << " " << id;
96 }
97}
98
99bool Id::isWeak() const {
100 return true;
101}
102
103bool Id::flatten(android::Res_value& out) const {
104 out.dataType = android::Res_value::TYPE_NULL;
105 out.data = android::Res_value::DATA_NULL_UNDEFINED;
106 return true;
107}
108
Adam Lesinski769de982015-04-10 19:43:55 -0700109Id* Id::clone(StringPool* /*newPool*/) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110 return new Id();
111}
112
113void Id::print(std::ostream& out) const {
114 out << "(id)";
115}
116
117String::String(const StringPool::Ref& ref) : value(ref) {
118}
119
120bool String::flatten(android::Res_value& outValue) const {
121 // Verify that our StringPool index is within encodeable limits.
122 if (value.getIndex() > std::numeric_limits<uint32_t>::max()) {
123 return false;
124 }
125
126 outValue.dataType = android::Res_value::TYPE_STRING;
127 outValue.data = static_cast<uint32_t>(value.getIndex());
128 return true;
129}
130
Adam Lesinski769de982015-04-10 19:43:55 -0700131String* String::clone(StringPool* newPool) const {
132 return new String(newPool->makeRef(*value));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133}
134
135void String::print(std::ostream& out) const {
136 out << "(string) \"" << *value << "\"";
137}
138
139StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
140}
141
142bool StyledString::flatten(android::Res_value& outValue) const {
143 if (value.getIndex() > std::numeric_limits<uint32_t>::max()) {
144 return false;
145 }
146
147 outValue.dataType = android::Res_value::TYPE_STRING;
148 outValue.data = static_cast<uint32_t>(value.getIndex());
149 return true;
150}
151
Adam Lesinski769de982015-04-10 19:43:55 -0700152StyledString* StyledString::clone(StringPool* newPool) const {
153 return new StyledString(newPool->makeRef(value));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154}
155
156void StyledString::print(std::ostream& out) const {
157 out << "(styled string) \"" << *value->str << "\"";
158}
159
160FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
161}
162
163bool FileReference::flatten(android::Res_value& outValue) const {
164 if (path.getIndex() > std::numeric_limits<uint32_t>::max()) {
165 return false;
166 }
167
168 outValue.dataType = android::Res_value::TYPE_STRING;
169 outValue.data = static_cast<uint32_t>(path.getIndex());
170 return true;
171}
172
Adam Lesinski769de982015-04-10 19:43:55 -0700173FileReference* FileReference::clone(StringPool* newPool) const {
174 return new FileReference(newPool->makeRef(*path));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175}
176
177void FileReference::print(std::ostream& out) const {
178 out << "(file) " << *path;
179}
180
181BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {
182}
183
184bool BinaryPrimitive::flatten(android::Res_value& outValue) const {
185 outValue = value;
186 return true;
187}
188
Adam Lesinski769de982015-04-10 19:43:55 -0700189BinaryPrimitive* BinaryPrimitive::clone(StringPool* /*newPool*/) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190 return new BinaryPrimitive(value);
191}
192
193void BinaryPrimitive::print(std::ostream& out) const {
194 switch (value.dataType) {
195 case android::Res_value::TYPE_NULL:
196 out << "(null)";
197 break;
198 case android::Res_value::TYPE_INT_DEC:
199 out << "(integer) " << value.data;
200 break;
201 case android::Res_value::TYPE_INT_HEX:
202 out << "(integer) " << std::hex << value.data << std::dec;
203 break;
204 case android::Res_value::TYPE_INT_BOOLEAN:
205 out << "(boolean) " << (value.data != 0 ? "true" : "false");
206 break;
207 case android::Res_value::TYPE_INT_COLOR_ARGB8:
208 case android::Res_value::TYPE_INT_COLOR_RGB8:
209 case android::Res_value::TYPE_INT_COLOR_ARGB4:
210 case android::Res_value::TYPE_INT_COLOR_RGB4:
211 out << "(color) #" << std::hex << value.data << std::dec;
212 break;
213 default:
214 out << "(unknown 0x" << std::hex << (int) value.dataType << ") 0x"
215 << std::hex << value.data << std::dec;
216 break;
217 }
218}
219
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220Attribute::Attribute(bool w, uint32_t t) : weak(w), typeMask(t) {
221}
222
223bool Attribute::isWeak() const {
224 return weak;
225}
226
Adam Lesinski769de982015-04-10 19:43:55 -0700227Attribute* Attribute::clone(StringPool* /*newPool*/) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228 Attribute* attr = new Attribute(weak);
229 attr->typeMask = typeMask;
230 std::copy(symbols.begin(), symbols.end(), std::back_inserter(attr->symbols));
231 return attr;
232}
233
234void Attribute::print(std::ostream& out) const {
235 out << "(attr)";
236 if (typeMask == android::ResTable_map::TYPE_ANY) {
237 out << " any";
238 return;
239 }
240
241 bool set = false;
242 if ((typeMask & android::ResTable_map::TYPE_REFERENCE) != 0) {
243 if (!set) {
244 out << " ";
245 set = true;
246 } else {
247 out << "|";
248 }
249 out << "reference";
250 }
251
252 if ((typeMask & android::ResTable_map::TYPE_STRING) != 0) {
253 if (!set) {
254 out << " ";
255 set = true;
256 } else {
257 out << "|";
258 }
259 out << "string";
260 }
261
262 if ((typeMask & android::ResTable_map::TYPE_INTEGER) != 0) {
263 if (!set) {
264 out << " ";
265 set = true;
266 } else {
267 out << "|";
268 }
269 out << "integer";
270 }
271
272 if ((typeMask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
273 if (!set) {
274 out << " ";
275 set = true;
276 } else {
277 out << "|";
278 }
279 out << "boolean";
280 }
281
282 if ((typeMask & android::ResTable_map::TYPE_COLOR) != 0) {
283 if (!set) {
284 out << " ";
285 set = true;
286 } else {
287 out << "|";
288 }
289 out << "color";
290 }
291
292 if ((typeMask & android::ResTable_map::TYPE_FLOAT) != 0) {
293 if (!set) {
294 out << " ";
295 set = true;
296 } else {
297 out << "|";
298 }
299 out << "float";
300 }
301
302 if ((typeMask & android::ResTable_map::TYPE_DIMENSION) != 0) {
303 if (!set) {
304 out << " ";
305 set = true;
306 } else {
307 out << "|";
308 }
309 out << "dimension";
310 }
311
312 if ((typeMask & android::ResTable_map::TYPE_FRACTION) != 0) {
313 if (!set) {
314 out << " ";
315 set = true;
316 } else {
317 out << "|";
318 }
319 out << "fraction";
320 }
321
322 if ((typeMask & android::ResTable_map::TYPE_ENUM) != 0) {
323 if (!set) {
324 out << " ";
325 set = true;
326 } else {
327 out << "|";
328 }
329 out << "enum";
330 }
331
332 if ((typeMask & android::ResTable_map::TYPE_FLAGS) != 0) {
333 if (!set) {
334 out << " ";
335 set = true;
336 } else {
337 out << "|";
338 }
339 out << "flags";
340 }
341
342 out << " ["
343 << util::joiner(symbols.begin(), symbols.end(), ", ")
344 << "]";
345
346 if (weak) {
347 out << " [weak]";
348 }
349}
350
351static ::std::ostream& operator<<(::std::ostream& out, const Attribute::Symbol& s) {
352 return out << s.symbol.name.entry << "=" << s.value;
353}
354
Adam Lesinski769de982015-04-10 19:43:55 -0700355Style::Style(bool weak) : weak(weak) {
356}
357
358bool Style::isWeak() const {
359 return weak;
360}
361
362Style* Style::clone(StringPool* newPool) const {
363 Style* style = new Style(weak);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364 style->parent = parent;
365 for (auto& entry : entries) {
366 style->entries.push_back(Entry{
367 entry.key,
Adam Lesinski769de982015-04-10 19:43:55 -0700368 std::unique_ptr<Item>(entry.value->clone(newPool))
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800369 });
370 }
371 return style;
372}
373
374void Style::print(std::ostream& out) const {
375 out << "(style) ";
376 if (!parent.name.entry.empty()) {
377 out << parent.name;
378 }
379 out << " ["
380 << util::joiner(entries.begin(), entries.end(), ", ")
381 << "]";
382}
383
384static ::std::ostream& operator<<(::std::ostream& out, const Style::Entry& value) {
385 out << value.key.name << " = ";
386 value.value->print(out);
387 return out;
388}
389
Adam Lesinski769de982015-04-10 19:43:55 -0700390Array* Array::clone(StringPool* newPool) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391 Array* array = new Array();
392 for (auto& item : items) {
Adam Lesinski769de982015-04-10 19:43:55 -0700393 array->items.emplace_back(std::unique_ptr<Item>(item->clone(newPool)));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394 }
395 return array;
396}
397
398void Array::print(std::ostream& out) const {
399 out << "(array) ["
400 << util::joiner(items.begin(), items.end(), ", ")
401 << "]";
402}
403
Adam Lesinski769de982015-04-10 19:43:55 -0700404Plural* Plural::clone(StringPool* newPool) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800405 Plural* p = new Plural();
406 const size_t count = values.size();
407 for (size_t i = 0; i < count; i++) {
408 if (values[i]) {
Adam Lesinski769de982015-04-10 19:43:55 -0700409 p->values[i] = std::unique_ptr<Item>(values[i]->clone(newPool));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410 }
411 }
412 return p;
413}
414
415void Plural::print(std::ostream& out) const {
416 out << "(plural)";
417}
418
419static ::std::ostream& operator<<(::std::ostream& out, const std::unique_ptr<Item>& item) {
420 return out << *item;
421}
422
Adam Lesinski769de982015-04-10 19:43:55 -0700423Styleable* Styleable::clone(StringPool* /*newPool*/) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424 Styleable* styleable = new Styleable();
425 std::copy(entries.begin(), entries.end(), std::back_inserter(styleable->entries));
426 return styleable;
427}
428
429void Styleable::print(std::ostream& out) const {
430 out << "(styleable) " << " ["
431 << util::joiner(entries.begin(), entries.end(), ", ")
432 << "]";
433}
434
435} // namespace aapt