blob: f2a1878d0dc5947490604178f5f927863bebe8c3 [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"
22#include "XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
Adam Lesinski9e10ac72015-10-16 14:37:48 -070024#include "util/Util.h"
25
Adam Lesinski769de982015-04-10 19:43:55 -070026#include <sstream>
27
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032static Maybe<StringPiece16> findAttribute(XmlPullParser* parser, const StringPiece16& name) {
33 auto iter = parser->findAttribute(u"", name);
34 if (iter != parser->endAttributes()) {
35 return StringPiece16(util::trimWhitespace(iter->value));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036 }
37 return {};
38}
39
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040static Maybe<StringPiece16> findNonEmptyAttribute(XmlPullParser* parser,
41 const StringPiece16& name) {
42 auto iter = parser->findAttribute(u"", name);
43 if (iter != parser->endAttributes()) {
44 StringPiece16 trimmed = util::trimWhitespace(iter->value);
45 if (!trimmed.empty()) {
46 return trimmed;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047 }
48 }
49 return {};
50}
51
Adam Lesinski27afb9e2015-11-06 18:25:04 -080052/**
53 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
54 */
55static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) {
56 return ns.empty() && (name == u"skip" || name == u"eat-comment");
57}
58
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070060 const ConfigDescription& config,
61 const ResourceParserOptions& options) :
62 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063}
64
65/**
66 * Build a string from XML that converts nested elements into Span objects.
67 */
68bool ResourceParser::flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,
69 StyleString* outStyleString) {
70 std::vector<Span> spanStack;
71
Adam Lesinskib23f1e02015-11-03 12:24:17 -080072 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 outRawString->clear();
74 outStyleString->spans.clear();
75 util::StringBuilder builder;
76 size_t depth = 1;
77 while (XmlPullParser::isGoodEvent(parser->next())) {
78 const XmlPullParser::Event event = parser->getEvent();
79 if (event == XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 if (!parser->getElementNamespace().empty()) {
81 // We already warned and skipped the start element, so just skip here too
82 continue;
83 }
84
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085 depth--;
86 if (depth == 0) {
87 break;
88 }
89
90 spanStack.back().lastChar = builder.str().size();
91 outStyleString->spans.push_back(spanStack.back());
92 spanStack.pop_back();
93
94 } else if (event == XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095 outRawString->append(parser->getText());
96 builder.append(parser->getText());
97
98 } else if (event == XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099 if (!parser->getElementNamespace().empty()) {
100 if (parser->getElementNamespace() != sXliffNamespaceUri) {
101 // Only warn if this isn't an xliff namespace.
102 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
103 << "skipping element '"
104 << parser->getElementName()
105 << "' with unknown namespace '"
106 << parser->getElementNamespace()
107 << "'");
108 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109 continue;
110 }
111 depth++;
112
113 // Build a span object out of the nested element.
114 std::u16string spanName = parser->getElementName();
115 const auto endAttrIter = parser->endAttributes();
116 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
117 spanName += u";";
118 spanName += attrIter->name;
119 spanName += u"=";
120 spanName += attrIter->value;
121 }
122
123 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
125 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800126 error = true;
127 } else {
128 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
131 } else if (event == XmlPullParser::Event::kComment) {
132 // Skip
133 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135 }
136 }
137 assert(spanStack.empty() && "spans haven't been fully processed");
138
139 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800140 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141}
142
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143bool ResourceParser::parse(XmlPullParser* parser) {
144 bool error = false;
145 const size_t depth = parser->getDepth();
146 while (XmlPullParser::nextChildNode(parser, depth)) {
147 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
148 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149 continue;
150 }
151
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
153 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
154 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155 return false;
156 }
157
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158 error |= !parseResources(parser);
159 break;
160 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 if (parser->getEvent() == XmlPullParser::Event::kBadDocument) {
163 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
164 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165 return false;
166 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168}
169
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700170static bool shouldStripResource(XmlPullParser* parser, const Maybe<std::u16string> productToMatch) {
171 assert(parser->getEvent() == XmlPullParser::Event::kStartElement);
172
173 if (Maybe<StringPiece16> maybeProduct = findNonEmptyAttribute(parser, u"product")) {
174 if (!productToMatch) {
175 if (maybeProduct.value() != u"default" && maybeProduct.value() != u"phone") {
176 // We didn't specify a product and this is not a default product, so skip.
177 return true;
178 }
179 } else {
180 if (productToMatch && maybeProduct.value() != productToMatch.value()) {
181 // We specified a product, but they don't match.
182 return true;
183 }
184 }
185 }
186 return false;
187}
188
189/**
190 * A parsed resource ready to be added to the ResourceTable.
191 */
192struct ParsedResource {
193 ResourceName name;
194 Source source;
195 ResourceId id;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700196 SymbolState symbolState = SymbolState::kUndefined;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700197 std::u16string comment;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700198 std::unique_ptr<Value> value;
199 std::list<ParsedResource> childResources;
200};
201
202// Recursively adds resources to the ResourceTable.
203static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
204 IDiagnostics* diag, ParsedResource* res) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700205 if (res->symbolState != SymbolState::kUndefined) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700206 Symbol symbol;
207 symbol.state = res->symbolState;
208 symbol.source = res->source;
209 symbol.comment = res->comment;
210 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700211 return false;
212 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700213 }
214
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800215 if (res->value) {
216 // Attach the comment, source and config to the value.
217 res->value->setComment(std::move(res->comment));
218 res->value->setSource(std::move(res->source));
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700219
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800220 if (!table->addResource(res->name, res->id, config, std::move(res->value), diag)) {
221 return false;
222 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700223 }
224
225 bool error = false;
226 for (ParsedResource& child : res->childResources) {
227 error |= !addResourcesToTable(table, config, diag, &child);
228 }
229 return !error;
230}
231
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232bool ResourceParser::parseResources(XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700233 std::set<ResourceName> strippedResources;
234
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237 const size_t depth = parser->getDepth();
238 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239 const XmlPullParser::Event event = parser->getEvent();
240 if (event == XmlPullParser::Event::kComment) {
241 comment = parser->getComment();
242 continue;
243 }
244
245 if (event == XmlPullParser::Event::kText) {
246 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
248 << "plain text not allowed here");
249 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250 }
251 continue;
252 }
253
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254 assert(event == XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257 // Skip unknown namespace.
258 continue;
259 }
260
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261 std::u16string elementName = parser->getElementName();
262 if (elementName == u"skip" || elementName == u"eat-comment") {
263 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264 continue;
265 }
266
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267 if (elementName == u"item") {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268 // Items simply have their type encoded in the type attribute.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 if (Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type")) {
270 elementName = maybeType.value().toString();
271 } else {
272 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
273 << "<item> must have a 'type' attribute");
274 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 continue;
276 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277 }
278
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700279 ParsedResource parsedResource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700280 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700281 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800283 if (Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name")) {
284 parsedResource.name.entry = maybeName.value().toString();
285
286 } else if (elementName != u"public-group") {
287 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
288 << "<" << elementName << "> tag must have a 'name' attribute");
289 error = true;
290 continue;
291 }
292
293 // Check if we should skip this product.
294 const bool stripResource = shouldStripResource(parser, mOptions.product);
295
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700296 bool result = true;
297 if (elementName == u"id") {
298 parsedResource.name.type = ResourceType::kId;
299 parsedResource.value = util::make_unique<Id>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300 } else if (elementName == u"string") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700301 parsedResource.name.type = ResourceType::kString;
302 result = parseString(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700303 } else if (elementName == u"color") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700304 parsedResource.name.type = ResourceType::kColor;
305 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306 } else if (elementName == u"drawable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700307 parsedResource.name.type = ResourceType::kDrawable;
308 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700309 } else if (elementName == u"bool") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700310 parsedResource.name.type = ResourceType::kBool;
311 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700312 } else if (elementName == u"integer") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700313 parsedResource.name.type = ResourceType::kInteger;
314 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315 } else if (elementName == u"dimen") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700316 parsedResource.name.type = ResourceType::kDimen;
317 result = parsePrimitive(parser, &parsedResource);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700318 } else if (elementName == u"fraction") {
319 parsedResource.name.type = ResourceType::kFraction;
320 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321 } else if (elementName == u"style") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700322 parsedResource.name.type = ResourceType::kStyle;
323 result = parseStyle(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700324 } else if (elementName == u"plurals") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700325 parsedResource.name.type = ResourceType::kPlurals;
326 result = parsePlural(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700327 } else if (elementName == u"array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700328 parsedResource.name.type = ResourceType::kArray;
329 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330 } else if (elementName == u"string-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700331 parsedResource.name.type = ResourceType::kArray;
332 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_STRING);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700333 } else if (elementName == u"integer-array") {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700334 parsedResource.name.type = ResourceType::kArray;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700335 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336 } else if (elementName == u"declare-styleable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700337 parsedResource.name.type = ResourceType::kStyleable;
338 result = parseDeclareStyleable(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339 } else if (elementName == u"attr") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700340 parsedResource.name.type = ResourceType::kAttr;
341 result = parseAttr(parser, &parsedResource);
342 } else if (elementName == u"public") {
343 result = parsePublic(parser, &parsedResource);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700344 } else if (elementName == u"java-symbol" || elementName == u"symbol") {
345 result = parseSymbol(parser, &parsedResource);
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800346 } else if (elementName == u"public-group") {
347 result = parsePublicGroup(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348 } else {
Adam Lesinskifa105052015-11-07 13:34:39 -0800349 // Try parsing the elementName (or type) as a resource. These shall only be
350 // resources like 'layout' or 'xml' and they can only be references.
351 if (const ResourceType* type = parseResourceType(elementName)) {
352 parsedResource.name.type = *type;
353 parsedResource.value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE,
354 false);
355 if (!parsedResource.value) {
356 mDiag->error(DiagMessage(parsedResource.source) << "invalid value for type '"
357 << *type << "'. Expected a reference");
358 result = false;
359 }
360 } else {
361 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
362 << "unknown resource type '" << elementName << "'");
363 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700364 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700365
366 if (result) {
367 // We successfully parsed the resource.
368
369 if (stripResource) {
370 // Record that we stripped out this resource name.
371 // We will check that at least one variant of this resource was included.
372 strippedResources.insert(parsedResource.name);
373 } else {
374 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
375 }
376 } else {
377 error = true;
378 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700380
381 // Check that we included at least one variant of each stripped resource.
382 for (const ResourceName& strippedResource : strippedResources) {
383 if (!mTable->findResource(strippedResource)) {
384 // Failed to find the resource.
385 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
386 "was filtered out but no product variant remains");
387 error = true;
388 }
389 }
390
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392}
393
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394enum {
395 kAllowRawString = true,
396 kNoRawString = false
397};
398
399/**
400 * Reads the entire XML subtree and attempts to parse it as some Item,
401 * with typeMask denoting which items it can be. If allowRawValue is
402 * true, a RawString is returned if the XML couldn't be parsed as
403 * an Item. If allowRawValue is false, nullptr is returned in this
404 * case.
405 */
Adam Lesinskie78fd612015-10-22 12:48:43 -0700406std::unique_ptr<Item> ResourceParser::parseXml(XmlPullParser* parser, const uint32_t typeMask,
407 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408 const size_t beginXmlLine = parser->getLineNumber();
409
410 std::u16string rawValue;
411 StyleString styleString;
412 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
413 return {};
414 }
415
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800416 if (!styleString.spans.empty()) {
417 // This can only be a StyledString.
418 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700419 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420 }
421
422 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700423 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700424 std::unique_ptr<Id> id = util::make_unique<Id>();
425 id->setSource(mSource.withLine(beginXmlLine));
426 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427 };
428
429 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700430 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
431 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800432 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700433 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700434 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
435 if (Maybe<ResourceName> transformedName =
436 parser->transformPackage(ref->name.value(), u"")) {
437 ref->name = std::move(transformedName);
Adam Lesinski24aad162015-04-24 19:19:30 -0700438 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700439 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800440 return processedItem;
441 }
442
443 // Try making a regular string.
444 if (typeMask & android::ResTable_map::TYPE_STRING) {
445 // Use the trimmed, escaped string.
446 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700447 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448 }
449
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800450 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700451 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800452 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700453 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700455
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456 return {};
457}
458
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700459bool ResourceParser::parseString(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700460 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800461
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800462 bool formatted = true;
463 if (Maybe<StringPiece16> formattedAttr = findAttribute(parser, u"formatted")) {
464 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
465 mDiag->error(DiagMessage(source) << "invalid value for 'formatted'. Must be a boolean");
466 return false;
467 }
468 }
469
Adam Lesinski9f222042015-11-04 13:51:45 -0800470 bool translateable = mOptions.translatable;
471 if (Maybe<StringPiece16> translateableAttr = findAttribute(parser, u"translatable")) {
472 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800473 mDiag->error(DiagMessage(source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800474 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800475 return false;
476 }
477 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800478
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700479 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
480 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700481 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482 return false;
483 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800484
Adam Lesinski9f222042015-11-04 13:51:45 -0800485 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800486 if (String* stringValue = valueCast<String>(outResource->value.get())) {
487 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800488 mDiag->error(DiagMessage(source)
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800489 << "multiple substitutions specified in non-positional format; "
490 "did you mean to add the formatted=\"false\" attribute?");
491 return false;
492 }
493 }
494 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700495 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496}
497
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700498bool ResourceParser::parseColor(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700499 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700501 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
502 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504 return false;
505 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700506 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507}
508
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700509bool ResourceParser::parsePrimitive(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700510 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800511
512 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700513 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700514 case ResourceType::kInteger:
515 typeMask |= android::ResTable_map::TYPE_INTEGER;
516 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800517
Adam Lesinskica5638f2015-10-21 14:42:43 -0700518 case ResourceType::kFraction:
519 // fallthrough
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700520 case ResourceType::kDimen:
521 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700522 | android::ResTable_map::TYPE_FLOAT
523 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700524 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800525
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526 case ResourceType::kBool:
527 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
528 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700530 default:
531 assert(false);
532 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533 }
534
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700535 outResource->value = parseXml(parser, typeMask, kNoRawString);
536 if (!outResource->value) {
537 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800538 return false;
539 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700540 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800541}
542
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700543bool ResourceParser::parsePublic(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700544 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800545
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700546 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
547 if (!maybeType) {
548 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549 return false;
550 }
551
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800553 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700554 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
555 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800556 return false;
557 }
558
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700559 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800560
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700561 if (Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800562 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700563 bool result = android::ResTable::stringToInt(maybeId.value().data(),
564 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700565 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800566 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700567 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
568 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800569 return false;
570 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700571 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800572 }
573
574 if (*parsedType == ResourceType::kId) {
575 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700576 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800577 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700578
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700579 outResource->symbolState = SymbolState::kPublic;
580 return true;
581}
582
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800583bool ResourceParser::parsePublicGroup(XmlPullParser* parser, ParsedResource* outResource) {
584 const Source source = mSource.withLine(parser->getLineNumber());
585
586 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
587 if (!maybeType) {
588 mDiag->error(DiagMessage(source) << "<public-group> must have a 'type' attribute");
589 return false;
590 }
591
592 const ResourceType* parsedType = parseResourceType(maybeType.value());
593 if (!parsedType) {
594 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
595 << "' in <public-group>");
596 return false;
597 }
598
599 Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"first-id");
600 if (!maybeId) {
601 mDiag->error(DiagMessage(source) << "<public-group> must have a 'first-id' attribute");
602 return false;
603 }
604
605 android::Res_value val;
606 bool result = android::ResTable::stringToInt(maybeId.value().data(),
607 maybeId.value().size(), &val);
608 ResourceId nextId(val.data);
609 if (!result || !nextId.isValid()) {
610 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
611 << "' in <public-group>");
612 return false;
613 }
614
615 std::u16string comment;
616 bool error = false;
617 const size_t depth = parser->getDepth();
618 while (XmlPullParser::nextChildNode(parser, depth)) {
619 if (parser->getEvent() == XmlPullParser::Event::kComment) {
620 comment = util::trimWhitespace(parser->getComment()).toString();
621 continue;
622 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
623 // Skip text.
624 continue;
625 }
626
627 const Source itemSource = mSource.withLine(parser->getLineNumber());
628 const std::u16string& elementNamespace = parser->getElementNamespace();
629 const std::u16string& elementName = parser->getElementName();
630 if (elementNamespace.empty() && elementName == u"public") {
631 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
632 if (!maybeName) {
633 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
634 error = true;
635 continue;
636 }
637
638 if (findNonEmptyAttribute(parser, u"id")) {
639 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
640 error = true;
641 continue;
642 }
643
644 if (findNonEmptyAttribute(parser, u"type")) {
645 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
646 error = true;
647 continue;
648 }
649
650 ParsedResource childResource;
651 childResource.name.type = *parsedType;
652 childResource.name.entry = maybeName.value().toString();
653 childResource.id = nextId;
654 childResource.comment = std::move(comment);
655 childResource.source = itemSource;
656 childResource.symbolState = SymbolState::kPublic;
657 outResource->childResources.push_back(std::move(childResource));
658
659 nextId.id += 1;
660
661 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
662 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
663 error = true;
664 }
665 }
666 return !error;
667}
668
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700669bool ResourceParser::parseSymbol(XmlPullParser* parser, ParsedResource* outResource) {
670 const Source source = mSource.withLine(parser->getLineNumber());
671
672 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
673 if (!maybeType) {
674 mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a "
675 "'type' attribute");
676 return false;
677 }
678
679 const ResourceType* parsedType = parseResourceType(maybeType.value());
680 if (!parsedType) {
681 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
682 << "' in <" << parser->getElementName() << ">");
683 return false;
684 }
685
686 outResource->name.type = *parsedType;
687 outResource->symbolState = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700688 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800689}
690
691static uint32_t parseFormatType(const StringPiece16& piece) {
692 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
693 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
694 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
695 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
696 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
697 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
698 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
699 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
700 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
701 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
702 return 0;
703}
704
705static uint32_t parseFormatAttribute(const StringPiece16& str) {
706 uint32_t mask = 0;
707 for (StringPiece16 part : util::tokenize(str, u'|')) {
708 StringPiece16 trimmedPart = util::trimWhitespace(part);
709 uint32_t type = parseFormatType(trimmedPart);
710 if (type == 0) {
711 return 0;
712 }
713 mask |= type;
714 }
715 return mask;
716}
717
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800718
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700719
720bool ResourceParser::parseAttr(XmlPullParser* parser, ParsedResource* outResource) {
721 outResource->source = mSource.withLine(parser->getLineNumber());
722 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800723}
724
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700725bool ResourceParser::parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800726 uint32_t typeMask = 0;
727
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700728 Maybe<StringPiece16> maybeFormat = findAttribute(parser, u"format");
729 if (maybeFormat) {
730 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800731 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700732 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
733 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700734 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800735 }
736 }
737
Adam Lesinski769de982015-04-10 19:43:55 -0700738 // If this is a declaration, the package name may be in the name. Separate these out.
739 // Eg. <attr name="android:text" />
740 // No format attribute is allowed.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700741 if (weak && !maybeFormat) {
Adam Lesinski769de982015-04-10 19:43:55 -0700742 StringPiece16 package, type, name;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700743 ResourceUtils::extractResourceName(outResource->name.entry, &package, &type, &name);
Adam Lesinski769de982015-04-10 19:43:55 -0700744 if (type.empty() && !package.empty()) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700745 outResource->name.package = package.toString();
746 outResource->name.entry = name.toString();
Adam Lesinski769de982015-04-10 19:43:55 -0700747 }
748 }
749
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800750 std::vector<Attribute::Symbol> items;
751
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700752 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800753 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700754 const size_t depth = parser->getDepth();
755 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700756 if (parser->getEvent() == XmlPullParser::Event::kComment) {
757 comment = util::trimWhitespace(parser->getComment()).toString();
758 continue;
759 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
760 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800761 continue;
762 }
763
Adam Lesinskica5638f2015-10-21 14:42:43 -0700764 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700765 const std::u16string& elementNamespace = parser->getElementNamespace();
766 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700767 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700768 if (elementName == u"enum") {
769 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700770 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700771 << "can not define an <enum>; already defined a <flag>");
772 error = true;
773 continue;
774 }
775 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700776
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700777 } else if (elementName == u"flag") {
778 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700779 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700780 << "can not define a <flag>; already defined an <enum>");
781 error = true;
782 continue;
783 }
784 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800785 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800786
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700787 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700788 ParsedResource childResource;
789 childResource.name = s.value().symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700790 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700791 childResource.value = util::make_unique<Id>();
792 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700793
794 s.value().symbol.setComment(std::move(comment));
795 s.value().symbol.setSource(itemSource);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700796 items.push_back(std::move(s.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800797 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700798 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800799 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700800 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
801 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800802 error = true;
803 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700804
805 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800806 }
807
808 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700809 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800810 }
811
812 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
813 attr->symbols.swap(items);
Adam Lesinskica2fc352015-04-03 12:08:26 -0700814 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700815 outResource->value = std::move(attr);
816 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800817}
818
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700819Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(XmlPullParser* parser,
820 const StringPiece16& tag) {
821 const Source source = mSource.withLine(parser->getLineNumber());
822
823 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
824 if (!maybeName) {
825 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
826 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800827 }
828
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700829 Maybe<StringPiece16> maybeValue = findNonEmptyAttribute(parser, u"value");
830 if (!maybeValue) {
831 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
832 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833 }
834
835 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700836 if (!android::ResTable::stringToInt(maybeValue.value().data(),
837 maybeValue.value().size(), &val)) {
838 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
839 << "' for <" << tag << ">; must be an integer");
840 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800841 }
842
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700843 return Attribute::Symbol{
Adam Lesinskie78fd612015-10-22 12:48:43 -0700844 Reference(ResourceName({}, ResourceType::kId, maybeName.value().toString())),
845 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800846}
847
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700848static Maybe<ResourceName> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800849 str = util::trimWhitespace(str);
850 const char16_t* const start = str.data();
851 const char16_t* const end = start + str.size();
852 const char16_t* p = start;
853
854 StringPiece16 package;
855 StringPiece16 name;
856 while (p != end) {
857 if (*p == u':') {
858 package = StringPiece16(start, p - start);
859 name = StringPiece16(p + 1, end - (p + 1));
860 break;
861 }
862 p++;
863 }
864
Adam Lesinskica5638f2015-10-21 14:42:43 -0700865 return ResourceName(package.toString(), ResourceType::kAttr,
866 name.empty() ? str.toString() : name.toString());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800867}
868
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700869bool ResourceParser::parseStyleItem(XmlPullParser* parser, Style* style) {
870 const Source source = mSource.withLine(parser->getLineNumber());
871
872 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
873 if (!maybeName) {
874 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875 return false;
876 }
877
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700878 Maybe<ResourceName> maybeKey = parseXmlAttributeName(maybeName.value());
879 if (!maybeKey) {
880 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800881 return false;
882 }
883
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700884 if (Maybe<ResourceName> transformedName = parser->transformPackage(maybeKey.value(), u"")) {
885 maybeKey = std::move(transformedName);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800886 }
887
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800888 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
889 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700890 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800891 return false;
892 }
893
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700894 style->entries.push_back(Style::Entry{ Reference(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800895 return true;
896}
897
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700898bool ResourceParser::parseStyle(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700899 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700900 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800901
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700902 Maybe<StringPiece16> maybeParent = findAttribute(parser, u"parent");
903 if (maybeParent) {
904 // If the parent is empty, we don't have a parent, but we also don't infer either.
905 if (!maybeParent.value().empty()) {
906 std::string errStr;
907 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
908 if (!style->parent) {
909 mDiag->error(DiagMessage(source) << errStr);
910 return false;
911 }
912
913 if (Maybe<ResourceName> transformedName =
914 parser->transformPackage(style->parent.value().name.value(), u"")) {
915 style->parent.value().name = std::move(transformedName);
916 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800917 }
918
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700919 } else {
920 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700921 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700922 size_t pos = styleName.find_last_of(u'.');
923 if (pos != std::string::npos) {
924 style->parentInferred = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700925 style->parent = Reference(
926 ResourceName({}, ResourceType::kStyle, styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700927 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800928 }
929
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800930 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700931 const size_t depth = parser->getDepth();
932 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800933 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700934 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 continue;
936 }
937
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700938 const std::u16string& elementNamespace = parser->getElementNamespace();
939 const std::u16string& elementName = parser->getElementName();
940 if (elementNamespace == u"" && elementName == u"item") {
941 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800942
Adam Lesinskica5638f2015-10-21 14:42:43 -0700943 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700944 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
945 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800946 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800947 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948 }
949
950 if (error) {
951 return false;
952 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700953
954 outResource->value = std::move(style);
955 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700956}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800957
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700958bool ResourceParser::parseArray(XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700959 uint32_t typeMask) {
960 const Source source = mSource.withLine(parser->getLineNumber());
961 std::unique_ptr<Array> array = util::make_unique<Array>();
962
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700963 bool error = false;
964 const size_t depth = parser->getDepth();
965 while (XmlPullParser::nextChildNode(parser, depth)) {
966 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
967 // Skip text and comments.
968 continue;
969 }
970
971 const Source itemSource = mSource.withLine(parser->getLineNumber());
972 const std::u16string& elementNamespace = parser->getElementNamespace();
973 const std::u16string& elementName = parser->getElementName();
974 if (elementNamespace.empty() && elementName == u"item") {
975 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
976 if (!item) {
977 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
978 error = true;
979 continue;
980 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700981 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700982 array->items.emplace_back(std::move(item));
983
Adam Lesinskica5638f2015-10-21 14:42:43 -0700984 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700985 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
986 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
987 error = true;
988 }
989 }
990
991 if (error) {
992 return false;
993 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700994
995 outResource->value = std::move(array);
996 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800997}
998
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700999bool ResourceParser::parsePlural(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001000 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001001 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1002
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001003 bool error = false;
1004 const size_t depth = parser->getDepth();
1005 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001006 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001007 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001008 continue;
1009 }
1010
Adam Lesinskica5638f2015-10-21 14:42:43 -07001011 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001012 const std::u16string& elementNamespace = parser->getElementNamespace();
1013 const std::u16string& elementName = parser->getElementName();
1014 if (elementNamespace.empty() && elementName == u"item") {
1015 const auto endAttrIter = parser->endAttributes();
1016 auto attrIter = parser->findAttribute(u"", u"quantity");
1017 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001018 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001019 << "'quantity'");
1020 error = true;
1021 continue;
1022 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001023
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001024 StringPiece16 trimmedQuantity = util::trimWhitespace(attrIter->value);
1025 size_t index = 0;
1026 if (trimmedQuantity == u"zero") {
1027 index = Plural::Zero;
1028 } else if (trimmedQuantity == u"one") {
1029 index = Plural::One;
1030 } else if (trimmedQuantity == u"two") {
1031 index = Plural::Two;
1032 } else if (trimmedQuantity == u"few") {
1033 index = Plural::Few;
1034 } else if (trimmedQuantity == u"many") {
1035 index = Plural::Many;
1036 } else if (trimmedQuantity == u"other") {
1037 index = Plural::Other;
1038 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001039 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001040 << "<item> in <plural> has invalid value '" << trimmedQuantity
1041 << "' for attribute 'quantity'");
1042 error = true;
1043 continue;
1044 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001045
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001046 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001047 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001048 << "duplicate quantity '" << trimmedQuantity << "'");
1049 error = true;
1050 continue;
1051 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001052
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001053 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1054 kNoRawString))) {
1055 error = true;
1056 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001057 plural->values[index]->setSource(itemSource);
1058
1059 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1060 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001061 << elementName << ">");
1062 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001063 }
1064 }
1065
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001066 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001067 return false;
1068 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001069
1070 outResource->value = std::move(plural);
1071 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001072}
1073
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001074bool ResourceParser::parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001075 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001076 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1077
Adam Lesinskib274e352015-11-06 15:14:35 -08001078 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001079 outResource->symbolState = SymbolState::kPublic;
1080
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001081 std::u16string comment;
1082 bool error = false;
1083 const size_t depth = parser->getDepth();
1084 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001085 if (parser->getEvent() == XmlPullParser::Event::kComment) {
1086 comment = util::trimWhitespace(parser->getComment()).toString();
1087 continue;
1088 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
1089 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001090 continue;
1091 }
1092
Adam Lesinskica5638f2015-10-21 14:42:43 -07001093 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001094 const std::u16string& elementNamespace = parser->getElementNamespace();
1095 const std::u16string& elementName = parser->getElementName();
1096 if (elementNamespace.empty() && elementName == u"attr") {
1097 const auto endAttrIter = parser->endAttributes();
1098 auto attrIter = parser->findAttribute(u"", u"name");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001099 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001100 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001101 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001102 continue;
1103 }
1104
Adam Lesinskica5638f2015-10-21 14:42:43 -07001105 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001106 ParsedResource childResource;
1107 childResource.name = ResourceName({}, ResourceType::kAttr, attrIter->value);
Adam Lesinskica5638f2015-10-21 14:42:43 -07001108 childResource.source = itemSource;
1109 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001110
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001111 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001112 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001113 continue;
1114 }
1115
Adam Lesinskica5638f2015-10-21 14:42:43 -07001116 // Create the reference to this attribute.
1117 Reference childRef(childResource.name);
1118 childRef.setComment(childResource.comment);
1119 childRef.setSource(itemSource);
1120 styleable->entries.push_back(std::move(childRef));
1121
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001122 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001123
Adam Lesinskica5638f2015-10-21 14:42:43 -07001124 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1125 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001126 << elementName << ">");
1127 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001128 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001129
1130 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001131 }
1132
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001133 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001134 return false;
1135 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001136
1137 outResource->value = std::move(styleable);
1138 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001139}
1140
1141} // namespace aapt