blob: 981ece709778b1cdbcb81ce1a94aa29027637777 [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 {
349 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
350 << "unknown resource type '" << elementName << "'");
351 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700352
353 if (result) {
354 // We successfully parsed the resource.
355
356 if (stripResource) {
357 // Record that we stripped out this resource name.
358 // We will check that at least one variant of this resource was included.
359 strippedResources.insert(parsedResource.name);
360 } else {
361 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
362 }
363 } else {
364 error = true;
365 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800366 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700367
368 // Check that we included at least one variant of each stripped resource.
369 for (const ResourceName& strippedResource : strippedResources) {
370 if (!mTable->findResource(strippedResource)) {
371 // Failed to find the resource.
372 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
373 "was filtered out but no product variant remains");
374 error = true;
375 }
376 }
377
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700378 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800379}
380
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381enum {
382 kAllowRawString = true,
383 kNoRawString = false
384};
385
386/**
387 * Reads the entire XML subtree and attempts to parse it as some Item,
388 * with typeMask denoting which items it can be. If allowRawValue is
389 * true, a RawString is returned if the XML couldn't be parsed as
390 * an Item. If allowRawValue is false, nullptr is returned in this
391 * case.
392 */
Adam Lesinskie78fd612015-10-22 12:48:43 -0700393std::unique_ptr<Item> ResourceParser::parseXml(XmlPullParser* parser, const uint32_t typeMask,
394 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800395 const size_t beginXmlLine = parser->getLineNumber();
396
397 std::u16string rawValue;
398 StyleString styleString;
399 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
400 return {};
401 }
402
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403 if (!styleString.spans.empty()) {
404 // This can only be a StyledString.
405 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407 }
408
409 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700410 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700411 std::unique_ptr<Id> id = util::make_unique<Id>();
412 id->setSource(mSource.withLine(beginXmlLine));
413 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800414 };
415
416 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
418 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800419 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700420 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700421 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
422 if (Maybe<ResourceName> transformedName =
423 parser->transformPackage(ref->name.value(), u"")) {
424 ref->name = std::move(transformedName);
Adam Lesinski24aad162015-04-24 19:19:30 -0700425 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700426 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800427 return processedItem;
428 }
429
430 // Try making a regular string.
431 if (typeMask & android::ResTable_map::TYPE_STRING) {
432 // Use the trimmed, escaped string.
433 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700434 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800435 }
436
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700438 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800439 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700442
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800443 return {};
444}
445
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700446bool ResourceParser::parseString(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700447 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800449 bool formatted = true;
450 if (Maybe<StringPiece16> formattedAttr = findAttribute(parser, u"formatted")) {
451 if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) {
452 mDiag->error(DiagMessage(source) << "invalid value for 'formatted'. Must be a boolean");
453 return false;
454 }
455 }
456
Adam Lesinski9f222042015-11-04 13:51:45 -0800457 bool translateable = mOptions.translatable;
458 if (Maybe<StringPiece16> translateableAttr = findAttribute(parser, u"translatable")) {
459 if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800460 mDiag->error(DiagMessage(source)
Adam Lesinski9f222042015-11-04 13:51:45 -0800461 << "invalid value for 'translatable'. Must be a boolean");
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800462 return false;
463 }
464 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700466 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
467 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700468 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469 return false;
470 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800471
Adam Lesinski9f222042015-11-04 13:51:45 -0800472 if (formatted && translateable) {
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800473 if (String* stringValue = valueCast<String>(outResource->value.get())) {
474 if (!util::verifyJavaStringFormat(*stringValue->value)) {
Adam Lesinski9f222042015-11-04 13:51:45 -0800475 mDiag->error(DiagMessage(source)
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800476 << "multiple substitutions specified in non-positional format; "
477 "did you mean to add the formatted=\"false\" attribute?");
478 return false;
479 }
480 }
481 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700482 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483}
484
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700485bool ResourceParser::parseColor(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800487
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700488 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
489 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700490 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491 return false;
492 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700493 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494}
495
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700496bool ResourceParser::parsePrimitive(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700497 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498
499 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700500 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 case ResourceType::kInteger:
502 typeMask |= android::ResTable_map::TYPE_INTEGER;
503 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504
Adam Lesinskica5638f2015-10-21 14:42:43 -0700505 case ResourceType::kFraction:
506 // fallthrough
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507 case ResourceType::kDimen:
508 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700509 | android::ResTable_map::TYPE_FLOAT
510 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700511 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700513 case ResourceType::kBool:
514 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
515 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800516
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 default:
518 assert(false);
519 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520 }
521
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700522 outResource->value = parseXml(parser, typeMask, kNoRawString);
523 if (!outResource->value) {
524 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800525 return false;
526 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700527 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528}
529
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700530bool ResourceParser::parsePublic(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700531 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800532
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700533 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
534 if (!maybeType) {
535 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800536 return false;
537 }
538
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700539 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800540 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
542 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800543 return false;
544 }
545
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700546 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800547
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700548 if (Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550 bool result = android::ResTable::stringToInt(maybeId.value().data(),
551 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700552 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800553 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700554 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
555 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800556 return false;
557 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700558 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800559 }
560
561 if (*parsedType == ResourceType::kId) {
562 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700563 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800564 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700565
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700566 outResource->symbolState = SymbolState::kPublic;
567 return true;
568}
569
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800570bool ResourceParser::parsePublicGroup(XmlPullParser* parser, ParsedResource* outResource) {
571 const Source source = mSource.withLine(parser->getLineNumber());
572
573 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
574 if (!maybeType) {
575 mDiag->error(DiagMessage(source) << "<public-group> must have a 'type' attribute");
576 return false;
577 }
578
579 const ResourceType* parsedType = parseResourceType(maybeType.value());
580 if (!parsedType) {
581 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
582 << "' in <public-group>");
583 return false;
584 }
585
586 Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"first-id");
587 if (!maybeId) {
588 mDiag->error(DiagMessage(source) << "<public-group> must have a 'first-id' attribute");
589 return false;
590 }
591
592 android::Res_value val;
593 bool result = android::ResTable::stringToInt(maybeId.value().data(),
594 maybeId.value().size(), &val);
595 ResourceId nextId(val.data);
596 if (!result || !nextId.isValid()) {
597 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
598 << "' in <public-group>");
599 return false;
600 }
601
602 std::u16string comment;
603 bool error = false;
604 const size_t depth = parser->getDepth();
605 while (XmlPullParser::nextChildNode(parser, depth)) {
606 if (parser->getEvent() == XmlPullParser::Event::kComment) {
607 comment = util::trimWhitespace(parser->getComment()).toString();
608 continue;
609 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
610 // Skip text.
611 continue;
612 }
613
614 const Source itemSource = mSource.withLine(parser->getLineNumber());
615 const std::u16string& elementNamespace = parser->getElementNamespace();
616 const std::u16string& elementName = parser->getElementName();
617 if (elementNamespace.empty() && elementName == u"public") {
618 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
619 if (!maybeName) {
620 mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute");
621 error = true;
622 continue;
623 }
624
625 if (findNonEmptyAttribute(parser, u"id")) {
626 mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>");
627 error = true;
628 continue;
629 }
630
631 if (findNonEmptyAttribute(parser, u"type")) {
632 mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>");
633 error = true;
634 continue;
635 }
636
637 ParsedResource childResource;
638 childResource.name.type = *parsedType;
639 childResource.name.entry = maybeName.value().toString();
640 childResource.id = nextId;
641 childResource.comment = std::move(comment);
642 childResource.source = itemSource;
643 childResource.symbolState = SymbolState::kPublic;
644 outResource->childResources.push_back(std::move(childResource));
645
646 nextId.id += 1;
647
648 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
649 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
650 error = true;
651 }
652 }
653 return !error;
654}
655
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700656bool ResourceParser::parseSymbol(XmlPullParser* parser, ParsedResource* outResource) {
657 const Source source = mSource.withLine(parser->getLineNumber());
658
659 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
660 if (!maybeType) {
661 mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a "
662 "'type' attribute");
663 return false;
664 }
665
666 const ResourceType* parsedType = parseResourceType(maybeType.value());
667 if (!parsedType) {
668 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
669 << "' in <" << parser->getElementName() << ">");
670 return false;
671 }
672
673 outResource->name.type = *parsedType;
674 outResource->symbolState = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700675 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800676}
677
678static uint32_t parseFormatType(const StringPiece16& piece) {
679 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
680 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
681 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
682 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
683 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
684 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
685 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
686 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
687 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
688 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
689 return 0;
690}
691
692static uint32_t parseFormatAttribute(const StringPiece16& str) {
693 uint32_t mask = 0;
694 for (StringPiece16 part : util::tokenize(str, u'|')) {
695 StringPiece16 trimmedPart = util::trimWhitespace(part);
696 uint32_t type = parseFormatType(trimmedPart);
697 if (type == 0) {
698 return 0;
699 }
700 mask |= type;
701 }
702 return mask;
703}
704
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800705
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700706
707bool ResourceParser::parseAttr(XmlPullParser* parser, ParsedResource* outResource) {
708 outResource->source = mSource.withLine(parser->getLineNumber());
709 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800710}
711
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700712bool ResourceParser::parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800713 uint32_t typeMask = 0;
714
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700715 Maybe<StringPiece16> maybeFormat = findAttribute(parser, u"format");
716 if (maybeFormat) {
717 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800718 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700719 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
720 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700721 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800722 }
723 }
724
Adam Lesinski769de982015-04-10 19:43:55 -0700725 // If this is a declaration, the package name may be in the name. Separate these out.
726 // Eg. <attr name="android:text" />
727 // No format attribute is allowed.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700728 if (weak && !maybeFormat) {
Adam Lesinski769de982015-04-10 19:43:55 -0700729 StringPiece16 package, type, name;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700730 ResourceUtils::extractResourceName(outResource->name.entry, &package, &type, &name);
Adam Lesinski769de982015-04-10 19:43:55 -0700731 if (type.empty() && !package.empty()) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700732 outResource->name.package = package.toString();
733 outResource->name.entry = name.toString();
Adam Lesinski769de982015-04-10 19:43:55 -0700734 }
735 }
736
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800737 std::vector<Attribute::Symbol> items;
738
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700739 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800740 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700741 const size_t depth = parser->getDepth();
742 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700743 if (parser->getEvent() == XmlPullParser::Event::kComment) {
744 comment = util::trimWhitespace(parser->getComment()).toString();
745 continue;
746 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
747 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800748 continue;
749 }
750
Adam Lesinskica5638f2015-10-21 14:42:43 -0700751 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700752 const std::u16string& elementNamespace = parser->getElementNamespace();
753 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700754 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 if (elementName == u"enum") {
756 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700757 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700758 << "can not define an <enum>; already defined a <flag>");
759 error = true;
760 continue;
761 }
762 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700763
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700764 } else if (elementName == u"flag") {
765 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700766 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700767 << "can not define a <flag>; already defined an <enum>");
768 error = true;
769 continue;
770 }
771 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800772 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800773
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700774 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700775 ParsedResource childResource;
776 childResource.name = s.value().symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700777 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700778 childResource.value = util::make_unique<Id>();
779 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700780
781 s.value().symbol.setComment(std::move(comment));
782 s.value().symbol.setSource(itemSource);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700783 items.push_back(std::move(s.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700785 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800786 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700787 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
788 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800789 error = true;
790 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700791
792 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800793 }
794
795 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700796 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800797 }
798
799 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
800 attr->symbols.swap(items);
Adam Lesinskica2fc352015-04-03 12:08:26 -0700801 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700802 outResource->value = std::move(attr);
803 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800804}
805
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700806Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(XmlPullParser* parser,
807 const StringPiece16& tag) {
808 const Source source = mSource.withLine(parser->getLineNumber());
809
810 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
811 if (!maybeName) {
812 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
813 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800814 }
815
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700816 Maybe<StringPiece16> maybeValue = findNonEmptyAttribute(parser, u"value");
817 if (!maybeValue) {
818 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
819 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800820 }
821
822 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700823 if (!android::ResTable::stringToInt(maybeValue.value().data(),
824 maybeValue.value().size(), &val)) {
825 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
826 << "' for <" << tag << ">; must be an integer");
827 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828 }
829
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700830 return Attribute::Symbol{
Adam Lesinskie78fd612015-10-22 12:48:43 -0700831 Reference(ResourceName({}, ResourceType::kId, maybeName.value().toString())),
832 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833}
834
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700835static Maybe<ResourceName> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800836 str = util::trimWhitespace(str);
837 const char16_t* const start = str.data();
838 const char16_t* const end = start + str.size();
839 const char16_t* p = start;
840
841 StringPiece16 package;
842 StringPiece16 name;
843 while (p != end) {
844 if (*p == u':') {
845 package = StringPiece16(start, p - start);
846 name = StringPiece16(p + 1, end - (p + 1));
847 break;
848 }
849 p++;
850 }
851
Adam Lesinskica5638f2015-10-21 14:42:43 -0700852 return ResourceName(package.toString(), ResourceType::kAttr,
853 name.empty() ? str.toString() : name.toString());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800854}
855
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700856bool ResourceParser::parseStyleItem(XmlPullParser* parser, Style* style) {
857 const Source source = mSource.withLine(parser->getLineNumber());
858
859 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
860 if (!maybeName) {
861 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862 return false;
863 }
864
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700865 Maybe<ResourceName> maybeKey = parseXmlAttributeName(maybeName.value());
866 if (!maybeKey) {
867 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868 return false;
869 }
870
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700871 if (Maybe<ResourceName> transformedName = parser->transformPackage(maybeKey.value(), u"")) {
872 maybeKey = std::move(transformedName);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873 }
874
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
876 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700877 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800878 return false;
879 }
880
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700881 style->entries.push_back(Style::Entry{ Reference(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800882 return true;
883}
884
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700885bool ResourceParser::parseStyle(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700886 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700887 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800888
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700889 Maybe<StringPiece16> maybeParent = findAttribute(parser, u"parent");
890 if (maybeParent) {
891 // If the parent is empty, we don't have a parent, but we also don't infer either.
892 if (!maybeParent.value().empty()) {
893 std::string errStr;
894 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
895 if (!style->parent) {
896 mDiag->error(DiagMessage(source) << errStr);
897 return false;
898 }
899
900 if (Maybe<ResourceName> transformedName =
901 parser->transformPackage(style->parent.value().name.value(), u"")) {
902 style->parent.value().name = std::move(transformedName);
903 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800904 }
905
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700906 } else {
907 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700908 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700909 size_t pos = styleName.find_last_of(u'.');
910 if (pos != std::string::npos) {
911 style->parentInferred = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700912 style->parent = Reference(
913 ResourceName({}, ResourceType::kStyle, styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700914 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800915 }
916
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800917 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700918 const size_t depth = parser->getDepth();
919 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800920 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700921 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800922 continue;
923 }
924
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700925 const std::u16string& elementNamespace = parser->getElementNamespace();
926 const std::u16string& elementName = parser->getElementName();
927 if (elementNamespace == u"" && elementName == u"item") {
928 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800929
Adam Lesinskica5638f2015-10-21 14:42:43 -0700930 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700931 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
932 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800933 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800934 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 }
936
937 if (error) {
938 return false;
939 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700940
941 outResource->value = std::move(style);
942 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700943}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800944
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700945bool ResourceParser::parseArray(XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700946 uint32_t typeMask) {
947 const Source source = mSource.withLine(parser->getLineNumber());
948 std::unique_ptr<Array> array = util::make_unique<Array>();
949
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700950 bool error = false;
951 const size_t depth = parser->getDepth();
952 while (XmlPullParser::nextChildNode(parser, depth)) {
953 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
954 // Skip text and comments.
955 continue;
956 }
957
958 const Source itemSource = mSource.withLine(parser->getLineNumber());
959 const std::u16string& elementNamespace = parser->getElementNamespace();
960 const std::u16string& elementName = parser->getElementName();
961 if (elementNamespace.empty() && elementName == u"item") {
962 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
963 if (!item) {
964 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
965 error = true;
966 continue;
967 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700968 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700969 array->items.emplace_back(std::move(item));
970
Adam Lesinskica5638f2015-10-21 14:42:43 -0700971 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700972 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
973 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
974 error = true;
975 }
976 }
977
978 if (error) {
979 return false;
980 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700981
982 outResource->value = std::move(array);
983 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800984}
985
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700986bool ResourceParser::parsePlural(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700987 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800988 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
989
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700990 bool error = false;
991 const size_t depth = parser->getDepth();
992 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800993 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700994 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800995 continue;
996 }
997
Adam Lesinskica5638f2015-10-21 14:42:43 -0700998 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700999 const std::u16string& elementNamespace = parser->getElementNamespace();
1000 const std::u16string& elementName = parser->getElementName();
1001 if (elementNamespace.empty() && elementName == u"item") {
1002 const auto endAttrIter = parser->endAttributes();
1003 auto attrIter = parser->findAttribute(u"", u"quantity");
1004 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001005 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001006 << "'quantity'");
1007 error = true;
1008 continue;
1009 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001010
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001011 StringPiece16 trimmedQuantity = util::trimWhitespace(attrIter->value);
1012 size_t index = 0;
1013 if (trimmedQuantity == u"zero") {
1014 index = Plural::Zero;
1015 } else if (trimmedQuantity == u"one") {
1016 index = Plural::One;
1017 } else if (trimmedQuantity == u"two") {
1018 index = Plural::Two;
1019 } else if (trimmedQuantity == u"few") {
1020 index = Plural::Few;
1021 } else if (trimmedQuantity == u"many") {
1022 index = Plural::Many;
1023 } else if (trimmedQuantity == u"other") {
1024 index = Plural::Other;
1025 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001026 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001027 << "<item> in <plural> has invalid value '" << trimmedQuantity
1028 << "' for attribute 'quantity'");
1029 error = true;
1030 continue;
1031 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001032
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001033 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001034 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001035 << "duplicate quantity '" << trimmedQuantity << "'");
1036 error = true;
1037 continue;
1038 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001039
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001040 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
1041 kNoRawString))) {
1042 error = true;
1043 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001044 plural->values[index]->setSource(itemSource);
1045
1046 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1047 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001048 << elementName << ">");
1049 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001050 }
1051 }
1052
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001053 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001054 return false;
1055 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001056
1057 outResource->value = std::move(plural);
1058 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001059}
1060
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001061bool ResourceParser::parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001062 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001063 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1064
Adam Lesinskib274e352015-11-06 15:14:35 -08001065 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
Adam Lesinski9f222042015-11-04 13:51:45 -08001066 outResource->symbolState = SymbolState::kPublic;
1067
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001068 std::u16string comment;
1069 bool error = false;
1070 const size_t depth = parser->getDepth();
1071 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001072 if (parser->getEvent() == XmlPullParser::Event::kComment) {
1073 comment = util::trimWhitespace(parser->getComment()).toString();
1074 continue;
1075 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
1076 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001077 continue;
1078 }
1079
Adam Lesinskica5638f2015-10-21 14:42:43 -07001080 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001081 const std::u16string& elementNamespace = parser->getElementNamespace();
1082 const std::u16string& elementName = parser->getElementName();
1083 if (elementNamespace.empty() && elementName == u"attr") {
1084 const auto endAttrIter = parser->endAttributes();
1085 auto attrIter = parser->findAttribute(u"", u"name");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001086 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -07001087 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001088 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001089 continue;
1090 }
1091
Adam Lesinskica5638f2015-10-21 14:42:43 -07001092 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001093 ParsedResource childResource;
1094 childResource.name = ResourceName({}, ResourceType::kAttr, attrIter->value);
Adam Lesinskica5638f2015-10-21 14:42:43 -07001095 childResource.source = itemSource;
1096 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001097
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001098 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001099 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001100 continue;
1101 }
1102
Adam Lesinskica5638f2015-10-21 14:42:43 -07001103 // Create the reference to this attribute.
1104 Reference childRef(childResource.name);
1105 childRef.setComment(childResource.comment);
1106 childRef.setSource(itemSource);
1107 styleable->entries.push_back(std::move(childRef));
1108
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001109 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001110
Adam Lesinskica5638f2015-10-21 14:42:43 -07001111 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
1112 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001113 << elementName << ">");
1114 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001115 }
Adam Lesinskica5638f2015-10-21 14:42:43 -07001116
1117 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001118 }
1119
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001120 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001121 return false;
1122 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001123
1124 outResource->value = std::move(styleable);
1125 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001126}
1127
1128} // namespace aapt