blob: 6a0787331e0251a5a38b576c9b57e70b6cd8434b [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 Lesinski7ff3ee12015-12-14 16:08:50 -080022#include "util/ImmutableMap.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070023#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080024#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070025
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080026#include <functional>
Adam Lesinski769de982015-04-10 19:43:55 -070027#include <sstream>
28
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029namespace aapt {
30
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032
Adam Lesinski27afb9e2015-11-06 18:25:04 -080033/**
34 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
35 */
36static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) {
37 return ns.empty() && (name == u"skip" || name == u"eat-comment");
38}
39
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080040static uint32_t parseFormatType(const StringPiece16& piece) {
41 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
42 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
43 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
44 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
45 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
46 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
47 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
48 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
49 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
50 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
51 return 0;
52}
53
54static uint32_t parseFormatAttribute(const StringPiece16& str) {
55 uint32_t mask = 0;
56 for (StringPiece16 part : util::tokenize(str, u'|')) {
57 StringPiece16 trimmedPart = util::trimWhitespace(part);
58 uint32_t type = parseFormatType(trimmedPart);
59 if (type == 0) {
60 return 0;
61 }
62 mask |= type;
63 }
64 return mask;
65}
66
67static bool shouldStripResource(const xml::XmlPullParser* parser,
68 const Maybe<std::u16string> productToMatch) {
69 assert(parser->getEvent() == xml::XmlPullParser::Event::kStartElement);
70
71 if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) {
72 if (!productToMatch) {
73 if (maybeProduct.value() != u"default" && maybeProduct.value() != u"phone") {
74 // We didn't specify a product and this is not a default product, so skip.
75 return true;
76 }
77 } else {
78 if (productToMatch && maybeProduct.value() != productToMatch.value()) {
79 // We specified a product, but they don't match.
80 return true;
81 }
82 }
83 }
84 return false;
85}
86
87/**
88 * A parsed resource ready to be added to the ResourceTable.
89 */
90struct ParsedResource {
91 ResourceName name;
92 Source source;
93 ResourceId id;
94 Maybe<SymbolState> symbolState;
95 std::u16string comment;
96 std::unique_ptr<Value> value;
97 std::list<ParsedResource> childResources;
98};
99
100// Recursively adds resources to the ResourceTable.
101static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
102 IDiagnostics* diag, ParsedResource* res) {
103 if (res->symbolState) {
104 Symbol symbol;
105 symbol.state = res->symbolState.value();
106 symbol.source = res->source;
107 symbol.comment = res->comment;
108 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
109 return false;
110 }
111 }
112
113 if (res->value) {
114 // Attach the comment, source and config to the value.
115 res->value->setComment(std::move(res->comment));
116 res->value->setSource(std::move(res->source));
117
118 if (!table->addResource(res->name, res->id, config, std::move(res->value), diag)) {
119 return false;
120 }
121 }
122
123 bool error = false;
124 for (ParsedResource& child : res->childResources) {
125 error |= !addResourcesToTable(table, config, diag, &child);
126 }
127 return !error;
128}
129
130// Convenient aliases for more readable function calls.
131enum {
132 kAllowRawString = true,
133 kNoRawString = false
134};
135
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700137 const ConfigDescription& config,
138 const ResourceParserOptions& options) :
139 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140}
141
142/**
143 * Build a string from XML that converts nested elements into Span objects.
144 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800145bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146 StyleString* outStyleString) {
147 std::vector<Span> spanStack;
148
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800149 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150 outRawString->clear();
151 outStyleString->spans.clear();
152 util::StringBuilder builder;
153 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -0800154 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
155 const xml::XmlPullParser::Event event = parser->getEvent();
156 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157 if (!parser->getElementNamespace().empty()) {
158 // We already warned and skipped the start element, so just skip here too
159 continue;
160 }
161
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162 depth--;
163 if (depth == 0) {
164 break;
165 }
166
167 spanStack.back().lastChar = builder.str().size();
168 outStyleString->spans.push_back(spanStack.back());
169 spanStack.pop_back();
170
Adam Lesinski467f1712015-11-16 17:35:44 -0800171 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172 outRawString->append(parser->getText());
173 builder.append(parser->getText());
174
Adam Lesinski467f1712015-11-16 17:35:44 -0800175 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176 if (!parser->getElementNamespace().empty()) {
177 if (parser->getElementNamespace() != sXliffNamespaceUri) {
178 // Only warn if this isn't an xliff namespace.
179 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
180 << "skipping element '"
181 << parser->getElementName()
182 << "' with unknown namespace '"
183 << parser->getElementNamespace()
184 << "'");
185 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186 continue;
187 }
188 depth++;
189
190 // Build a span object out of the nested element.
191 std::u16string spanName = parser->getElementName();
192 const auto endAttrIter = parser->endAttributes();
193 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
194 spanName += u";";
195 spanName += attrIter->name;
196 spanName += u"=";
197 spanName += attrIter->value;
198 }
199
200 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
202 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800203 error = true;
204 } else {
205 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207
Adam Lesinski467f1712015-11-16 17:35:44 -0800208 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209 // Skip
210 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212 }
213 }
214 assert(spanStack.empty() && "spans haven't been fully processed");
215
216 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800217 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218}
219
Adam Lesinski467f1712015-11-16 17:35:44 -0800220bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221 bool error = false;
222 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800223 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
224 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226 continue;
227 }
228
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
230 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
231 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232 return false;
233 }
234
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235 error |= !parseResources(parser);
236 break;
237 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238
Adam Lesinski467f1712015-11-16 17:35:44 -0800239 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
241 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242 return false;
243 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800245}
246
Adam Lesinski467f1712015-11-16 17:35:44 -0800247bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700248 std::set<ResourceName> strippedResources;
249
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800253 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
254 const xml::XmlPullParser::Event event = parser->getEvent();
255 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256 comment = parser->getComment();
257 continue;
258 }
259
Adam Lesinski467f1712015-11-16 17:35:44 -0800260 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
263 << "plain text not allowed here");
264 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 }
266 continue;
267 }
268
Adam Lesinski467f1712015-11-16 17:35:44 -0800269 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272 // Skip unknown namespace.
273 continue;
274 }
275
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700276 std::u16string elementName = parser->getElementName();
277 if (elementName == u"skip" || elementName == u"eat-comment") {
278 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279 continue;
280 }
281
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700282 ParsedResource parsedResource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700283 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700284 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800286 // Check if we should skip this product. We still need to parse it for correctness.
287 const bool stripResource = shouldStripResource(parser, mOptions.product);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800288
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800289 if (!parseResource(parser, &parsedResource)) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800290 error = true;
291 continue;
292 }
293
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800294 // We successfully parsed the resource.
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800295
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800296 if (stripResource) {
297 // Record that we stripped out this resource name.
298 // We will check that at least one variant of this resource was included.
299 strippedResources.insert(parsedResource.name);
300 } else if (!addResourcesToTable(mTable, mConfig, mDiag, &parsedResource)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700301 error = true;
302 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700304
305 // Check that we included at least one variant of each stripped resource.
306 for (const ResourceName& strippedResource : strippedResources) {
307 if (!mTable->findResource(strippedResource)) {
308 // Failed to find the resource.
309 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
310 "was filtered out but no product variant remains");
311 error = true;
312 }
313 }
314
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316}
317
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800318
319bool ResourceParser::parseResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
320 struct ItemTypeFormat {
321 ResourceType type;
322 uint32_t format;
323 };
324
325 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, ParsedResource*)>;
326
327 static const auto elToItemMap = ImmutableMap<std::u16string, ItemTypeFormat>::createPreSorted({
328 { u"bool", { ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN } },
329 { u"color", { ResourceType::kColor, android::ResTable_map::TYPE_COLOR } },
330 { u"dimen", { ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT
331 | android::ResTable_map::TYPE_FRACTION
332 | android::ResTable_map::TYPE_DIMENSION } },
333 { u"drawable", { ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR } },
334 { u"fraction", { ResourceType::kFraction, android::ResTable_map::TYPE_FLOAT
335 | android::ResTable_map::TYPE_FRACTION
336 | android::ResTable_map::TYPE_DIMENSION } },
337 { u"integer", { ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER } },
338 { u"string", { ResourceType::kString, android::ResTable_map::TYPE_STRING } },
339 });
340
341 static const auto elToBagMap = ImmutableMap<std::u16string, BagParseFunc>::createPreSorted({
342 { u"add-resource", std::mem_fn(&ResourceParser::parseAddResource) },
343 { u"array", std::mem_fn(&ResourceParser::parseArray) },
344 { u"attr", std::mem_fn(&ResourceParser::parseAttr) },
345 { u"declare-styleable", std::mem_fn(&ResourceParser::parseDeclareStyleable) },
346 { u"integer-array", std::mem_fn(&ResourceParser::parseIntegerArray) },
347 { u"java-symbol", std::mem_fn(&ResourceParser::parseSymbol) },
348 { u"plurals", std::mem_fn(&ResourceParser::parsePlural) },
349 { u"public", std::mem_fn(&ResourceParser::parsePublic) },
350 { u"public-group", std::mem_fn(&ResourceParser::parsePublicGroup) },
351 { u"string-array", std::mem_fn(&ResourceParser::parseStringArray) },
352 { u"style", std::mem_fn(&ResourceParser::parseStyle) },
353 { u"symbol", std::mem_fn(&ResourceParser::parseSymbol) },
354 });
355
356 std::u16string resourceType = parser->getElementName();
357
358 // The value format accepted for this resource.
359 uint32_t resourceFormat = 0u;
360
361 if (resourceType == u"item") {
362 // Items have their type encoded in the type attribute.
363 if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) {
364 resourceType = maybeType.value().toString();
365 } else {
366 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
367 << "<item> must have a 'type' attribute");
368 return false;
369 }
370
371 if (Maybe<StringPiece16> maybeFormat = xml::findNonEmptyAttribute(parser, u"format")) {
372 // An explicit format for this resource was specified. The resource will retain
373 // its type in its name, but the accepted value for this type is overridden.
374 resourceFormat = parseFormatType(maybeFormat.value());
375 if (!resourceFormat) {
376 mDiag->error(DiagMessage(outResource->source)
377 << "'" << maybeFormat.value() << "' is an invalid format");
378 return false;
379 }
380 }
381 }
382
383 // Get the name of the resource. This will be checked later, because not all
384 // XML elements require a name.
385 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
386
387 if (resourceType == u"id") {
388 if (!maybeName) {
389 mDiag->error(DiagMessage(outResource->source)
390 << "<" << parser->getElementName() << "> missing 'name' attribute");
391 return false;
392 }
393
394 outResource->name.type = ResourceType::kId;
395 outResource->name.entry = maybeName.value().toString();
396 outResource->value = util::make_unique<Id>();
397 return true;
398 }
399
400 const auto itemIter = elToItemMap.find(resourceType);
401 if (itemIter != elToItemMap.end()) {
402 // This is an item, record its type and format and start parsing.
403
404 if (!maybeName) {
405 mDiag->error(DiagMessage(outResource->source)
406 << "<" << parser->getElementName() << "> missing 'name' attribute");
407 return false;
408 }
409
410 outResource->name.type = itemIter->second.type;
411 outResource->name.entry = maybeName.value().toString();
412
413 // Only use the implicit format for this type if it wasn't overridden.
414 if (!resourceFormat) {
415 resourceFormat = itemIter->second.format;
416 }
417
418 if (!parseItem(parser, outResource, resourceFormat)) {
419 return false;
420 }
421 return true;
422 }
423
424 // This might be a bag or something.
425 const auto bagIter = elToBagMap.find(resourceType);
426 if (bagIter != elToBagMap.end()) {
427 // Ensure we have a name (unless this is a <public-group>).
428 if (resourceType != u"public-group") {
429 if (!maybeName) {
430 mDiag->error(DiagMessage(outResource->source)
431 << "<" << parser->getElementName() << "> missing 'name' attribute");
432 return false;
433 }
434
435 outResource->name.entry = maybeName.value().toString();
436 }
437
438 // Call the associated parse method. The type will be filled in by the
439 // parse func.
440 if (!bagIter->second(this, parser, outResource)) {
441 return false;
442 }
443 return true;
444 }
445
446 // Try parsing the elementName (or type) as a resource. These shall only be
447 // resources like 'layout' or 'xml' and they can only be references.
448 const ResourceType* parsedType = parseResourceType(resourceType);
449 if (parsedType) {
450 if (!maybeName) {
451 mDiag->error(DiagMessage(outResource->source)
452 << "<" << parser->getElementName() << "> missing 'name' attribute");
453 return false;
454 }
455
456 outResource->name.type = *parsedType;
457 outResource->name.entry = maybeName.value().toString();
458 outResource->value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
459 if (!outResource->value) {
460 mDiag->error(DiagMessage(outResource->source)
461 << "invalid value for type '" << *parsedType << "'. Expected a reference");
462 return false;
463 }
464 return true;
465 }
466
467 mDiag->warn(DiagMessage(outResource->source)
468 << "unknown resource type '" << parser->getElementName() << "'");
469 return false;
470}
471
472bool ResourceParser::parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
473 const uint32_t format) {
474 if (format == android::ResTable_map::TYPE_STRING) {
475 return parseString(parser, outResource);
476 }
477
478 outResource->value = parseXml(parser, format, kNoRawString);
479 if (!outResource->value) {
480 mDiag->error(DiagMessage(outResource->source) << "invalid " << outResource->name.type);
481 return false;
482 }
483 return true;
484}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485
486/**
487 * Reads the entire XML subtree and attempts to parse it as some Item,
488 * with typeMask denoting which items it can be. If allowRawValue is
489 * true, a RawString is returned if the XML couldn't be parsed as
490 * an Item. If allowRawValue is false, nullptr is returned in this
491 * case.
492 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800493std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700494 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495 const size_t beginXmlLine = parser->getLineNumber();
496
497 std::u16string rawValue;
498 StyleString styleString;
499 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
500 return {};
501 }
502
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503 if (!styleString.spans.empty()) {
504 // This can only be a StyledString.
505 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700506 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507 }
508
509 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700510 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700511 std::unique_ptr<Id> id = util::make_unique<Id>();
512 id->setSource(mSource.withLine(beginXmlLine));
513 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514 };
515
516 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
518 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800519 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700520 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800522 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700523 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524 return processedItem;
525 }
526
527 // Try making a regular string.
528 if (typeMask & android::ResTable_map::TYPE_STRING) {
529 // Use the trimmed, escaped string.
530 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700531 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532 }
533
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800534 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700535 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800536 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800538 }
539 return {};
540}
541
Adam Lesinski467f1712015-11-16 17:35:44 -0800542bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800543 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800544 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800545 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800546 mDiag->error(DiagMessage(outResource->source)
547 << "invalid value for 'formatted'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800548 return false;
549 }
550 }
551
Adam Lesinski9f222042015-11-04 13:51:45 -0800552 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800553 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800554 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800555 mDiag->error(DiagMessage(outResource->source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800556 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800557 return false;
558 }
559 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800560
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700561 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
562 if (!outResource->value) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800563 mDiag->error(DiagMessage(outResource->source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800564 return false;
565 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800566
Adam Lesinski9f222042015-11-04 13:51:45 -0800567 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800568 if (String* stringValue = valueCast<String>(outResource->value.get())) {
569 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800570 mDiag->error(DiagMessage(outResource->source)
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800571 << "multiple substitutions specified in non-positional format; "
572 "did you mean to add the formatted=\"false\" attribute?");
573 return false;
574 }
575 }
576 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700577 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578}
579
Adam Lesinski467f1712015-11-16 17:35:44 -0800580bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800581 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800583 mDiag->error(DiagMessage(outResource->source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584 return false;
585 }
586
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700587 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800588 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800589 mDiag->error(DiagMessage(outResource->source)
590 << "invalid resource type '" << maybeType.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800591 return false;
592 }
593
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700594 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800595
Adam Lesinski467f1712015-11-16 17:35:44 -0800596 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700598 bool result = android::ResTable::stringToInt(maybeId.value().data(),
599 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700600 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800601 if (!result || !resourceId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800602 mDiag->error(DiagMessage(outResource->source)
603 << "invalid resource ID '" << maybeId.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800604 return false;
605 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700606 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800607 }
608
609 if (*parsedType == ResourceType::kId) {
610 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700611 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800612 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700613
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700614 outResource->symbolState = SymbolState::kPublic;
615 return true;
616}
617
Adam Lesinski467f1712015-11-16 17:35:44 -0800618bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800619 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800620 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800621 mDiag->error(DiagMessage(outResource->source)
622 << "<public-group> must have a 'type' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800623 return false;
624 }
625
626 const ResourceType* parsedType = parseResourceType(maybeType.value());
627 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800628 mDiag->error(DiagMessage(outResource->source)
629 << "invalid resource type '" << maybeType.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800630 return false;
631 }
632
Adam Lesinski467f1712015-11-16 17:35:44 -0800633 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800634 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800635 mDiag->error(DiagMessage(outResource->source)
636 << "<public-group> must have a 'first-id' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800637 return false;
638 }
639
640 android::Res_value val;
641 bool result = android::ResTable::stringToInt(maybeId.value().data(),
642 maybeId.value().size(), &val);
643 ResourceId nextId(val.data);
644 if (!result || !nextId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800645 mDiag->error(DiagMessage(outResource->source)
646 << "invalid resource ID '" << maybeId.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800647 return false;
648 }
649
650 std::u16string comment;
651 bool error = false;
652 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800653 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
654 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800655 comment = util::trimWhitespace(parser->getComment()).toString();
656 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800657 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800658 // Skip text.
659 continue;
660 }
661
662 const Source itemSource = mSource.withLine(parser->getLineNumber());
663 const std::u16string& elementNamespace = parser->getElementNamespace();
664 const std::u16string& elementName = parser->getElementName();
665 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800666 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800667 if (!maybeName) {
668 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
669 error = true;
670 continue;
671 }
672
Adam Lesinski467f1712015-11-16 17:35:44 -0800673 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800674 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
675 error = true;
676 continue;
677 }
678
Adam Lesinski467f1712015-11-16 17:35:44 -0800679 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800680 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
681 error = true;
682 continue;
683 }
684
685 ParsedResource childResource;
686 childResource.name.type = *parsedType;
687 childResource.name.entry = maybeName.value().toString();
688 childResource.id = nextId;
689 childResource.comment = std::move(comment);
690 childResource.source = itemSource;
691 childResource.symbolState = SymbolState::kPublic;
692 outResource->childResources.push_back(std::move(childResource));
693
694 nextId.id += 1;
695
696 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
697 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
698 error = true;
699 }
700 }
701 return !error;
702}
703
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800704bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800705 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700706 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800707 mDiag->error(DiagMessage(outResource->source)
708 << "<" << parser->getElementName() << "> must have a 'type' attribute");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700709 return false;
710 }
711
712 const ResourceType* parsedType = parseResourceType(maybeType.value());
713 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800714 mDiag->error(DiagMessage(outResource->source)
715 << "invalid resource type '" << maybeType.value()
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700716 << "' in <" << parser->getElementName() << ">");
717 return false;
718 }
719
720 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700721 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800722}
723
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800724bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
725 if (parseSymbolImpl(parser, outResource)) {
726 outResource->symbolState = SymbolState::kPrivate;
727 return true;
728 }
729 return false;
730}
731
732bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
733 if (parseSymbolImpl(parser, outResource)) {
734 outResource->symbolState = SymbolState::kUndefined;
735 return true;
736 }
737 return false;
738}
739
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800740
Adam Lesinski467f1712015-11-16 17:35:44 -0800741bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700742 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800743}
744
Adam Lesinski467f1712015-11-16 17:35:44 -0800745bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
746 bool weak) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800747 outResource->name.type = ResourceType::kAttr;
748
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800749 uint32_t typeMask = 0;
750
Adam Lesinski467f1712015-11-16 17:35:44 -0800751 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700752 if (maybeFormat) {
753 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800754 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
756 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700757 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800758 }
759 }
760
Adam Lesinskia5870652015-11-20 15:32:30 -0800761 Maybe<int32_t> maybeMin, maybeMax;
762
763 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
764 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
765 if (!minStr.empty()) {
766 android::Res_value value;
767 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
768 maybeMin = static_cast<int32_t>(value.data);
769 }
770 }
771
772 if (!maybeMin) {
773 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
774 << "invalid 'min' value '" << minStr << "'");
775 return false;
776 }
777 }
778
779 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
780 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
781 if (!maxStr.empty()) {
782 android::Res_value value;
783 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
784 maybeMax = static_cast<int32_t>(value.data);
785 }
786 }
787
788 if (!maybeMax) {
789 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
790 << "invalid 'max' value '" << maxStr << "'");
791 return false;
792 }
793 }
794
795 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
796 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
797 << "'min' and 'max' can only be used when format='integer'");
798 return false;
799 }
800
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800801 struct SymbolComparator {
802 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
803 return a.symbol.name.value() < b.symbol.name.value();
804 }
805 };
806
807 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800808
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700809 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800810 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700811 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800812 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
813 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700814 comment = util::trimWhitespace(parser->getComment()).toString();
815 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800816 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700817 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800818 continue;
819 }
820
Adam Lesinskica5638f2015-10-21 14:42:43 -0700821 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700822 const std::u16string& elementNamespace = parser->getElementNamespace();
823 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700824 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700825 if (elementName == u"enum") {
826 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700827 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700828 << "can not define an <enum>; already defined a <flag>");
829 error = true;
830 continue;
831 }
832 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700833
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700834 } else if (elementName == u"flag") {
835 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700836 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700837 << "can not define a <flag>; already defined an <enum>");
838 error = true;
839 continue;
840 }
841 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800842 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800843
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700844 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800845 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700846 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800847 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700848 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700849 childResource.value = util::make_unique<Id>();
850 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700851
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800852 symbol.symbol.setComment(std::move(comment));
853 symbol.symbol.setSource(itemSource);
854
855 auto insertResult = items.insert(std::move(symbol));
856 if (!insertResult.second) {
857 const Attribute::Symbol& existingSymbol = *insertResult.first;
858 mDiag->error(DiagMessage(itemSource)
859 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
860 << "'");
861
862 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
863 << "first defined here");
864 error = true;
865 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800866 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700867 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700869 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
870 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800871 error = true;
872 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700873
874 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875 }
876
877 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700878 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800879 }
880
881 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800882 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700883 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800884 if (maybeMin) {
885 attr->minInt = maybeMin.value();
886 }
887
888 if (maybeMax) {
889 attr->maxInt = maybeMax.value();
890 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700891 outResource->value = std::move(attr);
892 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893}
894
Adam Lesinski467f1712015-11-16 17:35:44 -0800895Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700896 const StringPiece16& tag) {
897 const Source source = mSource.withLine(parser->getLineNumber());
898
Adam Lesinski467f1712015-11-16 17:35:44 -0800899 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700900 if (!maybeName) {
901 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
902 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800903 }
904
Adam Lesinski467f1712015-11-16 17:35:44 -0800905 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700906 if (!maybeValue) {
907 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
908 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800909 }
910
911 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700912 if (!android::ResTable::stringToInt(maybeValue.value().data(),
913 maybeValue.value().size(), &val)) {
914 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
915 << "' for <" << tag << ">; must be an integer");
916 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800917 }
918
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700919 return Attribute::Symbol{
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800920 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())),
Adam Lesinskie78fd612015-10-22 12:48:43 -0700921 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800922}
923
Adam Lesinski467f1712015-11-16 17:35:44 -0800924static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800925 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800926 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800927 const char16_t* const end = start + str.size();
928 const char16_t* p = start;
929
Adam Lesinski467f1712015-11-16 17:35:44 -0800930 Reference ref;
931 if (p != end && *p == u'*') {
932 ref.privateReference = true;
933 start++;
934 p++;
935 }
936
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800937 StringPiece16 package;
938 StringPiece16 name;
939 while (p != end) {
940 if (*p == u':') {
941 package = StringPiece16(start, p - start);
942 name = StringPiece16(p + 1, end - (p + 1));
943 break;
944 }
945 p++;
946 }
947
Adam Lesinski467f1712015-11-16 17:35:44 -0800948 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700949 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800950 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800951}
952
Adam Lesinski467f1712015-11-16 17:35:44 -0800953bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700954 const Source source = mSource.withLine(parser->getLineNumber());
955
Adam Lesinski467f1712015-11-16 17:35:44 -0800956 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700957 if (!maybeName) {
958 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800959 return false;
960 }
961
Adam Lesinski467f1712015-11-16 17:35:44 -0800962 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700963 if (!maybeKey) {
964 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800965 return false;
966 }
967
Adam Lesinski467f1712015-11-16 17:35:44 -0800968 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800969 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800970
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800971 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
972 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700973 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800974 return false;
975 }
976
Adam Lesinski467f1712015-11-16 17:35:44 -0800977 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800978 return true;
979}
980
Adam Lesinski467f1712015-11-16 17:35:44 -0800981bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800982 outResource->name.type = ResourceType::kStyle;
983
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700984 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800985
Adam Lesinski467f1712015-11-16 17:35:44 -0800986 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700987 if (maybeParent) {
988 // If the parent is empty, we don't have a parent, but we also don't infer either.
989 if (!maybeParent.value().empty()) {
990 std::string errStr;
991 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
992 if (!style->parent) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800993 mDiag->error(DiagMessage(outResource->source) << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700994 return false;
995 }
996
Adam Lesinski467f1712015-11-16 17:35:44 -0800997 // Transform the namespace prefix to the actual package name, and mark the reference as
998 // private if appropriate.
999 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001000 }
1001
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001002 } else {
1003 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001004 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001005 size_t pos = styleName.find_last_of(u'.');
1006 if (pos != std::string::npos) {
1007 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -08001008 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
1009 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001010 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001011 }
1012
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001013 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001014 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001015 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1016 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001017 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001018 continue;
1019 }
1020
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001021 const std::u16string& elementNamespace = parser->getElementNamespace();
1022 const std::u16string& elementName = parser->getElementName();
1023 if (elementNamespace == u"" && elementName == u"item") {
1024 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001025
Adam Lesinskica5638f2015-10-21 14:42:43 -07001026 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001027 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1028 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001029 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001030 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001031 }
1032
1033 if (error) {
1034 return false;
1035 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001036
1037 outResource->value = std::move(style);
1038 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001039}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001040
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001041bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1042 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_ANY);
1043}
1044
1045bool ResourceParser::parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1046 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_INTEGER);
1047}
1048
1049bool ResourceParser::parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1050 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_STRING);
1051}
1052
1053bool ResourceParser::parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
1054 const uint32_t typeMask) {
1055 outResource->name.type = ResourceType::kArray;
1056
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001057 std::unique_ptr<Array> array = util::make_unique<Array>();
1058
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001059 bool error = false;
1060 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001061 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1062 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001063 // Skip text and comments.
1064 continue;
1065 }
1066
1067 const Source itemSource = mSource.withLine(parser->getLineNumber());
1068 const std::u16string& elementNamespace = parser->getElementNamespace();
1069 const std::u16string& elementName = parser->getElementName();
1070 if (elementNamespace.empty() && elementName == u"item") {
1071 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1072 if (!item) {
1073 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1074 error = true;
1075 continue;
1076 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001077 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001078 array->items.emplace_back(std::move(item));
1079
Adam Lesinskica5638f2015-10-21 14:42:43 -07001080 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001081 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1082 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1083 error = true;
1084 }
1085 }
1086
1087 if (error) {
1088 return false;
1089 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001090
1091 outResource->value = std::move(array);
1092 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001093}
1094
Adam Lesinski467f1712015-11-16 17:35:44 -08001095bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001096 outResource->name.type = ResourceType::kPlurals;
1097
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001098 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1099
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001100 bool error = false;
1101 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001102 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1103 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001104 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001105 continue;
1106 }
1107
Adam Lesinskica5638f2015-10-21 14:42:43 -07001108 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001109 const std::u16string& elementNamespace = parser->getElementNamespace();
1110 const std::u16string& elementName = parser->getElementName();
1111 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001112 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1113 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001114 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001115 << "'quantity'");
1116 error = true;
1117 continue;
1118 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001119
Adam Lesinski467f1712015-11-16 17:35:44 -08001120 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001121 size_t index = 0;
1122 if (trimmedQuantity == u"zero") {
1123 index = Plural::Zero;
1124 } else if (trimmedQuantity == u"one") {
1125 index = Plural::One;
1126 } else if (trimmedQuantity == u"two") {
1127 index = Plural::Two;
1128 } else if (trimmedQuantity == u"few") {
1129 index = Plural::Few;
1130 } else if (trimmedQuantity == u"many") {
1131 index = Plural::Many;
1132 } else if (trimmedQuantity == u"other") {
1133 index = Plural::Other;
1134 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001135 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001136 << "<item> in <plural> has invalid value '" << trimmedQuantity
1137 << "' for attribute 'quantity'");
1138 error = true;
1139 continue;
1140 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001141
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001142 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001143 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001144 << "duplicate quantity '" << trimmedQuantity << "'");
1145 error = true;
1146 continue;
1147 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001148
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001149 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1150 kNoRawString))) {
1151 error = true;
1152 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001153 plural->values[index]->setSource(itemSource);
1154
1155 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1156 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001157 << elementName << ">");
1158 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001159 }
1160 }
1161
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001162 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001163 return false;
1164 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001165
1166 outResource->value = std::move(plural);
1167 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001168}
1169
Adam Lesinski467f1712015-11-16 17:35:44 -08001170bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001171 outResource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001172
Adam Lesinskib274e352015-11-06 15:14:35 -08001173 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001174 outResource->symbolState = SymbolState::kPublic;
1175
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001176 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1177
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001178 std::u16string comment;
1179 bool error = false;
1180 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001181 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1182 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001183 comment = util::trimWhitespace(parser->getComment()).toString();
1184 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001185 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001186 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001187 continue;
1188 }
1189
Adam Lesinskica5638f2015-10-21 14:42:43 -07001190 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001191 const std::u16string& elementNamespace = parser->getElementNamespace();
1192 const std::u16string& elementName = parser->getElementName();
1193 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001194 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1195 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001196 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001197 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001198 continue;
1199 }
1200
Adam Lesinski467f1712015-11-16 17:35:44 -08001201 // If this is a declaration, the package name may be in the name. Separate these out.
1202 // Eg. <attr name="android:text" />
1203 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1204 if (!maybeRef) {
1205 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1206 << maybeName.value() << "'");
1207 error = true;
1208 continue;
1209 }
1210
1211 Reference& childRef = maybeRef.value();
1212 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1213
Adam Lesinskica5638f2015-10-21 14:42:43 -07001214 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001215 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001216 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001217 childResource.source = itemSource;
1218 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001219
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001220 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001221 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001222 continue;
1223 }
1224
Adam Lesinskica5638f2015-10-21 14:42:43 -07001225 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001226 childRef.setComment(childResource.comment);
1227 childRef.setSource(itemSource);
1228 styleable->entries.push_back(std::move(childRef));
1229
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001230 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001231
Adam Lesinskica5638f2015-10-21 14:42:43 -07001232 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1233 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001234 << elementName << ">");
1235 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001236 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001237
1238 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001239 }
1240
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001241 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001242 return false;
1243 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001244
1245 outResource->value = std::move(styleable);
1246 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001247}
1248
1249} // namespace aapt