blob: d292f623a33e202f3c1071554068ec68f1f7764b [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 Lesinskiabf83cb2015-11-16 15:59:10 -0800750 struct SymbolComparator {
751 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
752 return a.symbol.name.value() < b.symbol.name.value();
753 }
754 };
755
756 std::set<Attribute::Symbol, SymbolComparator> items;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700758 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800759 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700760 const size_t depth = parser->getDepth();
761 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700762 if (parser->getEvent() == XmlPullParser::Event::kComment) {
763 comment = util::trimWhitespace(parser->getComment()).toString();
764 continue;
765 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
766 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800767 continue;
768 }
769
Adam Lesinskica5638f2015-10-21 14:42:43 -0700770 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700771 const std::u16string& elementNamespace = parser->getElementNamespace();
772 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700773 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700774 if (elementName == u"enum") {
775 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700776 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700777 << "can not define an <enum>; already defined a <flag>");
778 error = true;
779 continue;
780 }
781 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700782
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700783 } else if (elementName == u"flag") {
784 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700785 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700786 << "can not define a <flag>; already defined an <enum>");
787 error = true;
788 continue;
789 }
790 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800791 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800792
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700793 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800794 Attribute::Symbol& symbol = s.value();
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700795 ParsedResource childResource;
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800796 childResource.name = symbol.symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700797 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700798 childResource.value = util::make_unique<Id>();
799 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700800
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800801 symbol.symbol.setComment(std::move(comment));
802 symbol.symbol.setSource(itemSource);
803
804 auto insertResult = items.insert(std::move(symbol));
805 if (!insertResult.second) {
806 const Attribute::Symbol& existingSymbol = *insertResult.first;
807 mDiag->error(DiagMessage(itemSource)
808 << "duplicate symbol '" << existingSymbol.symbol.name.value().entry
809 << "'");
810
811 mDiag->note(DiagMessage(existingSymbol.symbol.getSource())
812 << "first defined here");
813 error = true;
814 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800815 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700816 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800817 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700818 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
819 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800820 error = true;
821 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700822
823 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800824 }
825
826 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700827 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828 }
829
830 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
Adam Lesinskiabf83cb2015-11-16 15:59:10 -0800831 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskica2fc352015-04-03 12:08:26 -0700832 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700833 outResource->value = std::move(attr);
834 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800835}
836
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700837Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(XmlPullParser* parser,
838 const StringPiece16& tag) {
839 const Source source = mSource.withLine(parser->getLineNumber());
840
841 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
842 if (!maybeName) {
843 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
844 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800845 }
846
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700847 Maybe<StringPiece16> maybeValue = findNonEmptyAttribute(parser, u"value");
848 if (!maybeValue) {
849 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
850 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800851 }
852
853 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700854 if (!android::ResTable::stringToInt(maybeValue.value().data(),
855 maybeValue.value().size(), &val)) {
856 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
857 << "' for <" << tag << ">; must be an integer");
858 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800859 }
860
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700861 return Attribute::Symbol{
Adam Lesinskie78fd612015-10-22 12:48:43 -0700862 Reference(ResourceName({}, ResourceType::kId, maybeName.value().toString())),
863 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800864}
865
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700866static Maybe<ResourceName> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800867 str = util::trimWhitespace(str);
868 const char16_t* const start = str.data();
869 const char16_t* const end = start + str.size();
870 const char16_t* p = start;
871
872 StringPiece16 package;
873 StringPiece16 name;
874 while (p != end) {
875 if (*p == u':') {
876 package = StringPiece16(start, p - start);
877 name = StringPiece16(p + 1, end - (p + 1));
878 break;
879 }
880 p++;
881 }
882
Adam Lesinskica5638f2015-10-21 14:42:43 -0700883 return ResourceName(package.toString(), ResourceType::kAttr,
884 name.empty() ? str.toString() : name.toString());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800885}
886
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700887bool ResourceParser::parseStyleItem(XmlPullParser* parser, Style* style) {
888 const Source source = mSource.withLine(parser->getLineNumber());
889
890 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
891 if (!maybeName) {
892 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893 return false;
894 }
895
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700896 Maybe<ResourceName> maybeKey = parseXmlAttributeName(maybeName.value());
897 if (!maybeKey) {
898 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800899 return false;
900 }
901
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700902 if (Maybe<ResourceName> transformedName = parser->transformPackage(maybeKey.value(), u"")) {
903 maybeKey = std::move(transformedName);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800904 }
905
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800906 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
907 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700908 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800909 return false;
910 }
911
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700912 style->entries.push_back(Style::Entry{ Reference(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800913 return true;
914}
915
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700916bool ResourceParser::parseStyle(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700917 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700918 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800919
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700920 Maybe<StringPiece16> maybeParent = findAttribute(parser, u"parent");
921 if (maybeParent) {
922 // If the parent is empty, we don't have a parent, but we also don't infer either.
923 if (!maybeParent.value().empty()) {
924 std::string errStr;
925 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
926 if (!style->parent) {
927 mDiag->error(DiagMessage(source) << errStr);
928 return false;
929 }
930
931 if (Maybe<ResourceName> transformedName =
932 parser->transformPackage(style->parent.value().name.value(), u"")) {
933 style->parent.value().name = std::move(transformedName);
934 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 }
936
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700937 } else {
938 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700939 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700940 size_t pos = styleName.find_last_of(u'.');
941 if (pos != std::string::npos) {
942 style->parentInferred = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700943 style->parent = Reference(
944 ResourceName({}, ResourceType::kStyle, styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700945 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800946 }
947
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700949 const size_t depth = parser->getDepth();
950 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800951 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700952 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800953 continue;
954 }
955
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700956 const std::u16string& elementNamespace = parser->getElementNamespace();
957 const std::u16string& elementName = parser->getElementName();
958 if (elementNamespace == u"" && elementName == u"item") {
959 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800960
Adam Lesinskica5638f2015-10-21 14:42:43 -0700961 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700962 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
963 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800964 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800965 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800966 }
967
968 if (error) {
969 return false;
970 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700971
972 outResource->value = std::move(style);
973 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700974}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800975
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700976bool ResourceParser::parseArray(XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700977 uint32_t typeMask) {
978 const Source source = mSource.withLine(parser->getLineNumber());
979 std::unique_ptr<Array> array = util::make_unique<Array>();
980
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700981 bool error = false;
982 const size_t depth = parser->getDepth();
983 while (XmlPullParser::nextChildNode(parser, depth)) {
984 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
985 // Skip text and comments.
986 continue;
987 }
988
989 const Source itemSource = mSource.withLine(parser->getLineNumber());
990 const std::u16string& elementNamespace = parser->getElementNamespace();
991 const std::u16string& elementName = parser->getElementName();
992 if (elementNamespace.empty() && elementName == u"item") {
993 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
994 if (!item) {
995 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
996 error = true;
997 continue;
998 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700999 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001000 array->items.emplace_back(std::move(item));
1001
Adam Lesinskica5638f2015-10-21 14:42:43 -07001002 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001003 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
1004 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
1005 error = true;
1006 }
1007 }
1008
1009 if (error) {
1010 return false;
1011 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001012
1013 outResource->value = std::move(array);
1014 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001015}
1016
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001017bool ResourceParser::parsePlural(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001018 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001019 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1020
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001021 bool error = false;
1022 const size_t depth = parser->getDepth();
1023 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001024 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001025 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001026 continue;
1027 }
1028
Adam Lesinskica5638f2015-10-21 14:42:43 -07001029 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001030 const std::u16string& elementNamespace = parser->getElementNamespace();
1031 const std::u16string& elementName = parser->getElementName();
1032 if (elementNamespace.empty() && elementName == u"item") {
1033 const auto endAttrIter = parser->endAttributes();
1034 auto attrIter = parser->findAttribute(u"", u"quantity");
1035 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001036 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001037 << "'quantity'");
1038 error = true;
1039 continue;
1040 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001041
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001042 StringPiece16 trimmedQuantity = util::trimWhitespace(attrIter->value);
1043 size_t index = 0;
1044 if (trimmedQuantity == u"zero") {
1045 index = Plural::Zero;
1046 } else if (trimmedQuantity == u"one") {
1047 index = Plural::One;
1048 } else if (trimmedQuantity == u"two") {
1049 index = Plural::Two;
1050 } else if (trimmedQuantity == u"few") {
1051 index = Plural::Few;
1052 } else if (trimmedQuantity == u"many") {
1053 index = Plural::Many;
1054 } else if (trimmedQuantity == u"other") {
1055 index = Plural::Other;
1056 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001057 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001058 << "<item> in <plural> has invalid value '" << trimmedQuantity
1059 << "' for attribute 'quantity'");
1060 error = true;
1061 continue;
1062 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001063
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001064 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001065 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001066 << "duplicate quantity '" << trimmedQuantity << "'");
1067 error = true;
1068 continue;
1069 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001070
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001071 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1072 kNoRawString))) {
1073 error = true;
1074 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001075 plural->values[index]->setSource(itemSource);
1076
1077 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1078 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001079 << elementName << ">");
1080 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001081 }
1082 }
1083
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001084 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001085 return false;
1086 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001087
1088 outResource->value = std::move(plural);
1089 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001090}
1091
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001092bool ResourceParser::parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001093 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001094 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1095
Adam Lesinskib274e352015-11-06 15:14:35 -08001096 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001097 outResource->symbolState = SymbolState::kPublic;
1098
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001099 std::u16string comment;
1100 bool error = false;
1101 const size_t depth = parser->getDepth();
1102 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001103 if (parser->getEvent() == XmlPullParser::Event::kComment) {
1104 comment = util::trimWhitespace(parser->getComment()).toString();
1105 continue;
1106 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
1107 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001108 continue;
1109 }
1110
Adam Lesinskica5638f2015-10-21 14:42:43 -07001111 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001112 const std::u16string& elementNamespace = parser->getElementNamespace();
1113 const std::u16string& elementName = parser->getElementName();
1114 if (elementNamespace.empty() && elementName == u"attr") {
1115 const auto endAttrIter = parser->endAttributes();
1116 auto attrIter = parser->findAttribute(u"", u"name");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001117 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001118 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001119 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001120 continue;
1121 }
1122
Adam Lesinskica5638f2015-10-21 14:42:43 -07001123 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001124 ParsedResource childResource;
1125 childResource.name = ResourceName({}, ResourceType::kAttr, attrIter->value);
Adam Lesinskica5638f2015-10-21 14:42:43 -07001126 childResource.source = itemSource;
1127 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001128
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001129 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001130 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001131 continue;
1132 }
1133
Adam Lesinskica5638f2015-10-21 14:42:43 -07001134 // Create the reference to this attribute.
1135 Reference childRef(childResource.name);
1136 childRef.setComment(childResource.comment);
1137 childRef.setSource(itemSource);
1138 styleable->entries.push_back(std::move(childRef));
1139
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001140 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001141
Adam Lesinskica5638f2015-10-21 14:42:43 -07001142 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1143 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001144 << elementName << ">");
1145 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001146 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001147
1148 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001149 }
1150
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001151 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001152 return false;
1153 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001154
1155 outResource->value = std::move(styleable);
1156 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001157}
1158
1159} // namespace aapt