blob: a84c306e2733eded59e4225b69320ebc2eebaf9e [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 Lesinski7656554f2016-03-10 21:55:04 -080084 StringPiece16 trimmedComment = util::trimWhitespace(res->comment);
85 if (trimmedComment.size() != res->comment.size()) {
86 // Only if there was a change do we re-assign.
87 res->comment = trimmedComment.toString();
88 }
89
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090 if (res->symbolState) {
91 Symbol symbol;
92 symbol.state = res->symbolState.value();
93 symbol.source = res->source;
94 symbol.comment = res->comment;
95 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
96 return false;
97 }
98 }
99
100 if (res->value) {
101 // Attach the comment, source and config to the value.
102 res->value->setComment(std::move(res->comment));
103 res->value->setSource(std::move(res->source));
104
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800105 if (!table->addResource(res->name, res->id, res->config, res->product,
106 std::move(res->value), diag)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800107 return false;
108 }
109 }
110
111 bool error = false;
112 for (ParsedResource& child : res->childResources) {
Adam Lesinski52364f72016-01-11 13:10:24 -0800113 error |= !addResourcesToTable(table, diag, &child);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800114 }
115 return !error;
116}
117
118// Convenient aliases for more readable function calls.
119enum {
120 kAllowRawString = true,
121 kNoRawString = false
122};
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700125 const ConfigDescription& config,
126 const ResourceParserOptions& options) :
127 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128}
129
130/**
131 * Build a string from XML that converts nested elements into Span objects.
132 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800133bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134 StyleString* outStyleString) {
135 std::vector<Span> spanStack;
136
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800137 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138 outRawString->clear();
139 outStyleString->spans.clear();
140 util::StringBuilder builder;
141 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -0800142 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
143 const xml::XmlPullParser::Event event = parser->getEvent();
144 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145 if (!parser->getElementNamespace().empty()) {
146 // We already warned and skipped the start element, so just skip here too
147 continue;
148 }
149
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150 depth--;
151 if (depth == 0) {
152 break;
153 }
154
Adam Lesinski458b8772016-04-25 14:20:21 -0700155 spanStack.back().lastChar = builder.str().size() - 1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156 outStyleString->spans.push_back(spanStack.back());
157 spanStack.pop_back();
158
Adam Lesinski467f1712015-11-16 17:35:44 -0800159 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160 outRawString->append(parser->getText());
161 builder.append(parser->getText());
162
Adam Lesinski467f1712015-11-16 17:35:44 -0800163 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164 if (!parser->getElementNamespace().empty()) {
165 if (parser->getElementNamespace() != sXliffNamespaceUri) {
166 // Only warn if this isn't an xliff namespace.
167 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
168 << "skipping element '"
169 << parser->getElementName()
170 << "' with unknown namespace '"
171 << parser->getElementNamespace()
172 << "'");
173 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174 continue;
175 }
176 depth++;
177
178 // Build a span object out of the nested element.
179 std::u16string spanName = parser->getElementName();
180 const auto endAttrIter = parser->endAttributes();
181 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
182 spanName += u";";
183 spanName += attrIter->name;
184 spanName += u"=";
185 spanName += attrIter->value;
186 }
187
188 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
190 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800191 error = true;
192 } else {
193 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinski467f1712015-11-16 17:35:44 -0800196 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197 // Skip
198 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200 }
201 }
202 assert(spanStack.empty() && "spans haven't been fully processed");
203
204 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800205 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206}
207
Adam Lesinski467f1712015-11-16 17:35:44 -0800208bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209 bool error = false;
210 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800211 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
212 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214 continue;
215 }
216
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
218 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
219 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220 return false;
221 }
222
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223 error |= !parseResources(parser);
224 break;
225 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinski467f1712015-11-16 17:35:44 -0800227 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700228 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
229 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230 return false;
231 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233}
234
Adam Lesinski467f1712015-11-16 17:35:44 -0800235bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700236 std::set<ResourceName> strippedResources;
237
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800241 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
242 const xml::XmlPullParser::Event event = parser->getEvent();
243 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244 comment = parser->getComment();
245 continue;
246 }
247
Adam Lesinski467f1712015-11-16 17:35:44 -0800248 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
251 << "plain text not allowed here");
252 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253 }
254 continue;
255 }
256
Adam Lesinski467f1712015-11-16 17:35:44 -0800257 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260 // Skip unknown namespace.
261 continue;
262 }
263
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264 std::u16string elementName = parser->getElementName();
265 if (elementName == u"skip" || elementName == u"eat-comment") {
266 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267 continue;
268 }
269
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700270 ParsedResource parsedResource;
Adam Lesinski52364f72016-01-11 13:10:24 -0800271 parsedResource.config = mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700272 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700273 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinski7751afc2016-01-06 15:45:28 -0800275 // Extract the product name if it exists.
Adam Lesinski7751afc2016-01-06 15:45:28 -0800276 if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) {
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800277 parsedResource.product = util::utf16ToUtf8(maybeProduct.value());
Adam Lesinski7751afc2016-01-06 15:45:28 -0800278 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800279
Adam Lesinski7751afc2016-01-06 15:45:28 -0800280 // Parse the resource regardless of product.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800281 if (!parseResource(parser, &parsedResource)) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800282 error = true;
283 continue;
284 }
285
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800286 if (!addResourcesToTable(mTable, mDiag, &parsedResource)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700287 error = true;
288 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700290
291 // Check that we included at least one variant of each stripped resource.
292 for (const ResourceName& strippedResource : strippedResources) {
293 if (!mTable->findResource(strippedResource)) {
294 // Failed to find the resource.
295 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
296 "was filtered out but no product variant remains");
297 error = true;
298 }
299 }
300
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302}
303
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800304
305bool ResourceParser::parseResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
306 struct ItemTypeFormat {
307 ResourceType type;
308 uint32_t format;
309 };
310
311 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, ParsedResource*)>;
312
313 static const auto elToItemMap = ImmutableMap<std::u16string, ItemTypeFormat>::createPreSorted({
314 { u"bool", { ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN } },
315 { u"color", { ResourceType::kColor, android::ResTable_map::TYPE_COLOR } },
316 { u"dimen", { ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT
317 | android::ResTable_map::TYPE_FRACTION
318 | android::ResTable_map::TYPE_DIMENSION } },
319 { u"drawable", { ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR } },
320 { u"fraction", { ResourceType::kFraction, android::ResTable_map::TYPE_FLOAT
321 | android::ResTable_map::TYPE_FRACTION
322 | android::ResTable_map::TYPE_DIMENSION } },
323 { u"integer", { ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER } },
324 { u"string", { ResourceType::kString, android::ResTable_map::TYPE_STRING } },
325 });
326
327 static const auto elToBagMap = ImmutableMap<std::u16string, BagParseFunc>::createPreSorted({
328 { u"add-resource", std::mem_fn(&ResourceParser::parseAddResource) },
329 { u"array", std::mem_fn(&ResourceParser::parseArray) },
330 { u"attr", std::mem_fn(&ResourceParser::parseAttr) },
331 { u"declare-styleable", std::mem_fn(&ResourceParser::parseDeclareStyleable) },
332 { u"integer-array", std::mem_fn(&ResourceParser::parseIntegerArray) },
333 { u"java-symbol", std::mem_fn(&ResourceParser::parseSymbol) },
334 { u"plurals", std::mem_fn(&ResourceParser::parsePlural) },
335 { u"public", std::mem_fn(&ResourceParser::parsePublic) },
336 { u"public-group", std::mem_fn(&ResourceParser::parsePublicGroup) },
337 { u"string-array", std::mem_fn(&ResourceParser::parseStringArray) },
338 { u"style", std::mem_fn(&ResourceParser::parseStyle) },
339 { u"symbol", std::mem_fn(&ResourceParser::parseSymbol) },
340 });
341
342 std::u16string resourceType = parser->getElementName();
343
344 // The value format accepted for this resource.
345 uint32_t resourceFormat = 0u;
346
347 if (resourceType == u"item") {
348 // Items have their type encoded in the type attribute.
349 if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) {
350 resourceType = maybeType.value().toString();
351 } else {
352 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
353 << "<item> must have a 'type' attribute");
354 return false;
355 }
356
357 if (Maybe<StringPiece16> maybeFormat = xml::findNonEmptyAttribute(parser, u"format")) {
358 // An explicit format for this resource was specified. The resource will retain
359 // its type in its name, but the accepted value for this type is overridden.
360 resourceFormat = parseFormatType(maybeFormat.value());
361 if (!resourceFormat) {
362 mDiag->error(DiagMessage(outResource->source)
363 << "'" << maybeFormat.value() << "' is an invalid format");
364 return false;
365 }
366 }
367 }
368
369 // Get the name of the resource. This will be checked later, because not all
370 // XML elements require a name.
371 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
372
373 if (resourceType == u"id") {
374 if (!maybeName) {
375 mDiag->error(DiagMessage(outResource->source)
376 << "<" << parser->getElementName() << "> missing 'name' attribute");
377 return false;
378 }
379
380 outResource->name.type = ResourceType::kId;
381 outResource->name.entry = maybeName.value().toString();
382 outResource->value = util::make_unique<Id>();
383 return true;
384 }
385
386 const auto itemIter = elToItemMap.find(resourceType);
387 if (itemIter != elToItemMap.end()) {
388 // This is an item, record its type and format and start parsing.
389
390 if (!maybeName) {
391 mDiag->error(DiagMessage(outResource->source)
392 << "<" << parser->getElementName() << "> missing 'name' attribute");
393 return false;
394 }
395
396 outResource->name.type = itemIter->second.type;
397 outResource->name.entry = maybeName.value().toString();
398
399 // Only use the implicit format for this type if it wasn't overridden.
400 if (!resourceFormat) {
401 resourceFormat = itemIter->second.format;
402 }
403
404 if (!parseItem(parser, outResource, resourceFormat)) {
405 return false;
406 }
407 return true;
408 }
409
410 // This might be a bag or something.
411 const auto bagIter = elToBagMap.find(resourceType);
412 if (bagIter != elToBagMap.end()) {
413 // Ensure we have a name (unless this is a <public-group>).
414 if (resourceType != u"public-group") {
415 if (!maybeName) {
416 mDiag->error(DiagMessage(outResource->source)
417 << "<" << parser->getElementName() << "> missing 'name' attribute");
418 return false;
419 }
420
421 outResource->name.entry = maybeName.value().toString();
422 }
423
424 // Call the associated parse method. The type will be filled in by the
425 // parse func.
426 if (!bagIter->second(this, parser, outResource)) {
427 return false;
428 }
429 return true;
430 }
431
432 // Try parsing the elementName (or type) as a resource. These shall only be
433 // resources like 'layout' or 'xml' and they can only be references.
434 const ResourceType* parsedType = parseResourceType(resourceType);
435 if (parsedType) {
436 if (!maybeName) {
437 mDiag->error(DiagMessage(outResource->source)
438 << "<" << parser->getElementName() << "> missing 'name' attribute");
439 return false;
440 }
441
442 outResource->name.type = *parsedType;
443 outResource->name.entry = maybeName.value().toString();
444 outResource->value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
445 if (!outResource->value) {
446 mDiag->error(DiagMessage(outResource->source)
447 << "invalid value for type '" << *parsedType << "'. Expected a reference");
448 return false;
449 }
450 return true;
451 }
452
453 mDiag->warn(DiagMessage(outResource->source)
454 << "unknown resource type '" << parser->getElementName() << "'");
455 return false;
456}
457
458bool ResourceParser::parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
459 const uint32_t format) {
460 if (format == android::ResTable_map::TYPE_STRING) {
461 return parseString(parser, outResource);
462 }
463
464 outResource->value = parseXml(parser, format, kNoRawString);
465 if (!outResource->value) {
466 mDiag->error(DiagMessage(outResource->source) << "invalid " << outResource->name.type);
467 return false;
468 }
469 return true;
470}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800471
472/**
473 * Reads the entire XML subtree and attempts to parse it as some Item,
474 * with typeMask denoting which items it can be. If allowRawValue is
475 * true, a RawString is returned if the XML couldn't be parsed as
476 * an Item. If allowRawValue is false, nullptr is returned in this
477 * case.
478 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800479std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700480 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481 const size_t beginXmlLine = parser->getLineNumber();
482
483 std::u16string rawValue;
484 StyleString styleString;
485 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
486 return {};
487 }
488
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800489 if (!styleString.spans.empty()) {
490 // This can only be a StyledString.
491 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493 }
494
495 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700496 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700497 std::unique_ptr<Id> id = util::make_unique<Id>();
498 id->setSource(mSource.withLine(beginXmlLine));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800499 mTable->addResource(name, {}, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500 };
501
502 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
504 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700506 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800508 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700509 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510 return processedItem;
511 }
512
513 // Try making a regular string.
514 if (typeMask & android::ResTable_map::TYPE_STRING) {
515 // Use the trimmed, escaped string.
516 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518 }
519
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700521 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700523 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524 }
525 return {};
526}
527
Adam Lesinski467f1712015-11-16 17:35:44 -0800528bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800529 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800530 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800531 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800532 mDiag->error(DiagMessage(outResource->source)
533 << "invalid value for 'formatted'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800534 return false;
535 }
536 }
537
Adam Lesinski9f222042015-11-04 13:51:45 -0800538 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800539 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800540 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800541 mDiag->error(DiagMessage(outResource->source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800542 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800543 return false;
544 }
545 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700547 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
548 if (!outResource->value) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800549 mDiag->error(DiagMessage(outResource->source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800550 return false;
551 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800552
Adam Lesinski393b5f02015-12-17 13:03:11 -0800553 if (String* stringValue = valueCast<String>(outResource->value.get())) {
554 stringValue->setTranslateable(translateable);
555
556 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800557 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski979ccb22016-01-11 10:42:19 -0800558 DiagMessage msg(outResource->source);
559 msg << "multiple substitutions specified in non-positional format; "
560 "did you mean to add the formatted=\"false\" attribute?";
561 if (mOptions.errorOnPositionalArguments) {
562 mDiag->error(msg);
563 return false;
564 }
565
566 mDiag->warn(msg);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800567 }
568 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800569
570 } else if (StyledString* stringValue = valueCast<StyledString>(outResource->value.get())) {
571 stringValue->setTranslateable(translateable);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800572 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700573 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574}
575
Adam Lesinski467f1712015-11-16 17:35:44 -0800576bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800577 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800579 mDiag->error(DiagMessage(outResource->source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800580 return false;
581 }
582
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700583 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800585 mDiag->error(DiagMessage(outResource->source)
586 << "invalid resource type '" << maybeType.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587 return false;
588 }
589
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700590 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800591
Adam Lesinski467f1712015-11-16 17:35:44 -0800592 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800593 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700594 bool result = android::ResTable::stringToInt(maybeId.value().data(),
595 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700596 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597 if (!result || !resourceId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800598 mDiag->error(DiagMessage(outResource->source)
599 << "invalid resource ID '" << maybeId.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800600 return false;
601 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700602 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800603 }
604
605 if (*parsedType == ResourceType::kId) {
606 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700607 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700609
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700610 outResource->symbolState = SymbolState::kPublic;
611 return true;
612}
613
Adam Lesinski467f1712015-11-16 17:35:44 -0800614bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800615 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800616 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800617 mDiag->error(DiagMessage(outResource->source)
618 << "<public-group> must have a 'type' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800619 return false;
620 }
621
622 const ResourceType* parsedType = parseResourceType(maybeType.value());
623 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800624 mDiag->error(DiagMessage(outResource->source)
625 << "invalid resource type '" << maybeType.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800626 return false;
627 }
628
Adam Lesinski467f1712015-11-16 17:35:44 -0800629 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800630 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800631 mDiag->error(DiagMessage(outResource->source)
632 << "<public-group> must have a 'first-id' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800633 return false;
634 }
635
636 android::Res_value val;
637 bool result = android::ResTable::stringToInt(maybeId.value().data(),
638 maybeId.value().size(), &val);
639 ResourceId nextId(val.data);
640 if (!result || !nextId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800641 mDiag->error(DiagMessage(outResource->source)
642 << "invalid resource ID '" << maybeId.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800643 return false;
644 }
645
646 std::u16string comment;
647 bool error = false;
648 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800649 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
650 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800651 comment = util::trimWhitespace(parser->getComment()).toString();
652 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800653 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800654 // Skip text.
655 continue;
656 }
657
658 const Source itemSource = mSource.withLine(parser->getLineNumber());
659 const std::u16string& elementNamespace = parser->getElementNamespace();
660 const std::u16string& elementName = parser->getElementName();
661 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800662 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800663 if (!maybeName) {
664 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
665 error = true;
666 continue;
667 }
668
Adam Lesinski467f1712015-11-16 17:35:44 -0800669 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800670 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
671 error = true;
672 continue;
673 }
674
Adam Lesinski467f1712015-11-16 17:35:44 -0800675 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800676 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
677 error = true;
678 continue;
679 }
680
681 ParsedResource childResource;
682 childResource.name.type = *parsedType;
683 childResource.name.entry = maybeName.value().toString();
684 childResource.id = nextId;
685 childResource.comment = std::move(comment);
686 childResource.source = itemSource;
687 childResource.symbolState = SymbolState::kPublic;
688 outResource->childResources.push_back(std::move(childResource));
689
690 nextId.id += 1;
691
692 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
693 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
694 error = true;
695 }
696 }
697 return !error;
698}
699
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800700bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800701 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700702 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800703 mDiag->error(DiagMessage(outResource->source)
704 << "<" << parser->getElementName() << "> must have a 'type' attribute");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700705 return false;
706 }
707
708 const ResourceType* parsedType = parseResourceType(maybeType.value());
709 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800710 mDiag->error(DiagMessage(outResource->source)
711 << "invalid resource type '" << maybeType.value()
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700712 << "' in <" << parser->getElementName() << ">");
713 return false;
714 }
715
716 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700717 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800718}
719
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800720bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
721 if (parseSymbolImpl(parser, outResource)) {
722 outResource->symbolState = SymbolState::kPrivate;
723 return true;
724 }
725 return false;
726}
727
728bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
729 if (parseSymbolImpl(parser, outResource)) {
730 outResource->symbolState = SymbolState::kUndefined;
731 return true;
732 }
733 return false;
734}
735
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800736
Adam Lesinski467f1712015-11-16 17:35:44 -0800737bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700738 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800739}
740
Adam Lesinski467f1712015-11-16 17:35:44 -0800741bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
742 bool weak) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800743 outResource->name.type = ResourceType::kAttr;
744
Adam Lesinski52364f72016-01-11 13:10:24 -0800745 // Attributes only end up in default configuration.
746 if (outResource->config != ConfigDescription::defaultConfig()) {
747 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
748 << outResource->config << "' for attribute " << outResource->name);
749 outResource->config = ConfigDescription::defaultConfig();
750 }
751
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800752 uint32_t typeMask = 0;
753
Adam Lesinski467f1712015-11-16 17:35:44 -0800754 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 if (maybeFormat) {
756 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700758 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
759 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700760 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800761 }
762 }
763
Adam Lesinskia5870652015-11-20 15:32:30 -0800764 Maybe<int32_t> maybeMin, maybeMax;
765
766 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
767 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
768 if (!minStr.empty()) {
769 android::Res_value value;
770 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
771 maybeMin = static_cast<int32_t>(value.data);
772 }
773 }
774
775 if (!maybeMin) {
776 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
777 << "invalid 'min' value '" << minStr << "'");
778 return false;
779 }
780 }
781
782 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
783 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
784 if (!maxStr.empty()) {
785 android::Res_value value;
786 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
787 maybeMax = static_cast<int32_t>(value.data);
788 }
789 }
790
791 if (!maybeMax) {
792 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
793 << "invalid 'max' value '" << maxStr << "'");
794 return false;
795 }
796 }
797
798 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
799 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
800 << "'min' and 'max' can only be used when format='integer'");
801 return false;
802 }
803
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800804 struct SymbolComparator {
805 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
806 return a.symbol.name.value() < b.symbol.name.value();
807 }
808 };
809
810 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800811
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700812 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800813 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700814 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800815 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
816 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700817 comment = util::trimWhitespace(parser->getComment()).toString();
818 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800819 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700820 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800821 continue;
822 }
823
Adam Lesinskica5638f2015-10-21 14:42:43 -0700824 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700825 const std::u16string& elementNamespace = parser->getElementNamespace();
826 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700827 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700828 if (elementName == u"enum") {
829 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700830 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700831 << "can not define an <enum>; already defined a <flag>");
832 error = true;
833 continue;
834 }
835 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700836
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700837 } else if (elementName == u"flag") {
838 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700839 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700840 << "can not define a <flag>; already defined an <enum>");
841 error = true;
842 continue;
843 }
844 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800845 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800846
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700847 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800848 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700849 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800850 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700851 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700852 childResource.value = util::make_unique<Id>();
853 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700854
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800855 symbol.symbol.setComment(std::move(comment));
856 symbol.symbol.setSource(itemSource);
857
858 auto insertResult = items.insert(std::move(symbol));
859 if (!insertResult.second) {
860 const Attribute::Symbol& existingSymbol = *insertResult.first;
861 mDiag->error(DiagMessage(itemSource)
862 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
863 << "'");
864
865 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
866 << "first defined here");
867 error = true;
868 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800869 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700870 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800871 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700872 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
873 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800874 error = true;
875 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700876
877 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800878 }
879
880 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700881 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800882 }
883
884 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800885 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700886 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800887 if (maybeMin) {
888 attr->minInt = maybeMin.value();
889 }
890
891 if (maybeMax) {
892 attr->maxInt = maybeMax.value();
893 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700894 outResource->value = std::move(attr);
895 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800896}
897
Adam Lesinski467f1712015-11-16 17:35:44 -0800898Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700899 const StringPiece16& tag) {
900 const Source source = mSource.withLine(parser->getLineNumber());
901
Adam Lesinski467f1712015-11-16 17:35:44 -0800902 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700903 if (!maybeName) {
904 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
905 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906 }
907
Adam Lesinski467f1712015-11-16 17:35:44 -0800908 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700909 if (!maybeValue) {
910 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
911 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800912 }
913
914 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700915 if (!android::ResTable::stringToInt(maybeValue.value().data(),
916 maybeValue.value().size(), &val)) {
917 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
918 << "' for <" << tag << ">; must be an integer");
919 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800920 }
921
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700922 return Attribute::Symbol{
Adam Lesinski52364f72016-01-11 13:10:24 -0800923 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())), val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800924}
925
Adam Lesinski467f1712015-11-16 17:35:44 -0800926static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800927 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800928 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800929 const char16_t* const end = start + str.size();
930 const char16_t* p = start;
931
Adam Lesinski467f1712015-11-16 17:35:44 -0800932 Reference ref;
933 if (p != end && *p == u'*') {
934 ref.privateReference = true;
935 start++;
936 p++;
937 }
938
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800939 StringPiece16 package;
940 StringPiece16 name;
941 while (p != end) {
942 if (*p == u':') {
943 package = StringPiece16(start, p - start);
944 name = StringPiece16(p + 1, end - (p + 1));
945 break;
946 }
947 p++;
948 }
949
Adam Lesinski467f1712015-11-16 17:35:44 -0800950 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700951 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800952 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800953}
954
Adam Lesinski467f1712015-11-16 17:35:44 -0800955bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700956 const Source source = mSource.withLine(parser->getLineNumber());
957
Adam Lesinski467f1712015-11-16 17:35:44 -0800958 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700959 if (!maybeName) {
960 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800961 return false;
962 }
963
Adam Lesinski467f1712015-11-16 17:35:44 -0800964 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700965 if (!maybeKey) {
966 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800967 return false;
968 }
969
Adam Lesinski467f1712015-11-16 17:35:44 -0800970 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800971 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800972
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800973 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
974 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700975 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800976 return false;
977 }
978
Adam Lesinski467f1712015-11-16 17:35:44 -0800979 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800980 return true;
981}
982
Adam Lesinski467f1712015-11-16 17:35:44 -0800983bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800984 outResource->name.type = ResourceType::kStyle;
985
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700986 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800987
Adam Lesinski467f1712015-11-16 17:35:44 -0800988 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700989 if (maybeParent) {
990 // If the parent is empty, we don't have a parent, but we also don't infer either.
991 if (!maybeParent.value().empty()) {
992 std::string errStr;
993 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
994 if (!style->parent) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800995 mDiag->error(DiagMessage(outResource->source) << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700996 return false;
997 }
998
Adam Lesinski467f1712015-11-16 17:35:44 -0800999 // Transform the namespace prefix to the actual package name, and mark the reference as
1000 // private if appropriate.
1001 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001002 }
1003
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001004 } else {
1005 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001006 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001007 size_t pos = styleName.find_last_of(u'.');
1008 if (pos != std::string::npos) {
1009 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -08001010 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
1011 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001012 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001013 }
1014
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001015 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001016 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001017 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1018 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001019 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001020 continue;
1021 }
1022
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001023 const std::u16string& elementNamespace = parser->getElementNamespace();
1024 const std::u16string& elementName = parser->getElementName();
1025 if (elementNamespace == u"" && elementName == u"item") {
1026 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001027
Adam Lesinskica5638f2015-10-21 14:42:43 -07001028 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001029 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1030 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001031 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001032 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001033 }
1034
1035 if (error) {
1036 return false;
1037 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001038
1039 outResource->value = std::move(style);
1040 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001041}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001042
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001043bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1044 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_ANY);
1045}
1046
1047bool ResourceParser::parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1048 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_INTEGER);
1049}
1050
1051bool ResourceParser::parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1052 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_STRING);
1053}
1054
1055bool ResourceParser::parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
1056 const uint32_t typeMask) {
1057 outResource->name.type = ResourceType::kArray;
1058
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001059 std::unique_ptr<Array> array = util::make_unique<Array>();
1060
Adam Lesinski458b8772016-04-25 14:20:21 -07001061 bool translateable = mOptions.translatable;
1062 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
1063 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
1064 mDiag->error(DiagMessage(outResource->source)
1065 << "invalid value for 'translatable'. Must be a boolean");
1066 return false;
1067 }
1068 }
1069 array->setTranslateable(translateable);
1070
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001071 bool error = false;
1072 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001073 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1074 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001075 // Skip text and comments.
1076 continue;
1077 }
1078
1079 const Source itemSource = mSource.withLine(parser->getLineNumber());
1080 const std::u16string& elementNamespace = parser->getElementNamespace();
1081 const std::u16string& elementName = parser->getElementName();
1082 if (elementNamespace.empty() && elementName == u"item") {
1083 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1084 if (!item) {
1085 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1086 error = true;
1087 continue;
1088 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001089 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001090 array->items.emplace_back(std::move(item));
1091
Adam Lesinskica5638f2015-10-21 14:42:43 -07001092 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001093 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1094 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1095 error = true;
1096 }
1097 }
1098
1099 if (error) {
1100 return false;
1101 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001102
1103 outResource->value = std::move(array);
1104 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001105}
1106
Adam Lesinski467f1712015-11-16 17:35:44 -08001107bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001108 outResource->name.type = ResourceType::kPlurals;
1109
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001110 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1111
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001112 bool error = false;
1113 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001114 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1115 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001116 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001117 continue;
1118 }
1119
Adam Lesinskica5638f2015-10-21 14:42:43 -07001120 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001121 const std::u16string& elementNamespace = parser->getElementNamespace();
1122 const std::u16string& elementName = parser->getElementName();
1123 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001124 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1125 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001126 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001127 << "'quantity'");
1128 error = true;
1129 continue;
1130 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001131
Adam Lesinski467f1712015-11-16 17:35:44 -08001132 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001133 size_t index = 0;
1134 if (trimmedQuantity == u"zero") {
1135 index = Plural::Zero;
1136 } else if (trimmedQuantity == u"one") {
1137 index = Plural::One;
1138 } else if (trimmedQuantity == u"two") {
1139 index = Plural::Two;
1140 } else if (trimmedQuantity == u"few") {
1141 index = Plural::Few;
1142 } else if (trimmedQuantity == u"many") {
1143 index = Plural::Many;
1144 } else if (trimmedQuantity == u"other") {
1145 index = Plural::Other;
1146 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001147 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001148 << "<item> in <plural> has invalid value '" << trimmedQuantity
1149 << "' for attribute 'quantity'");
1150 error = true;
1151 continue;
1152 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001153
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001154 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001155 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001156 << "duplicate quantity '" << trimmedQuantity << "'");
1157 error = true;
1158 continue;
1159 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001160
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001161 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1162 kNoRawString))) {
1163 error = true;
1164 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001165 plural->values[index]->setSource(itemSource);
1166
1167 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1168 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001169 << elementName << ">");
1170 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001171 }
1172 }
1173
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001174 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001175 return false;
1176 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001177
1178 outResource->value = std::move(plural);
1179 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001180}
1181
Adam Lesinski52364f72016-01-11 13:10:24 -08001182bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser,
1183 ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001184 outResource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001185
Adam Lesinskib274e352015-11-06 15:14:35 -08001186 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001187 outResource->symbolState = SymbolState::kPublic;
1188
Adam Lesinski52364f72016-01-11 13:10:24 -08001189 // Declare-styleable only ends up in default config;
1190 if (outResource->config != ConfigDescription::defaultConfig()) {
1191 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
1192 << outResource->config << "' for styleable "
1193 << outResource->name.entry);
1194 outResource->config = ConfigDescription::defaultConfig();
1195 }
1196
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001197 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1198
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001199 std::u16string comment;
1200 bool error = false;
1201 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001202 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1203 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001204 comment = util::trimWhitespace(parser->getComment()).toString();
1205 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001206 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001207 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001208 continue;
1209 }
1210
Adam Lesinskica5638f2015-10-21 14:42:43 -07001211 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001212 const std::u16string& elementNamespace = parser->getElementNamespace();
1213 const std::u16string& elementName = parser->getElementName();
1214 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001215 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1216 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001217 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001218 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001219 continue;
1220 }
1221
Adam Lesinski467f1712015-11-16 17:35:44 -08001222 // If this is a declaration, the package name may be in the name. Separate these out.
1223 // Eg. <attr name="android:text" />
1224 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1225 if (!maybeRef) {
1226 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1227 << maybeName.value() << "'");
1228 error = true;
1229 continue;
1230 }
1231
1232 Reference& childRef = maybeRef.value();
1233 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1234
Adam Lesinskica5638f2015-10-21 14:42:43 -07001235 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001236 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001237 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001238 childResource.source = itemSource;
1239 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001240
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001241 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001242 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001243 continue;
1244 }
1245
Adam Lesinskica5638f2015-10-21 14:42:43 -07001246 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001247 childRef.setComment(childResource.comment);
1248 childRef.setSource(itemSource);
1249 styleable->entries.push_back(std::move(childRef));
1250
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001251 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001252
Adam Lesinskica5638f2015-10-21 14:42:43 -07001253 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1254 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001255 << elementName << ">");
1256 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001257 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001258
1259 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001260 }
1261
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001262 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001263 return false;
1264 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001265
1266 outResource->value = std::move(styleable);
1267 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001268}
1269
1270} // namespace aapt