blob: 63629f0d6c10835657e082f3764a85afbe6b6980 [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 "util/Util.h"
22#include "ValueVisitor.h"
23#include "XmlPullParser.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024
Adam Lesinski769de982015-04-10 19:43:55 -070025#include <sstream>
26
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027namespace aapt {
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031static Maybe<StringPiece16> findAttribute(XmlPullParser* parser, const StringPiece16& name) {
32 auto iter = parser->findAttribute(u"", name);
33 if (iter != parser->endAttributes()) {
34 return StringPiece16(util::trimWhitespace(iter->value));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035 }
36 return {};
37}
38
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039static Maybe<StringPiece16> findNonEmptyAttribute(XmlPullParser* parser,
40 const StringPiece16& name) {
41 auto iter = parser->findAttribute(u"", name);
42 if (iter != parser->endAttributes()) {
43 StringPiece16 trimmed = util::trimWhitespace(iter->value);
44 if (!trimmed.empty()) {
45 return trimmed;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046 }
47 }
48 return {};
49}
50
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -070052 const ConfigDescription& config,
53 const ResourceParserOptions& options) :
54 mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055}
56
57/**
58 * Build a string from XML that converts nested elements into Span objects.
59 */
60bool ResourceParser::flattenXmlSubtree(XmlPullParser* parser, std::u16string* outRawString,
61 StyleString* outStyleString) {
62 std::vector<Span> spanStack;
63
64 outRawString->clear();
65 outStyleString->spans.clear();
66 util::StringBuilder builder;
67 size_t depth = 1;
68 while (XmlPullParser::isGoodEvent(parser->next())) {
69 const XmlPullParser::Event event = parser->getEvent();
70 if (event == XmlPullParser::Event::kEndElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 if (!parser->getElementNamespace().empty()) {
72 // We already warned and skipped the start element, so just skip here too
73 continue;
74 }
75
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076 depth--;
77 if (depth == 0) {
78 break;
79 }
80
81 spanStack.back().lastChar = builder.str().size();
82 outStyleString->spans.push_back(spanStack.back());
83 spanStack.pop_back();
84
85 } else if (event == XmlPullParser::Event::kText) {
86 // TODO(adamlesinski): Verify format strings.
87 outRawString->append(parser->getText());
88 builder.append(parser->getText());
89
90 } else if (event == XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 if (!parser->getElementNamespace().empty()) {
92 if (parser->getElementNamespace() != sXliffNamespaceUri) {
93 // Only warn if this isn't an xliff namespace.
94 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
95 << "skipping element '"
96 << parser->getElementName()
97 << "' with unknown namespace '"
98 << parser->getElementNamespace()
99 << "'");
100 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101 continue;
102 }
103 depth++;
104
105 // Build a span object out of the nested element.
106 std::u16string spanName = parser->getElementName();
107 const auto endAttrIter = parser->endAttributes();
108 for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) {
109 spanName += u";";
110 spanName += attrIter->name;
111 spanName += u"=";
112 spanName += attrIter->value;
113 }
114
115 if (builder.str().size() > std::numeric_limits<uint32_t>::max()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
117 << "style string '" << builder.str() << "' is too long");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118 return false;
119 }
120 spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) });
121
122 } else if (event == XmlPullParser::Event::kComment) {
123 // Skip
124 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 assert(false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126 }
127 }
128 assert(spanStack.empty() && "spans haven't been fully processed");
129
130 outStyleString->str = builder.str();
131 return true;
132}
133
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134bool ResourceParser::parse(XmlPullParser* parser) {
135 bool error = false;
136 const size_t depth = parser->getDepth();
137 while (XmlPullParser::nextChildNode(parser, depth)) {
138 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
139 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140 continue;
141 }
142
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143 if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") {
144 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
145 << "root element must be <resources>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146 return false;
147 }
148
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149 error |= !parseResources(parser);
150 break;
151 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153 if (parser->getEvent() == XmlPullParser::Event::kBadDocument) {
154 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
155 << "xml parser error: " << parser->getLastError());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156 return false;
157 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159}
160
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700161static bool shouldStripResource(XmlPullParser* parser, const Maybe<std::u16string> productToMatch) {
162 assert(parser->getEvent() == XmlPullParser::Event::kStartElement);
163
164 if (Maybe<StringPiece16> maybeProduct = findNonEmptyAttribute(parser, u"product")) {
165 if (!productToMatch) {
166 if (maybeProduct.value() != u"default" && maybeProduct.value() != u"phone") {
167 // We didn't specify a product and this is not a default product, so skip.
168 return true;
169 }
170 } else {
171 if (productToMatch && maybeProduct.value() != productToMatch.value()) {
172 // We specified a product, but they don't match.
173 return true;
174 }
175 }
176 }
177 return false;
178}
179
180/**
181 * A parsed resource ready to be added to the ResourceTable.
182 */
183struct ParsedResource {
184 ResourceName name;
185 Source source;
186 ResourceId id;
187 bool markPublic = false;
188 std::unique_ptr<Value> value;
189 std::list<ParsedResource> childResources;
190};
191
192// Recursively adds resources to the ResourceTable.
193static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
194 IDiagnostics* diag, ParsedResource* res) {
195 if (res->markPublic && !table->markPublic(res->name, res->id, res->source, diag)) {
196 return false;
197 }
198
199 if (!res->value) {
200 return true;
201 }
202
203 if (!table->addResource(res->name, res->id, config, res->source, std::move(res->value), diag)) {
204 return false;
205 }
206
207 bool error = false;
208 for (ParsedResource& child : res->childResources) {
209 error |= !addResourcesToTable(table, config, diag, &child);
210 }
211 return !error;
212}
213
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214bool ResourceParser::parseResources(XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700215 std::set<ResourceName> strippedResources;
216
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219 const size_t depth = parser->getDepth();
220 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221 const XmlPullParser::Event event = parser->getEvent();
222 if (event == XmlPullParser::Event::kComment) {
223 comment = parser->getComment();
224 continue;
225 }
226
227 if (event == XmlPullParser::Event::kText) {
228 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700229 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
230 << "plain text not allowed here");
231 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232 }
233 continue;
234 }
235
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236 assert(event == XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239 // Skip unknown namespace.
240 continue;
241 }
242
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243 std::u16string elementName = parser->getElementName();
244 if (elementName == u"skip" || elementName == u"eat-comment") {
245 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246 continue;
247 }
248
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
250 if (!maybeName) {
251 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
252 << "<" << elementName << "> tag must have a 'name' attribute");
253 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 continue;
255 }
256
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700257 // Check if we should skip this product.
258 const bool stripResource = shouldStripResource(parser, mOptions.product);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260 if (elementName == u"item") {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261 // Items simply have their type encoded in the type attribute.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262 if (Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type")) {
263 elementName = maybeType.value().toString();
264 } else {
265 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
266 << "<item> must have a 'type' attribute");
267 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268 continue;
269 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270 }
271
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700272 ParsedResource parsedResource;
273 parsedResource.name.entry = maybeName.value().toString();
274 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700276 bool result = true;
277 if (elementName == u"id") {
278 parsedResource.name.type = ResourceType::kId;
279 parsedResource.value = util::make_unique<Id>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280 } else if (elementName == u"string") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700281 parsedResource.name.type = ResourceType::kString;
282 result = parseString(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 } else if (elementName == u"color") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700284 parsedResource.name.type = ResourceType::kColor;
285 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 } else if (elementName == u"drawable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700287 parsedResource.name.type = ResourceType::kDrawable;
288 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 } else if (elementName == u"bool") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700290 parsedResource.name.type = ResourceType::kBool;
291 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292 } else if (elementName == u"integer") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700293 parsedResource.name.type = ResourceType::kInteger;
294 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295 } else if (elementName == u"dimen") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700296 parsedResource.name.type = ResourceType::kDimen;
297 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298 } else if (elementName == u"style") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700299 parsedResource.name.type = ResourceType::kStyle;
300 result = parseStyle(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 } else if (elementName == u"plurals") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700302 parsedResource.name.type = ResourceType::kPlurals;
303 result = parsePlural(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 } else if (elementName == u"array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700305 parsedResource.name.type = ResourceType::kArray;
306 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 } else if (elementName == u"string-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700308 parsedResource.name.type = ResourceType::kArray;
309 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_STRING);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700310 } else if (elementName == u"integer-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700311 parsedResource.name.type = ResourceType::kIntegerArray;
312 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313 } else if (elementName == u"declare-styleable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700314 parsedResource.name.type = ResourceType::kStyleable;
315 result = parseDeclareStyleable(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700316 } else if (elementName == u"attr") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700317 parsedResource.name.type = ResourceType::kAttr;
318 result = parseAttr(parser, &parsedResource);
319 } else if (elementName == u"public") {
320 result = parsePublic(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321 } else {
322 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
323 << "unknown resource type '" << elementName << "'");
324 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700325
326 if (result) {
327 // We successfully parsed the resource.
328
329 if (stripResource) {
330 // Record that we stripped out this resource name.
331 // We will check that at least one variant of this resource was included.
332 strippedResources.insert(parsedResource.name);
333 } else {
334 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
335 }
336 } else {
337 error = true;
338 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800339 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700340
341 // Check that we included at least one variant of each stripped resource.
342 for (const ResourceName& strippedResource : strippedResources) {
343 if (!mTable->findResource(strippedResource)) {
344 // Failed to find the resource.
345 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
346 "was filtered out but no product variant remains");
347 error = true;
348 }
349 }
350
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700351 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800352}
353
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800354enum {
355 kAllowRawString = true,
356 kNoRawString = false
357};
358
359/**
360 * Reads the entire XML subtree and attempts to parse it as some Item,
361 * with typeMask denoting which items it can be. If allowRawValue is
362 * true, a RawString is returned if the XML couldn't be parsed as
363 * an Item. If allowRawValue is false, nullptr is returned in this
364 * case.
365 */
366std::unique_ptr<Item> ResourceParser::parseXml(XmlPullParser* parser, uint32_t typeMask,
367 bool allowRawValue) {
368 const size_t beginXmlLine = parser->getLineNumber();
369
370 std::u16string rawValue;
371 StyleString styleString;
372 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
373 return {};
374 }
375
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800376 if (!styleString.spans.empty()) {
377 // This can only be a StyledString.
378 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700379 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800380 }
381
382 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700383 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700384 mTable->addResource(name, {}, mSource.withLine(beginXmlLine), util::make_unique<Id>(),
385 mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800386 };
387
388 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
390 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700392 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700393 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
394 if (Maybe<ResourceName> transformedName =
395 parser->transformPackage(ref->name.value(), u"")) {
396 ref->name = std::move(transformedName);
Adam Lesinski24aad162015-04-24 19:19:30 -0700397 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800399 return processedItem;
400 }
401
402 // Try making a regular string.
403 if (typeMask & android::ResTable_map::TYPE_STRING) {
404 // Use the trimmed, escaped string.
405 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407 }
408
409 // We can't parse this so return a RawString if we are allowed.
410 if (allowRawValue) {
411 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700412 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413 }
414 return {};
415}
416
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700417bool ResourceParser::parseString(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700418 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800419
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700420 // TODO(adamlesinski): Read "untranslateable" attribute.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700422 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
423 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700424 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425 return false;
426 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700427 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800428}
429
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700430bool ResourceParser::parseColor(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700431 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800432
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700433 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
434 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700435 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436 return false;
437 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700438 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800439}
440
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700441bool ResourceParser::parsePrimitive(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700442 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800443
444 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700445 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700446 case ResourceType::kInteger:
447 typeMask |= android::ResTable_map::TYPE_INTEGER;
448 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800449
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700450 case ResourceType::kDimen:
451 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700452 | android::ResTable_map::TYPE_FLOAT
453 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700456 case ResourceType::kBool:
457 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
458 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800459
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700460 default:
461 assert(false);
462 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463 }
464
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700465 outResource->value = parseXml(parser, typeMask, kNoRawString);
466 if (!outResource->value) {
467 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468 return false;
469 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700470 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800471}
472
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700473bool ResourceParser::parsePublic(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800475
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700476 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
477 if (!maybeType) {
478 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800479 return false;
480 }
481
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700482 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800483 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700484 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
485 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486 return false;
487 }
488
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700489 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700491 if (Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800492 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700493 bool result = android::ResTable::stringToInt(maybeId.value().data(),
494 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700495 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700497 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
498 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499 return false;
500 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700501 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502 }
503
504 if (*parsedType == ResourceType::kId) {
505 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700506 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700508
509 outResource->markPublic = true;
510 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800511}
512
513static uint32_t parseFormatType(const StringPiece16& piece) {
514 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
515 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
516 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
517 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
518 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
519 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
520 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
521 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
522 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
523 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
524 return 0;
525}
526
527static uint32_t parseFormatAttribute(const StringPiece16& str) {
528 uint32_t mask = 0;
529 for (StringPiece16 part : util::tokenize(str, u'|')) {
530 StringPiece16 trimmedPart = util::trimWhitespace(part);
531 uint32_t type = parseFormatType(trimmedPart);
532 if (type == 0) {
533 return 0;
534 }
535 mask |= type;
536 }
537 return mask;
538}
539
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700540
541bool ResourceParser::parseAttr(XmlPullParser* parser, ParsedResource* outResource) {
542 outResource->source = mSource.withLine(parser->getLineNumber());
543 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800544}
545
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700546bool ResourceParser::parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800547 uint32_t typeMask = 0;
548
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700549 Maybe<StringPiece16> maybeFormat = findAttribute(parser, u"format");
550 if (maybeFormat) {
551 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800552 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700553 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
554 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700555 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800556 }
557 }
558
Adam Lesinski769de982015-04-10 19:43:55 -0700559 // If this is a declaration, the package name may be in the name. Separate these out.
560 // Eg. <attr name="android:text" />
561 // No format attribute is allowed.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700562 if (weak && !maybeFormat) {
Adam Lesinski769de982015-04-10 19:43:55 -0700563 StringPiece16 package, type, name;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700564 ResourceUtils::extractResourceName(outResource->name.entry, &package, &type, &name);
Adam Lesinski769de982015-04-10 19:43:55 -0700565 if (type.empty() && !package.empty()) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700566 outResource->name.package = package.toString();
567 outResource->name.entry = name.toString();
Adam Lesinski769de982015-04-10 19:43:55 -0700568 }
569 }
570
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571 std::vector<Attribute::Symbol> items;
572
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700573 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700575 const size_t depth = parser->getDepth();
576 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800577 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800579 continue;
580 }
581
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700582 const std::u16string& elementNamespace = parser->getElementNamespace();
583 const std::u16string& elementName = parser->getElementName();
584 if (elementNamespace == u"" && (elementName == u"flag" || elementName == u"enum")) {
585 if (elementName == u"enum") {
586 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
587 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
588 << "can not define an <enum>; already defined a <flag>");
589 error = true;
590 continue;
591 }
592 typeMask |= android::ResTable_map::TYPE_ENUM;
593 } else if (elementName == u"flag") {
594 if (typeMask & android::ResTable_map::TYPE_ENUM) {
595 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
596 << "can not define a <flag>; already defined an <enum>");
597 error = true;
598 continue;
599 }
600 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800601 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800602
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700604 ParsedResource childResource;
605 childResource.name = s.value().symbol.name.value();
606 childResource.source = mSource.withLine(parser->getLineNumber());
607 childResource.value = util::make_unique<Id>();
608 outResource->childResources.push_back(std::move(childResource));
609 items.push_back(std::move(s.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800610 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700611 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800612 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613 } else if (elementName == u"skip" || elementName == u"eat-comment") {
614 comment = u"";
615
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800616 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
618 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800619 error = true;
620 }
621 }
622
623 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700624 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800625 }
626
627 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
628 attr->symbols.swap(items);
Adam Lesinskica2fc352015-04-03 12:08:26 -0700629 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700630 outResource->value = std::move(attr);
631 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800632}
633
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700634Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(XmlPullParser* parser,
635 const StringPiece16& tag) {
636 const Source source = mSource.withLine(parser->getLineNumber());
637
638 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
639 if (!maybeName) {
640 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
641 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800642 }
643
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644 Maybe<StringPiece16> maybeValue = findNonEmptyAttribute(parser, u"value");
645 if (!maybeValue) {
646 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
647 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800648 }
649
650 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651 if (!android::ResTable::stringToInt(maybeValue.value().data(),
652 maybeValue.value().size(), &val)) {
653 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
654 << "' for <" << tag << ">; must be an integer");
655 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800656 }
657
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700658 return Attribute::Symbol{
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700659 Reference(ResourceName{ {}, ResourceType::kId, maybeName.value().toString() }),
660 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800661}
662
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700663static Maybe<ResourceName> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800664 str = util::trimWhitespace(str);
665 const char16_t* const start = str.data();
666 const char16_t* const end = start + str.size();
667 const char16_t* p = start;
668
669 StringPiece16 package;
670 StringPiece16 name;
671 while (p != end) {
672 if (*p == u':') {
673 package = StringPiece16(start, p - start);
674 name = StringPiece16(p + 1, end - (p + 1));
675 break;
676 }
677 p++;
678 }
679
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700680 return ResourceName{ package.toString(), ResourceType::kAttr,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700681 name.empty() ? str.toString() : name.toString() };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800682}
683
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700684
685bool ResourceParser::parseStyleItem(XmlPullParser* parser, Style* style) {
686 const Source source = mSource.withLine(parser->getLineNumber());
687
688 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
689 if (!maybeName) {
690 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800691 return false;
692 }
693
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700694 Maybe<ResourceName> maybeKey = parseXmlAttributeName(maybeName.value());
695 if (!maybeKey) {
696 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800697 return false;
698 }
699
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700700 if (Maybe<ResourceName> transformedName = parser->transformPackage(maybeKey.value(), u"")) {
701 maybeKey = std::move(transformedName);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800702 }
703
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800704 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
705 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700706 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800707 return false;
708 }
709
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700710 style->entries.push_back(Style::Entry{ Reference(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800711 return true;
712}
713
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700714bool ResourceParser::parseStyle(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700715 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700716 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800717
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700718 Maybe<StringPiece16> maybeParent = findAttribute(parser, u"parent");
719 if (maybeParent) {
720 // If the parent is empty, we don't have a parent, but we also don't infer either.
721 if (!maybeParent.value().empty()) {
722 std::string errStr;
723 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
724 if (!style->parent) {
725 mDiag->error(DiagMessage(source) << errStr);
726 return false;
727 }
728
729 if (Maybe<ResourceName> transformedName =
730 parser->transformPackage(style->parent.value().name.value(), u"")) {
731 style->parent.value().name = std::move(transformedName);
732 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800733 }
734
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700735 } else {
736 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700737 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700738 size_t pos = styleName.find_last_of(u'.');
739 if (pos != std::string::npos) {
740 style->parentInferred = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700741 style->parent = Reference(
742 ResourceName({}, ResourceType::kStyle, styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700743 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800744 }
745
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800746 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700747 std::u16string comment;
748 const size_t depth = parser->getDepth();
749 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800750 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700751 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800752 continue;
753 }
754
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 const std::u16string& elementNamespace = parser->getElementNamespace();
756 const std::u16string& elementName = parser->getElementName();
757 if (elementNamespace == u"" && elementName == u"item") {
758 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800759
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700760 } else if (elementNamespace.empty() &&
761 (elementName == u"skip" || elementName == u"eat-comment")) {
762 comment = u"";
763
764 } else {
765 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
766 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800767 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800768 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800769 }
770
771 if (error) {
772 return false;
773 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700774
775 outResource->value = std::move(style);
776 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700777}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800778
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700779bool ResourceParser::parseArray(XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700780 uint32_t typeMask) {
781 const Source source = mSource.withLine(parser->getLineNumber());
782 std::unique_ptr<Array> array = util::make_unique<Array>();
783
784 std::u16string comment;
785 bool error = false;
786 const size_t depth = parser->getDepth();
787 while (XmlPullParser::nextChildNode(parser, depth)) {
788 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
789 // Skip text and comments.
790 continue;
791 }
792
793 const Source itemSource = mSource.withLine(parser->getLineNumber());
794 const std::u16string& elementNamespace = parser->getElementNamespace();
795 const std::u16string& elementName = parser->getElementName();
796 if (elementNamespace.empty() && elementName == u"item") {
797 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
798 if (!item) {
799 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
800 error = true;
801 continue;
802 }
803 array->items.emplace_back(std::move(item));
804
805 } else if (elementNamespace.empty() &&
806 (elementName == u"skip" || elementName == u"eat-comment")) {
807 comment = u"";
808
809 } else {
810 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
811 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
812 error = true;
813 }
814 }
815
816 if (error) {
817 return false;
818 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700819
820 outResource->value = std::move(array);
821 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800822}
823
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700824bool ResourceParser::parsePlural(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700825 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800826 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
827
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700828 std::u16string comment;
829 bool error = false;
830 const size_t depth = parser->getDepth();
831 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800832 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700833 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800834 continue;
835 }
836
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700837 const std::u16string& elementNamespace = parser->getElementNamespace();
838 const std::u16string& elementName = parser->getElementName();
839 if (elementNamespace.empty() && elementName == u"item") {
840 const auto endAttrIter = parser->endAttributes();
841 auto attrIter = parser->findAttribute(u"", u"quantity");
842 if (attrIter == endAttrIter || attrIter->value.empty()) {
843 mDiag->error(DiagMessage(source) << "<item> in <plurals> requires attribute "
844 << "'quantity'");
845 error = true;
846 continue;
847 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800848
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700849 StringPiece16 trimmedQuantity = util::trimWhitespace(attrIter->value);
850 size_t index = 0;
851 if (trimmedQuantity == u"zero") {
852 index = Plural::Zero;
853 } else if (trimmedQuantity == u"one") {
854 index = Plural::One;
855 } else if (trimmedQuantity == u"two") {
856 index = Plural::Two;
857 } else if (trimmedQuantity == u"few") {
858 index = Plural::Few;
859 } else if (trimmedQuantity == u"many") {
860 index = Plural::Many;
861 } else if (trimmedQuantity == u"other") {
862 index = Plural::Other;
863 } else {
864 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
865 << "<item> in <plural> has invalid value '" << trimmedQuantity
866 << "' for attribute 'quantity'");
867 error = true;
868 continue;
869 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800870
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700871 if (plural->values[index]) {
872 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
873 << "duplicate quantity '" << trimmedQuantity << "'");
874 error = true;
875 continue;
876 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800877
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700878 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
879 kNoRawString))) {
880 error = true;
881 }
882 } else if (elementNamespace.empty() &&
883 (elementName == u"skip" || elementName == u"eat-comment")) {
884 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800885 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700886 mDiag->error(DiagMessage(source) << "unknown tag <" << elementNamespace << ":"
887 << elementName << ">");
888 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800889 }
890 }
891
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700892 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893 return false;
894 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700895
896 outResource->value = std::move(plural);
897 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800898}
899
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700900bool ResourceParser::parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700901 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800902 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
903
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700904 std::u16string comment;
905 bool error = false;
906 const size_t depth = parser->getDepth();
907 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800908 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700909 // Ignore text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800910 continue;
911 }
912
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700913 const std::u16string& elementNamespace = parser->getElementNamespace();
914 const std::u16string& elementName = parser->getElementName();
915 if (elementNamespace.empty() && elementName == u"attr") {
916 const auto endAttrIter = parser->endAttributes();
917 auto attrIter = parser->findAttribute(u"", u"name");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800918 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700919 mDiag->error(DiagMessage(source) << "<attr> tag must have a 'name' attribute");
920 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800921 continue;
922 }
923
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700924 ParsedResource childResource;
925 childResource.name = ResourceName({}, ResourceType::kAttr, attrIter->value);
926 childResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800927
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700928 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700929 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800930 continue;
931 }
932
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700933 styleable->entries.push_back(Reference(childResource.name));
934 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700936 } else if (elementNamespace.empty() &&
937 (elementName == u"skip" || elementName == u"eat-comment")) {
938 comment = u"";
939
940 } else {
941 mDiag->error(DiagMessage(source) << "unknown tag <" << elementNamespace << ":"
942 << elementName << ">");
943 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800944 }
945 }
946
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700947 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948 return false;
949 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700950
951 outResource->value = std::move(styleable);
952 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800953}
954
955} // namespace aapt