blob: b100e84f8a46d993c90ca9968e261c431707dced [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
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080067/**
68 * A parsed resource ready to be added to the ResourceTable.
69 */
70struct ParsedResource {
71 ResourceName name;
Adam Lesinski52364f72016-01-11 13:10:24 -080072 ConfigDescription config;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080073 std::string product;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080074 Source source;
75 ResourceId id;
76 Maybe<SymbolState> symbolState;
77 std::u16string comment;
78 std::unique_ptr<Value> value;
79 std::list<ParsedResource> childResources;
80};
81
82// Recursively adds resources to the ResourceTable.
Adam Lesinski52364f72016-01-11 13:10:24 -080083static bool addResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080084 if (res->symbolState) {
85 Symbol symbol;
86 symbol.state = res->symbolState.value();
87 symbol.source = res->source;
88 symbol.comment = res->comment;
89 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
90 return false;
91 }
92 }
93
94 if (res->value) {
95 // Attach the comment, source and config to the value.
96 res->value->setComment(std::move(res->comment));
97 res->value->setSource(std::move(res->source));
98
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080099 if (!table->addResource(res->name, res->id, res->config, res->product,
100 std::move(res->value), diag)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800101 return false;
102 }
103 }
104
105 bool error = false;
106 for (ParsedResource& child : res->childResources) {
Adam Lesinski52364f72016-01-11 13:10:24 -0800107 error |= !addResourcesToTable(table, diag, &child);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800108 }
109 return !error;
110}
111
112// Convenient aliases for more readable function calls.
113enum {
114 kAllowRawString = true,
115 kNoRawString = false
116};
117
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700119 const ConfigDescription& config,
120 const ResourceParserOptions& options) :
121 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
124/**
125 * Build a string from XML that converts nested elements into Span objects.
126 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800127bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128 StyleString* outStyleString) {
129 std::vector<Span> spanStack;
130
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800131 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132 outRawString->clear();
133 outStyleString->spans.clear();
134 util::StringBuilder builder;
135 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -0800136 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
137 const xml::XmlPullParser::Event event = parser->getEvent();
138 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 if (!parser->getElementNamespace().empty()) {
140 // We already warned and skipped the start element, so just skip here too
141 continue;
142 }
143
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144 depth--;
145 if (depth == 0) {
146 break;
147 }
148
149 spanStack.back().lastChar = builder.str().size();
150 outStyleString->spans.push_back(spanStack.back());
151 spanStack.pop_back();
152
Adam Lesinski467f1712015-11-16 17:35:44 -0800153 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154 outRawString->append(parser->getText());
155 builder.append(parser->getText());
156
Adam Lesinski467f1712015-11-16 17:35:44 -0800157 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158 if (!parser->getElementNamespace().empty()) {
159 if (parser->getElementNamespace() != sXliffNamespaceUri) {
160 // Only warn if this isn't an xliff namespace.
161 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
162 << "skipping element '"
163 << parser->getElementName()
164 << "' with unknown namespace '"
165 << parser->getElementNamespace()
166 << "'");
167 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168 continue;
169 }
170 depth++;
171
172 // Build a span object out of the nested element.
173 std::u16string spanName = parser->getElementName();
174 const auto endAttrIter = parser->endAttributes();
175 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
176 spanName += u";";
177 spanName += attrIter->name;
178 spanName += u"=";
179 spanName += attrIter->value;
180 }
181
182 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
184 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800185 error = true;
186 } else {
187 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinski467f1712015-11-16 17:35:44 -0800190 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191 // Skip
192 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700193 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194 }
195 }
196 assert(spanStack.empty() && "spans haven't been fully processed");
197
198 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800199 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200}
201
Adam Lesinski467f1712015-11-16 17:35:44 -0800202bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203 bool error = false;
204 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800205 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
206 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208 continue;
209 }
210
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
212 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
213 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214 return false;
215 }
216
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 error |= !parseResources(parser);
218 break;
219 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220
Adam Lesinski467f1712015-11-16 17:35:44 -0800221 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
223 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 return false;
225 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227}
228
Adam Lesinski467f1712015-11-16 17:35:44 -0800229bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700230 std::set<ResourceName> strippedResources;
231
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800235 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
236 const xml::XmlPullParser::Event event = parser->getEvent();
237 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238 comment = parser->getComment();
239 continue;
240 }
241
Adam Lesinski467f1712015-11-16 17:35:44 -0800242 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800243 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
245 << "plain text not allowed here");
246 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247 }
248 continue;
249 }
250
Adam Lesinski467f1712015-11-16 17:35:44 -0800251 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 // Skip unknown namespace.
255 continue;
256 }
257
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258 std::u16string elementName = parser->getElementName();
259 if (elementName == u"skip" || elementName == u"eat-comment") {
260 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 continue;
262 }
263
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700264 ParsedResource parsedResource;
Adam Lesinski52364f72016-01-11 13:10:24 -0800265 parsedResource.config = mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700266 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700267 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Adam Lesinski7751afc2016-01-06 15:45:28 -0800269 // Extract the product name if it exists.
Adam Lesinski7751afc2016-01-06 15:45:28 -0800270 if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800271 parsedResource.product = util::utf16ToUtf8(maybeProduct.value());
Adam Lesinski7751afc2016-01-06 15:45:28 -0800272 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800273
Adam Lesinski7751afc2016-01-06 15:45:28 -0800274 // Parse the resource regardless of product.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800275 if (!parseResource(parser, &parsedResource)) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800276 error = true;
277 continue;
278 }
279
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800280 if (!addResourcesToTable(mTable, mDiag, &parsedResource)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700281 error = true;
282 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700284
285 // Check that we included at least one variant of each stripped resource.
286 for (const ResourceName& strippedResource : strippedResources) {
287 if (!mTable->findResource(strippedResource)) {
288 // Failed to find the resource.
289 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
290 "was filtered out but no product variant remains");
291 error = true;
292 }
293 }
294
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296}
297
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800298
299bool ResourceParser::parseResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
300 struct ItemTypeFormat {
301 ResourceType type;
302 uint32_t format;
303 };
304
305 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, ParsedResource*)>;
306
307 static const auto elToItemMap = ImmutableMap<std::u16string, ItemTypeFormat>::createPreSorted({
308 { u"bool", { ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN } },
309 { u"color", { ResourceType::kColor, android::ResTable_map::TYPE_COLOR } },
310 { u"dimen", { ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT
311 | android::ResTable_map::TYPE_FRACTION
312 | android::ResTable_map::TYPE_DIMENSION } },
313 { u"drawable", { ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR } },
314 { u"fraction", { ResourceType::kFraction, android::ResTable_map::TYPE_FLOAT
315 | android::ResTable_map::TYPE_FRACTION
316 | android::ResTable_map::TYPE_DIMENSION } },
317 { u"integer", { ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER } },
318 { u"string", { ResourceType::kString, android::ResTable_map::TYPE_STRING } },
319 });
320
321 static const auto elToBagMap = ImmutableMap<std::u16string, BagParseFunc>::createPreSorted({
322 { u"add-resource", std::mem_fn(&ResourceParser::parseAddResource) },
323 { u"array", std::mem_fn(&ResourceParser::parseArray) },
324 { u"attr", std::mem_fn(&ResourceParser::parseAttr) },
325 { u"declare-styleable", std::mem_fn(&ResourceParser::parseDeclareStyleable) },
326 { u"integer-array", std::mem_fn(&ResourceParser::parseIntegerArray) },
327 { u"java-symbol", std::mem_fn(&ResourceParser::parseSymbol) },
328 { u"plurals", std::mem_fn(&ResourceParser::parsePlural) },
329 { u"public", std::mem_fn(&ResourceParser::parsePublic) },
330 { u"public-group", std::mem_fn(&ResourceParser::parsePublicGroup) },
331 { u"string-array", std::mem_fn(&ResourceParser::parseStringArray) },
332 { u"style", std::mem_fn(&ResourceParser::parseStyle) },
333 { u"symbol", std::mem_fn(&ResourceParser::parseSymbol) },
334 });
335
336 std::u16string resourceType = parser->getElementName();
337
338 // The value format accepted for this resource.
339 uint32_t resourceFormat = 0u;
340
341 if (resourceType == u"item") {
342 // Items have their type encoded in the type attribute.
343 if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) {
344 resourceType = maybeType.value().toString();
345 } else {
346 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
347 << "<item> must have a 'type' attribute");
348 return false;
349 }
350
351 if (Maybe<StringPiece16> maybeFormat = xml::findNonEmptyAttribute(parser, u"format")) {
352 // An explicit format for this resource was specified. The resource will retain
353 // its type in its name, but the accepted value for this type is overridden.
354 resourceFormat = parseFormatType(maybeFormat.value());
355 if (!resourceFormat) {
356 mDiag->error(DiagMessage(outResource->source)
357 << "'" << maybeFormat.value() << "' is an invalid format");
358 return false;
359 }
360 }
361 }
362
363 // Get the name of the resource. This will be checked later, because not all
364 // XML elements require a name.
365 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
366
367 if (resourceType == u"id") {
368 if (!maybeName) {
369 mDiag->error(DiagMessage(outResource->source)
370 << "<" << parser->getElementName() << "> missing 'name' attribute");
371 return false;
372 }
373
374 outResource->name.type = ResourceType::kId;
375 outResource->name.entry = maybeName.value().toString();
376 outResource->value = util::make_unique<Id>();
377 return true;
378 }
379
380 const auto itemIter = elToItemMap.find(resourceType);
381 if (itemIter != elToItemMap.end()) {
382 // This is an item, record its type and format and start parsing.
383
384 if (!maybeName) {
385 mDiag->error(DiagMessage(outResource->source)
386 << "<" << parser->getElementName() << "> missing 'name' attribute");
387 return false;
388 }
389
390 outResource->name.type = itemIter->second.type;
391 outResource->name.entry = maybeName.value().toString();
392
393 // Only use the implicit format for this type if it wasn't overridden.
394 if (!resourceFormat) {
395 resourceFormat = itemIter->second.format;
396 }
397
398 if (!parseItem(parser, outResource, resourceFormat)) {
399 return false;
400 }
401 return true;
402 }
403
404 // This might be a bag or something.
405 const auto bagIter = elToBagMap.find(resourceType);
406 if (bagIter != elToBagMap.end()) {
407 // Ensure we have a name (unless this is a <public-group>).
408 if (resourceType != u"public-group") {
409 if (!maybeName) {
410 mDiag->error(DiagMessage(outResource->source)
411 << "<" << parser->getElementName() << "> missing 'name' attribute");
412 return false;
413 }
414
415 outResource->name.entry = maybeName.value().toString();
416 }
417
418 // Call the associated parse method. The type will be filled in by the
419 // parse func.
420 if (!bagIter->second(this, parser, outResource)) {
421 return false;
422 }
423 return true;
424 }
425
426 // Try parsing the elementName (or type) as a resource. These shall only be
427 // resources like 'layout' or 'xml' and they can only be references.
428 const ResourceType* parsedType = parseResourceType(resourceType);
429 if (parsedType) {
430 if (!maybeName) {
431 mDiag->error(DiagMessage(outResource->source)
432 << "<" << parser->getElementName() << "> missing 'name' attribute");
433 return false;
434 }
435
436 outResource->name.type = *parsedType;
437 outResource->name.entry = maybeName.value().toString();
438 outResource->value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
439 if (!outResource->value) {
440 mDiag->error(DiagMessage(outResource->source)
441 << "invalid value for type '" << *parsedType << "'. Expected a reference");
442 return false;
443 }
444 return true;
445 }
446
447 mDiag->warn(DiagMessage(outResource->source)
448 << "unknown resource type '" << parser->getElementName() << "'");
449 return false;
450}
451
452bool ResourceParser::parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
453 const uint32_t format) {
454 if (format == android::ResTable_map::TYPE_STRING) {
455 return parseString(parser, outResource);
456 }
457
458 outResource->value = parseXml(parser, format, kNoRawString);
459 if (!outResource->value) {
460 mDiag->error(DiagMessage(outResource->source) << "invalid " << outResource->name.type);
461 return false;
462 }
463 return true;
464}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465
466/**
467 * Reads the entire XML subtree and attempts to parse it as some Item,
468 * with typeMask denoting which items it can be. If allowRawValue is
469 * true, a RawString is returned if the XML couldn't be parsed as
470 * an Item. If allowRawValue is false, nullptr is returned in this
471 * case.
472 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800473std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700474 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800475 const size_t beginXmlLine = parser->getLineNumber();
476
477 std::u16string rawValue;
478 StyleString styleString;
479 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
480 return {};
481 }
482
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483 if (!styleString.spans.empty()) {
484 // This can only be a StyledString.
485 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487 }
488
489 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700490 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700491 std::unique_ptr<Id> id = util::make_unique<Id>();
492 id->setSource(mSource.withLine(beginXmlLine));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800493 mTable->addResource(name, {}, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494 };
495
496 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700497 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
498 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700500 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800502 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504 return processedItem;
505 }
506
507 // Try making a regular string.
508 if (typeMask & android::ResTable_map::TYPE_STRING) {
509 // Use the trimmed, escaped string.
510 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700511 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512 }
513
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700515 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800516 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518 }
519 return {};
520}
521
Adam Lesinski467f1712015-11-16 17:35:44 -0800522bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800523 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800524 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800525 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800526 mDiag->error(DiagMessage(outResource->source)
527 << "invalid value for 'formatted'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800528 return false;
529 }
530 }
531
Adam Lesinski9f222042015-11-04 13:51:45 -0800532 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800533 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800534 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800535 mDiag->error(DiagMessage(outResource->source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800536 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800537 return false;
538 }
539 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800540
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700541 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
542 if (!outResource->value) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800543 mDiag->error(DiagMessage(outResource->source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800544 return false;
545 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800546
Adam Lesinski393b5f02015-12-17 13:03:11 -0800547 if (String* stringValue = valueCast<String>(outResource->value.get())) {
548 stringValue->setTranslateable(translateable);
549
550 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800551 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski979ccb22016-01-11 10:42:19 -0800552 DiagMessage msg(outResource->source);
553 msg << "multiple substitutions specified in non-positional format; "
554 "did you mean to add the formatted=\"false\" attribute?";
555 if (mOptions.errorOnPositionalArguments) {
556 mDiag->error(msg);
557 return false;
558 }
559
560 mDiag->warn(msg);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800561 }
562 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800563
564 } else if (StyledString* stringValue = valueCast<StyledString>(outResource->value.get())) {
565 stringValue->setTranslateable(translateable);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800566 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700567 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800568}
569
Adam Lesinski467f1712015-11-16 17:35:44 -0800570bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800571 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700572 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800573 mDiag->error(DiagMessage(outResource->source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574 return false;
575 }
576
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700577 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800579 mDiag->error(DiagMessage(outResource->source)
580 << "invalid resource type '" << maybeType.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800581 return false;
582 }
583
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700584 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800585
Adam Lesinski467f1712015-11-16 17:35:44 -0800586 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700588 bool result = android::ResTable::stringToInt(maybeId.value().data(),
589 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700590 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800591 if (!result || !resourceId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800592 mDiag->error(DiagMessage(outResource->source)
593 << "invalid resource ID '" << maybeId.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594 return false;
595 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700596 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597 }
598
599 if (*parsedType == ResourceType::kId) {
600 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700601 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700603
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700604 outResource->symbolState = SymbolState::kPublic;
605 return true;
606}
607
Adam Lesinski467f1712015-11-16 17:35:44 -0800608bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800609 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800610 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800611 mDiag->error(DiagMessage(outResource->source)
612 << "<public-group> must have a 'type' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800613 return false;
614 }
615
616 const ResourceType* parsedType = parseResourceType(maybeType.value());
617 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800618 mDiag->error(DiagMessage(outResource->source)
619 << "invalid resource type '" << maybeType.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800620 return false;
621 }
622
Adam Lesinski467f1712015-11-16 17:35:44 -0800623 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800624 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800625 mDiag->error(DiagMessage(outResource->source)
626 << "<public-group> must have a 'first-id' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800627 return false;
628 }
629
630 android::Res_value val;
631 bool result = android::ResTable::stringToInt(maybeId.value().data(),
632 maybeId.value().size(), &val);
633 ResourceId nextId(val.data);
634 if (!result || !nextId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800635 mDiag->error(DiagMessage(outResource->source)
636 << "invalid resource ID '" << maybeId.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800637 return false;
638 }
639
640 std::u16string comment;
641 bool error = false;
642 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800643 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
644 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800645 comment = util::trimWhitespace(parser->getComment()).toString();
646 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800647 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800648 // Skip text.
649 continue;
650 }
651
652 const Source itemSource = mSource.withLine(parser->getLineNumber());
653 const std::u16string& elementNamespace = parser->getElementNamespace();
654 const std::u16string& elementName = parser->getElementName();
655 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800656 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800657 if (!maybeName) {
658 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
659 error = true;
660 continue;
661 }
662
Adam Lesinski467f1712015-11-16 17:35:44 -0800663 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800664 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
665 error = true;
666 continue;
667 }
668
Adam Lesinski467f1712015-11-16 17:35:44 -0800669 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800670 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
671 error = true;
672 continue;
673 }
674
675 ParsedResource childResource;
676 childResource.name.type = *parsedType;
677 childResource.name.entry = maybeName.value().toString();
678 childResource.id = nextId;
679 childResource.comment = std::move(comment);
680 childResource.source = itemSource;
681 childResource.symbolState = SymbolState::kPublic;
682 outResource->childResources.push_back(std::move(childResource));
683
684 nextId.id += 1;
685
686 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
687 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
688 error = true;
689 }
690 }
691 return !error;
692}
693
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800694bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800695 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700696 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800697 mDiag->error(DiagMessage(outResource->source)
698 << "<" << parser->getElementName() << "> must have a 'type' attribute");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700699 return false;
700 }
701
702 const ResourceType* parsedType = parseResourceType(maybeType.value());
703 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800704 mDiag->error(DiagMessage(outResource->source)
705 << "invalid resource type '" << maybeType.value()
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700706 << "' in <" << parser->getElementName() << ">");
707 return false;
708 }
709
710 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700711 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800712}
713
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800714bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
715 if (parseSymbolImpl(parser, outResource)) {
716 outResource->symbolState = SymbolState::kPrivate;
717 return true;
718 }
719 return false;
720}
721
722bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
723 if (parseSymbolImpl(parser, outResource)) {
724 outResource->symbolState = SymbolState::kUndefined;
725 return true;
726 }
727 return false;
728}
729
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800730
Adam Lesinski467f1712015-11-16 17:35:44 -0800731bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700732 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800733}
734
Adam Lesinski467f1712015-11-16 17:35:44 -0800735bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
736 bool weak) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800737 outResource->name.type = ResourceType::kAttr;
738
Adam Lesinski52364f72016-01-11 13:10:24 -0800739 // Attributes only end up in default configuration.
740 if (outResource->config != ConfigDescription::defaultConfig()) {
741 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
742 << outResource->config << "' for attribute " << outResource->name);
743 outResource->config = ConfigDescription::defaultConfig();
744 }
745
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800746 uint32_t typeMask = 0;
747
Adam Lesinski467f1712015-11-16 17:35:44 -0800748 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700749 if (maybeFormat) {
750 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800751 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700752 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
753 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700754 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800755 }
756 }
757
Adam Lesinskia5870652015-11-20 15:32:30 -0800758 Maybe<int32_t> maybeMin, maybeMax;
759
760 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
761 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
762 if (!minStr.empty()) {
763 android::Res_value value;
764 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
765 maybeMin = static_cast<int32_t>(value.data);
766 }
767 }
768
769 if (!maybeMin) {
770 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
771 << "invalid 'min' value '" << minStr << "'");
772 return false;
773 }
774 }
775
776 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
777 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
778 if (!maxStr.empty()) {
779 android::Res_value value;
780 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
781 maybeMax = static_cast<int32_t>(value.data);
782 }
783 }
784
785 if (!maybeMax) {
786 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
787 << "invalid 'max' value '" << maxStr << "'");
788 return false;
789 }
790 }
791
792 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
793 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
794 << "'min' and 'max' can only be used when format='integer'");
795 return false;
796 }
797
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800798 struct SymbolComparator {
799 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
800 return a.symbol.name.value() < b.symbol.name.value();
801 }
802 };
803
804 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800805
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700806 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800807 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700808 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800809 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
810 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700811 comment = util::trimWhitespace(parser->getComment()).toString();
812 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800813 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700814 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800815 continue;
816 }
817
Adam Lesinskica5638f2015-10-21 14:42:43 -0700818 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700819 const std::u16string& elementNamespace = parser->getElementNamespace();
820 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700821 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700822 if (elementName == u"enum") {
823 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700824 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700825 << "can not define an <enum>; already defined a <flag>");
826 error = true;
827 continue;
828 }
829 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700830
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700831 } else if (elementName == u"flag") {
832 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700833 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700834 << "can not define a <flag>; already defined an <enum>");
835 error = true;
836 continue;
837 }
838 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800839 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800840
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700841 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800842 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700843 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800844 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700845 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700846 childResource.value = util::make_unique<Id>();
847 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700848
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800849 symbol.symbol.setComment(std::move(comment));
850 symbol.symbol.setSource(itemSource);
851
852 auto insertResult = items.insert(std::move(symbol));
853 if (!insertResult.second) {
854 const Attribute::Symbol& existingSymbol = *insertResult.first;
855 mDiag->error(DiagMessage(itemSource)
856 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
857 << "'");
858
859 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
860 << "first defined here");
861 error = true;
862 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800863 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700864 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800865 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700866 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
867 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868 error = true;
869 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700870
871 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800872 }
873
874 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700875 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800876 }
877
878 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800879 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700880 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800881 if (maybeMin) {
882 attr->minInt = maybeMin.value();
883 }
884
885 if (maybeMax) {
886 attr->maxInt = maybeMax.value();
887 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700888 outResource->value = std::move(attr);
889 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800890}
891
Adam Lesinski467f1712015-11-16 17:35:44 -0800892Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700893 const StringPiece16& tag) {
894 const Source source = mSource.withLine(parser->getLineNumber());
895
Adam Lesinski467f1712015-11-16 17:35:44 -0800896 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700897 if (!maybeName) {
898 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
899 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800900 }
901
Adam Lesinski467f1712015-11-16 17:35:44 -0800902 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700903 if (!maybeValue) {
904 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
905 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906 }
907
908 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700909 if (!android::ResTable::stringToInt(maybeValue.value().data(),
910 maybeValue.value().size(), &val)) {
911 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
912 << "' for <" << tag << ">; must be an integer");
913 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800914 }
915
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700916 return Attribute::Symbol{
Adam Lesinski52364f72016-01-11 13:10:24 -0800917 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())), val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800918}
919
Adam Lesinski467f1712015-11-16 17:35:44 -0800920static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800921 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800922 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800923 const char16_t* const end = start + str.size();
924 const char16_t* p = start;
925
Adam Lesinski467f1712015-11-16 17:35:44 -0800926 Reference ref;
927 if (p != end && *p == u'*') {
928 ref.privateReference = true;
929 start++;
930 p++;
931 }
932
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800933 StringPiece16 package;
934 StringPiece16 name;
935 while (p != end) {
936 if (*p == u':') {
937 package = StringPiece16(start, p - start);
938 name = StringPiece16(p + 1, end - (p + 1));
939 break;
940 }
941 p++;
942 }
943
Adam Lesinski467f1712015-11-16 17:35:44 -0800944 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700945 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800946 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800947}
948
Adam Lesinski467f1712015-11-16 17:35:44 -0800949bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700950 const Source source = mSource.withLine(parser->getLineNumber());
951
Adam Lesinski467f1712015-11-16 17:35:44 -0800952 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700953 if (!maybeName) {
954 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800955 return false;
956 }
957
Adam Lesinski467f1712015-11-16 17:35:44 -0800958 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700959 if (!maybeKey) {
960 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800961 return false;
962 }
963
Adam Lesinski467f1712015-11-16 17:35:44 -0800964 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800965 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800966
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800967 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
968 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700969 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800970 return false;
971 }
972
Adam Lesinski467f1712015-11-16 17:35:44 -0800973 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800974 return true;
975}
976
Adam Lesinski467f1712015-11-16 17:35:44 -0800977bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800978 outResource->name.type = ResourceType::kStyle;
979
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700980 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800981
Adam Lesinski467f1712015-11-16 17:35:44 -0800982 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700983 if (maybeParent) {
984 // If the parent is empty, we don't have a parent, but we also don't infer either.
985 if (!maybeParent.value().empty()) {
986 std::string errStr;
987 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
988 if (!style->parent) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800989 mDiag->error(DiagMessage(outResource->source) << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700990 return false;
991 }
992
Adam Lesinski467f1712015-11-16 17:35:44 -0800993 // Transform the namespace prefix to the actual package name, and mark the reference as
994 // private if appropriate.
995 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800996 }
997
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700998 } else {
999 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001000 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001001 size_t pos = styleName.find_last_of(u'.');
1002 if (pos != std::string::npos) {
1003 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -08001004 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
1005 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001006 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001007 }
1008
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001009 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001010 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001011 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1012 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001013 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001014 continue;
1015 }
1016
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001017 const std::u16string& elementNamespace = parser->getElementNamespace();
1018 const std::u16string& elementName = parser->getElementName();
1019 if (elementNamespace == u"" && elementName == u"item") {
1020 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001021
Adam Lesinskica5638f2015-10-21 14:42:43 -07001022 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001023 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1024 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001025 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001026 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001027 }
1028
1029 if (error) {
1030 return false;
1031 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001032
1033 outResource->value = std::move(style);
1034 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001035}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001036
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001037bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1038 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_ANY);
1039}
1040
1041bool ResourceParser::parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1042 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_INTEGER);
1043}
1044
1045bool ResourceParser::parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1046 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_STRING);
1047}
1048
1049bool ResourceParser::parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
1050 const uint32_t typeMask) {
1051 outResource->name.type = ResourceType::kArray;
1052
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001053 std::unique_ptr<Array> array = util::make_unique<Array>();
1054
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001055 bool error = false;
1056 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001057 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1058 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001059 // Skip text and comments.
1060 continue;
1061 }
1062
1063 const Source itemSource = mSource.withLine(parser->getLineNumber());
1064 const std::u16string& elementNamespace = parser->getElementNamespace();
1065 const std::u16string& elementName = parser->getElementName();
1066 if (elementNamespace.empty() && elementName == u"item") {
1067 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1068 if (!item) {
1069 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1070 error = true;
1071 continue;
1072 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001073 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001074 array->items.emplace_back(std::move(item));
1075
Adam Lesinskica5638f2015-10-21 14:42:43 -07001076 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001077 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1078 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1079 error = true;
1080 }
1081 }
1082
1083 if (error) {
1084 return false;
1085 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001086
1087 outResource->value = std::move(array);
1088 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001089}
1090
Adam Lesinski467f1712015-11-16 17:35:44 -08001091bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001092 outResource->name.type = ResourceType::kPlurals;
1093
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001094 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1095
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001096 bool error = false;
1097 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001098 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1099 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001100 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001101 continue;
1102 }
1103
Adam Lesinskica5638f2015-10-21 14:42:43 -07001104 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001105 const std::u16string& elementNamespace = parser->getElementNamespace();
1106 const std::u16string& elementName = parser->getElementName();
1107 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001108 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1109 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001110 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001111 << "'quantity'");
1112 error = true;
1113 continue;
1114 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001115
Adam Lesinski467f1712015-11-16 17:35:44 -08001116 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001117 size_t index = 0;
1118 if (trimmedQuantity == u"zero") {
1119 index = Plural::Zero;
1120 } else if (trimmedQuantity == u"one") {
1121 index = Plural::One;
1122 } else if (trimmedQuantity == u"two") {
1123 index = Plural::Two;
1124 } else if (trimmedQuantity == u"few") {
1125 index = Plural::Few;
1126 } else if (trimmedQuantity == u"many") {
1127 index = Plural::Many;
1128 } else if (trimmedQuantity == u"other") {
1129 index = Plural::Other;
1130 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001131 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001132 << "<item> in <plural> has invalid value '" << trimmedQuantity
1133 << "' for attribute 'quantity'");
1134 error = true;
1135 continue;
1136 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001137
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001138 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001139 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001140 << "duplicate quantity '" << trimmedQuantity << "'");
1141 error = true;
1142 continue;
1143 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001144
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001145 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1146 kNoRawString))) {
1147 error = true;
1148 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001149 plural->values[index]->setSource(itemSource);
1150
1151 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1152 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001153 << elementName << ">");
1154 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001155 }
1156 }
1157
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001158 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001159 return false;
1160 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001161
1162 outResource->value = std::move(plural);
1163 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001164}
1165
Adam Lesinski52364f72016-01-11 13:10:24 -08001166bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser,
1167 ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001168 outResource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001169
Adam Lesinskib274e352015-11-06 15:14:35 -08001170 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001171 outResource->symbolState = SymbolState::kPublic;
1172
Adam Lesinski52364f72016-01-11 13:10:24 -08001173 // Declare-styleable only ends up in default config;
1174 if (outResource->config != ConfigDescription::defaultConfig()) {
1175 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
1176 << outResource->config << "' for styleable "
1177 << outResource->name.entry);
1178 outResource->config = ConfigDescription::defaultConfig();
1179 }
1180
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001181 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1182
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001183 std::u16string comment;
1184 bool error = false;
1185 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001186 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1187 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001188 comment = util::trimWhitespace(parser->getComment()).toString();
1189 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001190 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001191 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001192 continue;
1193 }
1194
Adam Lesinskica5638f2015-10-21 14:42:43 -07001195 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001196 const std::u16string& elementNamespace = parser->getElementNamespace();
1197 const std::u16string& elementName = parser->getElementName();
1198 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001199 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1200 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001201 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001202 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001203 continue;
1204 }
1205
Adam Lesinski467f1712015-11-16 17:35:44 -08001206 // If this is a declaration, the package name may be in the name. Separate these out.
1207 // Eg. <attr name="android:text" />
1208 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1209 if (!maybeRef) {
1210 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1211 << maybeName.value() << "'");
1212 error = true;
1213 continue;
1214 }
1215
1216 Reference& childRef = maybeRef.value();
1217 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1218
Adam Lesinskica5638f2015-10-21 14:42:43 -07001219 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001220 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001221 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001222 childResource.source = itemSource;
1223 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001224
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001225 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001226 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001227 continue;
1228 }
1229
Adam Lesinskica5638f2015-10-21 14:42:43 -07001230 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001231 childRef.setComment(childResource.comment);
1232 childRef.setSource(itemSource);
1233 styleable->entries.push_back(std::move(childRef));
1234
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001235 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001236
Adam Lesinskica5638f2015-10-21 14:42:43 -07001237 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1238 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001239 << elementName << ">");
1240 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001241 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001242
1243 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001244 }
1245
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001246 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001247 return false;
1248 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001249
1250 outResource->value = std::move(styleable);
1251 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001252}
1253
1254} // namespace aapt