blob: d4c536f61c45d6aa995005580234e5f8a8812752 [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 Lesinskia6fe3452015-12-09 15:20:52 -0800176 Maybe<SymbolState> symbolState;
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 Lesinskia6fe3452015-12-09 15:20:52 -0800185 if (res->symbolState) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700186 Symbol symbol;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800187 symbol.state = res->symbolState.value();
Adam Lesinskie78fd612015-10-22 12:48:43 -0700188 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 Lesinskia6fe3452015-12-09 15:20:52 -0800328 } else if (elementName == u"add-resource") {
329 result = parseAddResource(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330 } else {
Adam Lesinskifa105052015-11-07 13:34:39 -0800331 // Try parsing the elementName (or type) as a resource. These shall only be
332 // resources like 'layout' or 'xml' and they can only be references.
333 if (const ResourceType* type = parseResourceType(elementName)) {
334 parsedResource.name.type = *type;
335 parsedResource.value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE,
336 false);
337 if (!parsedResource.value) {
338 mDiag->error(DiagMessage(parsedResource.source) << "invalid value for type '"
339 << *type << "'. Expected a reference");
340 result = false;
341 }
342 } else {
343 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
344 << "unknown resource type '" << elementName << "'");
345 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700347
348 if (result) {
349 // We successfully parsed the resource.
350
351 if (stripResource) {
352 // Record that we stripped out this resource name.
353 // We will check that at least one variant of this resource was included.
354 strippedResources.insert(parsedResource.name);
355 } else {
356 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
357 }
358 } else {
359 error = true;
360 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800361 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700362
363 // Check that we included at least one variant of each stripped resource.
364 for (const ResourceName& strippedResource : strippedResources) {
365 if (!mTable->findResource(strippedResource)) {
366 // Failed to find the resource.
367 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
368 "was filtered out but no product variant remains");
369 error = true;
370 }
371 }
372
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700373 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800374}
375
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376enum {
377 kAllowRawString = true,
378 kNoRawString = false
379};
380
381/**
382 * Reads the entire XML subtree and attempts to parse it as some Item,
383 * with typeMask denoting which items it can be. If allowRawValue is
384 * true, a RawString is returned if the XML couldn't be parsed as
385 * an Item. If allowRawValue is false, nullptr is returned in this
386 * case.
387 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800388std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700389 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800390 const size_t beginXmlLine = parser->getLineNumber();
391
392 std::u16string rawValue;
393 StyleString styleString;
394 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
395 return {};
396 }
397
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800398 if (!styleString.spans.empty()) {
399 // This can only be a StyledString.
400 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700401 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800402 }
403
404 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700405 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700406 std::unique_ptr<Id> id = util::make_unique<Id>();
407 id->setSource(mSource.withLine(beginXmlLine));
408 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800409 };
410
411 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700412 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
413 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800414 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700415 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800417 transformReferenceFromNamespace(parser, u"", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700418 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800419 return processedItem;
420 }
421
422 // Try making a regular string.
423 if (typeMask & android::ResTable_map::TYPE_STRING) {
424 // Use the trimmed, escaped string.
425 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700426 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427 }
428
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800429 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700430 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700432 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700434
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435 return {};
436}
437
Adam Lesinski467f1712015-11-16 17:35:44 -0800438bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700439 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800441 bool formatted = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800442 if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800443 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
444 mDiag->error(DiagMessage(source) << "invalid value for 'formatted'. Must be a boolean");
445 return false;
446 }
447 }
448
Adam Lesinski9f222042015-11-04 13:51:45 -0800449 bool translateable = mOptions.translatable;
Adam Lesinski467f1712015-11-16 17:35:44 -0800450 if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800451 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800452 mDiag->error(DiagMessage(source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800453 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800454 return false;
455 }
456 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800457
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700458 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
459 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700460 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800461 return false;
462 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800463
Adam Lesinski9f222042015-11-04 13:51:45 -0800464 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800465 if (String* stringValue = valueCast<String>(outResource->value.get())) {
466 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800467 mDiag->error(DiagMessage(source)
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800468 << "multiple substitutions specified in non-positional format; "
469 "did you mean to add the formatted=\"false\" attribute?");
470 return false;
471 }
472 }
473 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700474 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800475}
476
Adam Lesinski467f1712015-11-16 17:35:44 -0800477bool ResourceParser::parseColor(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700478 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800479
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700480 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
481 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700482 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483 return false;
484 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700485 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486}
487
Adam Lesinski467f1712015-11-16 17:35:44 -0800488bool ResourceParser::parsePrimitive(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700489 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490
491 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700492 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700493 case ResourceType::kInteger:
494 typeMask |= android::ResTable_map::TYPE_INTEGER;
495 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496
Adam Lesinskica5638f2015-10-21 14:42:43 -0700497 case ResourceType::kFraction:
498 // fallthrough
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700499 case ResourceType::kDimen:
500 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700501 | android::ResTable_map::TYPE_FLOAT
502 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700505 case ResourceType::kBool:
506 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
507 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800508
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700509 default:
510 assert(false);
511 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512 }
513
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700514 outResource->value = parseXml(parser, typeMask, kNoRawString);
515 if (!outResource->value) {
516 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517 return false;
518 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700519 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520}
521
Adam Lesinski467f1712015-11-16 17:35:44 -0800522bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700523 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524
Adam Lesinski467f1712015-11-16 17:35:44 -0800525 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526 if (!maybeType) {
527 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528 return false;
529 }
530
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700531 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700533 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
534 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800535 return false;
536 }
537
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700538 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800539
Adam Lesinski467f1712015-11-16 17:35:44 -0800540 if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800541 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700542 bool result = android::ResTable::stringToInt(maybeId.value().data(),
543 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700544 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800545 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700546 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
547 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800548 return false;
549 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700550 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800551 }
552
553 if (*parsedType == ResourceType::kId) {
554 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700555 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800556 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700557
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700558 outResource->symbolState = SymbolState::kPublic;
559 return true;
560}
561
Adam Lesinski467f1712015-11-16 17:35:44 -0800562bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800563 const Source source = mSource.withLine(parser->getLineNumber());
564
Adam Lesinski467f1712015-11-16 17:35:44 -0800565 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800566 if (!maybeType) {
567 mDiag->error(DiagMessage(source) << "<public-group> must have a 'type' attribute");
568 return false;
569 }
570
571 const ResourceType* parsedType = parseResourceType(maybeType.value());
572 if (!parsedType) {
573 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
574 << "' in <public-group>");
575 return false;
576 }
577
Adam Lesinski467f1712015-11-16 17:35:44 -0800578 Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800579 if (!maybeId) {
580 mDiag->error(DiagMessage(source) << "<public-group> must have a 'first-id' attribute");
581 return false;
582 }
583
584 android::Res_value val;
585 bool result = android::ResTable::stringToInt(maybeId.value().data(),
586 maybeId.value().size(), &val);
587 ResourceId nextId(val.data);
588 if (!result || !nextId.isValid()) {
589 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
590 << "' in <public-group>");
591 return false;
592 }
593
594 std::u16string comment;
595 bool error = false;
596 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800597 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
598 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800599 comment = util::trimWhitespace(parser->getComment()).toString();
600 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800601 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800602 // Skip text.
603 continue;
604 }
605
606 const Source itemSource = mSource.withLine(parser->getLineNumber());
607 const std::u16string& elementNamespace = parser->getElementNamespace();
608 const std::u16string& elementName = parser->getElementName();
609 if (elementNamespace.empty() && elementName == u"public") {
Adam Lesinski467f1712015-11-16 17:35:44 -0800610 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800611 if (!maybeName) {
612 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
613 error = true;
614 continue;
615 }
616
Adam Lesinski467f1712015-11-16 17:35:44 -0800617 if (xml::findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800618 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
619 error = true;
620 continue;
621 }
622
Adam Lesinski467f1712015-11-16 17:35:44 -0800623 if (xml::findNonEmptyAttribute(parser, u"type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800624 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
625 error = true;
626 continue;
627 }
628
629 ParsedResource childResource;
630 childResource.name.type = *parsedType;
631 childResource.name.entry = maybeName.value().toString();
632 childResource.id = nextId;
633 childResource.comment = std::move(comment);
634 childResource.source = itemSource;
635 childResource.symbolState = SymbolState::kPublic;
636 outResource->childResources.push_back(std::move(childResource));
637
638 nextId.id += 1;
639
640 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
641 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
642 error = true;
643 }
644 }
645 return !error;
646}
647
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800648bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700649 const Source source = mSource.withLine(parser->getLineNumber());
650
Adam Lesinski467f1712015-11-16 17:35:44 -0800651 Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700652 if (!maybeType) {
653 mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a "
654 "'type' attribute");
655 return false;
656 }
657
658 const ResourceType* parsedType = parseResourceType(maybeType.value());
659 if (!parsedType) {
660 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
661 << "' in <" << parser->getElementName() << ">");
662 return false;
663 }
664
665 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700666 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800667}
668
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800669bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
670 if (parseSymbolImpl(parser, outResource)) {
671 outResource->symbolState = SymbolState::kPrivate;
672 return true;
673 }
674 return false;
675}
676
677bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
678 if (parseSymbolImpl(parser, outResource)) {
679 outResource->symbolState = SymbolState::kUndefined;
680 return true;
681 }
682 return false;
683}
684
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800685static uint32_t parseFormatType(const StringPiece16& piece) {
686 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
687 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
688 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
689 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
690 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
691 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
692 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
693 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
694 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
695 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
696 return 0;
697}
698
699static uint32_t parseFormatAttribute(const StringPiece16& str) {
700 uint32_t mask = 0;
701 for (StringPiece16 part : util::tokenize(str, u'|')) {
702 StringPiece16 trimmedPart = util::trimWhitespace(part);
703 uint32_t type = parseFormatType(trimmedPart);
704 if (type == 0) {
705 return 0;
706 }
707 mask |= type;
708 }
709 return mask;
710}
711
Adam Lesinski467f1712015-11-16 17:35:44 -0800712bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700713 outResource->source = mSource.withLine(parser->getLineNumber());
714 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800715}
716
Adam Lesinski467f1712015-11-16 17:35:44 -0800717bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
718 bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800719 uint32_t typeMask = 0;
720
Adam Lesinski467f1712015-11-16 17:35:44 -0800721 Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700722 if (maybeFormat) {
723 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800724 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700725 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
726 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700727 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800728 }
729 }
730
Adam Lesinskia5870652015-11-20 15:32:30 -0800731 Maybe<int32_t> maybeMin, maybeMax;
732
733 if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) {
734 StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value());
735 if (!minStr.empty()) {
736 android::Res_value value;
737 if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) {
738 maybeMin = static_cast<int32_t>(value.data);
739 }
740 }
741
742 if (!maybeMin) {
743 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
744 << "invalid 'min' value '" << minStr << "'");
745 return false;
746 }
747 }
748
749 if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) {
750 StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value());
751 if (!maxStr.empty()) {
752 android::Res_value value;
753 if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) {
754 maybeMax = static_cast<int32_t>(value.data);
755 }
756 }
757
758 if (!maybeMax) {
759 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
760 << "invalid 'max' value '" << maxStr << "'");
761 return false;
762 }
763 }
764
765 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
766 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
767 << "'min' and 'max' can only be used when format='integer'");
768 return false;
769 }
770
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800771 struct SymbolComparator {
772 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
773 return a.symbol.name.value() < b.symbol.name.value();
774 }
775 };
776
777 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800778
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700779 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800780 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700781 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800782 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
783 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700784 comment = util::trimWhitespace(parser->getComment()).toString();
785 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800786 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700787 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800788 continue;
789 }
790
Adam Lesinskica5638f2015-10-21 14:42:43 -0700791 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700792 const std::u16string& elementNamespace = parser->getElementNamespace();
793 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700794 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700795 if (elementName == u"enum") {
796 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700797 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700798 << "can not define an <enum>; already defined a <flag>");
799 error = true;
800 continue;
801 }
802 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700803
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700804 } else if (elementName == u"flag") {
805 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700806 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700807 << "can not define a <flag>; already defined an <enum>");
808 error = true;
809 continue;
810 }
811 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800812 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800813
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700814 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800815 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700816 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800817 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700818 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700819 childResource.value = util::make_unique<Id>();
820 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700821
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800822 symbol.symbol.setComment(std::move(comment));
823 symbol.symbol.setSource(itemSource);
824
825 auto insertResult = items.insert(std::move(symbol));
826 if (!insertResult.second) {
827 const Attribute::Symbol& existingSymbol = *insertResult.first;
828 mDiag->error(DiagMessage(itemSource)
829 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
830 << "'");
831
832 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
833 << "first defined here");
834 error = true;
835 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800836 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700837 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800838 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700839 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
840 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800841 error = true;
842 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700843
844 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800845 }
846
847 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700848 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800849 }
850
851 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800852 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700853 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800854 if (maybeMin) {
855 attr->minInt = maybeMin.value();
856 }
857
858 if (maybeMax) {
859 attr->maxInt = maybeMax.value();
860 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700861 outResource->value = std::move(attr);
862 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800863}
864
Adam Lesinski467f1712015-11-16 17:35:44 -0800865Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700866 const StringPiece16& tag) {
867 const Source source = mSource.withLine(parser->getLineNumber());
868
Adam Lesinski467f1712015-11-16 17:35:44 -0800869 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700870 if (!maybeName) {
871 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
872 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873 }
874
Adam Lesinski467f1712015-11-16 17:35:44 -0800875 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700876 if (!maybeValue) {
877 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
878 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800879 }
880
881 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700882 if (!android::ResTable::stringToInt(maybeValue.value().data(),
883 maybeValue.value().size(), &val)) {
884 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
885 << "' for <" << tag << ">; must be an integer");
886 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800887 }
888
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700889 return Attribute::Symbol{
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800890 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())),
Adam Lesinskie78fd612015-10-22 12:48:43 -0700891 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800892}
893
Adam Lesinski467f1712015-11-16 17:35:44 -0800894static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800895 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800896 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800897 const char16_t* const end = start + str.size();
898 const char16_t* p = start;
899
Adam Lesinski467f1712015-11-16 17:35:44 -0800900 Reference ref;
901 if (p != end && *p == u'*') {
902 ref.privateReference = true;
903 start++;
904 p++;
905 }
906
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800907 StringPiece16 package;
908 StringPiece16 name;
909 while (p != end) {
910 if (*p == u':') {
911 package = StringPiece16(start, p - start);
912 name = StringPiece16(p + 1, end - (p + 1));
913 break;
914 }
915 p++;
916 }
917
Adam Lesinski467f1712015-11-16 17:35:44 -0800918 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700919 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800920 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800921}
922
Adam Lesinski467f1712015-11-16 17:35:44 -0800923bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700924 const Source source = mSource.withLine(parser->getLineNumber());
925
Adam Lesinski467f1712015-11-16 17:35:44 -0800926 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700927 if (!maybeName) {
928 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800929 return false;
930 }
931
Adam Lesinski467f1712015-11-16 17:35:44 -0800932 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700933 if (!maybeKey) {
934 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 return false;
936 }
937
Adam Lesinski467f1712015-11-16 17:35:44 -0800938 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800939 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800940
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800941 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
942 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700943 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800944 return false;
945 }
946
Adam Lesinski467f1712015-11-16 17:35:44 -0800947 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948 return true;
949}
950
Adam Lesinski467f1712015-11-16 17:35:44 -0800951bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700952 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700953 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800954
Adam Lesinski467f1712015-11-16 17:35:44 -0800955 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700956 if (maybeParent) {
957 // If the parent is empty, we don't have a parent, but we also don't infer either.
958 if (!maybeParent.value().empty()) {
959 std::string errStr;
960 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
961 if (!style->parent) {
962 mDiag->error(DiagMessage(source) << errStr);
963 return false;
964 }
965
Adam Lesinski467f1712015-11-16 17:35:44 -0800966 // Transform the namespace prefix to the actual package name, and mark the reference as
967 // private if appropriate.
968 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800969 }
970
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700971 } else {
972 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700973 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700974 size_t pos = styleName.find_last_of(u'.');
975 if (pos != std::string::npos) {
976 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800977 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
978 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700979 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800980 }
981
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800982 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700983 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800984 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
985 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700986 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800987 continue;
988 }
989
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700990 const std::u16string& elementNamespace = parser->getElementNamespace();
991 const std::u16string& elementName = parser->getElementName();
992 if (elementNamespace == u"" && elementName == u"item") {
993 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800994
Adam Lesinskica5638f2015-10-21 14:42:43 -0700995 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700996 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
997 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800998 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800999 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001000 }
1001
1002 if (error) {
1003 return false;
1004 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001005
1006 outResource->value = std::move(style);
1007 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001008}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001009
Adam Lesinski467f1712015-11-16 17:35:44 -08001010bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001011 uint32_t typeMask) {
1012 const Source source = mSource.withLine(parser->getLineNumber());
1013 std::unique_ptr<Array> array = util::make_unique<Array>();
1014
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001015 bool error = false;
1016 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001017 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1018 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001019 // Skip text and comments.
1020 continue;
1021 }
1022
1023 const Source itemSource = mSource.withLine(parser->getLineNumber());
1024 const std::u16string& elementNamespace = parser->getElementNamespace();
1025 const std::u16string& elementName = parser->getElementName();
1026 if (elementNamespace.empty() && elementName == u"item") {
1027 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1028 if (!item) {
1029 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1030 error = true;
1031 continue;
1032 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001033 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001034 array->items.emplace_back(std::move(item));
1035
Adam Lesinskica5638f2015-10-21 14:42:43 -07001036 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001037 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1038 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1039 error = true;
1040 }
1041 }
1042
1043 if (error) {
1044 return false;
1045 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001046
1047 outResource->value = std::move(array);
1048 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001049}
1050
Adam Lesinski467f1712015-11-16 17:35:44 -08001051bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001052 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001053 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1054
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001055 bool error = false;
1056 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001057 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1058 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001059 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001060 continue;
1061 }
1062
Adam Lesinskica5638f2015-10-21 14:42:43 -07001063 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001064 const std::u16string& elementNamespace = parser->getElementNamespace();
1065 const std::u16string& elementName = parser->getElementName();
1066 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001067 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1068 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001069 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001070 << "'quantity'");
1071 error = true;
1072 continue;
1073 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001074
Adam Lesinski467f1712015-11-16 17:35:44 -08001075 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001076 size_t index = 0;
1077 if (trimmedQuantity == u"zero") {
1078 index = Plural::Zero;
1079 } else if (trimmedQuantity == u"one") {
1080 index = Plural::One;
1081 } else if (trimmedQuantity == u"two") {
1082 index = Plural::Two;
1083 } else if (trimmedQuantity == u"few") {
1084 index = Plural::Few;
1085 } else if (trimmedQuantity == u"many") {
1086 index = Plural::Many;
1087 } else if (trimmedQuantity == u"other") {
1088 index = Plural::Other;
1089 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001090 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001091 << "<item> in <plural> has invalid value '" << trimmedQuantity
1092 << "' for attribute 'quantity'");
1093 error = true;
1094 continue;
1095 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001096
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001097 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001098 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001099 << "duplicate quantity '" << trimmedQuantity << "'");
1100 error = true;
1101 continue;
1102 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001103
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001104 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1105 kNoRawString))) {
1106 error = true;
1107 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001108 plural->values[index]->setSource(itemSource);
1109
1110 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1111 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001112 << elementName << ">");
1113 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001114 }
1115 }
1116
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001117 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001118 return false;
1119 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001120
1121 outResource->value = std::move(plural);
1122 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001123}
1124
Adam Lesinski467f1712015-11-16 17:35:44 -08001125bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001126 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001127 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1128
Adam Lesinskib274e352015-11-06 15:14:35 -08001129 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001130 outResource->symbolState = SymbolState::kPublic;
1131
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001132 std::u16string comment;
1133 bool error = false;
1134 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001135 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1136 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001137 comment = util::trimWhitespace(parser->getComment()).toString();
1138 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001139 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001140 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001141 continue;
1142 }
1143
Adam Lesinskica5638f2015-10-21 14:42:43 -07001144 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001145 const std::u16string& elementNamespace = parser->getElementNamespace();
1146 const std::u16string& elementName = parser->getElementName();
1147 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001148 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1149 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001150 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001151 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001152 continue;
1153 }
1154
Adam Lesinski467f1712015-11-16 17:35:44 -08001155 // If this is a declaration, the package name may be in the name. Separate these out.
1156 // Eg. <attr name="android:text" />
1157 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1158 if (!maybeRef) {
1159 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1160 << maybeName.value() << "'");
1161 error = true;
1162 continue;
1163 }
1164
1165 Reference& childRef = maybeRef.value();
1166 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1167
Adam Lesinskica5638f2015-10-21 14:42:43 -07001168 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001169 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001170 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001171 childResource.source = itemSource;
1172 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001173
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001174 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001175 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001176 continue;
1177 }
1178
Adam Lesinskica5638f2015-10-21 14:42:43 -07001179 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001180 childRef.setComment(childResource.comment);
1181 childRef.setSource(itemSource);
1182 styleable->entries.push_back(std::move(childRef));
1183
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001184 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001185
Adam Lesinskica5638f2015-10-21 14:42:43 -07001186 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1187 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001188 << elementName << ">");
1189 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001190 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001191
1192 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001193 }
1194
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001195 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001196 return false;
1197 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001198
1199 outResource->value = std::move(styleable);
1200 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001201}
1202
1203} // namespace aapt