blob: b37d366a788788a2db735d178db186f0afb8e426 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "ResourceTable.h"
19#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "ValueVisitor.h"
Adam Lesinski7751afc2016-01-06 15:45:28 -080022#include "util/Comparators.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080023#include "util/ImmutableMap.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070024#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070026
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080027#include <functional>
Adam Lesinski769de982015-04-10 19:43:55 -070028#include <sstream>
29
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033
Adam Lesinski27afb9e2015-11-06 18:25:04 -080034/**
35 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
36 */
37static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) {
38 return ns.empty() && (name == u"skip" || name == u"eat-comment");
39}
40
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080041static uint32_t parseFormatType(const StringPiece16& piece) {
42 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
43 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
44 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
45 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
46 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
47 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
48 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
49 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
50 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
51 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
52 return 0;
53}
54
55static uint32_t parseFormatAttribute(const StringPiece16& str) {
56 uint32_t mask = 0;
57 for (StringPiece16 part : util::tokenize(str, u'|')) {
58 StringPiece16 trimmedPart = util::trimWhitespace(part);
59 uint32_t type = parseFormatType(trimmedPart);
60 if (type == 0) {
61 return 0;
62 }
63 mask |= type;
64 }
65 return mask;
66}
67
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068/**
69 * A parsed resource ready to be added to the ResourceTable.
70 */
71struct ParsedResource {
72 ResourceName name;
Adam Lesinski52364f72016-01-11 13:10:24 -080073 ConfigDescription config;
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
Adam Lesinski7751afc2016-01-06 15:45:28 -080082bool ResourceParser::shouldStripResource(const ResourceNameRef& name,
83 const Maybe<std::u16string>& product) const {
84 if (product) {
85 for (const std::u16string& productToMatch : mOptions.products) {
86 if (product.value() == productToMatch) {
87 // We specified a product, and it is in the list, so don't strip.
88 return false;
89 }
90 }
91 }
92
93 // Nothing matched, try 'default'. Default only matches if we didn't already use another
94 // product variant.
95 if (!product || product.value() == u"default") {
96 if (Maybe<ResourceTable::SearchResult> result = mTable->findResource(name)) {
97 const ResourceEntry* entry = result.value().entry;
98 auto iter = std::lower_bound(entry->values.begin(), entry->values.end(), mConfig,
99 cmp::lessThanConfig);
100 if (iter != entry->values.end() && iter->config == mConfig && !iter->value->isWeak()) {
101 // We have a value for this config already, and it is not weak,
102 // so filter out this default.
103 return true;
104 }
105 }
106 return false;
107 }
108 return true;
109}
110
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800111// Recursively adds resources to the ResourceTable.
Adam Lesinski52364f72016-01-11 13:10:24 -0800112static bool addResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800113 if (res->symbolState) {
114 Symbol symbol;
115 symbol.state = res->symbolState.value();
116 symbol.source = res->source;
117 symbol.comment = res->comment;
118 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
119 return false;
120 }
121 }
122
123 if (res->value) {
124 // Attach the comment, source and config to the value.
125 res->value->setComment(std::move(res->comment));
126 res->value->setSource(std::move(res->source));
127
Adam Lesinski52364f72016-01-11 13:10:24 -0800128 if (!table->addResource(res->name, res->id, res->config, std::move(res->value), diag)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800129 return false;
130 }
131 }
132
133 bool error = false;
134 for (ParsedResource& child : res->childResources) {
Adam Lesinski52364f72016-01-11 13:10:24 -0800135 error |= !addResourcesToTable(table, diag, &child);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800136 }
137 return !error;
138}
139
140// Convenient aliases for more readable function calls.
141enum {
142 kAllowRawString = true,
143 kNoRawString = false
144};
145
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700147 const ConfigDescription& config,
148 const ResourceParserOptions& options) :
149 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150}
151
152/**
153 * Build a string from XML that converts nested elements into Span objects.
154 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800155bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156 StyleString* outStyleString) {
157 std::vector<Span> spanStack;
158
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800159 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160 outRawString->clear();
161 outStyleString->spans.clear();
162 util::StringBuilder builder;
163 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -0800164 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
165 const xml::XmlPullParser::Event event = parser->getEvent();
166 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167 if (!parser->getElementNamespace().empty()) {
168 // We already warned and skipped the start element, so just skip here too
169 continue;
170 }
171
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172 depth--;
173 if (depth == 0) {
174 break;
175 }
176
177 spanStack.back().lastChar = builder.str().size();
178 outStyleString->spans.push_back(spanStack.back());
179 spanStack.pop_back();
180
Adam Lesinski467f1712015-11-16 17:35:44 -0800181 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182 outRawString->append(parser->getText());
183 builder.append(parser->getText());
184
Adam Lesinski467f1712015-11-16 17:35:44 -0800185 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 if (!parser->getElementNamespace().empty()) {
187 if (parser->getElementNamespace() != sXliffNamespaceUri) {
188 // Only warn if this isn't an xliff namespace.
189 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
190 << "skipping element '"
191 << parser->getElementName()
192 << "' with unknown namespace '"
193 << parser->getElementNamespace()
194 << "'");
195 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196 continue;
197 }
198 depth++;
199
200 // Build a span object out of the nested element.
201 std::u16string spanName = parser->getElementName();
202 const auto endAttrIter = parser->endAttributes();
203 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
204 spanName += u";";
205 spanName += attrIter->name;
206 spanName += u"=";
207 spanName += attrIter->value;
208 }
209
210 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
212 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800213 error = true;
214 } else {
215 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217
Adam Lesinski467f1712015-11-16 17:35:44 -0800218 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219 // Skip
220 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222 }
223 }
224 assert(spanStack.empty() && "spans haven't been fully processed");
225
226 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800227 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228}
229
Adam Lesinski467f1712015-11-16 17:35:44 -0800230bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231 bool error = false;
232 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800233 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
234 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 continue;
237 }
238
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
240 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
241 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242 return false;
243 }
244
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 error |= !parseResources(parser);
246 break;
247 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinski467f1712015-11-16 17:35:44 -0800249 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
251 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252 return false;
253 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255}
256
Adam Lesinski467f1712015-11-16 17:35:44 -0800257bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700258 std::set<ResourceName> strippedResources;
259
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800263 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
264 const xml::XmlPullParser::Event event = parser->getEvent();
265 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266 comment = parser->getComment();
267 continue;
268 }
269
Adam Lesinski467f1712015-11-16 17:35:44 -0800270 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
273 << "plain text not allowed here");
274 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 }
276 continue;
277 }
278
Adam Lesinski467f1712015-11-16 17:35:44 -0800279 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282 // Skip unknown namespace.
283 continue;
284 }
285
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 std::u16string elementName = parser->getElementName();
287 if (elementName == u"skip" || elementName == u"eat-comment") {
288 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289 continue;
290 }
291
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700292 ParsedResource parsedResource;
Adam Lesinski52364f72016-01-11 13:10:24 -0800293 parsedResource.config = mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700294 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700295 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296
Adam Lesinski7751afc2016-01-06 15:45:28 -0800297 // Extract the product name if it exists.
298 Maybe<std::u16string> product;
299 if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) {
300 product = maybeProduct.value().toString();
301 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800302
Adam Lesinski7751afc2016-01-06 15:45:28 -0800303 // Parse the resource regardless of product.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800304 if (!parseResource(parser, &parsedResource)) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800305 error = true;
306 continue;
307 }
308
Adam Lesinski7751afc2016-01-06 15:45:28 -0800309 // We successfully parsed the resource. Check if we should include it or strip it.
310 if (shouldStripResource(parsedResource.name, product)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800311 // Record that we stripped out this resource name.
312 // We will check that at least one variant of this resource was included.
313 strippedResources.insert(parsedResource.name);
Adam Lesinski52364f72016-01-11 13:10:24 -0800314 } else if (!addResourcesToTable(mTable, mDiag, &parsedResource)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700315 error = true;
316 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700318
319 // Check that we included at least one variant of each stripped resource.
320 for (const ResourceName& strippedResource : strippedResources) {
321 if (!mTable->findResource(strippedResource)) {
322 // Failed to find the resource.
323 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
324 "was filtered out but no product variant remains");
325 error = true;
326 }
327 }
328
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800330}
331
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800332
333bool ResourceParser::parseResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
334 struct ItemTypeFormat {
335 ResourceType type;
336 uint32_t format;
337 };
338
339 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, ParsedResource*)>;
340
341 static const auto elToItemMap = ImmutableMap<std::u16string, ItemTypeFormat>::createPreSorted({
342 { u"bool", { ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN } },
343 { u"color", { ResourceType::kColor, android::ResTable_map::TYPE_COLOR } },
344 { u"dimen", { ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT
345 | android::ResTable_map::TYPE_FRACTION
346 | android::ResTable_map::TYPE_DIMENSION } },
347 { u"drawable", { ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR } },
348 { u"fraction", { ResourceType::kFraction, android::ResTable_map::TYPE_FLOAT
349 | android::ResTable_map::TYPE_FRACTION
350 | android::ResTable_map::TYPE_DIMENSION } },
351 { u"integer", { ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER } },
352 { u"string", { ResourceType::kString, android::ResTable_map::TYPE_STRING } },
353 });
354
355 static const auto elToBagMap = ImmutableMap<std::u16string, BagParseFunc>::createPreSorted({
356 { u"add-resource", std::mem_fn(&ResourceParser::parseAddResource) },
357 { u"array", std::mem_fn(&ResourceParser::parseArray) },
358 { u"attr", std::mem_fn(&ResourceParser::parseAttr) },
359 { u"declare-styleable", std::mem_fn(&ResourceParser::parseDeclareStyleable) },
360 { u"integer-array", std::mem_fn(&ResourceParser::parseIntegerArray) },
361 { u"java-symbol", std::mem_fn(&ResourceParser::parseSymbol) },
362 { u"plurals", std::mem_fn(&ResourceParser::parsePlural) },
363 { u"public", std::mem_fn(&ResourceParser::parsePublic) },
364 { u"public-group", std::mem_fn(&ResourceParser::parsePublicGroup) },
365 { u"string-array", std::mem_fn(&ResourceParser::parseStringArray) },
366 { u"style", std::mem_fn(&ResourceParser::parseStyle) },
367 { u"symbol", std::mem_fn(&ResourceParser::parseSymbol) },
368 });
369
370 std::u16string resourceType = parser->getElementName();
371
372 // The value format accepted for this resource.
373 uint32_t resourceFormat = 0u;
374
375 if (resourceType == u"item") {
376 // Items have their type encoded in the type attribute.
377 if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) {
378 resourceType = maybeType.value().toString();
379 } else {
380 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
381 << "<item> must have a 'type' attribute");
382 return false;
383 }
384
385 if (Maybe<StringPiece16> maybeFormat = xml::findNonEmptyAttribute(parser, u"format")) {
386 // An explicit format for this resource was specified. The resource will retain
387 // its type in its name, but the accepted value for this type is overridden.
388 resourceFormat = parseFormatType(maybeFormat.value());
389 if (!resourceFormat) {
390 mDiag->error(DiagMessage(outResource->source)
391 << "'" << maybeFormat.value() << "' is an invalid format");
392 return false;
393 }
394 }
395 }
396
397 // Get the name of the resource. This will be checked later, because not all
398 // XML elements require a name.
399 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
400
401 if (resourceType == u"id") {
402 if (!maybeName) {
403 mDiag->error(DiagMessage(outResource->source)
404 << "<" << parser->getElementName() << "> missing 'name' attribute");
405 return false;
406 }
407
408 outResource->name.type = ResourceType::kId;
409 outResource->name.entry = maybeName.value().toString();
410 outResource->value = util::make_unique<Id>();
411 return true;
412 }
413
414 const auto itemIter = elToItemMap.find(resourceType);
415 if (itemIter != elToItemMap.end()) {
416 // This is an item, record its type and format and start parsing.
417
418 if (!maybeName) {
419 mDiag->error(DiagMessage(outResource->source)
420 << "<" << parser->getElementName() << "> missing 'name' attribute");
421 return false;
422 }
423
424 outResource->name.type = itemIter->second.type;
425 outResource->name.entry = maybeName.value().toString();
426
427 // Only use the implicit format for this type if it wasn't overridden.
428 if (!resourceFormat) {
429 resourceFormat = itemIter->second.format;
430 }
431
432 if (!parseItem(parser, outResource, resourceFormat)) {
433 return false;
434 }
435 return true;
436 }
437
438 // This might be a bag or something.
439 const auto bagIter = elToBagMap.find(resourceType);
440 if (bagIter != elToBagMap.end()) {
441 // Ensure we have a name (unless this is a <public-group>).
442 if (resourceType != u"public-group") {
443 if (!maybeName) {
444 mDiag->error(DiagMessage(outResource->source)
445 << "<" << parser->getElementName() << "> missing 'name' attribute");
446 return false;
447 }
448
449 outResource->name.entry = maybeName.value().toString();
450 }
451
452 // Call the associated parse method. The type will be filled in by the
453 // parse func.
454 if (!bagIter->second(this, parser, outResource)) {
455 return false;
456 }
457 return true;
458 }
459
460 // Try parsing the elementName (or type) as a resource. These shall only be
461 // resources like 'layout' or 'xml' and they can only be references.
462 const ResourceType* parsedType = parseResourceType(resourceType);
463 if (parsedType) {
464 if (!maybeName) {
465 mDiag->error(DiagMessage(outResource->source)
466 << "<" << parser->getElementName() << "> missing 'name' attribute");
467 return false;
468 }
469
470 outResource->name.type = *parsedType;
471 outResource->name.entry = maybeName.value().toString();
472 outResource->value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
473 if (!outResource->value) {
474 mDiag->error(DiagMessage(outResource->source)
475 << "invalid value for type '" << *parsedType << "'. Expected a reference");
476 return false;
477 }
478 return true;
479 }
480
481 mDiag->warn(DiagMessage(outResource->source)
482 << "unknown resource type '" << parser->getElementName() << "'");
483 return false;
484}
485
486bool ResourceParser::parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
487 const uint32_t format) {
488 if (format == android::ResTable_map::TYPE_STRING) {
489 return parseString(parser, outResource);
490 }
491
492 outResource->value = parseXml(parser, format, kNoRawString);
493 if (!outResource->value) {
494 mDiag->error(DiagMessage(outResource->source) << "invalid " << outResource->name.type);
495 return false;
496 }
497 return true;
498}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499
500/**
501 * Reads the entire XML subtree and attempts to parse it as some Item,
502 * with typeMask denoting which items it can be. If allowRawValue is
503 * true, a RawString is returned if the XML couldn't be parsed as
504 * an Item. If allowRawValue is false, nullptr is returned in this
505 * case.
506 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800507std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700508 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509 const size_t beginXmlLine = parser->getLineNumber();
510
511 std::u16string rawValue;
512 StyleString styleString;
513 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
514 return {};
515 }
516
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517 if (!styleString.spans.empty()) {
518 // This can only be a StyledString.
519 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700520 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521 }
522
523 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700524 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700525 std::unique_ptr<Id> id = util::make_unique<Id>();
526 id->setSource(mSource.withLine(beginXmlLine));
527 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528 };
529
530 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700531 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
532 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700534 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700535 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800536 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800538 return processedItem;
539 }
540
541 // Try making a regular string.
542 if (typeMask & android::ResTable_map::TYPE_STRING) {
543 // Use the trimmed, escaped string.
544 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700545 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546 }
547
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800548 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700549 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800550 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700551 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800552 }
553 return {};
554}
555
Adam Lesinski467f1712015-11-16 17:35:44 -0800556bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800557 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800558 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800559 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800560 mDiag->error(DiagMessage(outResource->source)
561 << "invalid value for 'formatted'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800562 return false;
563 }
564 }
565
Adam Lesinski9f222042015-11-04 13:51:45 -0800566 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800567 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800568 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800569 mDiag->error(DiagMessage(outResource->source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800570 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800571 return false;
572 }
573 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700575 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
576 if (!outResource->value) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800577 mDiag->error(DiagMessage(outResource->source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578 return false;
579 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800580
Adam Lesinski393b5f02015-12-17 13:03:11 -0800581 if (String* stringValue = valueCast<String>(outResource->value.get())) {
582 stringValue->setTranslateable(translateable);
583
584 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800585 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski979ccb22016-01-11 10:42:19 -0800586 DiagMessage msg(outResource->source);
587 msg << "multiple substitutions specified in non-positional format; "
588 "did you mean to add the formatted=\"false\" attribute?";
589 if (mOptions.errorOnPositionalArguments) {
590 mDiag->error(msg);
591 return false;
592 }
593
594 mDiag->warn(msg);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800595 }
596 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800597
598 } else if (StyledString* stringValue = valueCast<StyledString>(outResource->value.get())) {
599 stringValue->setTranslateable(translateable);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800600 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700601 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602}
603
Adam Lesinski467f1712015-11-16 17:35:44 -0800604bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800605 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800607 mDiag->error(DiagMessage(outResource->source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800608 return false;
609 }
610
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700611 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800612 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800613 mDiag->error(DiagMessage(outResource->source)
614 << "invalid resource type '" << maybeType.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615 return false;
616 }
617
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700618 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800619
Adam Lesinski467f1712015-11-16 17:35:44 -0800620 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800621 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700622 bool result = android::ResTable::stringToInt(maybeId.value().data(),
623 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700624 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800625 if (!result || !resourceId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800626 mDiag->error(DiagMessage(outResource->source)
627 << "invalid resource ID '" << maybeId.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800628 return false;
629 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700630 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800631 }
632
633 if (*parsedType == ResourceType::kId) {
634 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700635 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800636 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700637
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700638 outResource->symbolState = SymbolState::kPublic;
639 return true;
640}
641
Adam Lesinski467f1712015-11-16 17:35:44 -0800642bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800643 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800644 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800645 mDiag->error(DiagMessage(outResource->source)
646 << "<public-group> must have a 'type' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800647 return false;
648 }
649
650 const ResourceType* parsedType = parseResourceType(maybeType.value());
651 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800652 mDiag->error(DiagMessage(outResource->source)
653 << "invalid resource type '" << maybeType.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800654 return false;
655 }
656
Adam Lesinski467f1712015-11-16 17:35:44 -0800657 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800658 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800659 mDiag->error(DiagMessage(outResource->source)
660 << "<public-group> must have a 'first-id' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800661 return false;
662 }
663
664 android::Res_value val;
665 bool result = android::ResTable::stringToInt(maybeId.value().data(),
666 maybeId.value().size(), &val);
667 ResourceId nextId(val.data);
668 if (!result || !nextId.isValid()) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800669 mDiag->error(DiagMessage(outResource->source)
670 << "invalid resource ID '" << maybeId.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800671 return false;
672 }
673
674 std::u16string comment;
675 bool error = false;
676 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800677 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
678 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800679 comment = util::trimWhitespace(parser->getComment()).toString();
680 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800681 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800682 // Skip text.
683 continue;
684 }
685
686 const Source itemSource = mSource.withLine(parser->getLineNumber());
687 const std::u16string& elementNamespace = parser->getElementNamespace();
688 const std::u16string& elementName = parser->getElementName();
689 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800690 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800691 if (!maybeName) {
692 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
693 error = true;
694 continue;
695 }
696
Adam Lesinski467f1712015-11-16 17:35:44 -0800697 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800698 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
699 error = true;
700 continue;
701 }
702
Adam Lesinski467f1712015-11-16 17:35:44 -0800703 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800704 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
705 error = true;
706 continue;
707 }
708
709 ParsedResource childResource;
710 childResource.name.type = *parsedType;
711 childResource.name.entry = maybeName.value().toString();
712 childResource.id = nextId;
713 childResource.comment = std::move(comment);
714 childResource.source = itemSource;
715 childResource.symbolState = SymbolState::kPublic;
716 outResource->childResources.push_back(std::move(childResource));
717
718 nextId.id += 1;
719
720 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
721 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
722 error = true;
723 }
724 }
725 return !error;
726}
727
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800728bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800729 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700730 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800731 mDiag->error(DiagMessage(outResource->source)
732 << "<" << parser->getElementName() << "> must have a 'type' attribute");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700733 return false;
734 }
735
736 const ResourceType* parsedType = parseResourceType(maybeType.value());
737 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800738 mDiag->error(DiagMessage(outResource->source)
739 << "invalid resource type '" << maybeType.value()
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700740 << "' in <" << parser->getElementName() << ">");
741 return false;
742 }
743
744 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700745 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800746}
747
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800748bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
749 if (parseSymbolImpl(parser, outResource)) {
750 outResource->symbolState = SymbolState::kPrivate;
751 return true;
752 }
753 return false;
754}
755
756bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
757 if (parseSymbolImpl(parser, outResource)) {
758 outResource->symbolState = SymbolState::kUndefined;
759 return true;
760 }
761 return false;
762}
763
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800764
Adam Lesinski467f1712015-11-16 17:35:44 -0800765bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700766 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800767}
768
Adam Lesinski467f1712015-11-16 17:35:44 -0800769bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
770 bool weak) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800771 outResource->name.type = ResourceType::kAttr;
772
Adam Lesinski52364f72016-01-11 13:10:24 -0800773 // Attributes only end up in default configuration.
774 if (outResource->config != ConfigDescription::defaultConfig()) {
775 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
776 << outResource->config << "' for attribute " << outResource->name);
777 outResource->config = ConfigDescription::defaultConfig();
778 }
779
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800780 uint32_t typeMask = 0;
781
Adam Lesinski467f1712015-11-16 17:35:44 -0800782 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700783 if (maybeFormat) {
784 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800785 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700786 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
787 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700788 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800789 }
790 }
791
Adam Lesinskia5870652015-11-20 15:32:30 -0800792 Maybe<int32_t> maybeMin, maybeMax;
793
794 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
795 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
796 if (!minStr.empty()) {
797 android::Res_value value;
798 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
799 maybeMin = static_cast<int32_t>(value.data);
800 }
801 }
802
803 if (!maybeMin) {
804 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
805 << "invalid 'min' value '" << minStr << "'");
806 return false;
807 }
808 }
809
810 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
811 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
812 if (!maxStr.empty()) {
813 android::Res_value value;
814 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
815 maybeMax = static_cast<int32_t>(value.data);
816 }
817 }
818
819 if (!maybeMax) {
820 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
821 << "invalid 'max' value '" << maxStr << "'");
822 return false;
823 }
824 }
825
826 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
827 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
828 << "'min' and 'max' can only be used when format='integer'");
829 return false;
830 }
831
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800832 struct SymbolComparator {
833 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
834 return a.symbol.name.value() < b.symbol.name.value();
835 }
836 };
837
838 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800839
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700840 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800841 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700842 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800843 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
844 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700845 comment = util::trimWhitespace(parser->getComment()).toString();
846 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800847 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700848 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800849 continue;
850 }
851
Adam Lesinskica5638f2015-10-21 14:42:43 -0700852 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700853 const std::u16string& elementNamespace = parser->getElementNamespace();
854 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700855 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700856 if (elementName == u"enum") {
857 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700858 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700859 << "can not define an <enum>; already defined a <flag>");
860 error = true;
861 continue;
862 }
863 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700864
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700865 } else if (elementName == u"flag") {
866 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700867 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700868 << "can not define a <flag>; already defined an <enum>");
869 error = true;
870 continue;
871 }
872 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800874
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700875 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800876 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700877 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800878 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700879 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700880 childResource.value = util::make_unique<Id>();
881 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700882
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800883 symbol.symbol.setComment(std::move(comment));
884 symbol.symbol.setSource(itemSource);
885
886 auto insertResult = items.insert(std::move(symbol));
887 if (!insertResult.second) {
888 const Attribute::Symbol& existingSymbol = *insertResult.first;
889 mDiag->error(DiagMessage(itemSource)
890 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
891 << "'");
892
893 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
894 << "first defined here");
895 error = true;
896 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800897 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700898 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800899 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700900 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
901 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800902 error = true;
903 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700904
905 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906 }
907
908 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700909 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800910 }
911
912 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800913 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700914 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800915 if (maybeMin) {
916 attr->minInt = maybeMin.value();
917 }
918
919 if (maybeMax) {
920 attr->maxInt = maybeMax.value();
921 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700922 outResource->value = std::move(attr);
923 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800924}
925
Adam Lesinski467f1712015-11-16 17:35:44 -0800926Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700927 const StringPiece16& tag) {
928 const Source source = mSource.withLine(parser->getLineNumber());
929
Adam Lesinski467f1712015-11-16 17:35:44 -0800930 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700931 if (!maybeName) {
932 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
933 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800934 }
935
Adam Lesinski467f1712015-11-16 17:35:44 -0800936 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700937 if (!maybeValue) {
938 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
939 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800940 }
941
942 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700943 if (!android::ResTable::stringToInt(maybeValue.value().data(),
944 maybeValue.value().size(), &val)) {
945 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
946 << "' for <" << tag << ">; must be an integer");
947 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948 }
949
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700950 return Attribute::Symbol{
Adam Lesinski52364f72016-01-11 13:10:24 -0800951 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())), val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800952}
953
Adam Lesinski467f1712015-11-16 17:35:44 -0800954static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800955 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800956 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800957 const char16_t* const end = start + str.size();
958 const char16_t* p = start;
959
Adam Lesinski467f1712015-11-16 17:35:44 -0800960 Reference ref;
961 if (p != end && *p == u'*') {
962 ref.privateReference = true;
963 start++;
964 p++;
965 }
966
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800967 StringPiece16 package;
968 StringPiece16 name;
969 while (p != end) {
970 if (*p == u':') {
971 package = StringPiece16(start, p - start);
972 name = StringPiece16(p + 1, end - (p + 1));
973 break;
974 }
975 p++;
976 }
977
Adam Lesinski467f1712015-11-16 17:35:44 -0800978 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700979 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800980 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800981}
982
Adam Lesinski467f1712015-11-16 17:35:44 -0800983bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700984 const Source source = mSource.withLine(parser->getLineNumber());
985
Adam Lesinski467f1712015-11-16 17:35:44 -0800986 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700987 if (!maybeName) {
988 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800989 return false;
990 }
991
Adam Lesinski467f1712015-11-16 17:35:44 -0800992 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700993 if (!maybeKey) {
994 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800995 return false;
996 }
997
Adam Lesinski467f1712015-11-16 17:35:44 -0800998 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800999 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001000
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001001 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
1002 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001003 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001004 return false;
1005 }
1006
Adam Lesinski467f1712015-11-16 17:35:44 -08001007 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001008 return true;
1009}
1010
Adam Lesinski467f1712015-11-16 17:35:44 -08001011bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001012 outResource->name.type = ResourceType::kStyle;
1013
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001014 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001015
Adam Lesinski467f1712015-11-16 17:35:44 -08001016 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001017 if (maybeParent) {
1018 // If the parent is empty, we don't have a parent, but we also don't infer either.
1019 if (!maybeParent.value().empty()) {
1020 std::string errStr;
1021 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
1022 if (!style->parent) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001023 mDiag->error(DiagMessage(outResource->source) << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001024 return false;
1025 }
1026
Adam Lesinski467f1712015-11-16 17:35:44 -08001027 // Transform the namespace prefix to the actual package name, and mark the reference as
1028 // private if appropriate.
1029 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001030 }
1031
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001032 } else {
1033 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001034 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001035 size_t pos = styleName.find_last_of(u'.');
1036 if (pos != std::string::npos) {
1037 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -08001038 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
1039 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001040 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001041 }
1042
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001043 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001044 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001045 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1046 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001047 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001048 continue;
1049 }
1050
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001051 const std::u16string& elementNamespace = parser->getElementNamespace();
1052 const std::u16string& elementName = parser->getElementName();
1053 if (elementNamespace == u"" && elementName == u"item") {
1054 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001055
Adam Lesinskica5638f2015-10-21 14:42:43 -07001056 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001057 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1058 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001059 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001060 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001061 }
1062
1063 if (error) {
1064 return false;
1065 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001066
1067 outResource->value = std::move(style);
1068 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001069}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001070
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001071bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1072 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_ANY);
1073}
1074
1075bool ResourceParser::parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1076 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_INTEGER);
1077}
1078
1079bool ResourceParser::parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1080 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_STRING);
1081}
1082
1083bool ResourceParser::parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
1084 const uint32_t typeMask) {
1085 outResource->name.type = ResourceType::kArray;
1086
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001087 std::unique_ptr<Array> array = util::make_unique<Array>();
1088
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001089 bool error = false;
1090 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001091 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1092 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001093 // Skip text and comments.
1094 continue;
1095 }
1096
1097 const Source itemSource = mSource.withLine(parser->getLineNumber());
1098 const std::u16string& elementNamespace = parser->getElementNamespace();
1099 const std::u16string& elementName = parser->getElementName();
1100 if (elementNamespace.empty() && elementName == u"item") {
1101 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1102 if (!item) {
1103 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1104 error = true;
1105 continue;
1106 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001107 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001108 array->items.emplace_back(std::move(item));
1109
Adam Lesinskica5638f2015-10-21 14:42:43 -07001110 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001111 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1112 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1113 error = true;
1114 }
1115 }
1116
1117 if (error) {
1118 return false;
1119 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001120
1121 outResource->value = std::move(array);
1122 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001123}
1124
Adam Lesinski467f1712015-11-16 17:35:44 -08001125bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001126 outResource->name.type = ResourceType::kPlurals;
1127
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001128 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1129
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001130 bool error = false;
1131 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001132 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1133 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001134 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001135 continue;
1136 }
1137
Adam Lesinskica5638f2015-10-21 14:42:43 -07001138 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001139 const std::u16string& elementNamespace = parser->getElementNamespace();
1140 const std::u16string& elementName = parser->getElementName();
1141 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001142 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1143 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001144 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001145 << "'quantity'");
1146 error = true;
1147 continue;
1148 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001149
Adam Lesinski467f1712015-11-16 17:35:44 -08001150 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001151 size_t index = 0;
1152 if (trimmedQuantity == u"zero") {
1153 index = Plural::Zero;
1154 } else if (trimmedQuantity == u"one") {
1155 index = Plural::One;
1156 } else if (trimmedQuantity == u"two") {
1157 index = Plural::Two;
1158 } else if (trimmedQuantity == u"few") {
1159 index = Plural::Few;
1160 } else if (trimmedQuantity == u"many") {
1161 index = Plural::Many;
1162 } else if (trimmedQuantity == u"other") {
1163 index = Plural::Other;
1164 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001165 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001166 << "<item> in <plural> has invalid value '" << trimmedQuantity
1167 << "' for attribute 'quantity'");
1168 error = true;
1169 continue;
1170 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001171
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001172 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001173 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001174 << "duplicate quantity '" << trimmedQuantity << "'");
1175 error = true;
1176 continue;
1177 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001178
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001179 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1180 kNoRawString))) {
1181 error = true;
1182 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001183 plural->values[index]->setSource(itemSource);
1184
1185 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1186 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001187 << elementName << ">");
1188 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001189 }
1190 }
1191
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001192 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001193 return false;
1194 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001195
1196 outResource->value = std::move(plural);
1197 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001198}
1199
Adam Lesinski52364f72016-01-11 13:10:24 -08001200bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser,
1201 ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001202 outResource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001203
Adam Lesinskib274e352015-11-06 15:14:35 -08001204 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001205 outResource->symbolState = SymbolState::kPublic;
1206
Adam Lesinski52364f72016-01-11 13:10:24 -08001207 // Declare-styleable only ends up in default config;
1208 if (outResource->config != ConfigDescription::defaultConfig()) {
1209 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
1210 << outResource->config << "' for styleable "
1211 << outResource->name.entry);
1212 outResource->config = ConfigDescription::defaultConfig();
1213 }
1214
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001215 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1216
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001217 std::u16string comment;
1218 bool error = false;
1219 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001220 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1221 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001222 comment = util::trimWhitespace(parser->getComment()).toString();
1223 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001224 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001225 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001226 continue;
1227 }
1228
Adam Lesinskica5638f2015-10-21 14:42:43 -07001229 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001230 const std::u16string& elementNamespace = parser->getElementNamespace();
1231 const std::u16string& elementName = parser->getElementName();
1232 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001233 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1234 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001235 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001236 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001237 continue;
1238 }
1239
Adam Lesinski467f1712015-11-16 17:35:44 -08001240 // If this is a declaration, the package name may be in the name. Separate these out.
1241 // Eg. <attr name="android:text" />
1242 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1243 if (!maybeRef) {
1244 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1245 << maybeName.value() << "'");
1246 error = true;
1247 continue;
1248 }
1249
1250 Reference& childRef = maybeRef.value();
1251 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1252
Adam Lesinskica5638f2015-10-21 14:42:43 -07001253 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001254 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001255 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001256 childResource.source = itemSource;
1257 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001258
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001259 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001260 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001261 continue;
1262 }
1263
Adam Lesinskica5638f2015-10-21 14:42:43 -07001264 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001265 childRef.setComment(childResource.comment);
1266 childRef.setSource(itemSource);
1267 styleable->entries.push_back(std::move(childRef));
1268
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001269 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001270
Adam Lesinskica5638f2015-10-21 14:42:43 -07001271 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1272 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001273 << elementName << ">");
1274 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001275 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001276
1277 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001278 }
1279
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001280 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001281 return false;
1282 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001283
1284 outResource->value = std::move(styleable);
1285 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001286}
1287
1288} // namespace aapt