blob: 02fe59c0a03bf27adf1669610b95ff9e36f596bc [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 Lesinskiabf83cb2015-11-16 15:59:10 -0800714 struct SymbolComparator {
715 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
716 return a.symbol.name.value() < b.symbol.name.value();
717 }
718 };
719
720 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800721
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700722 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800723 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700724 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800725 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
726 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700727 comment = util::trimWhitespace(parser->getComment()).toString();
728 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800729 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700730 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800731 continue;
732 }
733
Adam Lesinskica5638f2015-10-21 14:42:43 -0700734 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700735 const std::u16string& elementNamespace = parser->getElementNamespace();
736 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700737 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700738 if (elementName == u"enum") {
739 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700740 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700741 << "can not define an <enum>; already defined a <flag>");
742 error = true;
743 continue;
744 }
745 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700746
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700747 } else if (elementName == u"flag") {
748 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700749 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700750 << "can not define a <flag>; already defined an <enum>");
751 error = true;
752 continue;
753 }
754 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800755 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800756
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700757 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800758 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700759 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800760 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700761 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700762 childResource.value = util::make_unique<Id>();
763 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700764
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800765 symbol.symbol.setComment(std::move(comment));
766 symbol.symbol.setSource(itemSource);
767
768 auto insertResult = items.insert(std::move(symbol));
769 if (!insertResult.second) {
770 const Attribute::Symbol& existingSymbol = *insertResult.first;
771 mDiag->error(DiagMessage(itemSource)
772 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
773 << "'");
774
775 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
776 << "first defined here");
777 error = true;
778 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800779 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700780 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800781 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700782 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
783 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784 error = true;
785 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700786
787 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800788 }
789
790 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700791 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800792 }
793
794 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800795 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700796 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700797 outResource->value = std::move(attr);
798 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800799}
800
Adam Lesinski467f1712015-11-16 17:35:44 -0800801Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700802 const StringPiece16& tag) {
803 const Source source = mSource.withLine(parser->getLineNumber());
804
Adam Lesinski467f1712015-11-16 17:35:44 -0800805 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700806 if (!maybeName) {
807 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
808 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800809 }
810
Adam Lesinski467f1712015-11-16 17:35:44 -0800811 Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700812 if (!maybeValue) {
813 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
814 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800815 }
816
817 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700818 if (!android::ResTable::stringToInt(maybeValue.value().data(),
819 maybeValue.value().size(), &val)) {
820 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
821 << "' for <" << tag << ">; must be an integer");
822 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800823 }
824
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700825 return Attribute::Symbol{
Adam Lesinskie78fd612015-10-22 12:48:43 -0700826 Reference(ResourceName({}, ResourceType::kId, maybeName.value().toString())),
827 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828}
829
Adam Lesinski467f1712015-11-16 17:35:44 -0800830static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800831 str = util::trimWhitespace(str);
Adam Lesinski467f1712015-11-16 17:35:44 -0800832 const char16_t* start = str.data();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833 const char16_t* const end = start + str.size();
834 const char16_t* p = start;
835
Adam Lesinski467f1712015-11-16 17:35:44 -0800836 Reference ref;
837 if (p != end && *p == u'*') {
838 ref.privateReference = true;
839 start++;
840 p++;
841 }
842
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800843 StringPiece16 package;
844 StringPiece16 name;
845 while (p != end) {
846 if (*p == u':') {
847 package = StringPiece16(start, p - start);
848 name = StringPiece16(p + 1, end - (p + 1));
849 break;
850 }
851 p++;
852 }
853
Adam Lesinski467f1712015-11-16 17:35:44 -0800854 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700855 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800856 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800857}
858
Adam Lesinski467f1712015-11-16 17:35:44 -0800859bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700860 const Source source = mSource.withLine(parser->getLineNumber());
861
Adam Lesinski467f1712015-11-16 17:35:44 -0800862 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700863 if (!maybeName) {
864 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800865 return false;
866 }
867
Adam Lesinski467f1712015-11-16 17:35:44 -0800868 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700869 if (!maybeKey) {
870 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800871 return false;
872 }
873
Adam Lesinski467f1712015-11-16 17:35:44 -0800874 transformReferenceFromNamespace(parser, u"", &maybeKey.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800876 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
877 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700878 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800879 return false;
880 }
881
Adam Lesinski467f1712015-11-16 17:35:44 -0800882 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800883 return true;
884}
885
Adam Lesinski467f1712015-11-16 17:35:44 -0800886bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700887 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700888 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800889
Adam Lesinski467f1712015-11-16 17:35:44 -0800890 Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700891 if (maybeParent) {
892 // If the parent is empty, we don't have a parent, but we also don't infer either.
893 if (!maybeParent.value().empty()) {
894 std::string errStr;
895 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
896 if (!style->parent) {
897 mDiag->error(DiagMessage(source) << errStr);
898 return false;
899 }
900
Adam Lesinski467f1712015-11-16 17:35:44 -0800901 // Transform the namespace prefix to the actual package name, and mark the reference as
902 // private if appropriate.
903 transformReferenceFromNamespace(parser, u"", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800904 }
905
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700906 } else {
907 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700908 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700909 size_t pos = styleName.find_last_of(u'.');
910 if (pos != std::string::npos) {
911 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -0800912 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
913 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700914 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800915 }
916
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800917 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700918 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800919 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
920 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700921 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800922 continue;
923 }
924
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700925 const std::u16string& elementNamespace = parser->getElementNamespace();
926 const std::u16string& elementName = parser->getElementName();
927 if (elementNamespace == u"" && elementName == u"item") {
928 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800929
Adam Lesinskica5638f2015-10-21 14:42:43 -0700930 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700931 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
932 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800933 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800934 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 }
936
937 if (error) {
938 return false;
939 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700940
941 outResource->value = std::move(style);
942 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700943}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800944
Adam Lesinski467f1712015-11-16 17:35:44 -0800945bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700946 uint32_t typeMask) {
947 const Source source = mSource.withLine(parser->getLineNumber());
948 std::unique_ptr<Array> array = util::make_unique<Array>();
949
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700950 bool error = false;
951 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800952 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
953 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700954 // Skip text and comments.
955 continue;
956 }
957
958 const Source itemSource = mSource.withLine(parser->getLineNumber());
959 const std::u16string& elementNamespace = parser->getElementNamespace();
960 const std::u16string& elementName = parser->getElementName();
961 if (elementNamespace.empty() && elementName == u"item") {
962 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
963 if (!item) {
964 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
965 error = true;
966 continue;
967 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700968 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700969 array->items.emplace_back(std::move(item));
970
Adam Lesinskica5638f2015-10-21 14:42:43 -0700971 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700972 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
973 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
974 error = true;
975 }
976 }
977
978 if (error) {
979 return false;
980 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700981
982 outResource->value = std::move(array);
983 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800984}
985
Adam Lesinski467f1712015-11-16 17:35:44 -0800986bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700987 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800988 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
989
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700990 bool error = false;
991 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800992 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
993 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700994 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800995 continue;
996 }
997
Adam Lesinskica5638f2015-10-21 14:42:43 -0700998 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700999 const std::u16string& elementNamespace = parser->getElementNamespace();
1000 const std::u16string& elementName = parser->getElementName();
1001 if (elementNamespace.empty() && elementName == u"item") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001002 Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity");
1003 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001004 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001005 << "'quantity'");
1006 error = true;
1007 continue;
1008 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001009
Adam Lesinski467f1712015-11-16 17:35:44 -08001010 StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001011 size_t index = 0;
1012 if (trimmedQuantity == u"zero") {
1013 index = Plural::Zero;
1014 } else if (trimmedQuantity == u"one") {
1015 index = Plural::One;
1016 } else if (trimmedQuantity == u"two") {
1017 index = Plural::Two;
1018 } else if (trimmedQuantity == u"few") {
1019 index = Plural::Few;
1020 } else if (trimmedQuantity == u"many") {
1021 index = Plural::Many;
1022 } else if (trimmedQuantity == u"other") {
1023 index = Plural::Other;
1024 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001025 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001026 << "<item> in <plural> has invalid value '" << trimmedQuantity
1027 << "' for attribute 'quantity'");
1028 error = true;
1029 continue;
1030 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001031
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001032 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001033 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001034 << "duplicate quantity '" << trimmedQuantity << "'");
1035 error = true;
1036 continue;
1037 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001038
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001039 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1040 kNoRawString))) {
1041 error = true;
1042 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001043 plural->values[index]->setSource(itemSource);
1044
1045 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1046 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001047 << elementName << ">");
1048 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001049 }
1050 }
1051
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001052 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001053 return false;
1054 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001055
1056 outResource->value = std::move(plural);
1057 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001058}
1059
Adam Lesinski467f1712015-11-16 17:35:44 -08001060bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001061 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001062 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1063
Adam Lesinskib274e352015-11-06 15:14:35 -08001064 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001065 outResource->symbolState = SymbolState::kPublic;
1066
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001067 std::u16string comment;
1068 bool error = false;
1069 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001070 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1071 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001072 comment = util::trimWhitespace(parser->getComment()).toString();
1073 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001074 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001075 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001076 continue;
1077 }
1078
Adam Lesinskica5638f2015-10-21 14:42:43 -07001079 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001080 const std::u16string& elementNamespace = parser->getElementNamespace();
1081 const std::u16string& elementName = parser->getElementName();
1082 if (elementNamespace.empty() && elementName == u"attr") {
Adam Lesinski467f1712015-11-16 17:35:44 -08001083 Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name");
1084 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001085 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001086 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001087 continue;
1088 }
1089
Adam Lesinski467f1712015-11-16 17:35:44 -08001090 // If this is a declaration, the package name may be in the name. Separate these out.
1091 // Eg. <attr name="android:text" />
1092 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1093 if (!maybeRef) {
1094 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1095 << maybeName.value() << "'");
1096 error = true;
1097 continue;
1098 }
1099
1100 Reference& childRef = maybeRef.value();
1101 xml::transformReferenceFromNamespace(parser, u"", &childRef);
1102
Adam Lesinskica5638f2015-10-21 14:42:43 -07001103 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001104 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001105 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001106 childResource.source = itemSource;
1107 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001108
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001109 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001110 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001111 continue;
1112 }
1113
Adam Lesinskica5638f2015-10-21 14:42:43 -07001114 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001115 childRef.setComment(childResource.comment);
1116 childRef.setSource(itemSource);
1117 styleable->entries.push_back(std::move(childRef));
1118
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001119 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001120
Adam Lesinskica5638f2015-10-21 14:42:43 -07001121 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1122 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001123 << elementName << ">");
1124 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001125 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001126
1127 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001128 }
1129
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001130 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001131 return false;
1132 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001133
1134 outResource->value = std::move(styleable);
1135 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001136}
1137
1138} // namespace aapt