blob: 0c7a4d578be508e482ba2cd52630cd21e5512579 [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 Lesinski1ab598f2015-08-14 14:26:04 -070052ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070053 const ConfigDescription& config,
54 const ResourceParserOptions& options) :
55 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056}
57
58/**
59 * Build a string from XML that converts nested elements into Span objects.
60 */
61bool ResourceParser::flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,
62 StyleString* outStyleString) {
63 std::vector<Span> spanStack;
64
65 outRawString->clear();
66 outStyleString->spans.clear();
67 util::StringBuilder builder;
68 size_t depth = 1;
69 while (XmlPullParser::isGoodEvent(parser->next())) {
70 const XmlPullParser::Event event = parser->getEvent();
71 if (event == XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 if (!parser->getElementNamespace().empty()) {
73 // We already warned and skipped the start element, so just skip here too
74 continue;
75 }
76
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077 depth--;
78 if (depth == 0) {
79 break;
80 }
81
82 spanStack.back().lastChar = builder.str().size();
83 outStyleString->spans.push_back(spanStack.back());
84 spanStack.pop_back();
85
86 } else if (event == XmlPullParser::Event::kText) {
87 // TODO(adamlesinski): Verify format strings.
88 outRawString->append(parser->getText());
89 builder.append(parser->getText());
90
91 } else if (event == XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092 if (!parser->getElementNamespace().empty()) {
93 if (parser->getElementNamespace() != sXliffNamespaceUri) {
94 // Only warn if this isn't an xliff namespace.
95 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
96 << "skipping element '"
97 << parser->getElementName()
98 << "' with unknown namespace '"
99 << parser->getElementNamespace()
100 << "'");
101 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102 continue;
103 }
104 depth++;
105
106 // Build a span object out of the nested element.
107 std::u16string spanName = parser->getElementName();
108 const auto endAttrIter = parser->endAttributes();
109 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
110 spanName += u";";
111 spanName += attrIter->name;
112 spanName += u"=";
113 spanName += attrIter->value;
114 }
115
116 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
118 << "style string '" << builder.str() << "' is too long");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119 return false;
120 }
121 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
122
123 } else if (event == XmlPullParser::Event::kComment) {
124 // Skip
125 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127 }
128 }
129 assert(spanStack.empty() && "spans haven't been fully processed");
130
131 outStyleString->str = builder.str();
132 return true;
133}
134
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135bool ResourceParser::parse(XmlPullParser* parser) {
136 bool error = false;
137 const size_t depth = parser->getDepth();
138 while (XmlPullParser::nextChildNode(parser, depth)) {
139 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
140 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141 continue;
142 }
143
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
145 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
146 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147 return false;
148 }
149
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150 error |= !parseResources(parser);
151 break;
152 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800153
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154 if (parser->getEvent() == XmlPullParser::Event::kBadDocument) {
155 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
156 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157 return false;
158 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160}
161
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700162static bool shouldStripResource(XmlPullParser* parser, const Maybe<std::u16string> productToMatch) {
163 assert(parser->getEvent() == XmlPullParser::Event::kStartElement);
164
165 if (Maybe<StringPiece16> maybeProduct = findNonEmptyAttribute(parser, u"product")) {
166 if (!productToMatch) {
167 if (maybeProduct.value() != u"default" && maybeProduct.value() != u"phone") {
168 // We didn't specify a product and this is not a default product, so skip.
169 return true;
170 }
171 } else {
172 if (productToMatch && maybeProduct.value() != productToMatch.value()) {
173 // We specified a product, but they don't match.
174 return true;
175 }
176 }
177 }
178 return false;
179}
180
181/**
182 * A parsed resource ready to be added to the ResourceTable.
183 */
184struct ParsedResource {
185 ResourceName name;
186 Source source;
187 ResourceId id;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700188 SymbolState symbolState = SymbolState::kUndefined;
Adam Lesinskie78fd612015-10-22 12:48:43 -0700189 std::u16string comment;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700190 std::unique_ptr<Value> value;
191 std::list<ParsedResource> childResources;
192};
193
194// Recursively adds resources to the ResourceTable.
195static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
196 IDiagnostics* diag, ParsedResource* res) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700197 if (res->symbolState != SymbolState::kUndefined) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700198 Symbol symbol;
199 symbol.state = res->symbolState;
200 symbol.source = res->source;
201 symbol.comment = res->comment;
202 if (!table->setSymbolState(res->name, res->id, symbol, diag)) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700203 return false;
204 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700205 }
206
207 if (!res->value) {
208 return true;
209 }
210
Adam Lesinskie78fd612015-10-22 12:48:43 -0700211 // Attach the comment, source and config to the value.
212 res->value->setComment(std::move(res->comment));
213 res->value->setSource(std::move(res->source));
214
215 if (!table->addResource(res->name, res->id, config, std::move(res->value), diag)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700216 return false;
217 }
218
219 bool error = false;
220 for (ParsedResource& child : res->childResources) {
221 error |= !addResourcesToTable(table, config, diag, &child);
222 }
223 return !error;
224}
225
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226bool ResourceParser::parseResources(XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700227 std::set<ResourceName> strippedResources;
228
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231 const size_t depth = parser->getDepth();
232 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233 const XmlPullParser::Event event = parser->getEvent();
234 if (event == XmlPullParser::Event::kComment) {
235 comment = parser->getComment();
236 continue;
237 }
238
239 if (event == XmlPullParser::Event::kText) {
240 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
242 << "plain text not allowed here");
243 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244 }
245 continue;
246 }
247
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248 assert(event == XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251 // Skip unknown namespace.
252 continue;
253 }
254
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 std::u16string elementName = parser->getElementName();
256 if (elementName == u"skip" || elementName == u"eat-comment") {
257 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258 continue;
259 }
260
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
262 if (!maybeName) {
263 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
264 << "<" << elementName << "> tag must have a 'name' attribute");
265 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266 continue;
267 }
268
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700269 // Check if we should skip this product.
270 const bool stripResource = shouldStripResource(parser, mOptions.product);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272 if (elementName == u"item") {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273 // Items simply have their type encoded in the type attribute.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274 if (Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type")) {
275 elementName = maybeType.value().toString();
276 } else {
277 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
278 << "<item> must have a 'type' attribute");
279 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280 continue;
281 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282 }
283
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700284 ParsedResource parsedResource;
285 parsedResource.name.entry = maybeName.value().toString();
286 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinskie78fd612015-10-22 12:48:43 -0700287 parsedResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700289 bool result = true;
290 if (elementName == u"id") {
291 parsedResource.name.type = ResourceType::kId;
292 parsedResource.value = util::make_unique<Id>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293 } else if (elementName == u"string") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700294 parsedResource.name.type = ResourceType::kString;
295 result = parseString(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296 } else if (elementName == u"color") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700297 parsedResource.name.type = ResourceType::kColor;
298 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700299 } else if (elementName == u"drawable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700300 parsedResource.name.type = ResourceType::kDrawable;
301 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700302 } else if (elementName == u"bool") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700303 parsedResource.name.type = ResourceType::kBool;
304 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700305 } else if (elementName == u"integer") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700306 parsedResource.name.type = ResourceType::kInteger;
307 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700308 } else if (elementName == u"dimen") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700309 parsedResource.name.type = ResourceType::kDimen;
310 result = parsePrimitive(parser, &parsedResource);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700311 } else if (elementName == u"fraction") {
312 parsedResource.name.type = ResourceType::kFraction;
313 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314 } else if (elementName == u"style") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700315 parsedResource.name.type = ResourceType::kStyle;
316 result = parseStyle(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700317 } else if (elementName == u"plurals") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700318 parsedResource.name.type = ResourceType::kPlurals;
319 result = parsePlural(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700320 } else if (elementName == u"array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700321 parsedResource.name.type = ResourceType::kArray;
322 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700323 } else if (elementName == u"string-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700324 parsedResource.name.type = ResourceType::kArray;
325 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_STRING);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700326 } else if (elementName == u"integer-array") {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700327 parsedResource.name.type = ResourceType::kArray;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700328 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329 } else if (elementName == u"declare-styleable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700330 parsedResource.name.type = ResourceType::kStyleable;
331 result = parseDeclareStyleable(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700332 } else if (elementName == u"attr") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700333 parsedResource.name.type = ResourceType::kAttr;
334 result = parseAttr(parser, &parsedResource);
335 } else if (elementName == u"public") {
336 result = parsePublic(parser, &parsedResource);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700337 } else if (elementName == u"java-symbol" || elementName == u"symbol") {
338 result = parseSymbol(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339 } else {
340 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
341 << "unknown resource type '" << elementName << "'");
342 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700343
344 if (result) {
345 // We successfully parsed the resource.
346
347 if (stripResource) {
348 // Record that we stripped out this resource name.
349 // We will check that at least one variant of this resource was included.
350 strippedResources.insert(parsedResource.name);
351 } else {
352 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
353 }
354 } else {
355 error = true;
356 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800357 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700358
359 // Check that we included at least one variant of each stripped resource.
360 for (const ResourceName& strippedResource : strippedResources) {
361 if (!mTable->findResource(strippedResource)) {
362 // Failed to find the resource.
363 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
364 "was filtered out but no product variant remains");
365 error = true;
366 }
367 }
368
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700369 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800370}
371
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800372enum {
373 kAllowRawString = true,
374 kNoRawString = false
375};
376
377/**
378 * Reads the entire XML subtree and attempts to parse it as some Item,
379 * with typeMask denoting which items it can be. If allowRawValue is
380 * true, a RawString is returned if the XML couldn't be parsed as
381 * an Item. If allowRawValue is false, nullptr is returned in this
382 * case.
383 */
Adam Lesinskie78fd612015-10-22 12:48:43 -0700384std::unique_ptr<Item> ResourceParser::parseXml(XmlPullParser* parser, const uint32_t typeMask,
385 const bool allowRawValue) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800386 const size_t beginXmlLine = parser->getLineNumber();
387
388 std::u16string rawValue;
389 StyleString styleString;
390 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
391 return {};
392 }
393
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394 if (!styleString.spans.empty()) {
395 // This can only be a StyledString.
396 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700397 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800398 }
399
400 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700401 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700402 std::unique_ptr<Id> id = util::make_unique<Id>();
403 id->setSource(mSource.withLine(beginXmlLine));
404 mTable->addResource(name, {}, std::move(id), mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800405 };
406
407 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700408 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
409 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700411 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700412 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
413 if (Maybe<ResourceName> transformedName =
414 parser->transformPackage(ref->name.value(), u"")) {
415 ref->name = std::move(transformedName);
Adam Lesinski24aad162015-04-24 19:19:30 -0700416 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418 return processedItem;
419 }
420
421 // Try making a regular string.
422 if (typeMask & android::ResTable_map::TYPE_STRING) {
423 // Use the trimmed, escaped string.
424 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700425 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800426 }
427
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428 if (allowRawValue) {
Adam Lesinskie78fd612015-10-22 12:48:43 -0700429 // We can't parse this so return a RawString if we are allowed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700431 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800432 }
Adam Lesinskie78fd612015-10-22 12:48:43 -0700433
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434 return {};
435}
436
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700437bool ResourceParser::parseString(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700438 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800439
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440 // TODO(adamlesinski): Read "untranslateable" attribute.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700442 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
443 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700444 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445 return false;
446 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700447 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448}
449
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700450bool ResourceParser::parseColor(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700451 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800452
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700453 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
454 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700455 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456 return false;
457 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700458 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459}
460
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700461bool ResourceParser::parsePrimitive(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700462 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463
464 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700465 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700466 case ResourceType::kInteger:
467 typeMask |= android::ResTable_map::TYPE_INTEGER;
468 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469
Adam Lesinskica5638f2015-10-21 14:42:43 -0700470 case ResourceType::kFraction:
471 // fallthrough
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700472 case ResourceType::kDimen:
473 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700474 | android::ResTable_map::TYPE_FLOAT
475 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700476 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700478 case ResourceType::kBool:
479 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
480 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700482 default:
483 assert(false);
484 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485 }
486
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700487 outResource->value = parseXml(parser, typeMask, kNoRawString);
488 if (!outResource->value) {
489 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490 return false;
491 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700492 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800493}
494
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700495bool ResourceParser::parsePublic(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700496 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700498 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
499 if (!maybeType) {
500 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501 return false;
502 }
503
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700504 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700506 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
507 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800508 return false;
509 }
510
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700511 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700513 if (Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800514 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700515 bool result = android::ResTable::stringToInt(maybeId.value().data(),
516 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700517 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800518 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
520 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521 return false;
522 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700523 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800524 }
525
526 if (*parsedType == ResourceType::kId) {
527 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700528 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800529 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700530
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700531 outResource->symbolState = SymbolState::kPublic;
532 return true;
533}
534
535bool ResourceParser::parseSymbol(XmlPullParser* parser, ParsedResource* outResource) {
536 const Source source = mSource.withLine(parser->getLineNumber());
537
538 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
539 if (!maybeType) {
540 mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a "
541 "'type' attribute");
542 return false;
543 }
544
545 const ResourceType* parsedType = parseResourceType(maybeType.value());
546 if (!parsedType) {
547 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
548 << "' in <" << parser->getElementName() << ">");
549 return false;
550 }
551
552 outResource->name.type = *parsedType;
553 outResource->symbolState = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700554 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800555}
556
557static uint32_t parseFormatType(const StringPiece16& piece) {
558 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
559 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
560 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
561 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
562 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
563 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
564 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
565 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
566 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
567 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
568 return 0;
569}
570
571static uint32_t parseFormatAttribute(const StringPiece16& str) {
572 uint32_t mask = 0;
573 for (StringPiece16 part : util::tokenize(str, u'|')) {
574 StringPiece16 trimmedPart = util::trimWhitespace(part);
575 uint32_t type = parseFormatType(trimmedPart);
576 if (type == 0) {
577 return 0;
578 }
579 mask |= type;
580 }
581 return mask;
582}
583
Adam Lesinskica5638f2015-10-21 14:42:43 -0700584/**
585 * Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
586 */
587static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) {
588 return ns.empty() && (name == u"skip" || name == u"eat-comment");
589}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700590
591bool ResourceParser::parseAttr(XmlPullParser* parser, ParsedResource* outResource) {
592 outResource->source = mSource.withLine(parser->getLineNumber());
593 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800594}
595
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700596bool ResourceParser::parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800597 uint32_t typeMask = 0;
598
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599 Maybe<StringPiece16> maybeFormat = findAttribute(parser, u"format");
600 if (maybeFormat) {
601 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
604 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700605 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606 }
607 }
608
Adam Lesinski769de982015-04-10 19:43:55 -0700609 // If this is a declaration, the package name may be in the name. Separate these out.
610 // Eg. <attr name="android:text" />
611 // No format attribute is allowed.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700612 if (weak && !maybeFormat) {
Adam Lesinski769de982015-04-10 19:43:55 -0700613 StringPiece16 package, type, name;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700614 ResourceUtils::extractResourceName(outResource->name.entry, &package, &type, &name);
Adam Lesinski769de982015-04-10 19:43:55 -0700615 if (type.empty() && !package.empty()) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700616 outResource->name.package = package.toString();
617 outResource->name.entry = name.toString();
Adam Lesinski769de982015-04-10 19:43:55 -0700618 }
619 }
620
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800621 std::vector<Attribute::Symbol> items;
622
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800624 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700625 const size_t depth = parser->getDepth();
626 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700627 if (parser->getEvent() == XmlPullParser::Event::kComment) {
628 comment = util::trimWhitespace(parser->getComment()).toString();
629 continue;
630 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
631 // Skip text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800632 continue;
633 }
634
Adam Lesinskica5638f2015-10-21 14:42:43 -0700635 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700636 const std::u16string& elementNamespace = parser->getElementNamespace();
637 const std::u16string& elementName = parser->getElementName();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700638 if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700639 if (elementName == u"enum") {
640 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700641 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700642 << "can not define an <enum>; already defined a <flag>");
643 error = true;
644 continue;
645 }
646 typeMask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskica5638f2015-10-21 14:42:43 -0700647
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700648 } else if (elementName == u"flag") {
649 if (typeMask & android::ResTable_map::TYPE_ENUM) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700650 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651 << "can not define a <flag>; already defined an <enum>");
652 error = true;
653 continue;
654 }
655 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800656 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800657
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700658 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700659 ParsedResource childResource;
660 childResource.name = s.value().symbol.name.value();
Adam Lesinskica5638f2015-10-21 14:42:43 -0700661 childResource.source = itemSource;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700662 childResource.value = util::make_unique<Id>();
663 outResource->childResources.push_back(std::move(childResource));
Adam Lesinskica5638f2015-10-21 14:42:43 -0700664
665 s.value().symbol.setComment(std::move(comment));
666 s.value().symbol.setSource(itemSource);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700667 items.push_back(std::move(s.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800668 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700669 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800670 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700671 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
672 mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800673 error = true;
674 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700675
676 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800677 }
678
679 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700680 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800681 }
682
683 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
684 attr->symbols.swap(items);
Adam Lesinskica2fc352015-04-03 12:08:26 -0700685 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700686 outResource->value = std::move(attr);
687 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800688}
689
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700690Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(XmlPullParser* parser,
691 const StringPiece16& tag) {
692 const Source source = mSource.withLine(parser->getLineNumber());
693
694 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
695 if (!maybeName) {
696 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
697 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800698 }
699
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700700 Maybe<StringPiece16> maybeValue = findNonEmptyAttribute(parser, u"value");
701 if (!maybeValue) {
702 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
703 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800704 }
705
706 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707 if (!android::ResTable::stringToInt(maybeValue.value().data(),
708 maybeValue.value().size(), &val)) {
709 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
710 << "' for <" << tag << ">; must be an integer");
711 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800712 }
713
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700714 return Attribute::Symbol{
Adam Lesinskie78fd612015-10-22 12:48:43 -0700715 Reference(ResourceName({}, ResourceType::kId, maybeName.value().toString())),
716 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800717}
718
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700719static Maybe<ResourceName> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800720 str = util::trimWhitespace(str);
721 const char16_t* const start = str.data();
722 const char16_t* const end = start + str.size();
723 const char16_t* p = start;
724
725 StringPiece16 package;
726 StringPiece16 name;
727 while (p != end) {
728 if (*p == u':') {
729 package = StringPiece16(start, p - start);
730 name = StringPiece16(p + 1, end - (p + 1));
731 break;
732 }
733 p++;
734 }
735
Adam Lesinskica5638f2015-10-21 14:42:43 -0700736 return ResourceName(package.toString(), ResourceType::kAttr,
737 name.empty() ? str.toString() : name.toString());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800738}
739
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700740bool ResourceParser::parseStyleItem(XmlPullParser* parser, Style* style) {
741 const Source source = mSource.withLine(parser->getLineNumber());
742
743 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
744 if (!maybeName) {
745 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800746 return false;
747 }
748
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700749 Maybe<ResourceName> maybeKey = parseXmlAttributeName(maybeName.value());
750 if (!maybeKey) {
751 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800752 return false;
753 }
754
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 if (Maybe<ResourceName> transformedName = parser->transformPackage(maybeKey.value(), u"")) {
756 maybeKey = std::move(transformedName);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757 }
758
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800759 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
760 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700761 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800762 return false;
763 }
764
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700765 style->entries.push_back(Style::Entry{ Reference(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800766 return true;
767}
768
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700769bool ResourceParser::parseStyle(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700770 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700771 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800772
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700773 Maybe<StringPiece16> maybeParent = findAttribute(parser, u"parent");
774 if (maybeParent) {
775 // If the parent is empty, we don't have a parent, but we also don't infer either.
776 if (!maybeParent.value().empty()) {
777 std::string errStr;
778 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
779 if (!style->parent) {
780 mDiag->error(DiagMessage(source) << errStr);
781 return false;
782 }
783
784 if (Maybe<ResourceName> transformedName =
785 parser->transformPackage(style->parent.value().name.value(), u"")) {
786 style->parent.value().name = std::move(transformedName);
787 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800788 }
789
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700790 } else {
791 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700792 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700793 size_t pos = styleName.find_last_of(u'.');
794 if (pos != std::string::npos) {
795 style->parentInferred = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700796 style->parent = Reference(
797 ResourceName({}, ResourceType::kStyle, styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700798 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800799 }
800
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800801 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700802 const size_t depth = parser->getDepth();
803 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800804 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700805 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800806 continue;
807 }
808
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700809 const std::u16string& elementNamespace = parser->getElementNamespace();
810 const std::u16string& elementName = parser->getElementName();
811 if (elementNamespace == u"" && elementName == u"item") {
812 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800813
Adam Lesinskica5638f2015-10-21 14:42:43 -0700814 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700815 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
816 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800817 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800818 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800819 }
820
821 if (error) {
822 return false;
823 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700824
825 outResource->value = std::move(style);
826 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700827}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800828
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700829bool ResourceParser::parseArray(XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700830 uint32_t typeMask) {
831 const Source source = mSource.withLine(parser->getLineNumber());
832 std::unique_ptr<Array> array = util::make_unique<Array>();
833
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700834 bool error = false;
835 const size_t depth = parser->getDepth();
836 while (XmlPullParser::nextChildNode(parser, depth)) {
837 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
838 // Skip text and comments.
839 continue;
840 }
841
842 const Source itemSource = mSource.withLine(parser->getLineNumber());
843 const std::u16string& elementNamespace = parser->getElementNamespace();
844 const std::u16string& elementName = parser->getElementName();
845 if (elementNamespace.empty() && elementName == u"item") {
846 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
847 if (!item) {
848 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
849 error = true;
850 continue;
851 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700852 item->setSource(itemSource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700853 array->items.emplace_back(std::move(item));
854
Adam Lesinskica5638f2015-10-21 14:42:43 -0700855 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700856 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
857 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
858 error = true;
859 }
860 }
861
862 if (error) {
863 return false;
864 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700865
866 outResource->value = std::move(array);
867 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868}
869
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700870bool ResourceParser::parsePlural(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700871 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800872 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
873
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700874 bool error = false;
875 const size_t depth = parser->getDepth();
876 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800877 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700878 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800879 continue;
880 }
881
Adam Lesinskica5638f2015-10-21 14:42:43 -0700882 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700883 const std::u16string& elementNamespace = parser->getElementNamespace();
884 const std::u16string& elementName = parser->getElementName();
885 if (elementNamespace.empty() && elementName == u"item") {
886 const auto endAttrIter = parser->endAttributes();
887 auto attrIter = parser->findAttribute(u"", u"quantity");
888 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700889 mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute "
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700890 << "'quantity'");
891 error = true;
892 continue;
893 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800894
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700895 StringPiece16 trimmedQuantity = util::trimWhitespace(attrIter->value);
896 size_t index = 0;
897 if (trimmedQuantity == u"zero") {
898 index = Plural::Zero;
899 } else if (trimmedQuantity == u"one") {
900 index = Plural::One;
901 } else if (trimmedQuantity == u"two") {
902 index = Plural::Two;
903 } else if (trimmedQuantity == u"few") {
904 index = Plural::Few;
905 } else if (trimmedQuantity == u"many") {
906 index = Plural::Many;
907 } else if (trimmedQuantity == u"other") {
908 index = Plural::Other;
909 } else {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700910 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700911 << "<item> in <plural> has invalid value '" << trimmedQuantity
912 << "' for attribute 'quantity'");
913 error = true;
914 continue;
915 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800916
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700917 if (plural->values[index]) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700918 mDiag->error(DiagMessage(itemSource)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700919 << "duplicate quantity '" << trimmedQuantity << "'");
920 error = true;
921 continue;
922 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800923
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700924 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
925 kNoRawString))) {
926 error = true;
927 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700928 plural->values[index]->setSource(itemSource);
929
930 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
931 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700932 << elementName << ">");
933 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800934 }
935 }
936
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700937 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800938 return false;
939 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700940
941 outResource->value = std::move(plural);
942 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800943}
944
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700945bool ResourceParser::parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700946 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800947 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
948
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700949 std::u16string comment;
950 bool error = false;
951 const size_t depth = parser->getDepth();
952 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700953 if (parser->getEvent() == XmlPullParser::Event::kComment) {
954 comment = util::trimWhitespace(parser->getComment()).toString();
955 continue;
956 } else if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
957 // Ignore text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800958 continue;
959 }
960
Adam Lesinskica5638f2015-10-21 14:42:43 -0700961 const Source itemSource = mSource.withLine(parser->getLineNumber());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700962 const std::u16string& elementNamespace = parser->getElementNamespace();
963 const std::u16string& elementName = parser->getElementName();
964 if (elementNamespace.empty() && elementName == u"attr") {
965 const auto endAttrIter = parser->endAttributes();
966 auto attrIter = parser->findAttribute(u"", u"name");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800967 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinskica5638f2015-10-21 14:42:43 -0700968 mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700969 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800970 continue;
971 }
972
Adam Lesinskica5638f2015-10-21 14:42:43 -0700973 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700974 ParsedResource childResource;
975 childResource.name = ResourceName({}, ResourceType::kAttr, attrIter->value);
Adam Lesinskica5638f2015-10-21 14:42:43 -0700976 childResource.source = itemSource;
977 childResource.comment = std::move(comment);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800978
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700979 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700980 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800981 continue;
982 }
983
Adam Lesinskica5638f2015-10-21 14:42:43 -0700984 // Create the reference to this attribute.
985 Reference childRef(childResource.name);
986 childRef.setComment(childResource.comment);
987 childRef.setSource(itemSource);
988 styleable->entries.push_back(std::move(childRef));
989
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700990 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800991
Adam Lesinskica5638f2015-10-21 14:42:43 -0700992 } else if (!shouldIgnoreElement(elementNamespace, elementName)) {
993 mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":"
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700994 << elementName << ">");
995 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800996 }
Adam Lesinskica5638f2015-10-21 14:42:43 -0700997
998 comment = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800999 }
1000
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001001 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001002 return false;
1003 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001004
1005 outResource->value = std::move(styleable);
1006 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001007}
1008
1009} // namespace aapt