blob: a144c6ab2be76102aa484723a7c90c6b3a86ecbe [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 Lesinski7ff3ee12015-12-14 16:08:50 -080022#include "util/ImmutableMap.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070023#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080024#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070025
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080026#include <functional>
Adam Lesinski769de982015-04-10 19:43:55 -070027#include <sstream>
28
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029namespace aapt {
30
Adam Lesinskid0f116b2016-07-08 15:00:32 -070031constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032
Adam Lesinski27afb9e2015-11-06 18:25:04 -080033/**
34 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
35 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070036static bool shouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
37 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080038}
39
Adam Lesinskid0f116b2016-07-08 15:00:32 -070040static uint32_t parseFormatType(const StringPiece& piece) {
41 if (piece == "reference") return android::ResTable_map::TYPE_REFERENCE;
42 else if (piece == "string") return android::ResTable_map::TYPE_STRING;
43 else if (piece == "integer") return android::ResTable_map::TYPE_INTEGER;
44 else if (piece == "boolean") return android::ResTable_map::TYPE_BOOLEAN;
45 else if (piece == "color") return android::ResTable_map::TYPE_COLOR;
46 else if (piece == "float") return android::ResTable_map::TYPE_FLOAT;
47 else if (piece == "dimension") return android::ResTable_map::TYPE_DIMENSION;
48 else if (piece == "fraction") return android::ResTable_map::TYPE_FRACTION;
49 else if (piece == "enum") return android::ResTable_map::TYPE_ENUM;
50 else if (piece == "flags") return android::ResTable_map::TYPE_FLAGS;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080051 return 0;
52}
53
Adam Lesinskid0f116b2016-07-08 15:00:32 -070054static uint32_t parseFormatAttribute(const StringPiece& str) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080055 uint32_t mask = 0;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070056 for (StringPiece part : util::tokenize(str, '|')) {
57 StringPiece trimmedPart = util::trimWhitespace(part);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080058 uint32_t type = parseFormatType(trimmedPart);
59 if (type == 0) {
60 return 0;
61 }
62 mask |= type;
63 }
64 return mask;
65}
66
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080067/**
68 * A parsed resource ready to be added to the ResourceTable.
69 */
70struct ParsedResource {
71 ResourceName name;
Adam Lesinski52364f72016-01-11 13:10:24 -080072 ConfigDescription config;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080073 std::string product;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080074 Source source;
75 ResourceId id;
76 Maybe<SymbolState> symbolState;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070077 std::string comment;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080078 std::unique_ptr<Value> value;
79 std::list<ParsedResource> childResources;
80};
81
82// Recursively adds resources to the ResourceTable.
Adam Lesinski52364f72016-01-11 13:10:24 -080083static bool addResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070084 StringPiece trimmedComment = util::trimWhitespace(res->comment);
Adam Lesinski7656554f2016-03-10 21:55:04 -080085 if (trimmedComment.size() != res->comment.size()) {
86 // Only if there was a change do we re-assign.
87 res->comment = trimmedComment.toString();
88 }
89
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090 if (res->symbolState) {
91 Symbol symbol;
92 symbol.state = res->symbolState.value();
93 symbol.source = res->source;
94 symbol.comment = res->comment;
95 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
96 return false;
97 }
98 }
99
100 if (res->value) {
101 // Attach the comment, source and config to the value.
102 res->value->setComment(std::move(res->comment));
103 res->value->setSource(std::move(res->source));
104
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800105 if (!table->addResource(res->name, res->id, res->config, res->product,
106 std::move(res->value), diag)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800107 return false;
108 }
109 }
110
111 bool error = false;
112 for (ParsedResource& child : res->childResources) {
Adam Lesinski52364f72016-01-11 13:10:24 -0800113 error |= !addResourcesToTable(table, diag, &child);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800114 }
115 return !error;
116}
117
118// Convenient aliases for more readable function calls.
119enum {
120 kAllowRawString = true,
121 kNoRawString = false
122};
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700125 const ConfigDescription& config,
126 const ResourceParserOptions& options) :
127 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128}
129
130/**
131 * Build a string from XML that converts nested elements into Span objects.
132 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700133bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::string* outRawString,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134 StyleString* outStyleString) {
135 std::vector<Span> spanStack;
136
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800137 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138 outRawString->clear();
139 outStyleString->spans.clear();
140 util::StringBuilder builder;
141 size_t depth = 1;
Adam Lesinski467f1712015-11-16 17:35:44 -0800142 while (xml::XmlPullParser::isGoodEvent(parser->next())) {
143 const xml::XmlPullParser::Event event = parser->getEvent();
144 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145 if (!parser->getElementNamespace().empty()) {
146 // We already warned and skipped the start element, so just skip here too
147 continue;
148 }
149
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150 depth--;
151 if (depth == 0) {
152 break;
153 }
154
Adam Lesinski458b8772016-04-25 14:20:21 -0700155 spanStack.back().lastChar = builder.str().size() - 1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156 outStyleString->spans.push_back(spanStack.back());
157 spanStack.pop_back();
158
Adam Lesinski467f1712015-11-16 17:35:44 -0800159 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160 outRawString->append(parser->getText());
161 builder.append(parser->getText());
162
Adam Lesinski467f1712015-11-16 17:35:44 -0800163 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164 if (!parser->getElementNamespace().empty()) {
165 if (parser->getElementNamespace() != sXliffNamespaceUri) {
166 // Only warn if this isn't an xliff namespace.
167 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
168 << "skipping element '"
169 << parser->getElementName()
170 << "' with unknown namespace '"
171 << parser->getElementNamespace()
172 << "'");
173 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174 continue;
175 }
176 depth++;
177
178 // Build a span object out of the nested element.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700179 std::string spanName = parser->getElementName();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180 const auto endAttrIter = parser->endAttributes();
181 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700182 spanName += ";";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183 spanName += attrIter->name;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700184 spanName += "=";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185 spanName += attrIter->value;
186 }
187
188 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
190 << "style string '" << builder.str() << "' is too long");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800191 error = true;
192 } else {
193 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinski467f1712015-11-16 17:35:44 -0800196 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197 // Skip
198 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200 }
201 }
202 assert(spanStack.empty() && "spans haven't been fully processed");
203
204 outStyleString->str = builder.str();
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800205 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206}
207
Adam Lesinski467f1712015-11-16 17:35:44 -0800208bool ResourceParser::parse(xml::XmlPullParser* parser) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209 bool error = false;
210 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800211 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
212 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214 continue;
215 }
216
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700217 if (!parser->getElementNamespace().empty() || parser->getElementName() != "resources") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
219 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220 return false;
221 }
222
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223 error |= !parseResources(parser);
224 break;
225 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinski467f1712015-11-16 17:35:44 -0800227 if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700228 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
229 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230 return false;
231 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233}
234
Adam Lesinski467f1712015-11-16 17:35:44 -0800235bool ResourceParser::parseResources(xml::XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700236 std::set<ResourceName> strippedResources;
237
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238 bool error = false;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700239 std::string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800241 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
242 const xml::XmlPullParser::Event event = parser->getEvent();
243 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244 comment = parser->getComment();
245 continue;
246 }
247
Adam Lesinski467f1712015-11-16 17:35:44 -0800248 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
251 << "plain text not allowed here");
252 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253 }
254 continue;
255 }
256
Adam Lesinski467f1712015-11-16 17:35:44 -0800257 assert(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260 // Skip unknown namespace.
261 continue;
262 }
263
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700264 std::string elementName = parser->getElementName();
265 if (elementName == "skip" || elementName == "eat-comment") {
266 comment = "";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267 continue;
268 }
269
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700270 ParsedResource parsedResource;
Adam Lesinski52364f72016-01-11 13:10:24 -0800271 parsedResource.config = mConfig;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700272 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700273 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinski7751afc2016-01-06 15:45:28 -0800275 // Extract the product name if it exists.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700276 if (Maybe<StringPiece> maybeProduct = xml::findNonEmptyAttribute(parser, "product")) {
277 parsedResource.product = maybeProduct.value().toString();
Adam Lesinski7751afc2016-01-06 15:45:28 -0800278 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800279
Adam Lesinski7751afc2016-01-06 15:45:28 -0800280 // Parse the resource regardless of product.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800281 if (!parseResource(parser, &parsedResource)) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800282 error = true;
283 continue;
284 }
285
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800286 if (!addResourcesToTable(mTable, mDiag, &parsedResource)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700287 error = true;
288 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700290
291 // Check that we included at least one variant of each stripped resource.
292 for (const ResourceName& strippedResource : strippedResources) {
293 if (!mTable->findResource(strippedResource)) {
294 // Failed to find the resource.
295 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
296 "was filtered out but no product variant remains");
297 error = true;
298 }
299 }
300
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302}
303
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800304
305bool ResourceParser::parseResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
306 struct ItemTypeFormat {
307 ResourceType type;
308 uint32_t format;
309 };
310
311 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, ParsedResource*)>;
312
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700313 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::createPreSorted({
314 { "bool", { ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN } },
315 { "color", { ResourceType::kColor, android::ResTable_map::TYPE_COLOR } },
316 { "dimen", { ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800317 | android::ResTable_map::TYPE_FRACTION
318 | android::ResTable_map::TYPE_DIMENSION } },
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700319 { "drawable", { ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR } },
320 { "fraction", { ResourceType::kFraction, android::ResTable_map::TYPE_FLOAT
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800321 | android::ResTable_map::TYPE_FRACTION
322 | android::ResTable_map::TYPE_DIMENSION } },
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700323 { "integer", { ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER } },
324 { "string", { ResourceType::kString, android::ResTable_map::TYPE_STRING } },
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800325 });
326
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700327 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::createPreSorted({
328 { "add-resource", std::mem_fn(&ResourceParser::parseAddResource) },
329 { "array", std::mem_fn(&ResourceParser::parseArray) },
330 { "attr", std::mem_fn(&ResourceParser::parseAttr) },
331 { "declare-styleable", std::mem_fn(&ResourceParser::parseDeclareStyleable) },
332 { "integer-array", std::mem_fn(&ResourceParser::parseIntegerArray) },
333 { "java-symbol", std::mem_fn(&ResourceParser::parseSymbol) },
334 { "plurals", std::mem_fn(&ResourceParser::parsePlural) },
335 { "public", std::mem_fn(&ResourceParser::parsePublic) },
336 { "public-group", std::mem_fn(&ResourceParser::parsePublicGroup) },
337 { "string-array", std::mem_fn(&ResourceParser::parseStringArray) },
338 { "style", std::mem_fn(&ResourceParser::parseStyle) },
339 { "symbol", std::mem_fn(&ResourceParser::parseSymbol) },
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800340 });
341
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700342 std::string resourceType = parser->getElementName();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800343
344 // The value format accepted for this resource.
345 uint32_t resourceFormat = 0u;
346
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700347 if (resourceType == "item") {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800348 // Items have their type encoded in the type attribute.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700349 if (Maybe<StringPiece> maybeType = xml::findNonEmptyAttribute(parser, "type")) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800350 resourceType = maybeType.value().toString();
351 } else {
352 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
353 << "<item> must have a 'type' attribute");
354 return false;
355 }
356
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700357 if (Maybe<StringPiece> maybeFormat = xml::findNonEmptyAttribute(parser, "format")) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800358 // An explicit format for this resource was specified. The resource will retain
359 // its type in its name, but the accepted value for this type is overridden.
360 resourceFormat = parseFormatType(maybeFormat.value());
361 if (!resourceFormat) {
362 mDiag->error(DiagMessage(outResource->source)
363 << "'" << maybeFormat.value() << "' is an invalid format");
364 return false;
365 }
366 }
367 }
368
369 // Get the name of the resource. This will be checked later, because not all
370 // XML elements require a name.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700371 Maybe<StringPiece> maybeName = xml::findNonEmptyAttribute(parser, "name");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800372
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700373 if (resourceType == "id") {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800374 if (!maybeName) {
375 mDiag->error(DiagMessage(outResource->source)
376 << "<" << parser->getElementName() << "> missing 'name' attribute");
377 return false;
378 }
379
380 outResource->name.type = ResourceType::kId;
381 outResource->name.entry = maybeName.value().toString();
382 outResource->value = util::make_unique<Id>();
383 return true;
384 }
385
386 const auto itemIter = elToItemMap.find(resourceType);
387 if (itemIter != elToItemMap.end()) {
388 // This is an item, record its type and format and start parsing.
389
390 if (!maybeName) {
391 mDiag->error(DiagMessage(outResource->source)
392 << "<" << parser->getElementName() << "> missing 'name' attribute");
393 return false;
394 }
395
396 outResource->name.type = itemIter->second.type;
397 outResource->name.entry = maybeName.value().toString();
398
399 // Only use the implicit format for this type if it wasn't overridden.
400 if (!resourceFormat) {
401 resourceFormat = itemIter->second.format;
402 }
403
404 if (!parseItem(parser, outResource, resourceFormat)) {
405 return false;
406 }
407 return true;
408 }
409
410 // This might be a bag or something.
411 const auto bagIter = elToBagMap.find(resourceType);
412 if (bagIter != elToBagMap.end()) {
413 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700414 if (resourceType != "public-group") {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800415 if (!maybeName) {
416 mDiag->error(DiagMessage(outResource->source)
417 << "<" << parser->getElementName() << "> missing 'name' attribute");
418 return false;
419 }
420
421 outResource->name.entry = maybeName.value().toString();
422 }
423
424 // Call the associated parse method. The type will be filled in by the
425 // parse func.
426 if (!bagIter->second(this, parser, outResource)) {
427 return false;
428 }
429 return true;
430 }
431
432 // Try parsing the elementName (or type) as a resource. These shall only be
433 // resources like 'layout' or 'xml' and they can only be references.
434 const ResourceType* parsedType = parseResourceType(resourceType);
435 if (parsedType) {
436 if (!maybeName) {
437 mDiag->error(DiagMessage(outResource->source)
438 << "<" << parser->getElementName() << "> missing 'name' attribute");
439 return false;
440 }
441
442 outResource->name.type = *parsedType;
443 outResource->name.entry = maybeName.value().toString();
444 outResource->value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
445 if (!outResource->value) {
446 mDiag->error(DiagMessage(outResource->source)
447 << "invalid value for type '" << *parsedType << "'. Expected a reference");
448 return false;
449 }
450 return true;
451 }
452
453 mDiag->warn(DiagMessage(outResource->source)
454 << "unknown resource type '" << parser->getElementName() << "'");
455 return false;
456}
457
458bool ResourceParser::parseItem(xml::XmlPullParser* parser, ParsedResource* outResource,
459 const uint32_t format) {
460 if (format == android::ResTable_map::TYPE_STRING) {
461 return parseString(parser, outResource);
462 }
463
464 outResource->value = parseXml(parser, format, kNoRawString);
465 if (!outResource->value) {
466 mDiag->error(DiagMessage(outResource->source) << "invalid " << outResource->name.type);
467 return false;
468 }
469 return true;
470}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800471
472/**
473 * Reads the entire XML subtree and attempts to parse it as some Item,
474 * with typeMask denoting which items it can be. If allowRawValue is
475 * true, a RawString is returned if the XML couldn't be parsed as
476 * an Item. If allowRawValue is false, nullptr is returned in this
477 * case.
478 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800479std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700480 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481 const size_t beginXmlLine = parser->getLineNumber();
482
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700483 std::string rawValue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484 StyleString styleString;
485 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
486 return {};
487 }
488
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800489 if (!styleString.spans.empty()) {
490 // This can only be a StyledString.
491 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493 }
494
495 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700496 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700497 std::unique_ptr<Id> id = util::make_unique<Id>();
498 id->setSource(mSource.withLine(beginXmlLine));
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800499 mTable->addResource(name, {}, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500 };
501
502 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
504 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700506 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700508 transformReferenceFromNamespace(parser, "", ref);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700509 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800510 return processedItem;
511 }
512
513 // Try making a regular string.
514 if (typeMask & android::ResTable_map::TYPE_STRING) {
515 // Use the trimmed, escaped string.
516 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518 }
519
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700521 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800522 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700523 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524 }
525 return {};
526}
527
Adam Lesinski467f1712015-11-16 17:35:44 -0800528bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800529 bool formatted = true;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700530 if (Maybe<StringPiece> formattedAttr = xml::findAttribute(parser, "formatted")) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800531 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800532 mDiag->error(DiagMessage(outResource->source)
533 << "invalid value for 'formatted'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800534 return false;
535 }
536 }
537
Adam Lesinski9f222042015-11-04 13:51:45 -0800538 bool translateable = mOptions.translatable;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700539 if (Maybe<StringPiece> translateableAttr = xml::findAttribute(parser, "translatable")) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800540 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800541 mDiag->error(DiagMessage(outResource->source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800542 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800543 return false;
544 }
545 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800546
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700547 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
548 if (!outResource->value) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800549 mDiag->error(DiagMessage(outResource->source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800550 return false;
551 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800552
Adam Lesinski393b5f02015-12-17 13:03:11 -0800553 if (String* stringValue = valueCast<String>(outResource->value.get())) {
554 stringValue->setTranslateable(translateable);
555
556 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800557 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski979ccb22016-01-11 10:42:19 -0800558 DiagMessage msg(outResource->source);
559 msg << "multiple substitutions specified in non-positional format; "
560 "did you mean to add the formatted=\"false\" attribute?";
561 if (mOptions.errorOnPositionalArguments) {
562 mDiag->error(msg);
563 return false;
564 }
565
566 mDiag->warn(msg);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800567 }
568 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800569
570 } else if (StyledString* stringValue = valueCast<StyledString>(outResource->value.get())) {
571 stringValue->setTranslateable(translateable);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800572 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700573 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574}
575
Adam Lesinski467f1712015-11-16 17:35:44 -0800576bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700577 Maybe<StringPiece> maybeType = xml::findNonEmptyAttribute(parser, "type");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800579 mDiag->error(DiagMessage(outResource->source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800580 return false;
581 }
582
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700583 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800585 mDiag->error(DiagMessage(outResource->source)
586 << "invalid resource type '" << maybeType.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800587 return false;
588 }
589
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700590 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800591
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700592 if (Maybe<StringPiece> maybeIdStr = xml::findNonEmptyAttribute(parser, "id")) {
593 Maybe<ResourceId> maybeId = ResourceUtils::tryParseResourceId(maybeIdStr.value());
594 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800595 mDiag->error(DiagMessage(outResource->source)
596 << "invalid resource ID '" << maybeId.value() << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597 return false;
598 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700599 outResource->id = maybeId.value();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800600 }
601
602 if (*parsedType == ResourceType::kId) {
603 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700604 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800605 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700606
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700607 outResource->symbolState = SymbolState::kPublic;
608 return true;
609}
610
Adam Lesinski467f1712015-11-16 17:35:44 -0800611bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700612 Maybe<StringPiece> maybeType = xml::findNonEmptyAttribute(parser, "type");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800613 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800614 mDiag->error(DiagMessage(outResource->source)
615 << "<public-group> must have a 'type' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800616 return false;
617 }
618
619 const ResourceType* parsedType = parseResourceType(maybeType.value());
620 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800621 mDiag->error(DiagMessage(outResource->source)
622 << "invalid resource type '" << maybeType.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800623 return false;
624 }
625
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700626 Maybe<StringPiece> maybeIdStr = xml::findNonEmptyAttribute(parser, "first-id");
627 if (!maybeIdStr) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800628 mDiag->error(DiagMessage(outResource->source)
629 << "<public-group> must have a 'first-id' attribute");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800630 return false;
631 }
632
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700633 Maybe<ResourceId> maybeId = ResourceUtils::tryParseResourceId(maybeIdStr.value());
634 if (!maybeId) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800635 mDiag->error(DiagMessage(outResource->source)
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700636 << "invalid resource ID '" << maybeIdStr.value() << "' in <public-group>");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800637 return false;
638 }
639
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700640 ResourceId nextId = maybeId.value();
641
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700642 std::string comment;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800643 bool error = false;
644 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800645 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
646 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800647 comment = util::trimWhitespace(parser->getComment()).toString();
648 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800649 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800650 // Skip text.
651 continue;
652 }
653
654 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700655 const std::string& elementNamespace = parser->getElementNamespace();
656 const std::string& elementName = parser->getElementName();
657 if (elementNamespace.empty() && elementName == "public") {
658 Maybe<StringPiece> maybeName = xml::findNonEmptyAttribute(parser, "name");
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800659 if (!maybeName) {
660 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
661 error = true;
662 continue;
663 }
664
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700665 if (xml::findNonEmptyAttribute(parser, "id")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800666 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
667 error = true;
668 continue;
669 }
670
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700671 if (xml::findNonEmptyAttribute(parser, "type")) {
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800672 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
673 error = true;
674 continue;
675 }
676
677 ParsedResource childResource;
678 childResource.name.type = *parsedType;
679 childResource.name.entry = maybeName.value().toString();
680 childResource.id = nextId;
681 childResource.comment = std::move(comment);
682 childResource.source = itemSource;
683 childResource.symbolState = SymbolState::kPublic;
684 outResource->childResources.push_back(std::move(childResource));
685
686 nextId.id += 1;
687
688 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
689 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
690 error = true;
691 }
692 }
693 return !error;
694}
695
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800696bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700697 Maybe<StringPiece> maybeType = xml::findNonEmptyAttribute(parser, "type");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700698 if (!maybeType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800699 mDiag->error(DiagMessage(outResource->source)
700 << "<" << parser->getElementName() << "> must have a 'type' attribute");
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700701 return false;
702 }
703
704 const ResourceType* parsedType = parseResourceType(maybeType.value());
705 if (!parsedType) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800706 mDiag->error(DiagMessage(outResource->source)
707 << "invalid resource type '" << maybeType.value()
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700708 << "' in <" << parser->getElementName() << ">");
709 return false;
710 }
711
712 outResource->name.type = *parsedType;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700713 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800714}
715
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800716bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) {
717 if (parseSymbolImpl(parser, outResource)) {
718 outResource->symbolState = SymbolState::kPrivate;
719 return true;
720 }
721 return false;
722}
723
724bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) {
725 if (parseSymbolImpl(parser, outResource)) {
726 outResource->symbolState = SymbolState::kUndefined;
727 return true;
728 }
729 return false;
730}
731
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732
Adam Lesinski467f1712015-11-16 17:35:44 -0800733bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700734 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800735}
736
Adam Lesinski467f1712015-11-16 17:35:44 -0800737bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
738 bool weak) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800739 outResource->name.type = ResourceType::kAttr;
740
Adam Lesinski52364f72016-01-11 13:10:24 -0800741 // Attributes only end up in default configuration.
742 if (outResource->config != ConfigDescription::defaultConfig()) {
743 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
744 << outResource->config << "' for attribute " << outResource->name);
745 outResource->config = ConfigDescription::defaultConfig();
746 }
747
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800748 uint32_t typeMask = 0;
749
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700750 Maybe<StringPiece> maybeFormat = xml::findAttribute(parser, "format");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700751 if (maybeFormat) {
752 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800753 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700754 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
755 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700756 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757 }
758 }
759
Adam Lesinskia5870652015-11-20 15:32:30 -0800760 Maybe<int32_t> maybeMin, maybeMax;
761
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700762 if (Maybe<StringPiece> maybeMinStr = xml::findAttribute(parser, "min")) {
763 StringPiece minStr = util::trimWhitespace(maybeMinStr.value());
Adam Lesinskia5870652015-11-20 15:32:30 -0800764 if (!minStr.empty()) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700765 std::u16string minStr16 = util::utf8ToUtf16(minStr);
Adam Lesinskia5870652015-11-20 15:32:30 -0800766 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700767 if (android::ResTable::stringToInt(minStr16.data(), minStr16.size(), &value)) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800768 maybeMin = static_cast<int32_t>(value.data);
769 }
770 }
771
772 if (!maybeMin) {
773 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
774 << "invalid 'min' value '" << minStr << "'");
775 return false;
776 }
777 }
778
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700779 if (Maybe<StringPiece> maybeMaxStr = xml::findAttribute(parser, "max")) {
780 StringPiece maxStr = util::trimWhitespace(maybeMaxStr.value());
Adam Lesinskia5870652015-11-20 15:32:30 -0800781 if (!maxStr.empty()) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700782 std::u16string maxStr16 = util::utf8ToUtf16(maxStr);
Adam Lesinskia5870652015-11-20 15:32:30 -0800783 android::Res_value value;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700784 if (android::ResTable::stringToInt(maxStr16.data(), maxStr16.size(), &value)) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800785 maybeMax = static_cast<int32_t>(value.data);
786 }
787 }
788
789 if (!maybeMax) {
790 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
791 << "invalid 'max' value '" << maxStr << "'");
792 return false;
793 }
794 }
795
796 if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) {
797 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
798 << "'min' and 'max' can only be used when format='integer'");
799 return false;
800 }
801
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800802 struct SymbolComparator {
803 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
804 return a.symbol.name.value() < b.symbol.name.value();
805 }
806 };
807
808 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800809
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700810 std::string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800811 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700812 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -0800813 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
814 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700815 comment = util::trimWhitespace(parser->getComment()).toString();
816 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -0800817 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700818 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800819 continue;
820 }
821
Adam Lesinskica5638f2015-10-21 14:42:43 -0700822 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700823 const std::string& elementNamespace = parser->getElementNamespace();
824 const std::string& elementName = parser->getElementName();
825 if (elementNamespace.empty() && (elementName == "flag" || elementName == "enum")) {
826 if (elementName == "enum") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700827 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700828 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700829 << "can not define an <enum>; already defined a <flag>");
830 error = true;
831 continue;
832 }
833 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700834
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700835 } else if (elementName == "flag") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700836 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700837 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700838 << "can not define a <flag>; already defined an <enum>");
839 error = true;
840 continue;
841 }
842 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800843 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800844
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700845 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800846 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700847 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800848 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700849 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700850 childResource.value = util::make_unique<Id>();
851 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700852
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800853 symbol.symbol.setComment(std::move(comment));
854 symbol.symbol.setSource(itemSource);
855
856 auto insertResult = items.insert(std::move(symbol));
857 if (!insertResult.second) {
858 const Attribute::Symbol& existingSymbol = *insertResult.first;
859 mDiag->error(DiagMessage(itemSource)
860 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
861 << "'");
862
863 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
864 << "first defined here");
865 error = true;
866 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800867 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700868 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800869 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700870 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
871 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800872 error = true;
873 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700874
875 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800876 }
877
878 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700879 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800880 }
881
882 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800883 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700884 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinskia5870652015-11-20 15:32:30 -0800885 if (maybeMin) {
886 attr->minInt = maybeMin.value();
887 }
888
889 if (maybeMax) {
890 attr->maxInt = maybeMax.value();
891 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700892 outResource->value = std::move(attr);
893 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800894}
895
Adam Lesinski467f1712015-11-16 17:35:44 -0800896Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700897 const StringPiece& tag) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700898 const Source source = mSource.withLine(parser->getLineNumber());
899
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700900 Maybe<StringPiece> maybeName = xml::findNonEmptyAttribute(parser, "name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700901 if (!maybeName) {
902 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
903 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800904 }
905
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700906 Maybe<StringPiece> maybeValue = xml::findNonEmptyAttribute(parser, "value");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700907 if (!maybeValue) {
908 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
909 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800910 }
911
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700912 std::u16string value16 = util::utf8ToUtf16(maybeValue.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800913 android::Res_value val;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700914 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700915 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
916 << "' for <" << tag << ">; must be an integer");
917 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800918 }
919
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700920 return Attribute::Symbol{
Adam Lesinski52364f72016-01-11 13:10:24 -0800921 Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())), val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800922}
923
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700924static Maybe<Reference> parseXmlAttributeName(StringPiece str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800925 str = util::trimWhitespace(str);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700926 const char* start = str.data();
927 const char* const end = start + str.size();
928 const char* p = start;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800929
Adam Lesinski467f1712015-11-16 17:35:44 -0800930 Reference ref;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700931 if (p != end && *p == '*') {
Adam Lesinski467f1712015-11-16 17:35:44 -0800932 ref.privateReference = true;
933 start++;
934 p++;
935 }
936
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700937 StringPiece package;
938 StringPiece name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800939 while (p != end) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700940 if (*p == ':') {
941 package = StringPiece(start, p - start);
942 name = StringPiece(p + 1, end - (p + 1));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800943 break;
944 }
945 p++;
946 }
947
Adam Lesinski467f1712015-11-16 17:35:44 -0800948 ref.name = ResourceName(package.toString(), ResourceType::kAttr,
Adam Lesinskica5638f2015-10-21 14:42:43 -0700949 name.empty() ? str.toString() : name.toString());
Adam Lesinski467f1712015-11-16 17:35:44 -0800950 return Maybe<Reference>(std::move(ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800951}
952
Adam Lesinski467f1712015-11-16 17:35:44 -0800953bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700954 const Source source = mSource.withLine(parser->getLineNumber());
955
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700956 Maybe<StringPiece> maybeName = xml::findNonEmptyAttribute(parser, "name");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700957 if (!maybeName) {
958 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800959 return false;
960 }
961
Adam Lesinski467f1712015-11-16 17:35:44 -0800962 Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700963 if (!maybeKey) {
964 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800965 return false;
966 }
967
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700968 transformReferenceFromNamespace(parser, "", &maybeKey.value());
Adam Lesinski28cacf02015-11-23 14:22:47 -0800969 maybeKey.value().setSource(source);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800970
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800971 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
972 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700973 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800974 return false;
975 }
976
Adam Lesinski467f1712015-11-16 17:35:44 -0800977 style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800978 return true;
979}
980
Adam Lesinski467f1712015-11-16 17:35:44 -0800981bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800982 outResource->name.type = ResourceType::kStyle;
983
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700984 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800985
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700986 Maybe<StringPiece> maybeParent = xml::findAttribute(parser, "parent");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700987 if (maybeParent) {
988 // If the parent is empty, we don't have a parent, but we also don't infer either.
989 if (!maybeParent.value().empty()) {
990 std::string errStr;
991 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
992 if (!style->parent) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800993 mDiag->error(DiagMessage(outResource->source) << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700994 return false;
995 }
996
Adam Lesinski467f1712015-11-16 17:35:44 -0800997 // Transform the namespace prefix to the actual package name, and mark the reference as
998 // private if appropriate.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700999 transformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001000 }
1001
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001002 } else {
1003 // No parent was specified, so try inferring it from the style name.
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001004 std::string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001005 size_t pos = styleName.find_last_of(u'.');
1006 if (pos != std::string::npos) {
1007 style->parentInferred = true;
Adam Lesinski467f1712015-11-16 17:35:44 -08001008 style->parent = Reference(ResourceName({}, ResourceType::kStyle,
1009 styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -07001010 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001011 }
1012
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001013 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001014 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001015 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1016 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001017 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001018 continue;
1019 }
1020
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001021 const std::string& elementNamespace = parser->getElementNamespace();
1022 const std::string& elementName = parser->getElementName();
1023 if (elementNamespace == "" && elementName == "item") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001024 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001025
Adam Lesinskica5638f2015-10-21 14:42:43 -07001026 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001027 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1028 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001029 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001030 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001031 }
1032
1033 if (error) {
1034 return false;
1035 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001036
1037 outResource->value = std::move(style);
1038 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001039}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001040
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001041bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1042 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_ANY);
1043}
1044
1045bool ResourceParser::parseIntegerArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1046 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_INTEGER);
1047}
1048
1049bool ResourceParser::parseStringArray(xml::XmlPullParser* parser, ParsedResource* outResource) {
1050 return parseArrayImpl(parser, outResource, android::ResTable_map::TYPE_STRING);
1051}
1052
1053bool ResourceParser::parseArrayImpl(xml::XmlPullParser* parser, ParsedResource* outResource,
1054 const uint32_t typeMask) {
1055 outResource->name.type = ResourceType::kArray;
1056
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001057 std::unique_ptr<Array> array = util::make_unique<Array>();
1058
Adam Lesinski458b8772016-04-25 14:20:21 -07001059 bool translateable = mOptions.translatable;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001060 if (Maybe<StringPiece> translateableAttr = xml::findAttribute(parser, "translatable")) {
Adam Lesinski458b8772016-04-25 14:20:21 -07001061 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
1062 mDiag->error(DiagMessage(outResource->source)
1063 << "invalid value for 'translatable'. Must be a boolean");
1064 return false;
1065 }
1066 }
1067 array->setTranslateable(translateable);
1068
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001069 bool error = false;
1070 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001071 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1072 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001073 // Skip text and comments.
1074 continue;
1075 }
1076
1077 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001078 const std::string& elementNamespace = parser->getElementNamespace();
1079 const std::string& elementName = parser->getElementName();
1080 if (elementNamespace.empty() && elementName == "item") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001081 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
1082 if (!item) {
1083 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
1084 error = true;
1085 continue;
1086 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001087 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001088 array->items.emplace_back(std::move(item));
1089
Adam Lesinskica5638f2015-10-21 14:42:43 -07001090 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001091 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1092 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1093 error = true;
1094 }
1095 }
1096
1097 if (error) {
1098 return false;
1099 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001100
1101 outResource->value = std::move(array);
1102 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001103}
1104
Adam Lesinski467f1712015-11-16 17:35:44 -08001105bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001106 outResource->name.type = ResourceType::kPlurals;
1107
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001108 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1109
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001110 bool error = false;
1111 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001112 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1113 if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001114 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001115 continue;
1116 }
1117
Adam Lesinskica5638f2015-10-21 14:42:43 -07001118 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001119 const std::string& elementNamespace = parser->getElementNamespace();
1120 const std::string& elementName = parser->getElementName();
1121 if (elementNamespace.empty() && elementName == "item") {
1122 Maybe<StringPiece> maybeQuantity = xml::findNonEmptyAttribute(parser, "quantity");
Adam Lesinski467f1712015-11-16 17:35:44 -08001123 if (!maybeQuantity) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001124 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001125 << "'quantity'");
1126 error = true;
1127 continue;
1128 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001129
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001130 StringPiece trimmedQuantity = util::trimWhitespace(maybeQuantity.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001131 size_t index = 0;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001132 if (trimmedQuantity == "zero") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001133 index = Plural::Zero;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001134 } else if (trimmedQuantity == "one") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001135 index = Plural::One;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001136 } else if (trimmedQuantity == "two") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001137 index = Plural::Two;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001138 } else if (trimmedQuantity == "few") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001139 index = Plural::Few;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001140 } else if (trimmedQuantity == "many") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001141 index = Plural::Many;
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001142 } else if (trimmedQuantity == "other") {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001143 index = Plural::Other;
1144 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001145 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001146 << "<item> in <plural> has invalid value '" << trimmedQuantity
1147 << "' for attribute 'quantity'");
1148 error = true;
1149 continue;
1150 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001151
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001152 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001153 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001154 << "duplicate quantity '" << trimmedQuantity << "'");
1155 error = true;
1156 continue;
1157 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001158
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001159 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1160 kNoRawString))) {
1161 error = true;
1162 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001163 plural->values[index]->setSource(itemSource);
1164
1165 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1166 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001167 << elementName << ">");
1168 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001169 }
1170 }
1171
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001172 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001173 return false;
1174 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001175
1176 outResource->value = std::move(plural);
1177 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001178}
1179
Adam Lesinski52364f72016-01-11 13:10:24 -08001180bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser,
1181 ParsedResource* outResource) {
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001182 outResource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001183
Adam Lesinskib274e352015-11-06 15:14:35 -08001184 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001185 outResource->symbolState = SymbolState::kPublic;
1186
Adam Lesinski52364f72016-01-11 13:10:24 -08001187 // Declare-styleable only ends up in default config;
1188 if (outResource->config != ConfigDescription::defaultConfig()) {
1189 mDiag->warn(DiagMessage(outResource->source) << "ignoring configuration '"
1190 << outResource->config << "' for styleable "
1191 << outResource->name.entry);
1192 outResource->config = ConfigDescription::defaultConfig();
1193 }
1194
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001195 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1196
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001197 std::string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001198 bool error = false;
1199 const size_t depth = parser->getDepth();
Adam Lesinski467f1712015-11-16 17:35:44 -08001200 while (xml::XmlPullParser::nextChildNode(parser, depth)) {
1201 if (parser->getEvent() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001202 comment = util::trimWhitespace(parser->getComment()).toString();
1203 continue;
Adam Lesinski467f1712015-11-16 17:35:44 -08001204 } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001205 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001206 continue;
1207 }
1208
Adam Lesinskica5638f2015-10-21 14:42:43 -07001209 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001210 const std::string& elementNamespace = parser->getElementNamespace();
1211 const std::string& elementName = parser->getElementName();
1212 if (elementNamespace.empty() && elementName == "attr") {
1213 Maybe<StringPiece> maybeName = xml::findNonEmptyAttribute(parser, "name");
Adam Lesinski467f1712015-11-16 17:35:44 -08001214 if (!maybeName) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001215 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001216 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001217 continue;
1218 }
1219
Adam Lesinski467f1712015-11-16 17:35:44 -08001220 // If this is a declaration, the package name may be in the name. Separate these out.
1221 // Eg. <attr name="android:text" />
1222 Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value());
1223 if (!maybeRef) {
1224 mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '"
1225 << maybeName.value() << "'");
1226 error = true;
1227 continue;
1228 }
1229
1230 Reference& childRef = maybeRef.value();
Adam Lesinskid0f116b2016-07-08 15:00:32 -07001231 xml::transformReferenceFromNamespace(parser, "", &childRef);
Adam Lesinski467f1712015-11-16 17:35:44 -08001232
Adam Lesinskica5638f2015-10-21 14:42:43 -07001233 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001234 ParsedResource childResource;
Adam Lesinski467f1712015-11-16 17:35:44 -08001235 childResource.name = childRef.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -07001236 childResource.source = itemSource;
1237 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001238
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001239 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001240 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001241 continue;
1242 }
1243
Adam Lesinskica5638f2015-10-21 14:42:43 -07001244 // Create the reference to this attribute.
Adam Lesinskica5638f2015-10-21 14:42:43 -07001245 childRef.setComment(childResource.comment);
1246 childRef.setSource(itemSource);
1247 styleable->entries.push_back(std::move(childRef));
1248
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001249 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001250
Adam Lesinskica5638f2015-10-21 14:42:43 -07001251 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1252 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001253 << elementName << ">");
1254 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001255 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001256
1257 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001258 }
1259
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001260 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001261 return false;
1262 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001263
1264 outResource->value = std::move(styleable);
1265 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001266}
1267
1268} // namespace aapt