blob: e1f9642d27d7278d98664eb52521237429c328a9 [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
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "ResourceTable.h"
19#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "ValueVisitor.h"
Adam Lesinski7751afc2016-01-06 15:45:28 -080022#include "util/Comparators.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080023#include "util/ImmutableMap.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070024#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070026
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080027#include <functional>
Adam Lesinski769de982015-04-10 19:43:55 -070028#include <sstream>
29
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033
Adam Lesinski27afb9e2015-11-06 18:25:04 -080034/**
35 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
36 */
37static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) {
38 return ns.empty() && (name == u"skip" || name == u"eat-comment");
39}
40
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080041static uint32_t parseFormatType(const StringPiece16& piece) {
42 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
43 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
44 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
45 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
46 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
47 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
48 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
49 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
50 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
51 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
52 return 0;
53}
54
55static uint32_t parseFormatAttribute(const StringPiece16& str) {
56 uint32_t mask = 0;
57 for (StringPiece16 part : util::tokenize(str, u'|')) {
58 StringPiece16 trimmedPart = util::trimWhitespace(part);
59 uint32_t type = parseFormatType(trimmedPart);
60 if (type == 0) {
61 return 0;
62 }
63 mask |= type;
64 }
65 return mask;
66}
67
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068/**
69 * A parsed resource ready to be added to the ResourceTable.
70 */
71struct ParsedResource {
72 ResourceName name;
73 Source source;
74 ResourceId id;
75 Maybe<SymbolState> symbolState;
76 std::u16string comment;
77 std::unique_ptr<Value> value;
78 std::list<ParsedResource> childResources;
79};
80
Adam Lesinski7751afc2016-01-06 15:45:28 -080081bool ResourceParser::shouldStripResource(const ResourceNameRef& name,
82 const Maybe<std::u16string>& product) const {
83 if (product) {
84 for (const std::u16string& productToMatch : mOptions.products) {
85 if (product.value() == productToMatch) {
86 // We specified a product, and it is in the list, so don't strip.
87 return false;
88 }
89 }
90 }
91
92 // Nothing matched, try 'default'. Default only matches if we didn't already use another
93 // product variant.
94 if (!product || product.value() == u"default") {
95 if (Maybe<ResourceTable::SearchResult> result = mTable->findResource(name)) {
96 const ResourceEntry* entry = result.value().entry;
97 auto iter = std::lower_bound(entry->values.begin(), entry->values.end(), mConfig,
98 cmp::lessThanConfig);
99 if (iter != entry->values.end() && iter->config == mConfig && !iter->value->isWeak()) {
100 // We have a value for this config already, and it is not weak,
101 // so filter out this default.
102 return true;
103 }
104 }
105 return false;
106 }
107 return true;
108}
109
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800110// Recursively adds resources to the ResourceTable.
111static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
112 IDiagnostics* diag, ParsedResource* res) {
113 if (res->symbolState) {
114 Symbol symbol;
115 symbol.state = res->symbolState.value();
116 symbol.source = res->source;
117 symbol.comment = res->comment;
118 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
119 return false;
120 }
121 }
122
123 if (res->value) {
124 // Attach the comment, source and config to the value.
125 res->value->setComment(std::move(res->comment));
126 res->value->setSource(std::move(res->source));
127
128 if (!table->addResource(res->name, res->id, config, std::move(res->value), diag)) {
129 return false;
130 }
131 }
132
133 bool error = false;
134 for (ParsedResource& child : res->childResources) {
135 error |= !addResourcesToTable(table, config, diag, &child);
136 }
137 return !error;
138}
139
140// Convenient aliases for more readable function calls.
141enum {
142 kAllowRawString = true,
143 kNoRawString = false
144};
145
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700147 const ConfigDescription& config,
148 const ResourceParserOptions& options) :
149 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150}
151
152/**
153 * Build a string from XML that converts nested elements into Span objects.
154 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800155bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156 StyleString* outStyleString) {
157 std::vector<Span> spanStack;
158
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800159 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160 outRawString->clear();
161 outStyleString->spans.clear();
162 util::StringBuilder builder;
163 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -0800164 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
165 const xml::XmlPullParser::Event event = parser->getEvent();
166 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167 if (!parser->getElementNamespace().empty()) {
168 // We already warned and skipped the start element, so just skip here too
169 continue;
170 }
171
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172 depth--;
173 if (depth == 0) {
174 break;
175 }
176
177 spanStack.back().lastChar = builder.str().size();
178 outStyleString->spans.push_back(spanStack.back());
179 spanStack.pop_back();
180
Adam Lesinski467f1712015-11-16 17:35:44 -0800181 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182 outRawString->append(parser->getText());
183 builder.append(parser->getText());
184
Adam Lesinski467f1712015-11-16 17:35:44 -0800185 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 if (!parser->getElementNamespace().empty()) {
187 if (parser->getElementNamespace() != sXliffNamespaceUri) {
188 // Only warn if this isn't an xliff namespace.
189 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
190 << "skipping element '"
191 << parser->getElementName()
192 << "' with unknown namespace '"
193 << parser->getElementNamespace()
194 << "'");
195 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196 continue;
197 }
198 depth++;
199
200 // Build a span object out of the nested element.
201 std::u16string spanName = parser->getElementName();
202 const auto endAttrIter = parser->endAttributes();
203 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
204 spanName += u";";
205 spanName += attrIter->name;
206 spanName += u"=";
207 spanName += attrIter->value;
208 }
209
210 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
212 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800213 error = true;
214 } else {
215 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217
Adam Lesinski467f1712015-11-16 17:35:44 -0800218 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219 // Skip
220 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222 }
223 }
224 assert(spanStack.empty() && "spans haven't been fully processed");
225
226 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800227 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228}
229
Adam Lesinski467f1712015-11-16 17:35:44 -0800230bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231 bool error = false;
232 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800233 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
234 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 continue;
237 }
238
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
240 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
241 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242 return false;
243 }
244
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 error |= !parseResources(parser);
246 break;
247 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinski467f1712015-11-16 17:35:44 -0800249 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
251 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252 return false;
253 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255}
256
Adam Lesinski467f1712015-11-16 17:35:44 -0800257bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700258 std::set<ResourceName> strippedResources;
259
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800263 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
264 const xml::XmlPullParser::Event event = parser->getEvent();
265 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266 comment = parser->getComment();
267 continue;
268 }
269
Adam Lesinski467f1712015-11-16 17:35:44 -0800270 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
273 << "plain text not allowed here");
274 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 }
276 continue;
277 }
278
Adam Lesinski467f1712015-11-16 17:35:44 -0800279 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282 // Skip unknown namespace.
283 continue;
284 }
285
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 std::u16string elementName = parser->getElementName();
287 if (elementName == u"skip" || elementName == u"eat-comment") {
288 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289 continue;
290 }
291
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700292 ParsedResource parsedResource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700293 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700294 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinski7751afc2016-01-06 15:45:28 -0800296 // Extract the product name if it exists.
297 Maybe<std::u16string> product;
298 if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) {
299 product = maybeProduct.value().toString();
300 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800301
Adam Lesinski7751afc2016-01-06 15:45:28 -0800302 // Parse the resource regardless of product.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800303 if (!parseResource(parser, &parsedResource)) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800304 error = true;
305 continue;
306 }
307
Adam Lesinski7751afc2016-01-06 15:45:28 -0800308 // We successfully parsed the resource. Check if we should include it or strip it.
309 if (shouldStripResource(parsedResource.name, product)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800310 // Record that we stripped out this resource name.
311 // We will check that at least one variant of this resource was included.
312 strippedResources.insert(parsedResource.name);
313 } else if (!addResourcesToTable(mTable, mConfig, mDiag, &parsedResource)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700314 error = true;
315 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700317
318 // Check that we included at least one variant of each stripped resource.
319 for (const ResourceName& strippedResource : strippedResources) {
320 if (!mTable->findResource(strippedResource)) {
321 // Failed to find the resource.
322 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
323 "was filtered out but no product variant remains");
324 error = true;
325 }
326 }
327
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700328 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329}
330
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800331
332bool ResourceParser::parseResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
333 struct ItemTypeFormat {
334 ResourceType type;
335 uint32_t format;
336 };
337
338 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, ParsedResource*)>;
339
340 static const auto elToItemMap = ImmutableMap<std::u16string, ItemTypeFormat>::createPreSorted({
341 { u"bool", { ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN } },
342 { u"color", { ResourceType::kColor, android::ResTable_map::TYPE_COLOR } },
343 { u"dimen", { ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT
344 | android::ResTable_map::TYPE_FRACTION
345 | android::ResTable_map::TYPE_DIMENSION } },
346 { u"drawable", { ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR } },
347 { u"fraction", { ResourceType::kFraction, android::ResTable_map::TYPE_FLOAT
348 | android::ResTable_map::TYPE_FRACTION
349 | android::ResTable_map::TYPE_DIMENSION } },
350 { u"integer", { ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER } },
351 { u"string", { ResourceType::kString, android::ResTable_map::TYPE_STRING } },
352 });
353
354 static const auto elToBagMap = ImmutableMap<std::u16string, BagParseFunc>::createPreSorted({
355 { u"add-resource", std::mem_fn(&ResourceParser::parseAddResource) },
356 { u"array", std::mem_fn(&ResourceParser::parseArray) },
357 { u"attr", std::mem_fn(&ResourceParser::parseAttr) },
358 { u"declare-styleable", std::mem_fn(&ResourceParser::parseDeclareStyleable) },
359 { u"integer-array", std::mem_fn(&ResourceParser::parseIntegerArray) },
360 { u"java-symbol", std::mem_fn(&ResourceParser::parseSymbol) },
361 { u"plurals", std::mem_fn(&ResourceParser::parsePlural) },
362 { u"public", std::mem_fn(&ResourceParser::parsePublic) },
363 { u"public-group", std::mem_fn(&ResourceParser::parsePublicGroup) },
364 { u"string-array", std::mem_fn(&ResourceParser::parseStringArray) },
365 { u"style", std::mem_fn(&ResourceParser::parseStyle) },
366 { u"symbol", std::mem_fn(&ResourceParser::parseSymbol) },
367 });
368
369 std::u16string resourceType = parser->getElementName();
370
371 // The value format accepted for this resource.
372 uint32_t resourceFormat = 0u;
373
374 if (resourceType == u"item") {
375 // Items have their type encoded in the type attribute.
376 if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) {
377 resourceType = maybeType.value().toString();
378 } else {
379 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
380 << "<item> must have a 'type' attribute");
381 return false;
382 }
383
384 if (Maybe<StringPiece16> maybeFormat = xml::findNonEmptyAttribute(parser, u"format")) {
385 // An explicit format for this resource was specified. The resource will retain
386 // its type in its name, but the accepted value for this type is overridden.
387 resourceFormat = parseFormatType(maybeFormat.value());
388 if (!resourceFormat) {
389 mDiag->error(DiagMessage(outResource->source)
390 << "'" << maybeFormat.value() << "' is an invalid format");
391 return false;
392 }
393 }
394 }
395
396 // Get the name of the resource. This will be checked later, because not all
397 // XML elements require a name.
398 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
399
400 if (resourceType == u"id") {
401 if (!maybeName) {
402 mDiag->error(DiagMessage(outResource->source)
403 << "<" << parser->getElementName() << "> missing 'name' attribute");
404 return false;
405 }
406
407 outResource->name.type = ResourceType::kId;
408 outResource->name.entry = maybeName.value().toString();
409 outResource->value = util::make_unique<Id>();
410 return true;
411 }
412
413 const auto itemIter = elToItemMap.find(resourceType);
414 if (itemIter != elToItemMap.end()) {
415 // This is an item, record its type and format and start parsing.
416
417 if (!maybeName) {
418 mDiag->error(DiagMessage(outResource->source)
419 << "<" << parser->getElementName() << "> missing 'name' attribute");
420 return false;
421 }
422
423 outResource->name.type = itemIter->second.type;
424 outResource->name.entry = maybeName.value().toString();
425
426 // Only use the implicit format for this type if it wasn't overridden.
427 if (!resourceFormat) {
428 resourceFormat = itemIter->second.format;
429 }
430
431 if (!parseItem(parser, outResource, resourceFormat)) {
432 return false;
433 }
434 return true;
435 }
436
437 // This might be a bag or something.
438 const auto bagIter = elToBagMap.find(resourceType);
439 if (bagIter != elToBagMap.end()) {
440 // Ensure we have a name (unless this is a <public-group>).
441 if (resourceType != u"public-group") {
442 if (!maybeName) {
443 mDiag->error(DiagMessage(outResource->source)
444 << "<" << parser->getElementName() << "> missing 'name' attribute");
445 return false;
446 }
447
448 outResource->name.entry = maybeName.value().toString();
449 }
450
451 // Call the associated parse method. The type will be filled in by the
452 // parse func.
453 if (!bagIter->second(this, parser, outResource)) {
454 return false;
455 }
456 return true;
457 }
458
459 // Try parsing the elementName (or type) as a resource. These shall only be
460 // resources like 'layout' or 'xml' and they can only be references.
461 const ResourceType* parsedType = parseResourceType(resourceType);
462 if (parsedType) {
463 if (!maybeName) {
464 mDiag->error(DiagMessage(outResource->source)
465 << "<" << parser->getElementName() << "> missing 'name' attribute");
466 return false;
467 }
468
469 outResource->name.type = *parsedType;
470 outResource->name.entry = maybeName.value().toString();
471 outResource->value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
472 if (!outResource->value) {
473 mDiag->error(DiagMessage(outResource->source)
474 << "invalid value for type '" << *parsedType << "'. Expected a reference");
475 return false;
476 }
477 return true;
478 }
479
480 mDiag->warn(DiagMessage(outResource->source)
481 << "unknown resource type '" << parser->getElementName() << "'");
482 return false;
483}
484
485bool ResourceParser::parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
486 const uint32_t format) {
487 if (format == android::ResTable_map::TYPE_STRING) {
488 return parseString(parser, outResource);
489 }
490
491 outResource->value = parseXml(parser, format, kNoRawString);
492 if (!outResource->value) {
493 mDiag->error(DiagMessage(outResource->source) << "invalid " << outResource->name.type);
494 return false;
495 }
496 return true;
497}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498
499/**
500 * Reads the entire XML subtree and attempts to parse it as some Item,
501 * with typeMask denoting which items it can be. If allowRawValue is
502 * true, a RawString is returned if the XML couldn't be parsed as
503 * an Item. If allowRawValue is false, nullptr is returned in this
504 * case.
505 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800506std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700507 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800508 const size_t beginXmlLine = parser->getLineNumber();
509
510 std::u16string rawValue;
511 StyleString styleString;
512 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
513 return {};
514 }
515
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800516 if (!styleString.spans.empty()) {
517 // This can only be a StyledString.
518 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520 }
521
522 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700523 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700524 std::unique_ptr<Id> id = util::make_unique<Id>();
525 id->setSource(mSource.withLine(beginXmlLine));
526 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800527 };
528
529 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700530 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
531 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700533 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700534 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800535 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700536 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800537 return processedItem;
538 }
539
540 // Try making a regular string.
541 if (typeMask & android::ResTable_map::TYPE_STRING) {
542 // Use the trimmed, escaped string.
543 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700544 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800545 }
546
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800547 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700548 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800551 }
552 return {};
553}
554
Adam Lesinski467f1712015-11-16 17:35:44 -0800555bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800556 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800557 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800558 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800559 mDiag->error(DiagMessage(outResource->source)
560 << "invalid value for 'formatted'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800561 return false;
562 }
563 }
564
Adam Lesinski9f222042015-11-04 13:51:45 -0800565 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800566 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800567 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800568 mDiag->error(DiagMessage(outResource->source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800569 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800570 return false;
571 }
572 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800573
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700574 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
575 if (!outResource->value) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800576 mDiag->error(DiagMessage(outResource->source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800577 return false;
578 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800579
Adam Lesinski393b5f02015-12-17 13:03:11 -0800580 if (String* stringValue = valueCast<String>(outResource->value.get())) {
581 stringValue->setTranslateable(translateable);
582
583 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800584 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800585 mDiag->error(DiagMessage(outResource->source)
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800586 << "multiple substitutions specified in non-positional format; "
587 "did you mean to add the formatted=\"false\" attribute?");
588 return false;
589 }
590 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800591
592 } else if (StyledString* stringValue = valueCast<StyledString>(outResource->value.get())) {
593 stringValue->setTranslateable(translateable);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800594 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700595 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800596}
597
Adam Lesinski467f1712015-11-16 17:35:44 -0800598bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800599 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800601 mDiag->error(DiagMessage(outResource->source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602 return false;
603 }
604
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800607 mDiag->error(DiagMessage(outResource->source)
608 << "invalid resource type '" << maybeType.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800609 return false;
610 }
611
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700612 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800613
Adam Lesinski467f1712015-11-16 17:35:44 -0800614 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700616 bool result = android::ResTable::stringToInt(maybeId.value().data(),
617 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700618 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800619 if (!result || !resourceId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800620 mDiag->error(DiagMessage(outResource->source)
621 << "invalid resource ID '" << maybeId.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800622 return false;
623 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700624 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800625 }
626
627 if (*parsedType == ResourceType::kId) {
628 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700629 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800630 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700631
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700632 outResource->symbolState = SymbolState::kPublic;
633 return true;
634}
635
Adam Lesinski467f1712015-11-16 17:35:44 -0800636bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800637 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800638 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800639 mDiag->error(DiagMessage(outResource->source)
640 << "<public-group> must have a 'type' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800641 return false;
642 }
643
644 const ResourceType* parsedType = parseResourceType(maybeType.value());
645 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800646 mDiag->error(DiagMessage(outResource->source)
647 << "invalid resource type '" << maybeType.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800648 return false;
649 }
650
Adam Lesinski467f1712015-11-16 17:35:44 -0800651 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800652 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800653 mDiag->error(DiagMessage(outResource->source)
654 << "<public-group> must have a 'first-id' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800655 return false;
656 }
657
658 android::Res_value val;
659 bool result = android::ResTable::stringToInt(maybeId.value().data(),
660 maybeId.value().size(), &val);
661 ResourceId nextId(val.data);
662 if (!result || !nextId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800663 mDiag->error(DiagMessage(outResource->source)
664 << "invalid resource ID '" << maybeId.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800665 return false;
666 }
667
668 std::u16string comment;
669 bool error = false;
670 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800671 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
672 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800673 comment = util::trimWhitespace(parser->getComment()).toString();
674 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800675 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800676 // Skip text.
677 continue;
678 }
679
680 const Source itemSource = mSource.withLine(parser->getLineNumber());
681 const std::u16string& elementNamespace = parser->getElementNamespace();
682 const std::u16string& elementName = parser->getElementName();
683 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800684 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800685 if (!maybeName) {
686 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
687 error = true;
688 continue;
689 }
690
Adam Lesinski467f1712015-11-16 17:35:44 -0800691 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800692 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
693 error = true;
694 continue;
695 }
696
Adam Lesinski467f1712015-11-16 17:35:44 -0800697 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800698 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
699 error = true;
700 continue;
701 }
702
703 ParsedResource childResource;
704 childResource.name.type = *parsedType;
705 childResource.name.entry = maybeName.value().toString();
706 childResource.id = nextId;
707 childResource.comment = std::move(comment);
708 childResource.source = itemSource;
709 childResource.symbolState = SymbolState::kPublic;
710 outResource->childResources.push_back(std::move(childResource));
711
712 nextId.id += 1;
713
714 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
715 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
716 error = true;
717 }
718 }
719 return !error;
720}
721
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800722bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800723 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700724 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800725 mDiag->error(DiagMessage(outResource->source)
726 << "<" << parser->getElementName() << "> must have a 'type' attribute");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700727 return false;
728 }
729
730 const ResourceType* parsedType = parseResourceType(maybeType.value());
731 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800732 mDiag->error(DiagMessage(outResource->source)
733 << "invalid resource type '" << maybeType.value()
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700734 << "' in <" << parser->getElementName() << ">");
735 return false;
736 }
737
738 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700739 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800740}
741
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800742bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
743 if (parseSymbolImpl(parser, outResource)) {
744 outResource->symbolState = SymbolState::kPrivate;
745 return true;
746 }
747 return false;
748}
749
750bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
751 if (parseSymbolImpl(parser, outResource)) {
752 outResource->symbolState = SymbolState::kUndefined;
753 return true;
754 }
755 return false;
756}
757
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800758
Adam Lesinski467f1712015-11-16 17:35:44 -0800759bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700760 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800761}
762
Adam Lesinski467f1712015-11-16 17:35:44 -0800763bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
764 bool weak) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800765 outResource->name.type = ResourceType::kAttr;
766
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800767 uint32_t typeMask = 0;
768
Adam Lesinski467f1712015-11-16 17:35:44 -0800769 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700770 if (maybeFormat) {
771 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800772 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700773 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
774 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700775 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800776 }
777 }
778
Adam Lesinskia5870652015-11-20 15:32:30 -0800779 Maybe<int32_t> maybeMin, maybeMax;
780
781 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
782 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
783 if (!minStr.empty()) {
784 android::Res_value value;
785 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
786 maybeMin = static_cast<int32_t>(value.data);
787 }
788 }
789
790 if (!maybeMin) {
791 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
792 << "invalid 'min' value '" << minStr << "'");
793 return false;
794 }
795 }
796
797 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
798 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
799 if (!maxStr.empty()) {
800 android::Res_value value;
801 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
802 maybeMax = static_cast<int32_t>(value.data);
803 }
804 }
805
806 if (!maybeMax) {
807 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
808 << "invalid 'max' value '" << maxStr << "'");
809 return false;
810 }
811 }
812
813 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
814 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
815 << "'min' and 'max' can only be used when format='integer'");
816 return false;
817 }
818
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800819 struct SymbolComparator {
820 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
821 return a.symbol.name.value() < b.symbol.name.value();
822 }
823 };
824
825 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800826
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700827 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700829 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800830 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
831 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700832 comment = util::trimWhitespace(parser->getComment()).toString();
833 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800834 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700835 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800836 continue;
837 }
838
Adam Lesinskica5638f2015-10-21 14:42:43 -0700839 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700840 const std::u16string& elementNamespace = parser->getElementNamespace();
841 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700842 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700843 if (elementName == u"enum") {
844 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700845 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700846 << "can not define an <enum>; already defined a <flag>");
847 error = true;
848 continue;
849 }
850 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700851
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700852 } else if (elementName == u"flag") {
853 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700854 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700855 << "can not define a <flag>; already defined an <enum>");
856 error = true;
857 continue;
858 }
859 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800860 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800861
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700862 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800863 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700864 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800865 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700866 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700867 childResource.value = util::make_unique<Id>();
868 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700869
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800870 symbol.symbol.setComment(std::move(comment));
871 symbol.symbol.setSource(itemSource);
872
873 auto insertResult = items.insert(std::move(symbol));
874 if (!insertResult.second) {
875 const Attribute::Symbol& existingSymbol = *insertResult.first;
876 mDiag->error(DiagMessage(itemSource)
877 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
878 << "'");
879
880 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
881 << "first defined here");
882 error = true;
883 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800884 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700885 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800886 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700887 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
888 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800889 error = true;
890 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700891
892 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893 }
894
895 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700896 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800897 }
898
899 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800900 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700901 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800902 if (maybeMin) {
903 attr->minInt = maybeMin.value();
904 }
905
906 if (maybeMax) {
907 attr->maxInt = maybeMax.value();
908 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700909 outResource->value = std::move(attr);
910 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800911}
912
Adam Lesinski467f1712015-11-16 17:35:44 -0800913Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700914 const StringPiece16& tag) {
915 const Source source = mSource.withLine(parser->getLineNumber());
916
Adam Lesinski467f1712015-11-16 17:35:44 -0800917 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700918 if (!maybeName) {
919 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
920 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800921 }
922
Adam Lesinski467f1712015-11-16 17:35:44 -0800923 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700924 if (!maybeValue) {
925 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
926 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800927 }
928
929 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700930 if (!android::ResTable::stringToInt(maybeValue.value().data(),
931 maybeValue.value().size(), &val)) {
932 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
933 << "' for <" << tag << ">; must be an integer");
934 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 }
936
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700937 return Attribute::Symbol{
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800938 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())),
Adam Lesinskie78fd612015-10-22 12:48:43 -0700939 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800940}
941
Adam Lesinski467f1712015-11-16 17:35:44 -0800942static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800943 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800944 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800945 const char16_t* const end = start + str.size();
946 const char16_t* p = start;
947
Adam Lesinski467f1712015-11-16 17:35:44 -0800948 Reference ref;
949 if (p != end && *p == u'*') {
950 ref.privateReference = true;
951 start++;
952 p++;
953 }
954
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800955 StringPiece16 package;
956 StringPiece16 name;
957 while (p != end) {
958 if (*p == u':') {
959 package = StringPiece16(start, p - start);
960 name = StringPiece16(p + 1, end - (p + 1));
961 break;
962 }
963 p++;
964 }
965
Adam Lesinski467f1712015-11-16 17:35:44 -0800966 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700967 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800968 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800969}
970
Adam Lesinski467f1712015-11-16 17:35:44 -0800971bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700972 const Source source = mSource.withLine(parser->getLineNumber());
973
Adam Lesinski467f1712015-11-16 17:35:44 -0800974 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700975 if (!maybeName) {
976 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800977 return false;
978 }
979
Adam Lesinski467f1712015-11-16 17:35:44 -0800980 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700981 if (!maybeKey) {
982 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800983 return false;
984 }
985
Adam Lesinski467f1712015-11-16 17:35:44 -0800986 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800987 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800988
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800989 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
990 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700991 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800992 return false;
993 }
994
Adam Lesinski467f1712015-11-16 17:35:44 -0800995 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800996 return true;
997}
998
Adam Lesinski467f1712015-11-16 17:35:44 -0800999bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001000 outResource->name.type = ResourceType::kStyle;
1001
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001002 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001003
Adam Lesinski467f1712015-11-16 17:35:44 -08001004 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001005 if (maybeParent) {
1006 // If the parent is empty, we don't have a parent, but we also don't infer either.
1007 if (!maybeParent.value().empty()) {
1008 std::string errStr;
1009 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
1010 if (!style->parent) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001011 mDiag->error(DiagMessage(outResource->source) << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001012 return false;
1013 }
1014
Adam Lesinski467f1712015-11-16 17:35:44 -08001015 // Transform the namespace prefix to the actual package name, and mark the reference as
1016 // private if appropriate.
1017 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001018 }
1019
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001020 } else {
1021 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001022 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001023 size_t pos = styleName.find_last_of(u'.');
1024 if (pos != std::string::npos) {
1025 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -08001026 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
1027 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001028 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001029 }
1030
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001031 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001032 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001033 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1034 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001035 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001036 continue;
1037 }
1038
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001039 const std::u16string& elementNamespace = parser->getElementNamespace();
1040 const std::u16string& elementName = parser->getElementName();
1041 if (elementNamespace == u"" && elementName == u"item") {
1042 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001043
Adam Lesinskica5638f2015-10-21 14:42:43 -07001044 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001045 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1046 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001047 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001048 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001049 }
1050
1051 if (error) {
1052 return false;
1053 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001054
1055 outResource->value = std::move(style);
1056 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001057}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001058
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001059bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1060 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_ANY);
1061}
1062
1063bool ResourceParser::parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1064 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_INTEGER);
1065}
1066
1067bool ResourceParser::parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1068 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_STRING);
1069}
1070
1071bool ResourceParser::parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
1072 const uint32_t typeMask) {
1073 outResource->name.type = ResourceType::kArray;
1074
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001075 std::unique_ptr<Array> array = util::make_unique<Array>();
1076
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001077 bool error = false;
1078 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001079 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1080 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001081 // Skip text and comments.
1082 continue;
1083 }
1084
1085 const Source itemSource = mSource.withLine(parser->getLineNumber());
1086 const std::u16string& elementNamespace = parser->getElementNamespace();
1087 const std::u16string& elementName = parser->getElementName();
1088 if (elementNamespace.empty() && elementName == u"item") {
1089 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1090 if (!item) {
1091 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1092 error = true;
1093 continue;
1094 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001095 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001096 array->items.emplace_back(std::move(item));
1097
Adam Lesinskica5638f2015-10-21 14:42:43 -07001098 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001099 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1100 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1101 error = true;
1102 }
1103 }
1104
1105 if (error) {
1106 return false;
1107 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001108
1109 outResource->value = std::move(array);
1110 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001111}
1112
Adam Lesinski467f1712015-11-16 17:35:44 -08001113bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001114 outResource->name.type = ResourceType::kPlurals;
1115
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001116 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1117
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001118 bool error = false;
1119 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001120 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1121 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001122 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001123 continue;
1124 }
1125
Adam Lesinskica5638f2015-10-21 14:42:43 -07001126 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001127 const std::u16string& elementNamespace = parser->getElementNamespace();
1128 const std::u16string& elementName = parser->getElementName();
1129 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001130 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1131 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001132 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001133 << "'quantity'");
1134 error = true;
1135 continue;
1136 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001137
Adam Lesinski467f1712015-11-16 17:35:44 -08001138 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001139 size_t index = 0;
1140 if (trimmedQuantity == u"zero") {
1141 index = Plural::Zero;
1142 } else if (trimmedQuantity == u"one") {
1143 index = Plural::One;
1144 } else if (trimmedQuantity == u"two") {
1145 index = Plural::Two;
1146 } else if (trimmedQuantity == u"few") {
1147 index = Plural::Few;
1148 } else if (trimmedQuantity == u"many") {
1149 index = Plural::Many;
1150 } else if (trimmedQuantity == u"other") {
1151 index = Plural::Other;
1152 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001153 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001154 << "<item> in <plural> has invalid value '" << trimmedQuantity
1155 << "' for attribute 'quantity'");
1156 error = true;
1157 continue;
1158 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001159
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001160 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001161 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001162 << "duplicate quantity '" << trimmedQuantity << "'");
1163 error = true;
1164 continue;
1165 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001166
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001167 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1168 kNoRawString))) {
1169 error = true;
1170 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001171 plural->values[index]->setSource(itemSource);
1172
1173 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1174 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001175 << elementName << ">");
1176 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001177 }
1178 }
1179
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001180 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001181 return false;
1182 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001183
1184 outResource->value = std::move(plural);
1185 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001186}
1187
Adam Lesinski467f1712015-11-16 17:35:44 -08001188bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001189 outResource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001190
Adam Lesinskib274e352015-11-06 15:14:35 -08001191 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001192 outResource->symbolState = SymbolState::kPublic;
1193
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001194 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1195
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001196 std::u16string comment;
1197 bool error = false;
1198 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001199 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1200 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001201 comment = util::trimWhitespace(parser->getComment()).toString();
1202 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001203 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001204 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001205 continue;
1206 }
1207
Adam Lesinskica5638f2015-10-21 14:42:43 -07001208 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001209 const std::u16string& elementNamespace = parser->getElementNamespace();
1210 const std::u16string& elementName = parser->getElementName();
1211 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001212 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1213 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001214 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001215 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001216 continue;
1217 }
1218
Adam Lesinski467f1712015-11-16 17:35:44 -08001219 // If this is a declaration, the package name may be in the name. Separate these out.
1220 // Eg. <attr name="android:text" />
1221 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1222 if (!maybeRef) {
1223 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1224 << maybeName.value() << "'");
1225 error = true;
1226 continue;
1227 }
1228
1229 Reference& childRef = maybeRef.value();
1230 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1231
Adam Lesinskica5638f2015-10-21 14:42:43 -07001232 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001233 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001234 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001235 childResource.source = itemSource;
1236 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001237
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001238 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001239 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001240 continue;
1241 }
1242
Adam Lesinskica5638f2015-10-21 14:42:43 -07001243 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001244 childRef.setComment(childResource.comment);
1245 childRef.setSource(itemSource);
1246 styleable->entries.push_back(std::move(childRef));
1247
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001248 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001249
Adam Lesinskica5638f2015-10-21 14:42:43 -07001250 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1251 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001252 << elementName << ">");
1253 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001254 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001255
1256 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001257 }
1258
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001259 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001260 return false;
1261 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001262
1263 outResource->value = std::move(styleable);
1264 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001265}
1266
1267} // namespace aapt