blob: c2ddb5c233c16e763fb3754b3e899eab035e730b [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 Lesinski9e10ac72015-10-16 14:37:48 -070022#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080023#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070024
Adam Lesinski769de982015-04-10 19:43:55 -070025#include <sstream>
26
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027namespace aapt {
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030
Adam Lesinski27afb9e2015-11-06 18:25:04 -080031/**
32 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
33 */
34static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) {
35 return ns.empty() && (name == u"skip" || name == u"eat-comment");
36}
37
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070039 const ConfigDescription& config,
40 const ResourceParserOptions& options) :
41 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042}
43
44/**
45 * Build a string from XML that converts nested elements into Span objects.
46 */
Adam Lesinski467f1712015-11-16 17:35:44 -080047bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048 StyleString* outStyleString) {
49 std::vector<Span> spanStack;
50
Adam Lesinskib23f1e02015-11-03 12:24:17 -080051 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052 outRawString->clear();
53 outStyleString->spans.clear();
54 util::StringBuilder builder;
55 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -080056 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
57 const xml::XmlPullParser::Event event = parser->getEvent();
58 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 if (!parser->getElementNamespace().empty()) {
60 // We already warned and skipped the start element, so just skip here too
61 continue;
62 }
63
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064 depth--;
65 if (depth == 0) {
66 break;
67 }
68
69 spanStack.back().lastChar = builder.str().size();
70 outStyleString->spans.push_back(spanStack.back());
71 spanStack.pop_back();
72
Adam Lesinski467f1712015-11-16 17:35:44 -080073 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074 outRawString->append(parser->getText());
75 builder.append(parser->getText());
76
Adam Lesinski467f1712015-11-16 17:35:44 -080077 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 if (!parser->getElementNamespace().empty()) {
79 if (parser->getElementNamespace() != sXliffNamespaceUri) {
80 // Only warn if this isn't an xliff namespace.
81 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
82 << "skipping element '"
83 << parser->getElementName()
84 << "' with unknown namespace '"
85 << parser->getElementNamespace()
86 << "'");
87 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088 continue;
89 }
90 depth++;
91
92 // Build a span object out of the nested element.
93 std::u16string spanName = parser->getElementName();
94 const auto endAttrIter = parser->endAttributes();
95 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
96 spanName += u";";
97 spanName += attrIter->name;
98 spanName += u"=";
99 spanName += attrIter->value;
100 }
101
102 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
104 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800105 error = true;
106 } else {
107 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Adam Lesinski467f1712015-11-16 17:35:44 -0800110 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111 // Skip
112 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114 }
115 }
116 assert(spanStack.empty() && "spans haven't been fully processed");
117
118 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800119 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120}
121
Adam Lesinski467f1712015-11-16 17:35:44 -0800122bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123 bool error = false;
124 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800125 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
126 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128 continue;
129 }
130
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
132 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
133 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134 return false;
135 }
136
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137 error |= !parseResources(parser);
138 break;
139 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinski467f1712015-11-16 17:35:44 -0800141 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
143 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144 return false;
145 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147}
148
Adam Lesinski467f1712015-11-16 17:35:44 -0800149static bool shouldStripResource(const xml::XmlPullParser* parser,
150 const Maybe<std::u16string> productToMatch) {
151 assert(parser->getEvent() == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700152
Adam Lesinski467f1712015-11-16 17:35:44 -0800153 if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700154 if (!productToMatch) {
155 if (maybeProduct.value() != u"default" && maybeProduct.value() != u"phone") {
156 // We didn't specify a product and this is not a default product, so skip.
157 return true;
158 }
159 } else {
160 if (productToMatch && maybeProduct.value() != productToMatch.value()) {
161 // We specified a product, but they don't match.
162 return true;
163 }
164 }
165 }
166 return false;
167}
168
169/**
170 * A parsed resource ready to be added to the ResourceTable.
171 */
172struct ParsedResource {
173 ResourceName name;
174 Source source;
175 ResourceId id;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700176 SymbolState symbolState = SymbolState::kUndefined;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700177 std::u16string comment;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700178 std::unique_ptr<Value> value;
179 std::list<ParsedResource> childResources;
180};
181
182// Recursively adds resources to the ResourceTable.
183static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
184 IDiagnostics* diag, ParsedResource* res) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700185 if (res->symbolState != SymbolState::kUndefined) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700186 Symbol symbol;
187 symbol.state = res->symbolState;
188 symbol.source = res->source;
189 symbol.comment = res->comment;
190 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700191 return false;
192 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700193 }
194
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800195 if (res->value) {
196 // Attach the comment, source and config to the value.
197 res->value->setComment(std::move(res->comment));
198 res->value->setSource(std::move(res->source));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700199
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800200 if (!table->addResource(res->name, res->id, config, std::move(res->value), diag)) {
201 return false;
202 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700203 }
204
205 bool error = false;
206 for (ParsedResource& child : res->childResources) {
207 error |= !addResourcesToTable(table, config, diag, &child);
208 }
209 return !error;
210}
211
Adam Lesinski467f1712015-11-16 17:35:44 -0800212bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700213 std::set<ResourceName> strippedResources;
214
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800218 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
219 const xml::XmlPullParser::Event event = parser->getEvent();
220 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221 comment = parser->getComment();
222 continue;
223 }
224
Adam Lesinski467f1712015-11-16 17:35:44 -0800225 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
228 << "plain text not allowed here");
229 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230 }
231 continue;
232 }
233
Adam Lesinski467f1712015-11-16 17:35:44 -0800234 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237 // Skip unknown namespace.
238 continue;
239 }
240
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241 std::u16string elementName = parser->getElementName();
242 if (elementName == u"skip" || elementName == u"eat-comment") {
243 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244 continue;
245 }
246
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247 if (elementName == u"item") {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248 // Items simply have their type encoded in the type attribute.
Adam Lesinski467f1712015-11-16 17:35:44 -0800249 if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 elementName = maybeType.value().toString();
251 } else {
252 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
253 << "<item> must have a 'type' attribute");
254 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255 continue;
256 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257 }
258
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700259 ParsedResource parsedResource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700260 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700261 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800262
Adam Lesinski467f1712015-11-16 17:35:44 -0800263 if (Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800264 parsedResource.name.entry = maybeName.value().toString();
265
266 } else if (elementName != u"public-group") {
267 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
268 << "<" << elementName << "> tag must have a 'name' attribute");
269 error = true;
270 continue;
271 }
272
273 // Check if we should skip this product.
274 const bool stripResource = shouldStripResource(parser, mOptions.product);
275
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700276 bool result = true;
277 if (elementName == u"id") {
278 parsedResource.name.type = ResourceType::kId;
279 parsedResource.value = util::make_unique<Id>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280 } else if (elementName == u"string") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700281 parsedResource.name.type = ResourceType::kString;
282 result = parseString(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 } else if (elementName == u"color") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700284 parsedResource.name.type = ResourceType::kColor;
285 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 } else if (elementName == u"drawable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700287 parsedResource.name.type = ResourceType::kDrawable;
288 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 } else if (elementName == u"bool") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700290 parsedResource.name.type = ResourceType::kBool;
291 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292 } else if (elementName == u"integer") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700293 parsedResource.name.type = ResourceType::kInteger;
294 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295 } else if (elementName == u"dimen") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700296 parsedResource.name.type = ResourceType::kDimen;
297 result = parsePrimitive(parser, &parsedResource);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700298 } else if (elementName == u"fraction") {
299 parsedResource.name.type = ResourceType::kFraction;
300 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 } else if (elementName == u"style") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700302 parsedResource.name.type = ResourceType::kStyle;
303 result = parseStyle(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 } else if (elementName == u"plurals") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700305 parsedResource.name.type = ResourceType::kPlurals;
306 result = parsePlural(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 } else if (elementName == u"array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700308 parsedResource.name.type = ResourceType::kArray;
309 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700310 } else if (elementName == u"string-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700311 parsedResource.name.type = ResourceType::kArray;
312 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_STRING);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313 } else if (elementName == u"integer-array") {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700314 parsedResource.name.type = ResourceType::kArray;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700315 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700316 } else if (elementName == u"declare-styleable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700317 parsedResource.name.type = ResourceType::kStyleable;
318 result = parseDeclareStyleable(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700319 } else if (elementName == u"attr") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700320 parsedResource.name.type = ResourceType::kAttr;
321 result = parseAttr(parser, &parsedResource);
322 } else if (elementName == u"public") {
323 result = parsePublic(parser, &parsedResource);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700324 } else if (elementName == u"java-symbol" || elementName == u"symbol") {
325 result = parseSymbol(parser, &parsedResource);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800326 } else if (elementName == u"public-group") {
327 result = parsePublicGroup(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700328 } else {
Adam Lesinskifa105052015-11-07 13:34:39 -0800329 // Try parsing the elementName (or type) as a resource. These shall only be
330 // resources like 'layout' or 'xml' and they can only be references.
331 if (const ResourceType* type = parseResourceType(elementName)) {
332 parsedResource.name.type = *type;
333 parsedResource.value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE,
334 false);
335 if (!parsedResource.value) {
336 mDiag->error(DiagMessage(parsedResource.source) << "invalid value for type '"
337 << *type << "'. Expected a reference");
338 result = false;
339 }
340 } else {
341 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
342 << "unknown resource type '" << elementName << "'");
343 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700344 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700345
346 if (result) {
347 // We successfully parsed the resource.
348
349 if (stripResource) {
350 // Record that we stripped out this resource name.
351 // We will check that at least one variant of this resource was included.
352 strippedResources.insert(parsedResource.name);
353 } else {
354 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
355 }
356 } else {
357 error = true;
358 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700360
361 // Check that we included at least one variant of each stripped resource.
362 for (const ResourceName& strippedResource : strippedResources) {
363 if (!mTable->findResource(strippedResource)) {
364 // Failed to find the resource.
365 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
366 "was filtered out but no product variant remains");
367 error = true;
368 }
369 }
370
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700371 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372}
373
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374enum {
375 kAllowRawString = true,
376 kNoRawString = false
377};
378
379/**
380 * Reads the entire XML subtree and attempts to parse it as some Item,
381 * with typeMask denoting which items it can be. If allowRawValue is
382 * true, a RawString is returned if the XML couldn't be parsed as
383 * an Item. If allowRawValue is false, nullptr is returned in this
384 * case.
385 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800386std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700387 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800388 const size_t beginXmlLine = parser->getLineNumber();
389
390 std::u16string rawValue;
391 StyleString styleString;
392 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
393 return {};
394 }
395
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800396 if (!styleString.spans.empty()) {
397 // This can only be a StyledString.
398 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400 }
401
402 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700403 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700404 std::unique_ptr<Id> id = util::make_unique<Id>();
405 id->setSource(mSource.withLine(beginXmlLine));
406 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407 };
408
409 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700410 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
411 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800412 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700413 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700414 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800415 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800417 return processedItem;
418 }
419
420 // Try making a regular string.
421 if (typeMask & android::ResTable_map::TYPE_STRING) {
422 // Use the trimmed, escaped string.
423 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700424 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425 }
426
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700428 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700430 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700432
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433 return {};
434}
435
Adam Lesinski467f1712015-11-16 17:35:44 -0800436bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700437 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800438
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800439 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800440 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800441 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
442 mDiag->error(DiagMessage(source) << "invalid value for 'formatted'. Must be a boolean");
443 return false;
444 }
445 }
446
Adam Lesinski9f222042015-11-04 13:51:45 -0800447 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800448 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800449 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800450 mDiag->error(DiagMessage(source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800451 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800452 return false;
453 }
454 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700456 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
457 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700458 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459 return false;
460 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800461
Adam Lesinski9f222042015-11-04 13:51:45 -0800462 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800463 if (String* stringValue = valueCast<String>(outResource->value.get())) {
464 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800465 mDiag->error(DiagMessage(source)
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800466 << "multiple substitutions specified in non-positional format; "
467 "did you mean to add the formatted=\"false\" attribute?");
468 return false;
469 }
470 }
471 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700472 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473}
474
Adam Lesinski467f1712015-11-16 17:35:44 -0800475bool ResourceParser::parseColor(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700476 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700478 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
479 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700480 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481 return false;
482 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700483 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484}
485
Adam Lesinski467f1712015-11-16 17:35:44 -0800486bool ResourceParser::parsePrimitive(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700487 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488
489 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700490 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700491 case ResourceType::kInteger:
492 typeMask |= android::ResTable_map::TYPE_INTEGER;
493 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494
Adam Lesinskica5638f2015-10-21 14:42:43 -0700495 case ResourceType::kFraction:
496 // fallthrough
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700497 case ResourceType::kDimen:
498 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700499 | android::ResTable_map::TYPE_FLOAT
500 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 case ResourceType::kBool:
504 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
505 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800506
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507 default:
508 assert(false);
509 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510 }
511
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700512 outResource->value = parseXml(parser, typeMask, kNoRawString);
513 if (!outResource->value) {
514 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800515 return false;
516 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700517 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518}
519
Adam Lesinski467f1712015-11-16 17:35:44 -0800520bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522
Adam Lesinski467f1712015-11-16 17:35:44 -0800523 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700524 if (!maybeType) {
525 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800526 return false;
527 }
528
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700529 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800530 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700531 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
532 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533 return false;
534 }
535
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700536 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800537
Adam Lesinski467f1712015-11-16 17:35:44 -0800538 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800539 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700540 bool result = android::ResTable::stringToInt(maybeId.value().data(),
541 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700542 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800543 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700544 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
545 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546 return false;
547 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700548 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549 }
550
551 if (*parsedType == ResourceType::kId) {
552 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700553 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800554 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700555
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700556 outResource->symbolState = SymbolState::kPublic;
557 return true;
558}
559
Adam Lesinski467f1712015-11-16 17:35:44 -0800560bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800561 const Source source = mSource.withLine(parser->getLineNumber());
562
Adam Lesinski467f1712015-11-16 17:35:44 -0800563 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800564 if (!maybeType) {
565 mDiag->error(DiagMessage(source) << "<public-group> must have a 'type' attribute");
566 return false;
567 }
568
569 const ResourceType* parsedType = parseResourceType(maybeType.value());
570 if (!parsedType) {
571 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
572 << "' in <public-group>");
573 return false;
574 }
575
Adam Lesinski467f1712015-11-16 17:35:44 -0800576 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800577 if (!maybeId) {
578 mDiag->error(DiagMessage(source) << "<public-group> must have a 'first-id' attribute");
579 return false;
580 }
581
582 android::Res_value val;
583 bool result = android::ResTable::stringToInt(maybeId.value().data(),
584 maybeId.value().size(), &val);
585 ResourceId nextId(val.data);
586 if (!result || !nextId.isValid()) {
587 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
588 << "' in <public-group>");
589 return false;
590 }
591
592 std::u16string comment;
593 bool error = false;
594 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800595 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
596 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800597 comment = util::trimWhitespace(parser->getComment()).toString();
598 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800599 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800600 // Skip text.
601 continue;
602 }
603
604 const Source itemSource = mSource.withLine(parser->getLineNumber());
605 const std::u16string& elementNamespace = parser->getElementNamespace();
606 const std::u16string& elementName = parser->getElementName();
607 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800608 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800609 if (!maybeName) {
610 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
611 error = true;
612 continue;
613 }
614
Adam Lesinski467f1712015-11-16 17:35:44 -0800615 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800616 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
617 error = true;
618 continue;
619 }
620
Adam Lesinski467f1712015-11-16 17:35:44 -0800621 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800622 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
623 error = true;
624 continue;
625 }
626
627 ParsedResource childResource;
628 childResource.name.type = *parsedType;
629 childResource.name.entry = maybeName.value().toString();
630 childResource.id = nextId;
631 childResource.comment = std::move(comment);
632 childResource.source = itemSource;
633 childResource.symbolState = SymbolState::kPublic;
634 outResource->childResources.push_back(std::move(childResource));
635
636 nextId.id += 1;
637
638 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
639 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
640 error = true;
641 }
642 }
643 return !error;
644}
645
Adam Lesinski467f1712015-11-16 17:35:44 -0800646bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700647 const Source source = mSource.withLine(parser->getLineNumber());
648
Adam Lesinski467f1712015-11-16 17:35:44 -0800649 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700650 if (!maybeType) {
651 mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a "
652 "'type' attribute");
653 return false;
654 }
655
656 const ResourceType* parsedType = parseResourceType(maybeType.value());
657 if (!parsedType) {
658 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
659 << "' in <" << parser->getElementName() << ">");
660 return false;
661 }
662
663 outResource->name.type = *parsedType;
664 outResource->symbolState = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700665 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800666}
667
668static uint32_t parseFormatType(const StringPiece16& piece) {
669 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
670 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
671 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
672 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
673 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
674 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
675 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
676 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
677 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
678 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
679 return 0;
680}
681
682static uint32_t parseFormatAttribute(const StringPiece16& str) {
683 uint32_t mask = 0;
684 for (StringPiece16 part : util::tokenize(str, u'|')) {
685 StringPiece16 trimmedPart = util::trimWhitespace(part);
686 uint32_t type = parseFormatType(trimmedPart);
687 if (type == 0) {
688 return 0;
689 }
690 mask |= type;
691 }
692 return mask;
693}
694
Adam Lesinski467f1712015-11-16 17:35:44 -0800695bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700696 outResource->source = mSource.withLine(parser->getLineNumber());
697 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800698}
699
Adam Lesinski467f1712015-11-16 17:35:44 -0800700bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
701 bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800702 uint32_t typeMask = 0;
703
Adam Lesinski467f1712015-11-16 17:35:44 -0800704 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700705 if (maybeFormat) {
706 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800707 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700708 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
709 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700710 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800711 }
712 }
713
Adam Lesinskia5870652015-11-20 15:32:30 -0800714 Maybe<int32_t> maybeMin, maybeMax;
715
716 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
717 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
718 if (!minStr.empty()) {
719 android::Res_value value;
720 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
721 maybeMin = static_cast<int32_t>(value.data);
722 }
723 }
724
725 if (!maybeMin) {
726 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
727 << "invalid 'min' value '" << minStr << "'");
728 return false;
729 }
730 }
731
732 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
733 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
734 if (!maxStr.empty()) {
735 android::Res_value value;
736 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
737 maybeMax = static_cast<int32_t>(value.data);
738 }
739 }
740
741 if (!maybeMax) {
742 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
743 << "invalid 'max' value '" << maxStr << "'");
744 return false;
745 }
746 }
747
748 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
749 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
750 << "'min' and 'max' can only be used when format='integer'");
751 return false;
752 }
753
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800754 struct SymbolComparator {
755 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
756 return a.symbol.name.value() < b.symbol.name.value();
757 }
758 };
759
760 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800761
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700762 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800763 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700764 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800765 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
766 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700767 comment = util::trimWhitespace(parser->getComment()).toString();
768 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800769 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700770 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800771 continue;
772 }
773
Adam Lesinskica5638f2015-10-21 14:42:43 -0700774 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700775 const std::u16string& elementNamespace = parser->getElementNamespace();
776 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700777 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700778 if (elementName == u"enum") {
779 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700780 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700781 << "can not define an <enum>; already defined a <flag>");
782 error = true;
783 continue;
784 }
785 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700786
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700787 } else if (elementName == u"flag") {
788 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700789 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700790 << "can not define a <flag>; already defined an <enum>");
791 error = true;
792 continue;
793 }
794 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800795 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800796
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700797 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800798 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700799 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800800 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700801 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700802 childResource.value = util::make_unique<Id>();
803 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700804
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800805 symbol.symbol.setComment(std::move(comment));
806 symbol.symbol.setSource(itemSource);
807
808 auto insertResult = items.insert(std::move(symbol));
809 if (!insertResult.second) {
810 const Attribute::Symbol& existingSymbol = *insertResult.first;
811 mDiag->error(DiagMessage(itemSource)
812 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
813 << "'");
814
815 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
816 << "first defined here");
817 error = true;
818 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800819 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700820 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800821 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700822 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
823 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800824 error = true;
825 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700826
827 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828 }
829
830 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700831 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800832 }
833
834 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800835 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700836 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800837 if (maybeMin) {
838 attr->minInt = maybeMin.value();
839 }
840
841 if (maybeMax) {
842 attr->maxInt = maybeMax.value();
843 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700844 outResource->value = std::move(attr);
845 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800846}
847
Adam Lesinski467f1712015-11-16 17:35:44 -0800848Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700849 const StringPiece16& tag) {
850 const Source source = mSource.withLine(parser->getLineNumber());
851
Adam Lesinski467f1712015-11-16 17:35:44 -0800852 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700853 if (!maybeName) {
854 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
855 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800856 }
857
Adam Lesinski467f1712015-11-16 17:35:44 -0800858 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700859 if (!maybeValue) {
860 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
861 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862 }
863
864 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700865 if (!android::ResTable::stringToInt(maybeValue.value().data(),
866 maybeValue.value().size(), &val)) {
867 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
868 << "' for <" << tag << ">; must be an integer");
869 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800870 }
871
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700872 return Attribute::Symbol{
Adam Lesinskie78fd612015-10-22 12:48:43 -0700873 Reference(ResourceName({}, ResourceType::kId, maybeName.value().toString())),
874 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875}
876
Adam Lesinski467f1712015-11-16 17:35:44 -0800877static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800878 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800879 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800880 const char16_t* const end = start + str.size();
881 const char16_t* p = start;
882
Adam Lesinski467f1712015-11-16 17:35:44 -0800883 Reference ref;
884 if (p != end && *p == u'*') {
885 ref.privateReference = true;
886 start++;
887 p++;
888 }
889
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800890 StringPiece16 package;
891 StringPiece16 name;
892 while (p != end) {
893 if (*p == u':') {
894 package = StringPiece16(start, p - start);
895 name = StringPiece16(p + 1, end - (p + 1));
896 break;
897 }
898 p++;
899 }
900
Adam Lesinski467f1712015-11-16 17:35:44 -0800901 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700902 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800903 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800904}
905
Adam Lesinski467f1712015-11-16 17:35:44 -0800906bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700907 const Source source = mSource.withLine(parser->getLineNumber());
908
Adam Lesinski467f1712015-11-16 17:35:44 -0800909 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700910 if (!maybeName) {
911 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800912 return false;
913 }
914
Adam Lesinski467f1712015-11-16 17:35:44 -0800915 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700916 if (!maybeKey) {
917 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800918 return false;
919 }
920
Adam Lesinski467f1712015-11-16 17:35:44 -0800921 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800922 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800923
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800924 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
925 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700926 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800927 return false;
928 }
929
Adam Lesinski467f1712015-11-16 17:35:44 -0800930 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800931 return true;
932}
933
Adam Lesinski467f1712015-11-16 17:35:44 -0800934bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700935 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700936 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800937
Adam Lesinski467f1712015-11-16 17:35:44 -0800938 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700939 if (maybeParent) {
940 // If the parent is empty, we don't have a parent, but we also don't infer either.
941 if (!maybeParent.value().empty()) {
942 std::string errStr;
943 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
944 if (!style->parent) {
945 mDiag->error(DiagMessage(source) << errStr);
946 return false;
947 }
948
Adam Lesinski467f1712015-11-16 17:35:44 -0800949 // Transform the namespace prefix to the actual package name, and mark the reference as
950 // private if appropriate.
951 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800952 }
953
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700954 } else {
955 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700956 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700957 size_t pos = styleName.find_last_of(u'.');
958 if (pos != std::string::npos) {
959 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800960 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
961 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700962 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800963 }
964
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800965 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700966 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800967 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
968 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700969 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800970 continue;
971 }
972
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700973 const std::u16string& elementNamespace = parser->getElementNamespace();
974 const std::u16string& elementName = parser->getElementName();
975 if (elementNamespace == u"" && elementName == u"item") {
976 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800977
Adam Lesinskica5638f2015-10-21 14:42:43 -0700978 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700979 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
980 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800981 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800982 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800983 }
984
985 if (error) {
986 return false;
987 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700988
989 outResource->value = std::move(style);
990 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700991}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800992
Adam Lesinski467f1712015-11-16 17:35:44 -0800993bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700994 uint32_t typeMask) {
995 const Source source = mSource.withLine(parser->getLineNumber());
996 std::unique_ptr<Array> array = util::make_unique<Array>();
997
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700998 bool error = false;
999 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001000 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1001 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001002 // Skip text and comments.
1003 continue;
1004 }
1005
1006 const Source itemSource = mSource.withLine(parser->getLineNumber());
1007 const std::u16string& elementNamespace = parser->getElementNamespace();
1008 const std::u16string& elementName = parser->getElementName();
1009 if (elementNamespace.empty() && elementName == u"item") {
1010 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1011 if (!item) {
1012 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1013 error = true;
1014 continue;
1015 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001016 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001017 array->items.emplace_back(std::move(item));
1018
Adam Lesinskica5638f2015-10-21 14:42:43 -07001019 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001020 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1021 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1022 error = true;
1023 }
1024 }
1025
1026 if (error) {
1027 return false;
1028 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001029
1030 outResource->value = std::move(array);
1031 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001032}
1033
Adam Lesinski467f1712015-11-16 17:35:44 -08001034bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001035 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001036 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1037
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001038 bool error = false;
1039 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001040 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1041 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001042 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001043 continue;
1044 }
1045
Adam Lesinskica5638f2015-10-21 14:42:43 -07001046 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001047 const std::u16string& elementNamespace = parser->getElementNamespace();
1048 const std::u16string& elementName = parser->getElementName();
1049 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001050 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1051 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001052 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001053 << "'quantity'");
1054 error = true;
1055 continue;
1056 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001057
Adam Lesinski467f1712015-11-16 17:35:44 -08001058 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001059 size_t index = 0;
1060 if (trimmedQuantity == u"zero") {
1061 index = Plural::Zero;
1062 } else if (trimmedQuantity == u"one") {
1063 index = Plural::One;
1064 } else if (trimmedQuantity == u"two") {
1065 index = Plural::Two;
1066 } else if (trimmedQuantity == u"few") {
1067 index = Plural::Few;
1068 } else if (trimmedQuantity == u"many") {
1069 index = Plural::Many;
1070 } else if (trimmedQuantity == u"other") {
1071 index = Plural::Other;
1072 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001073 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001074 << "<item> in <plural> has invalid value '" << trimmedQuantity
1075 << "' for attribute 'quantity'");
1076 error = true;
1077 continue;
1078 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001079
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001080 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001081 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001082 << "duplicate quantity '" << trimmedQuantity << "'");
1083 error = true;
1084 continue;
1085 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001086
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001087 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1088 kNoRawString))) {
1089 error = true;
1090 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001091 plural->values[index]->setSource(itemSource);
1092
1093 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1094 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001095 << elementName << ">");
1096 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001097 }
1098 }
1099
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001100 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001101 return false;
1102 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001103
1104 outResource->value = std::move(plural);
1105 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001106}
1107
Adam Lesinski467f1712015-11-16 17:35:44 -08001108bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001109 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001110 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1111
Adam Lesinskib274e352015-11-06 15:14:35 -08001112 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001113 outResource->symbolState = SymbolState::kPublic;
1114
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001115 std::u16string comment;
1116 bool error = false;
1117 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001118 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1119 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001120 comment = util::trimWhitespace(parser->getComment()).toString();
1121 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001122 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001123 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001124 continue;
1125 }
1126
Adam Lesinskica5638f2015-10-21 14:42:43 -07001127 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001128 const std::u16string& elementNamespace = parser->getElementNamespace();
1129 const std::u16string& elementName = parser->getElementName();
1130 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001131 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1132 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001133 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001134 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001135 continue;
1136 }
1137
Adam Lesinski467f1712015-11-16 17:35:44 -08001138 // If this is a declaration, the package name may be in the name. Separate these out.
1139 // Eg. <attr name="android:text" />
1140 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1141 if (!maybeRef) {
1142 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1143 << maybeName.value() << "'");
1144 error = true;
1145 continue;
1146 }
1147
1148 Reference& childRef = maybeRef.value();
1149 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1150
Adam Lesinskica5638f2015-10-21 14:42:43 -07001151 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001152 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001153 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001154 childResource.source = itemSource;
1155 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001156
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001157 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001158 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001159 continue;
1160 }
1161
Adam Lesinskica5638f2015-10-21 14:42:43 -07001162 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001163 childRef.setComment(childResource.comment);
1164 childRef.setSource(itemSource);
1165 styleable->entries.push_back(std::move(childRef));
1166
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001167 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001168
Adam Lesinskica5638f2015-10-21 14:42:43 -07001169 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1170 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001171 << elementName << ">");
1172 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001173 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001174
1175 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001176 }
1177
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001178 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001179 return false;
1180 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001181
1182 outResource->value = std::move(styleable);
1183 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001184}
1185
1186} // namespace aapt