blob: bfef9d06d7424601bfc07f6d25088e2ca67dfbc0 [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 Lesinski9ba47d82015-10-13 11:37:10 -0700189 std::unique_ptr<Value> value;
190 std::list<ParsedResource> childResources;
191};
192
193// Recursively adds resources to the ResourceTable.
194static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config,
195 IDiagnostics* diag, ParsedResource* res) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700196 if (res->symbolState != SymbolState::kUndefined) {
197 if (!table->setSymbolState(res->name, res->id, res->source, res->symbolState, diag)) {
198 return false;
199 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700200 }
201
202 if (!res->value) {
203 return true;
204 }
205
206 if (!table->addResource(res->name, res->id, config, res->source, std::move(res->value), diag)) {
207 return false;
208 }
209
210 bool error = false;
211 for (ParsedResource& child : res->childResources) {
212 error |= !addResourcesToTable(table, config, diag, &child);
213 }
214 return !error;
215}
216
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217bool ResourceParser::parseResources(XmlPullParser* parser) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700218 std::set<ResourceName> strippedResources;
219
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700220 bool error = false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221 std::u16string comment;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 const size_t depth = parser->getDepth();
223 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 const XmlPullParser::Event event = parser->getEvent();
225 if (event == XmlPullParser::Event::kComment) {
226 comment = parser->getComment();
227 continue;
228 }
229
230 if (event == XmlPullParser::Event::kText) {
231 if (!util::trimWhitespace(parser->getText()).empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
233 << "plain text not allowed here");
234 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235 }
236 continue;
237 }
238
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239 assert(event == XmlPullParser::Event::kStartElement);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800240
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241 if (!parser->getElementNamespace().empty()) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242 // Skip unknown namespace.
243 continue;
244 }
245
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 std::u16string elementName = parser->getElementName();
247 if (elementName == u"skip" || elementName == u"eat-comment") {
248 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249 continue;
250 }
251
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
253 if (!maybeName) {
254 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
255 << "<" << elementName << "> tag must have a 'name' attribute");
256 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257 continue;
258 }
259
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700260 // Check if we should skip this product.
261 const bool stripResource = shouldStripResource(parser, mOptions.product);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800262
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263 if (elementName == u"item") {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264 // Items simply have their type encoded in the type attribute.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265 if (Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type")) {
266 elementName = maybeType.value().toString();
267 } else {
268 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
269 << "<item> must have a 'type' attribute");
270 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271 continue;
272 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273 }
274
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700275 ParsedResource parsedResource;
276 parsedResource.name.entry = maybeName.value().toString();
277 parsedResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700279 bool result = true;
280 if (elementName == u"id") {
281 parsedResource.name.type = ResourceType::kId;
282 parsedResource.value = util::make_unique<Id>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 } else if (elementName == u"string") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700284 parsedResource.name.type = ResourceType::kString;
285 result = parseString(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 } else if (elementName == u"color") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700287 parsedResource.name.type = ResourceType::kColor;
288 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700289 } else if (elementName == u"drawable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700290 parsedResource.name.type = ResourceType::kDrawable;
291 result = parseColor(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700292 } else if (elementName == u"bool") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700293 parsedResource.name.type = ResourceType::kBool;
294 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700295 } else if (elementName == u"integer") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700296 parsedResource.name.type = ResourceType::kInteger;
297 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700298 } else if (elementName == u"dimen") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700299 parsedResource.name.type = ResourceType::kDimen;
300 result = parsePrimitive(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 } else if (elementName == u"style") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700302 parsedResource.name.type = ResourceType::kStyle;
303 result = parseStyle(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 } else if (elementName == u"plurals") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700305 parsedResource.name.type = ResourceType::kPlurals;
306 result = parsePlural(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 } else if (elementName == u"array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700308 parsedResource.name.type = ResourceType::kArray;
309 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700310 } else if (elementName == u"string-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700311 parsedResource.name.type = ResourceType::kArray;
312 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_STRING);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313 } else if (elementName == u"integer-array") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700314 parsedResource.name.type = ResourceType::kIntegerArray;
315 result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700316 } else if (elementName == u"declare-styleable") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700317 parsedResource.name.type = ResourceType::kStyleable;
318 result = parseDeclareStyleable(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700319 } else if (elementName == u"attr") {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700320 parsedResource.name.type = ResourceType::kAttr;
321 result = parseAttr(parser, &parsedResource);
322 } else if (elementName == u"public") {
323 result = parsePublic(parser, &parsedResource);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700324 } else if (elementName == u"java-symbol" || elementName == u"symbol") {
325 result = parseSymbol(parser, &parsedResource);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700326 } else {
327 mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber()))
328 << "unknown resource type '" << elementName << "'");
329 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700330
331 if (result) {
332 // We successfully parsed the resource.
333
334 if (stripResource) {
335 // Record that we stripped out this resource name.
336 // We will check that at least one variant of this resource was included.
337 strippedResources.insert(parsedResource.name);
338 } else {
339 error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource);
340 }
341 } else {
342 error = true;
343 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800344 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700345
346 // Check that we included at least one variant of each stripped resource.
347 for (const ResourceName& strippedResource : strippedResources) {
348 if (!mTable->findResource(strippedResource)) {
349 // Failed to find the resource.
350 mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' "
351 "was filtered out but no product variant remains");
352 error = true;
353 }
354 }
355
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700356 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800357}
358
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800359enum {
360 kAllowRawString = true,
361 kNoRawString = false
362};
363
364/**
365 * Reads the entire XML subtree and attempts to parse it as some Item,
366 * with typeMask denoting which items it can be. If allowRawValue is
367 * true, a RawString is returned if the XML couldn't be parsed as
368 * an Item. If allowRawValue is false, nullptr is returned in this
369 * case.
370 */
371std::unique_ptr<Item> ResourceParser::parseXml(XmlPullParser* parser, uint32_t typeMask,
372 bool allowRawValue) {
373 const size_t beginXmlLine = parser->getLineNumber();
374
375 std::u16string rawValue;
376 StyleString styleString;
377 if (!flattenXmlSubtree(parser, &rawValue, &styleString)) {
378 return {};
379 }
380
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381 if (!styleString.spans.empty()) {
382 // This can only be a StyledString.
383 return util::make_unique<StyledString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700384 mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800385 }
386
387 auto onCreateReference = [&](const ResourceName& name) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700388 // name.package can be empty here, as it will assume the package name of the table.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700389 mTable->addResource(name, {}, mSource.withLine(beginXmlLine), util::make_unique<Id>(),
390 mDiag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391 };
392
393 // Process the raw value.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700394 std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask,
395 onCreateReference);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800396 if (processedItem) {
Adam Lesinski24aad162015-04-24 19:19:30 -0700397 // Fix up the reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398 if (Reference* ref = valueCast<Reference>(processedItem.get())) {
399 if (Maybe<ResourceName> transformedName =
400 parser->transformPackage(ref->name.value(), u"")) {
401 ref->name = std::move(transformedName);
Adam Lesinski24aad162015-04-24 19:19:30 -0700402 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404 return processedItem;
405 }
406
407 // Try making a regular string.
408 if (typeMask & android::ResTable_map::TYPE_STRING) {
409 // Use the trimmed, escaped string.
410 return util::make_unique<String>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700411 mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800412 }
413
414 // We can't parse this so return a RawString if we are allowed.
415 if (allowRawValue) {
416 return util::make_unique<RawString>(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417 mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig }));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418 }
419 return {};
420}
421
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700422bool ResourceParser::parseString(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700423 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700425 // TODO(adamlesinski): Read "untranslateable" attribute.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800426
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700427 outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
428 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700429 mDiag->error(DiagMessage(source) << "not a valid string");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800430 return false;
431 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700432 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433}
434
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700435bool ResourceParser::parseColor(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700438 outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString);
439 if (!outResource->value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440 mDiag->error(DiagMessage(source) << "invalid color");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441 return false;
442 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700443 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444}
445
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700446bool ResourceParser::parsePrimitive(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700447 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448
449 uint32_t typeMask = 0;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700450 switch (outResource->name.type) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700451 case ResourceType::kInteger:
452 typeMask |= android::ResTable_map::TYPE_INTEGER;
453 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800454
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700455 case ResourceType::kDimen:
456 typeMask |= android::ResTable_map::TYPE_DIMENSION
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700457 | android::ResTable_map::TYPE_FLOAT
458 | android::ResTable_map::TYPE_FRACTION;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700459 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800460
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700461 case ResourceType::kBool:
462 typeMask |= android::ResTable_map::TYPE_BOOLEAN;
463 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700465 default:
466 assert(false);
467 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468 }
469
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700470 outResource->value = parseXml(parser, typeMask, kNoRawString);
471 if (!outResource->value) {
472 mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473 return false;
474 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700475 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800476}
477
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700478bool ResourceParser::parsePublic(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700479 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800480
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700481 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
482 if (!maybeType) {
483 mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484 return false;
485 }
486
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700487 const ResourceType* parsedType = parseResourceType(maybeType.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488 if (!parsedType) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700489 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
490 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491 return false;
492 }
493
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700494 outResource->name.type = *parsedType;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700496 if (Maybe<StringPiece16> maybeId = findNonEmptyAttribute(parser, u"id")) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700498 bool result = android::ResTable::stringToInt(maybeId.value().data(),
499 maybeId.value().size(), &val);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700500 ResourceId resourceId(val.data);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501 if (!result || !resourceId.isValid()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502 mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value()
503 << "' in <public>");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504 return false;
505 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700506 outResource->id = resourceId;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507 }
508
509 if (*parsedType == ResourceType::kId) {
510 // An ID marked as public is also the definition of an ID.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700511 outResource->value = util::make_unique<Id>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700513
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700514 outResource->symbolState = SymbolState::kPublic;
515 return true;
516}
517
518bool ResourceParser::parseSymbol(XmlPullParser* parser, ParsedResource* outResource) {
519 const Source source = mSource.withLine(parser->getLineNumber());
520
521 Maybe<StringPiece16> maybeType = findNonEmptyAttribute(parser, u"type");
522 if (!maybeType) {
523 mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a "
524 "'type' attribute");
525 return false;
526 }
527
528 const ResourceType* parsedType = parseResourceType(maybeType.value());
529 if (!parsedType) {
530 mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value()
531 << "' in <" << parser->getElementName() << ">");
532 return false;
533 }
534
535 outResource->name.type = *parsedType;
536 outResource->symbolState = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700537 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800538}
539
540static uint32_t parseFormatType(const StringPiece16& piece) {
541 if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE;
542 else if (piece == u"string") return android::ResTable_map::TYPE_STRING;
543 else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER;
544 else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN;
545 else if (piece == u"color") return android::ResTable_map::TYPE_COLOR;
546 else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT;
547 else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION;
548 else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION;
549 else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM;
550 else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS;
551 return 0;
552}
553
554static uint32_t parseFormatAttribute(const StringPiece16& str) {
555 uint32_t mask = 0;
556 for (StringPiece16 part : util::tokenize(str, u'|')) {
557 StringPiece16 trimmedPart = util::trimWhitespace(part);
558 uint32_t type = parseFormatType(trimmedPart);
559 if (type == 0) {
560 return 0;
561 }
562 mask |= type;
563 }
564 return mask;
565}
566
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700567
568bool ResourceParser::parseAttr(XmlPullParser* parser, ParsedResource* outResource) {
569 outResource->source = mSource.withLine(parser->getLineNumber());
570 return parseAttrImpl(parser, outResource, false);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800571}
572
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700573bool ResourceParser::parseAttrImpl(XmlPullParser* parser, ParsedResource* outResource, bool weak) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800574 uint32_t typeMask = 0;
575
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700576 Maybe<StringPiece16> maybeFormat = findAttribute(parser, u"format");
577 if (maybeFormat) {
578 typeMask = parseFormatAttribute(maybeFormat.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800579 if (typeMask == 0) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700580 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
581 << "invalid attribute format '" << maybeFormat.value() << "'");
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700582 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800583 }
584 }
585
Adam Lesinski769de982015-04-10 19:43:55 -0700586 // If this is a declaration, the package name may be in the name. Separate these out.
587 // Eg. <attr name="android:text" />
588 // No format attribute is allowed.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700589 if (weak && !maybeFormat) {
Adam Lesinski769de982015-04-10 19:43:55 -0700590 StringPiece16 package, type, name;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700591 ResourceUtils::extractResourceName(outResource->name.entry, &package, &type, &name);
Adam Lesinski769de982015-04-10 19:43:55 -0700592 if (type.empty() && !package.empty()) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700593 outResource->name.package = package.toString();
594 outResource->name.entry = name.toString();
Adam Lesinski769de982015-04-10 19:43:55 -0700595 }
596 }
597
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800598 std::vector<Attribute::Symbol> items;
599
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600 std::u16string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800601 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700602 const size_t depth = parser->getDepth();
603 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800604 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605 // Skip comments and text.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606 continue;
607 }
608
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609 const std::u16string& elementNamespace = parser->getElementNamespace();
610 const std::u16string& elementName = parser->getElementName();
611 if (elementNamespace == u"" && (elementName == u"flag" || elementName == u"enum")) {
612 if (elementName == u"enum") {
613 if (typeMask & android::ResTable_map::TYPE_FLAGS) {
614 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
615 << "can not define an <enum>; already defined a <flag>");
616 error = true;
617 continue;
618 }
619 typeMask |= android::ResTable_map::TYPE_ENUM;
620 } else if (elementName == u"flag") {
621 if (typeMask & android::ResTable_map::TYPE_ENUM) {
622 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
623 << "can not define a <flag>; already defined an <enum>");
624 error = true;
625 continue;
626 }
627 typeMask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800628 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800629
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700630 if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700631 ParsedResource childResource;
632 childResource.name = s.value().symbol.name.value();
633 childResource.source = mSource.withLine(parser->getLineNumber());
634 childResource.value = util::make_unique<Id>();
635 outResource->childResources.push_back(std::move(childResource));
636 items.push_back(std::move(s.value()));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800637 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800639 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700640 } else if (elementName == u"skip" || elementName == u"eat-comment") {
641 comment = u"";
642
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800643 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
645 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800646 error = true;
647 }
648 }
649
650 if (error) {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700651 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800652 }
653
654 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
655 attr->symbols.swap(items);
Adam Lesinskica2fc352015-04-03 12:08:26 -0700656 attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700657 outResource->value = std::move(attr);
658 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659}
660
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700661Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(XmlPullParser* parser,
662 const StringPiece16& tag) {
663 const Source source = mSource.withLine(parser->getLineNumber());
664
665 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
666 if (!maybeName) {
667 mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">");
668 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800669 }
670
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700671 Maybe<StringPiece16> maybeValue = findNonEmptyAttribute(parser, u"value");
672 if (!maybeValue) {
673 mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">");
674 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800675 }
676
677 android::Res_value val;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700678 if (!android::ResTable::stringToInt(maybeValue.value().data(),
679 maybeValue.value().size(), &val)) {
680 mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value()
681 << "' for <" << tag << ">; must be an integer");
682 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800683 }
684
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700685 return Attribute::Symbol{
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700686 Reference(ResourceName{ {}, ResourceType::kId, maybeName.value().toString() }),
687 val.data };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800688}
689
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700690static Maybe<ResourceName> parseXmlAttributeName(StringPiece16 str) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800691 str = util::trimWhitespace(str);
692 const char16_t* const start = str.data();
693 const char16_t* const end = start + str.size();
694 const char16_t* p = start;
695
696 StringPiece16 package;
697 StringPiece16 name;
698 while (p != end) {
699 if (*p == u':') {
700 package = StringPiece16(start, p - start);
701 name = StringPiece16(p + 1, end - (p + 1));
702 break;
703 }
704 p++;
705 }
706
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707 return ResourceName{ package.toString(), ResourceType::kAttr,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700708 name.empty() ? str.toString() : name.toString() };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800709}
710
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700711
712bool ResourceParser::parseStyleItem(XmlPullParser* parser, Style* style) {
713 const Source source = mSource.withLine(parser->getLineNumber());
714
715 Maybe<StringPiece16> maybeName = findNonEmptyAttribute(parser, u"name");
716 if (!maybeName) {
717 mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800718 return false;
719 }
720
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700721 Maybe<ResourceName> maybeKey = parseXmlAttributeName(maybeName.value());
722 if (!maybeKey) {
723 mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800724 return false;
725 }
726
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700727 if (Maybe<ResourceName> transformedName = parser->transformPackage(maybeKey.value(), u"")) {
728 maybeKey = std::move(transformedName);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800729 }
730
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800731 std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString);
732 if (!value) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700733 mDiag->error(DiagMessage(source) << "could not parse style item");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800734 return false;
735 }
736
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700737 style->entries.push_back(Style::Entry{ Reference(maybeKey.value()), std::move(value) });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800738 return true;
739}
740
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700741bool ResourceParser::parseStyle(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700742 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700743 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800744
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700745 Maybe<StringPiece16> maybeParent = findAttribute(parser, u"parent");
746 if (maybeParent) {
747 // If the parent is empty, we don't have a parent, but we also don't infer either.
748 if (!maybeParent.value().empty()) {
749 std::string errStr;
750 style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr);
751 if (!style->parent) {
752 mDiag->error(DiagMessage(source) << errStr);
753 return false;
754 }
755
756 if (Maybe<ResourceName> transformedName =
757 parser->transformPackage(style->parent.value().name.value(), u"")) {
758 style->parent.value().name = std::move(transformedName);
759 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800760 }
761
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700762 } else {
763 // No parent was specified, so try inferring it from the style name.
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700764 std::u16string styleName = outResource->name.entry;
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700765 size_t pos = styleName.find_last_of(u'.');
766 if (pos != std::string::npos) {
767 style->parentInferred = true;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700768 style->parent = Reference(
769 ResourceName({}, ResourceType::kStyle, styleName.substr(0, pos)));
Adam Lesinskibdaa0922015-05-08 20:16:23 -0700770 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800771 }
772
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800773 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700774 std::u16string comment;
775 const size_t depth = parser->getDepth();
776 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800777 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700778 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800779 continue;
780 }
781
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700782 const std::u16string& elementNamespace = parser->getElementNamespace();
783 const std::u16string& elementName = parser->getElementName();
784 if (elementNamespace == u"" && elementName == u"item") {
785 error |= !parseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800786
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700787 } else if (elementNamespace.empty() &&
788 (elementName == u"skip" || elementName == u"eat-comment")) {
789 comment = u"";
790
791 } else {
792 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
793 << ":" << elementName << ">");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800794 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800795 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800796 }
797
798 if (error) {
799 return false;
800 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700801
802 outResource->value = std::move(style);
803 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700804}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800805
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700806bool ResourceParser::parseArray(XmlPullParser* parser, ParsedResource* outResource,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700807 uint32_t typeMask) {
808 const Source source = mSource.withLine(parser->getLineNumber());
809 std::unique_ptr<Array> array = util::make_unique<Array>();
810
811 std::u16string comment;
812 bool error = false;
813 const size_t depth = parser->getDepth();
814 while (XmlPullParser::nextChildNode(parser, depth)) {
815 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
816 // Skip text and comments.
817 continue;
818 }
819
820 const Source itemSource = mSource.withLine(parser->getLineNumber());
821 const std::u16string& elementNamespace = parser->getElementNamespace();
822 const std::u16string& elementName = parser->getElementName();
823 if (elementNamespace.empty() && elementName == u"item") {
824 std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString);
825 if (!item) {
826 mDiag->error(DiagMessage(itemSource) << "could not parse array item");
827 error = true;
828 continue;
829 }
830 array->items.emplace_back(std::move(item));
831
832 } else if (elementNamespace.empty() &&
833 (elementName == u"skip" || elementName == u"eat-comment")) {
834 comment = u"";
835
836 } else {
837 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
838 << "unknown tag <" << elementNamespace << ":" << elementName << ">");
839 error = true;
840 }
841 }
842
843 if (error) {
844 return false;
845 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700846
847 outResource->value = std::move(array);
848 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800849}
850
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700851bool ResourceParser::parsePlural(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700852 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800853 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
854
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700855 std::u16string comment;
856 bool error = false;
857 const size_t depth = parser->getDepth();
858 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800859 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700860 // Skip text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800861 continue;
862 }
863
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700864 const std::u16string& elementNamespace = parser->getElementNamespace();
865 const std::u16string& elementName = parser->getElementName();
866 if (elementNamespace.empty() && elementName == u"item") {
867 const auto endAttrIter = parser->endAttributes();
868 auto attrIter = parser->findAttribute(u"", u"quantity");
869 if (attrIter == endAttrIter || attrIter->value.empty()) {
870 mDiag->error(DiagMessage(source) << "<item> in <plurals> requires attribute "
871 << "'quantity'");
872 error = true;
873 continue;
874 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700876 StringPiece16 trimmedQuantity = util::trimWhitespace(attrIter->value);
877 size_t index = 0;
878 if (trimmedQuantity == u"zero") {
879 index = Plural::Zero;
880 } else if (trimmedQuantity == u"one") {
881 index = Plural::One;
882 } else if (trimmedQuantity == u"two") {
883 index = Plural::Two;
884 } else if (trimmedQuantity == u"few") {
885 index = Plural::Few;
886 } else if (trimmedQuantity == u"many") {
887 index = Plural::Many;
888 } else if (trimmedQuantity == u"other") {
889 index = Plural::Other;
890 } else {
891 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
892 << "<item> in <plural> has invalid value '" << trimmedQuantity
893 << "' for attribute 'quantity'");
894 error = true;
895 continue;
896 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800897
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700898 if (plural->values[index]) {
899 mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber()))
900 << "duplicate quantity '" << trimmedQuantity << "'");
901 error = true;
902 continue;
903 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800904
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700905 if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING,
906 kNoRawString))) {
907 error = true;
908 }
909 } else if (elementNamespace.empty() &&
910 (elementName == u"skip" || elementName == u"eat-comment")) {
911 comment = u"";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800912 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700913 mDiag->error(DiagMessage(source) << "unknown tag <" << elementNamespace << ":"
914 << elementName << ">");
915 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800916 }
917 }
918
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700919 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800920 return false;
921 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700922
923 outResource->value = std::move(plural);
924 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800925}
926
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700927bool ResourceParser::parseDeclareStyleable(XmlPullParser* parser, ParsedResource* outResource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700928 const Source source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800929 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
930
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700931 std::u16string comment;
932 bool error = false;
933 const size_t depth = parser->getDepth();
934 while (XmlPullParser::nextChildNode(parser, depth)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800935 if (parser->getEvent() != XmlPullParser::Event::kStartElement) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700936 // Ignore text and comments.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800937 continue;
938 }
939
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700940 const std::u16string& elementNamespace = parser->getElementNamespace();
941 const std::u16string& elementName = parser->getElementName();
942 if (elementNamespace.empty() && elementName == u"attr") {
943 const auto endAttrIter = parser->endAttributes();
944 auto attrIter = parser->findAttribute(u"", u"name");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800945 if (attrIter == endAttrIter || attrIter->value.empty()) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700946 mDiag->error(DiagMessage(source) << "<attr> tag must have a 'name' attribute");
947 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948 continue;
949 }
950
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700951 ParsedResource childResource;
952 childResource.name = ResourceName({}, ResourceType::kAttr, attrIter->value);
953 childResource.source = mSource.withLine(parser->getLineNumber());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800954
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700955 if (!parseAttrImpl(parser, &childResource, true)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700956 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800957 continue;
958 }
959
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700960 styleable->entries.push_back(Reference(childResource.name));
961 outResource->childResources.push_back(std::move(childResource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800962
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700963 } else if (elementNamespace.empty() &&
964 (elementName == u"skip" || elementName == u"eat-comment")) {
965 comment = u"";
966
967 } else {
968 mDiag->error(DiagMessage(source) << "unknown tag <" << elementNamespace << ":"
969 << elementName << ">");
970 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800971 }
972 }
973
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700974 if (error) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800975 return false;
976 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700977
978 outResource->value = std::move(styleable);
979 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800980}
981
982} // namespace aapt