blob: b16def4025fff16892883c82cd2afe0b2c9a179c [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 Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
20#include <sstream>
21
22#include "android-base/logging.h"
23
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include "ResourceTable.h"
25#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "ValueVisitor.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080028#include "util/ImmutableMap.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070029#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080030#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070031
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034constexpr const char* sXliffNamespaceUri =
35 "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinski27afb9e2015-11-06 18:25:04 -080037/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 * Returns true if the element is <skip> or <eat-comment> and can be safely
39 * ignored.
Adam Lesinski27afb9e2015-11-06 18:25:04 -080040 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041static bool ShouldIgnoreElement(const StringPiece& ns,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 const StringPiece& name) {
43 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080044}
45
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046static uint32_t ParseFormatType(const StringPiece& piece) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 if (piece == "reference")
48 return android::ResTable_map::TYPE_REFERENCE;
49 else if (piece == "string")
50 return android::ResTable_map::TYPE_STRING;
51 else if (piece == "integer")
52 return android::ResTable_map::TYPE_INTEGER;
53 else if (piece == "boolean")
54 return android::ResTable_map::TYPE_BOOLEAN;
55 else if (piece == "color")
56 return android::ResTable_map::TYPE_COLOR;
57 else if (piece == "float")
58 return android::ResTable_map::TYPE_FLOAT;
59 else if (piece == "dimension")
60 return android::ResTable_map::TYPE_DIMENSION;
61 else if (piece == "fraction")
62 return android::ResTable_map::TYPE_FRACTION;
63 else if (piece == "enum")
64 return android::ResTable_map::TYPE_ENUM;
65 else if (piece == "flags")
66 return android::ResTable_map::TYPE_FLAGS;
67 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068}
69
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 for (StringPiece part : util::Tokenize(str, '|')) {
73 StringPiece trimmed_part = util::TrimWhitespace(part);
74 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 if (type == 0) {
76 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080077 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 mask |= type;
79 }
80 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080081}
82
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080083/**
84 * A parsed resource ready to be added to the ResourceTable.
85 */
86struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 ResourceName name;
88 ConfigDescription config;
89 std::string product;
90 Source source;
91 ResourceId id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 Maybe<SymbolState> symbol_state;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 std::string comment;
94 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080096};
97
98// Recursively adds resources to the ResourceTable.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
102 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 // Only if there was a change do we re-assign.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 res->comment = trimmed_comment.ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 if (res->symbol_state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 symbol.state = res->symbol_state.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 symbol.source = res->source;
111 symbol.comment = res->comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 if (!table->SetSymbolState(res->name, res->id, symbol, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800114 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 if (res->value) {
118 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 res->value->SetComment(std::move(res->comment));
120 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 if (!table->AddResource(res->name, res->id, res->config, res->product,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 std::move(res->value), diag)) {
124 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800125 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800127
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 for (ParsedResource& child : res->child_resources) {
130 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 }
132 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800133}
134
135// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
139 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700140 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 : diag_(diag),
143 table_(table),
144 source_(source),
145 config_(config),
146 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147
148/**
149 * Build a string from XML that converts nested elements into Span objects.
150 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151bool ResourceParser::FlattenXmlSubtree(xml::XmlPullParser* parser,
152 std::string* out_raw_string,
153 StyleString* out_style_string) {
154 std::vector<Span> span_stack;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 out_raw_string->clear();
158 out_style_string->spans.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 util::StringBuilder builder;
160 size_t depth = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
162 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 // We already warned and skipped the start element, so just skip here
166 // too
167 continue;
168 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 depth--;
171 if (depth == 0) {
172 break;
173 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 span_stack.back().last_char = builder.Utf16Len() - 1;
176 out_style_string->spans.push_back(span_stack.back());
177 span_stack.pop_back();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 out_raw_string->append(parser->text());
181 builder.Append(parser->text());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 if (!parser->element_namespace().empty()) {
185 if (parser->element_namespace() != sXliffNamespaceUri) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 // Only warn if this isn't an xliff namespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
188 << "skipping element '" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 << "' with unknown namespace '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 << parser->element_namespace() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 continue;
193 }
194 depth++;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 // Build a span object out of the nested element.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 std::string span_name = parser->element_name();
198 const auto end_attr_iter = parser->end_attributes();
199 for (auto attr_iter = parser->begin_attributes();
200 attr_iter != end_attr_iter; ++attr_iter) {
201 span_name += ";";
202 span_name += attr_iter->name;
203 span_name += "=";
204 span_name += attr_iter->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 }
206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 if (builder.Utf16Len() > std::numeric_limits<uint32_t>::max()) {
208 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
209 << "style string '" << builder.ToString()
210 << "' is too long");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 error = true;
212 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 span_stack.push_back(
214 Span{span_name, static_cast<uint32_t>(builder.Utf16Len())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
216
217 } else if (event == xml::XmlPullParser::Event::kComment) {
218 // Skip
219 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 LOG(FATAL) << "unhandled XML event";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 }
222 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 CHECK(span_stack.empty()) << "spans haven't been fully processed";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 out_style_string->str = builder.ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227}
228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 const size_t depth = parser->depth();
232 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
233 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 // Skip comments and text.
235 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 if (!parser->element_namespace().empty() ||
239 parser->element_name() != "resources") {
240 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 << "root element must be <resources>");
242 return false;
243 }
244
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 break;
247 };
248
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
250 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
251 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 return false;
253 }
254 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255}
256
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
258 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700259
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 bool error = false;
261 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 const size_t depth = parser->depth();
263 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
264 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700269
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (!util::TrimWhitespace(parser->text()).empty()) {
272 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 << "plain text not allowed here");
274 error = true;
275 }
276 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700277 }
278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 // Skip unknown namespace.
283 continue;
284 }
285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 std::string element_name = parser->element_name();
287 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 comment = "";
289 continue;
290 }
291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 ParsedResource parsed_resource;
293 parsed_resource.config = config_;
294 parsed_resource.source = source_.WithLine(parser->line_number());
295 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296
297 // Extract the product name if it exists.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 if (Maybe<StringPiece> maybe_product =
299 xml::FindNonEmptyAttribute(parser, "product")) {
300 parsed_resource.product = maybe_product.value().ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 }
302
303 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 error = true;
306 continue;
307 }
308
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 error = true;
311 }
312 }
313
314 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 for (const ResourceName& stripped_resource : stripped_resources) {
316 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 // Failed to find the resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 diag_->Error(DiagMessage(source_)
319 << "resource '" << stripped_resource
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 << "' "
321 "was filtered out but no product variant remains");
322 error = true;
323 }
324 }
325
326 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800327}
328
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
330 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 struct ItemTypeFormat {
332 ResourceType type;
333 uint32_t format;
334 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800335
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
337 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800338
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 static const auto elToItemMap =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
342 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
343 {"dimen",
344 {ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT |
345 android::ResTable_map::TYPE_FRACTION |
346 android::ResTable_map::TYPE_DIMENSION}},
347 {"drawable",
348 {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
349 {"fraction",
350 {ResourceType::kFraction,
351 android::ResTable_map::TYPE_FLOAT |
352 android::ResTable_map::TYPE_FRACTION |
353 android::ResTable_map::TYPE_DIMENSION}},
354 {"integer",
355 {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
356 {"string",
357 {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
358 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800359
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 static const auto elToBagMap =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
362 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
363 {"array", std::mem_fn(&ResourceParser::ParseArray)},
364 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 {"declare-styleable",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
367 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
368 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
369 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
370 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
371 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
372 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
373 {"style", std::mem_fn(&ResourceParser::ParseStyle)},
374 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800378
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800381
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 if (resource_type == "item") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 // Items have their type encoded in the type attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 if (Maybe<StringPiece> maybe_type =
385 xml::FindNonEmptyAttribute(parser, "type")) {
386 resource_type = maybe_type.value().ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 << "<item> must have a 'type' attribute");
390 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800391 }
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 if (Maybe<StringPiece> maybe_format =
394 xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 // An explicit format for this resource was specified. The resource will
396 // retain
397 // its type in its name, but the accepted value for this type is
398 // overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 resource_format = ParseFormatType(maybe_format.value());
400 if (!resource_format) {
401 diag_->Error(DiagMessage(out_resource->source)
402 << "'" << maybe_format.value()
403 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800404 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 }
406 }
407 }
408
409 // Get the name of the resource. This will be checked later, because not all
410 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 if (resource_type == "id") {
414 if (!maybe_name) {
415 diag_->Error(DiagMessage(out_resource->source)
416 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 << "> missing 'name' attribute");
418 return false;
419 }
420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 out_resource->name.type = ResourceType::kId;
422 out_resource->name.entry = maybe_name.value().ToString();
423 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return true;
425 }
426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 const auto item_iter = elToItemMap.find(resource_type);
428 if (item_iter != elToItemMap.end()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 // This is an item, record its type and format and start parsing.
430
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 if (!maybe_name) {
432 diag_->Error(DiagMessage(out_resource->source)
433 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 << "> missing 'name' attribute");
435 return false;
436 }
437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 out_resource->name.type = item_iter->second.type;
439 out_resource->name.entry = maybe_name.value().ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440
441 // Only use the implicit format for this type if it wasn't overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 if (!resource_format) {
443 resource_format = item_iter->second.format;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 }
445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 if (!ParseItem(parser, out_resource, resource_format)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800448 }
449 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 }
451
452 // This might be a bag or something.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 const auto bag_iter = elToBagMap.find(resource_type);
454 if (bag_iter != elToBagMap.end()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 if (resource_type != "public-group") {
457 if (!maybe_name) {
458 diag_->Error(DiagMessage(out_resource->source)
459 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 << "> missing 'name' attribute");
461 return false;
462 }
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 out_resource->name.entry = maybe_name.value().ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 }
466
467 // Call the associated parse method. The type will be filled in by the
468 // parse func.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 if (!bag_iter->second(this, parser, out_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 return false;
471 }
472 return true;
473 }
474
475 // Try parsing the elementName (or type) as a resource. These shall only be
476 // resources like 'layout' or 'xml' and they can only be references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 const ResourceType* parsed_type = ParseResourceType(resource_type);
478 if (parsed_type) {
479 if (!maybe_name) {
480 diag_->Error(DiagMessage(out_resource->source)
481 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 << "> missing 'name' attribute");
483 return false;
484 }
485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 out_resource->name.type = *parsed_type;
487 out_resource->name.entry = maybe_name.value().ToString();
488 out_resource->value =
489 ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
490 if (!out_resource->value) {
491 diag_->Error(DiagMessage(out_resource->source)
492 << "invalid value for type '" << *parsed_type
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 << "'. Expected a reference");
494 return false;
495 }
496 return true;
497 }
498
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 diag_->Warn(DiagMessage(out_resource->source)
500 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 return false;
502}
503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
505 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 const uint32_t format) {
507 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 }
510
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 out_resource->value = ParseXml(parser, format, kNoRawString);
512 if (!out_resource->value) {
513 diag_->Error(DiagMessage(out_resource->source) << "invalid "
514 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 return false;
516 }
517 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800518}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800519
520/**
521 * Reads the entire XML subtree and attempts to parse it as some Item,
522 * with typeMask denoting which items it can be. If allowRawValue is
523 * true, a RawString is returned if the XML couldn't be parsed as
524 * an Item. If allowRawValue is false, nullptr is returned in this
525 * case.
526 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
528 const uint32_t type_mask,
529 const bool allow_raw_value) {
530 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800531
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 std::string raw_value;
533 StyleString style_string;
534 if (!FlattenXmlSubtree(parser, &raw_value, &style_string)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800535 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 }
537
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 // This can only be a StyledString.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 return util::make_unique<StyledString>(table_->string_pool.MakeRef(
541 style_string,
542 StringPool::Context(StringPool::Context::kStylePriority, config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 }
544
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 // name.package can be empty here, as it will assume the package name of the
547 // table.
548 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 id->SetSource(source_.WithLine(begin_xml_line));
550 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 };
552
553 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 std::unique_ptr<Item> processed_item =
555 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
556 on_create_reference);
557 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
560 TransformReferenceFromNamespace(parser, "", ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
564
565 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 // Use the trimmed, escaped string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 return util::make_unique<String>(table_->string_pool.MakeRef(
569 style_string.str, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 }
571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 // We can't parse this so return a RawString if we are allowed.
574 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 }
577 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800578}
579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580bool ResourceParser::ParseString(xml::XmlPullParser* parser,
581 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if (Maybe<StringPiece> formatted_attr =
584 xml::FindAttribute(parser, "formatted")) {
585 Maybe<bool> maybe_formatted =
586 ResourceUtils::ParseBool(formatted_attr.value());
587 if (!maybe_formatted) {
588 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 << "invalid value for 'formatted'. Must be a boolean");
590 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800591 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800594
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 bool translateable = options_.translatable;
596 if (Maybe<StringPiece> translateable_attr =
597 xml::FindAttribute(parser, "translatable")) {
598 Maybe<bool> maybe_translateable =
599 ResourceUtils::ParseBool(translateable_attr.value());
600 if (!maybe_translateable) {
601 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 << "invalid value for 'translatable'. Must be a boolean");
603 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800604 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 translateable = maybe_translateable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 out_resource->value =
609 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
610 if (!out_resource->value) {
611 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return false;
613 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800614
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
616 string_value->SetTranslateable(translateable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800617
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 if (formatted && translateable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 if (!util::VerifyJavaStringFormat(*string_value->value)) {
620 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 msg << "multiple substitutions specified in non-positional format; "
622 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 if (options_.error_on_positional_arguments) {
624 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800626 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800627
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800630 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 } else if (StyledString* string_value =
633 ValueCast<StyledString>(out_resource->value.get())) {
634 string_value->SetTranslateable(translateable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 }
636 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800637}
638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
640 ParsedResource* out_resource) {
641 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
642 if (!maybe_type) {
643 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 << "<public> must have a 'type' attribute");
645 return false;
646 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800647
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
649 if (!parsed_type) {
650 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
651 << maybe_type.value()
652 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 return false;
654 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800657
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 if (Maybe<StringPiece> maybe_id_str =
659 xml::FindNonEmptyAttribute(parser, "id")) {
660 Maybe<ResourceId> maybe_id =
661 ResourceUtils::ParseResourceId(maybe_id_str.value());
662 if (!maybe_id) {
663 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
664 << maybe_id.value()
665 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800667 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800670
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700675
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800678}
679
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
681 ParsedResource* out_resource) {
682 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
683 if (!maybe_type) {
684 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800686 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800688
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
690 if (!parsed_type) {
691 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
692 << maybe_type.value()
693 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800694 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 }
696
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 Maybe<StringPiece> maybe_id_str =
698 xml::FindNonEmptyAttribute(parser, "first-id");
699 if (!maybe_id_str) {
700 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 << "<public-group> must have a 'first-id' attribute");
702 return false;
703 }
704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 Maybe<ResourceId> maybe_id =
706 ResourceUtils::ParseResourceId(maybe_id_str.value());
707 if (!maybe_id) {
708 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
709 << maybe_id_str.value()
710 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 return false;
712 }
713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715
716 std::string comment;
717 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700718 const size_t depth = parser->depth();
719 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
720 if (parser->event() == xml::XmlPullParser::Event::kComment) {
721 comment = util::TrimWhitespace(parser->comment()).ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 // Skip text.
725 continue;
726 }
727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 const Source item_source = source_.WithLine(parser->line_number());
729 const std::string& element_namespace = parser->element_namespace();
730 const std::string& element_name = parser->element_name();
731 if (element_namespace.empty() && element_name == "public") {
732 Maybe<StringPiece> maybe_name =
733 xml::FindNonEmptyAttribute(parser, "name");
734 if (!maybe_name) {
735 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 << "<public> must have a 'name' attribute");
737 error = true;
738 continue;
739 }
740
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 if (xml::FindNonEmptyAttribute(parser, "id")) {
742 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 << "'id' is ignored within <public-group>");
744 error = true;
745 continue;
746 }
747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 if (xml::FindNonEmptyAttribute(parser, "type")) {
749 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 << "'type' is ignored within <public-group>");
751 error = true;
752 continue;
753 }
754
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755 ParsedResource child_resource;
756 child_resource.name.type = *parsed_type;
757 child_resource.name.entry = maybe_name.value().ToString();
758 child_resource.id = next_id;
759 child_resource.comment = std::move(comment);
760 child_resource.source = item_source;
761 child_resource.symbol_state = SymbolState::kPublic;
762 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
767 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 error = true;
769 }
770 }
771 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800772}
773
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
775 ParsedResource* out_resource) {
776 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
777 if (!maybe_type) {
778 diag_->Error(DiagMessage(out_resource->source)
779 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780 << "> must have a 'type' attribute");
781 return false;
782 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800783
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
785 if (!parsed_type) {
786 diag_->Error(DiagMessage(out_resource->source)
787 << "invalid resource type '" << maybe_type.value() << "' in <"
788 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789 return false;
790 }
791
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800794}
795
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700796bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
797 ParsedResource* out_resource) {
798 if (ParseSymbolImpl(parser, out_resource)) {
799 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700800 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 }
802 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800803}
804
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
806 ParsedResource* out_resource) {
807 if (ParseSymbolImpl(parser, out_resource)) {
808 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 return true;
810 }
811 return false;
812}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700813
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
815 ParsedResource* out_resource) {
816 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817}
818
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
820 ParsedResource* out_resource, bool weak) {
821 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822
823 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 if (out_resource->config != ConfigDescription::DefaultConfig()) {
825 diag_->Warn(DiagMessage(out_resource->source)
826 << "ignoring configuration '" << out_resource->config
827 << "' for attribute " << out_resource->name);
828 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 }
830
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
834 if (maybe_format) {
835 type_mask = ParseFormatAttribute(maybe_format.value());
836 if (type_mask == 0) {
837 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
838 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 << "'");
840 return false;
841 }
842 }
843
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
847 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
848 if (!min_str.empty()) {
849 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700851 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700853 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800855 }
856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 if (!maybe_min) {
858 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
859 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 return false;
861 }
862 }
863
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
865 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
866 if (!max_str.empty()) {
867 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873 }
874
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 if (!maybe_max) {
876 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
877 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return false;
879 }
880 }
881
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700882 if ((maybe_min || maybe_max) &&
883 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
884 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 << "'min' and 'max' can only be used when format='integer'");
886 return false;
887 }
888
889 struct SymbolComparator {
890 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
891 return a.symbol.name.value() < b.symbol.name.value();
892 }
893 };
894
895 std::set<Attribute::Symbol, SymbolComparator> items;
896
897 std::string comment;
898 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 const size_t depth = parser->depth();
900 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
901 if (parser->event() == xml::XmlPullParser::Event::kComment) {
902 comment = util::TrimWhitespace(parser->comment()).ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 // Skip text.
906 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800907 }
908
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700909 const Source item_source = source_.WithLine(parser->line_number());
910 const std::string& element_namespace = parser->element_namespace();
911 const std::string& element_name = parser->element_name();
912 if (element_namespace.empty() &&
913 (element_name == "flag" || element_name == "enum")) {
914 if (element_name == "enum") {
915 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
916 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 << "can not define an <enum>; already defined a <flag>");
918 error = true;
919 continue;
920 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700923 } else if (element_name == "flag") {
924 if (type_mask & android::ResTable_map::TYPE_ENUM) {
925 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700926 << "can not define a <flag>; already defined an <enum>");
927 error = true;
928 continue;
929 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700930 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 }
932
933 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700934 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700936 ParsedResource child_resource;
937 child_resource.name = symbol.symbol.name.value();
938 child_resource.source = item_source;
939 child_resource.value = util::make_unique<Id>();
940 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 symbol.symbol.SetComment(std::move(comment));
943 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700945 auto insert_result = items.insert(std::move(symbol));
946 if (!insert_result.second) {
947 const Attribute::Symbol& existing_symbol = *insert_result.first;
948 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700950 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 << "first defined here");
954 error = true;
955 }
956 } else {
957 error = true;
958 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700959 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
960 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 error = true;
962 }
963
964 comment = {};
965 }
966
967 if (error) {
968 return false;
969 }
970
971 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
972 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 attr->type_mask =
974 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
975 if (maybe_min) {
976 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 }
978
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 if (maybe_max) {
980 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700982 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 return true;
984}
985
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700988 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
991 if (!maybe_name) {
992 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 << tag << ">");
994 return {};
995 }
996
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
998 if (!maybe_value) {
999 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 << tag << ">");
1001 return {};
1002 }
1003
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001004 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 android::Res_value val;
1006 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008 << "' for <" << tag
1009 << ">; must be an integer");
1010 return {};
1011 }
1012
1013 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001014 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001015 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001016}
1017
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1019 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001020
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1022 if (!maybe_name) {
1023 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 return false;
1025 }
1026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 Maybe<Reference> maybe_key =
1028 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1029 if (!maybe_key) {
1030 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1031 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 return false;
1033 }
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1036 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 return false;
1042 }
1043
1044 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001045 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046 return true;
1047}
1048
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001049bool ResourceParser::ParseStyle(xml::XmlPullParser* parser,
1050 ParsedResource* out_resource) {
1051 out_resource->name.type = ResourceType::kStyle;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001052
1053 std::unique_ptr<Style> style = util::make_unique<Style>();
1054
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1056 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 // If the parent is empty, we don't have a parent, but we also don't infer
1058 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 if (!maybe_parent.value().empty()) {
1060 std::string err_str;
1061 style->parent = ResourceUtils::ParseStyleParentReference(
1062 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001065 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 }
1067
1068 // Transform the namespace prefix to the actual package name, and mark the
1069 // reference as
1070 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001072 }
1073
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 } else {
1075 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001076 std::string style_name = out_resource->name.entry;
1077 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 }
1083 }
1084
1085 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 const size_t depth = parser->depth();
1087 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1088 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 // Skip text and comments.
1090 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001091 }
1092
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 const std::string& element_namespace = parser->element_namespace();
1094 const std::string& element_name = parser->element_name();
1095 if (element_namespace == "" && element_name == "item") {
1096 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001097
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1099 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1100 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001101 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001102 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001103 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001104
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001105 if (error) {
1106 return false;
1107 }
1108
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001109 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001111}
1112
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001113bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1114 ParsedResource* out_resource) {
1115 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001116}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001117
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001118bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1119 ParsedResource* out_resource) {
1120 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001122}
1123
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001124bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1125 ParsedResource* out_resource) {
1126 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001128}
1129
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1131 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001132 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001133 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001134
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001136
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001137 bool translateable = options_.translatable;
1138 if (Maybe<StringPiece> translateable_attr =
1139 xml::FindAttribute(parser, "translatable")) {
1140 Maybe<bool> maybe_translateable =
1141 ResourceUtils::ParseBool(translateable_attr.value());
1142 if (!maybe_translateable) {
1143 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 << "invalid value for 'translatable'. Must be a boolean");
1145 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001146 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001147 translateable = maybe_translateable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001148 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001149 array->SetTranslateable(translateable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001150
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001151 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 const size_t depth = parser->depth();
1153 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1154 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 // Skip text and comments.
1156 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001157 }
1158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 const Source item_source = source_.WithLine(parser->line_number());
1160 const std::string& element_namespace = parser->element_namespace();
1161 const std::string& element_name = parser->element_name();
1162 if (element_namespace.empty() && element_name == "item") {
1163 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001164 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001166 error = true;
1167 continue;
1168 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001169 item->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001170 array->items.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001171
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001172 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1173 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1174 << "unknown tag <" << element_namespace << ":"
1175 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001176 error = true;
1177 }
1178 }
1179
1180 if (error) {
1181 return false;
1182 }
1183
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001184 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001186}
1187
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001188bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1189 ParsedResource* out_resource) {
1190 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001191
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001193
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 const size_t depth = parser->depth();
1196 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1197 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001198 // Skip text and comments.
1199 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001200 }
1201
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202 const Source item_source = source_.WithLine(parser->line_number());
1203 const std::string& element_namespace = parser->element_namespace();
1204 const std::string& element_name = parser->element_name();
1205 if (element_namespace.empty() && element_name == "item") {
1206 Maybe<StringPiece> maybe_quantity =
1207 xml::FindNonEmptyAttribute(parser, "quantity");
1208 if (!maybe_quantity) {
1209 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210 << "<item> in <plurals> requires attribute "
1211 << "'quantity'");
1212 error = true;
1213 continue;
1214 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001215
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216 StringPiece trimmed_quantity =
1217 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001218 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001219 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001222 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001227 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 index = Plural::Other;
1231 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 error = true;
1236 continue;
1237 }
1238
1239 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1241 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 error = true;
1243 continue;
1244 }
1245
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1248 error = true;
1249 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001250 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001251
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1253 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1254 << element_namespace << ":"
1255 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001256 error = true;
1257 }
1258 }
1259
1260 if (error) {
1261 return false;
1262 }
1263
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001265 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001266}
1267
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001268bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1269 ParsedResource* out_resource) {
1270 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001271
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001272 // Declare-styleable is kPrivate by default, because it technically only
1273 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001274 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001275
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1278 diag_->Warn(DiagMessage(out_resource->source)
1279 << "ignoring configuration '" << out_resource->config
1280 << "' for styleable " << out_resource->name.entry);
1281 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001282 }
1283
1284 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1285
1286 std::string comment;
1287 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 const size_t depth = parser->depth();
1289 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1290 if (parser->event() == xml::XmlPullParser::Event::kComment) {
1291 comment = util::TrimWhitespace(parser->comment()).ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001293 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001294 // Ignore text.
1295 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001296 }
1297
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 const Source item_source = source_.WithLine(parser->line_number());
1299 const std::string& element_namespace = parser->element_namespace();
1300 const std::string& element_name = parser->element_name();
1301 if (element_namespace.empty() && element_name == "attr") {
1302 Maybe<StringPiece> maybe_name =
1303 xml::FindNonEmptyAttribute(parser, "name");
1304 if (!maybe_name) {
1305 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 << "<attr> tag must have a 'name' attribute");
1307 error = true;
1308 continue;
1309 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001310
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 // If this is a declaration, the package name may be in the name. Separate
1312 // these out.
1313 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001314 Maybe<Reference> maybe_ref =
1315 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1316 if (!maybe_ref) {
1317 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1318 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001319 error = true;
1320 continue;
1321 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001322
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 Reference& child_ref = maybe_ref.value();
1324 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001325
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 ParsedResource child_resource;
1328 child_resource.name = child_ref.name.value();
1329 child_resource.source = item_source;
1330 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001331
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 error = true;
1334 continue;
1335 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001336
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001337 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 child_ref.SetComment(child_resource.comment);
1339 child_ref.SetSource(item_source);
1340 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001341
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001342 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001343
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1345 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1346 << element_namespace << ":"
1347 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001349 }
1350
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001351 comment = {};
1352 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001353
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 if (error) {
1355 return false;
1356 }
1357
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001360}
1361
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362} // namespace aapt