blob: 79379fe4b5ee81e2fef8fbaf3bcf4d0f1ade27d9 [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 Lesinskid5083f62017-01-16 15:07:21 -080032using android::StringPiece;
33
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034namespace aapt {
35
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036constexpr const char* sXliffNamespaceUri =
37 "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinski27afb9e2015-11-06 18:25:04 -080039/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 * Returns true if the element is <skip> or <eat-comment> and can be safely
41 * ignored.
Adam Lesinski27afb9e2015-11-06 18:25:04 -080042 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043static bool ShouldIgnoreElement(const StringPiece& ns,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 const StringPiece& name) {
45 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080046}
47
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048static uint32_t ParseFormatType(const StringPiece& piece) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 if (piece == "reference")
50 return android::ResTable_map::TYPE_REFERENCE;
51 else if (piece == "string")
52 return android::ResTable_map::TYPE_STRING;
53 else if (piece == "integer")
54 return android::ResTable_map::TYPE_INTEGER;
55 else if (piece == "boolean")
56 return android::ResTable_map::TYPE_BOOLEAN;
57 else if (piece == "color")
58 return android::ResTable_map::TYPE_COLOR;
59 else if (piece == "float")
60 return android::ResTable_map::TYPE_FLOAT;
61 else if (piece == "dimension")
62 return android::ResTable_map::TYPE_DIMENSION;
63 else if (piece == "fraction")
64 return android::ResTable_map::TYPE_FRACTION;
65 else if (piece == "enum")
66 return android::ResTable_map::TYPE_ENUM;
67 else if (piece == "flags")
68 return android::ResTable_map::TYPE_FLAGS;
69 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080070}
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 for (StringPiece part : util::Tokenize(str, '|')) {
75 StringPiece trimmed_part = util::TrimWhitespace(part);
76 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 if (type == 0) {
78 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080079 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 mask |= type;
81 }
82 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080083}
84
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080085/**
86 * A parsed resource ready to be added to the ResourceTable.
87 */
88struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 ResourceName name;
90 ConfigDescription config;
91 std::string product;
92 Source source;
93 ResourceId id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 Maybe<SymbolState> symbol_state;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::string comment;
96 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080098};
99
100// Recursively adds resources to the ResourceTable.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
104 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 if (res->symbol_state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 symbol.state = res->symbol_state.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 symbol.source = res->source;
113 symbol.comment = res->comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 if (!table->SetSymbolState(res->name, res->id, symbol, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800116 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 if (res->value) {
120 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 res->value->SetComment(std::move(res->comment));
122 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 if (!table->AddResource(res->name, res->id, res->config, res->product,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 std::move(res->value), diag)) {
126 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800127 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 for (ParsedResource& child : res->child_resources) {
132 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 }
134 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800135}
136
137// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
141 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700142 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 : diag_(diag),
145 table_(table),
146 source_(source),
147 config_(config),
148 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800149
150/**
151 * Build a string from XML that converts nested elements into Span objects.
152 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153bool ResourceParser::FlattenXmlSubtree(xml::XmlPullParser* parser,
154 std::string* out_raw_string,
155 StyleString* out_style_string) {
156 std::vector<Span> span_stack;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 out_raw_string->clear();
160 out_style_string->spans.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 util::StringBuilder builder;
162 size_t depth = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
164 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 if (event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 // We already warned and skipped the start element, so just skip here
168 // too
169 continue;
170 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 depth--;
173 if (depth == 0) {
174 break;
175 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 span_stack.back().last_char = builder.Utf16Len() - 1;
178 out_style_string->spans.push_back(span_stack.back());
179 span_stack.pop_back();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 } else if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 out_raw_string->append(parser->text());
183 builder.Append(parser->text());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 } else if (event == xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 if (!parser->element_namespace().empty()) {
187 if (parser->element_namespace() != sXliffNamespaceUri) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 // Only warn if this isn't an xliff namespace.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
190 << "skipping element '" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 << "' with unknown namespace '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 << parser->element_namespace() << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 continue;
195 }
196 depth++;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 // Build a span object out of the nested element.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 std::string span_name = parser->element_name();
200 const auto end_attr_iter = parser->end_attributes();
201 for (auto attr_iter = parser->begin_attributes();
202 attr_iter != end_attr_iter; ++attr_iter) {
203 span_name += ";";
204 span_name += attr_iter->name;
205 span_name += "=";
206 span_name += attr_iter->value;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 }
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (builder.Utf16Len() > std::numeric_limits<uint32_t>::max()) {
210 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
211 << "style string '" << builder.ToString()
212 << "' is too long");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 error = true;
214 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 span_stack.push_back(
216 Span{span_name, static_cast<uint32_t>(builder.Utf16Len())});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
218
219 } else if (event == xml::XmlPullParser::Event::kComment) {
220 // Skip
221 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 LOG(FATAL) << "unhandled XML event";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 }
224 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 CHECK(span_stack.empty()) << "spans haven't been fully processed";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 out_style_string->str = builder.ToString();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229}
230
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 const size_t depth = parser->depth();
234 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
235 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 // Skip comments and text.
237 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 if (!parser->element_namespace().empty() ||
241 parser->element_name() != "resources") {
242 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 << "root element must be <resources>");
244 return false;
245 }
246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 break;
249 };
250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
252 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
253 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 return false;
255 }
256 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800257}
258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
260 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 bool error = false;
263 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 const size_t depth = parser->depth();
265 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
266 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (!util::TrimWhitespace(parser->text()).empty()) {
274 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 << "plain text not allowed here");
276 error = true;
277 }
278 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700279 }
280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 // Skip unknown namespace.
285 continue;
286 }
287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 std::string element_name = parser->element_name();
289 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 comment = "";
291 continue;
292 }
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 ParsedResource parsed_resource;
295 parsed_resource.config = config_;
296 parsed_resource.source = source_.WithLine(parser->line_number());
297 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298
299 // Extract the product name if it exists.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 if (Maybe<StringPiece> maybe_product =
301 xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800302 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 }
304
305 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700306 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 error = true;
308 continue;
309 }
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 error = true;
313 }
314 }
315
316 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 for (const ResourceName& stripped_resource : stripped_resources) {
318 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 // Failed to find the resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 diag_->Error(DiagMessage(source_)
321 << "resource '" << stripped_resource
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 << "' "
323 "was filtered out but no product variant remains");
324 error = true;
325 }
326 }
327
328 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329}
330
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
332 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 struct ItemTypeFormat {
334 ResourceType type;
335 uint32_t format;
336 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800337
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
339 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800340
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 static const auto elToItemMap =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
344 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
345 {"dimen",
346 {ResourceType::kDimen, android::ResTable_map::TYPE_FLOAT |
347 android::ResTable_map::TYPE_FRACTION |
348 android::ResTable_map::TYPE_DIMENSION}},
349 {"drawable",
350 {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
351 {"fraction",
352 {ResourceType::kFraction,
353 android::ResTable_map::TYPE_FLOAT |
354 android::ResTable_map::TYPE_FRACTION |
355 android::ResTable_map::TYPE_DIMENSION}},
356 {"integer",
357 {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
358 {"string",
359 {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
360 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800361
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 static const auto elToBagMap =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
364 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
365 {"array", std::mem_fn(&ResourceParser::ParseArray)},
366 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367 {"declare-styleable",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
369 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
370 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
371 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
372 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
373 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
374 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
375 {"style", std::mem_fn(&ResourceParser::ParseStyle)},
376 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800380
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 if (resource_type == "item") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 // Items have their type encoded in the type attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 if (Maybe<StringPiece> maybe_type =
387 xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800388 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 << "<item> must have a 'type' attribute");
392 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800393 }
394
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 if (Maybe<StringPiece> maybe_format =
396 xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 // An explicit format for this resource was specified. The resource will
398 // retain
399 // its type in its name, but the accepted value for this type is
400 // overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 resource_format = ParseFormatType(maybe_format.value());
402 if (!resource_format) {
403 diag_->Error(DiagMessage(out_resource->source)
404 << "'" << maybe_format.value()
405 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800406 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 }
408 }
409 }
410
411 // Get the name of the resource. This will be checked later, because not all
412 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 if (resource_type == "id") {
416 if (!maybe_name) {
417 diag_->Error(DiagMessage(out_resource->source)
418 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 << "> missing 'name' attribute");
420 return false;
421 }
422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800424 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 return true;
427 }
428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 const auto item_iter = elToItemMap.find(resource_type);
430 if (item_iter != elToItemMap.end()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 // This is an item, record its type and format and start parsing.
432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 if (!maybe_name) {
434 diag_->Error(DiagMessage(out_resource->source)
435 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 << "> missing 'name' attribute");
437 return false;
438 }
439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 out_resource->name.type = item_iter->second.type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800441 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442
443 // Only use the implicit format for this type if it wasn't overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 if (!resource_format) {
445 resource_format = item_iter->second.format;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 }
447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 if (!ParseItem(parser, out_resource, resource_format)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800450 }
451 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 }
453
454 // This might be a bag or something.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 const auto bag_iter = elToBagMap.find(resource_type);
456 if (bag_iter != elToBagMap.end()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 if (resource_type != "public-group") {
459 if (!maybe_name) {
460 diag_->Error(DiagMessage(out_resource->source)
461 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 << "> missing 'name' attribute");
463 return false;
464 }
465
Adam Lesinskid5083f62017-01-16 15:07:21 -0800466 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 }
468
469 // Call the associated parse method. The type will be filled in by the
470 // parse func.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 if (!bag_iter->second(this, parser, out_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 return false;
473 }
474 return true;
475 }
476
477 // Try parsing the elementName (or type) as a resource. These shall only be
478 // resources like 'layout' or 'xml' and they can only be references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 const ResourceType* parsed_type = ParseResourceType(resource_type);
480 if (parsed_type) {
481 if (!maybe_name) {
482 diag_->Error(DiagMessage(out_resource->source)
483 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 << "> missing 'name' attribute");
485 return false;
486 }
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800489 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 out_resource->value =
491 ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
492 if (!out_resource->value) {
493 diag_->Error(DiagMessage(out_resource->source)
494 << "invalid value for type '" << *parsed_type
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 << "'. Expected a reference");
496 return false;
497 }
498 return true;
499 }
500
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 diag_->Warn(DiagMessage(out_resource->source)
502 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 return false;
504}
505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
507 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 const uint32_t format) {
509 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 }
512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 out_resource->value = ParseXml(parser, format, kNoRawString);
514 if (!out_resource->value) {
515 diag_->Error(DiagMessage(out_resource->source) << "invalid "
516 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 return false;
518 }
519 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800520}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800521
522/**
523 * Reads the entire XML subtree and attempts to parse it as some Item,
524 * with typeMask denoting which items it can be. If allowRawValue is
525 * true, a RawString is returned if the XML couldn't be parsed as
526 * an Item. If allowRawValue is false, nullptr is returned in this
527 * case.
528 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
530 const uint32_t type_mask,
531 const bool allow_raw_value) {
532 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 std::string raw_value;
535 StyleString style_string;
536 if (!FlattenXmlSubtree(parser, &raw_value, &style_string)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800537 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
539
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 // This can only be a StyledString.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 return util::make_unique<StyledString>(table_->string_pool.MakeRef(
543 style_string,
544 StringPool::Context(StringPool::Context::kStylePriority, config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 }
546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 // name.package can be empty here, as it will assume the package name of the
549 // table.
550 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 id->SetSource(source_.WithLine(begin_xml_line));
552 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 };
554
555 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 std::unique_ptr<Item> processed_item =
557 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
558 on_create_reference);
559 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
562 TransformReferenceFromNamespace(parser, "", ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 }
566
567 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 // Use the trimmed, escaped string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 return util::make_unique<String>(table_->string_pool.MakeRef(
571 style_string.str, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 }
573
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 // We can't parse this so return a RawString if we are allowed.
576 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 }
579 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800580}
581
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582bool ResourceParser::ParseString(xml::XmlPullParser* parser,
583 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 if (Maybe<StringPiece> formatted_attr =
586 xml::FindAttribute(parser, "formatted")) {
587 Maybe<bool> maybe_formatted =
588 ResourceUtils::ParseBool(formatted_attr.value());
589 if (!maybe_formatted) {
590 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 << "invalid value for 'formatted'. Must be a boolean");
592 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800593 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 bool translateable = options_.translatable;
598 if (Maybe<StringPiece> translateable_attr =
599 xml::FindAttribute(parser, "translatable")) {
600 Maybe<bool> maybe_translateable =
601 ResourceUtils::ParseBool(translateable_attr.value());
602 if (!maybe_translateable) {
603 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 << "invalid value for 'translatable'. Must be a boolean");
605 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800606 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 translateable = maybe_translateable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 out_resource->value =
611 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
612 if (!out_resource->value) {
613 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 return false;
615 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800616
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
618 string_value->SetTranslateable(translateable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800619
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 if (formatted && translateable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 if (!util::VerifyJavaStringFormat(*string_value->value)) {
622 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 msg << "multiple substitutions specified in non-positional format; "
624 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 if (options_.error_on_positional_arguments) {
626 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800628 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800632 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 } else if (StyledString* string_value =
635 ValueCast<StyledString>(out_resource->value.get())) {
636 string_value->SetTranslateable(translateable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 }
638 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800639}
640
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
642 ParsedResource* out_resource) {
643 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
644 if (!maybe_type) {
645 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 << "<public> must have a 'type' attribute");
647 return false;
648 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800649
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
651 if (!parsed_type) {
652 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
653 << maybe_type.value()
654 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 return false;
656 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800657
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 if (Maybe<StringPiece> maybe_id_str =
661 xml::FindNonEmptyAttribute(parser, "id")) {
662 Maybe<ResourceId> maybe_id =
663 ResourceUtils::ParseResourceId(maybe_id_str.value());
664 if (!maybe_id) {
665 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
666 << maybe_id.value()
667 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800669 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800672
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800680}
681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
683 ParsedResource* out_resource) {
684 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
685 if (!maybe_type) {
686 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800688 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800690
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
692 if (!parsed_type) {
693 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
694 << maybe_type.value()
695 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800696 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 }
698
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700699 Maybe<StringPiece> maybe_id_str =
700 xml::FindNonEmptyAttribute(parser, "first-id");
701 if (!maybe_id_str) {
702 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 << "<public-group> must have a 'first-id' attribute");
704 return false;
705 }
706
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 Maybe<ResourceId> maybe_id =
708 ResourceUtils::ParseResourceId(maybe_id_str.value());
709 if (!maybe_id) {
710 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
711 << maybe_id_str.value()
712 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 return false;
714 }
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717
718 std::string comment;
719 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 const size_t depth = parser->depth();
721 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
722 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800723 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 // Skip text.
727 continue;
728 }
729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 const Source item_source = source_.WithLine(parser->line_number());
731 const std::string& element_namespace = parser->element_namespace();
732 const std::string& element_name = parser->element_name();
733 if (element_namespace.empty() && element_name == "public") {
734 Maybe<StringPiece> maybe_name =
735 xml::FindNonEmptyAttribute(parser, "name");
736 if (!maybe_name) {
737 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 << "<public> must have a 'name' attribute");
739 error = true;
740 continue;
741 }
742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 if (xml::FindNonEmptyAttribute(parser, "id")) {
744 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 << "'id' is ignored within <public-group>");
746 error = true;
747 continue;
748 }
749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 if (xml::FindNonEmptyAttribute(parser, "type")) {
751 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 << "'type' is ignored within <public-group>");
753 error = true;
754 continue;
755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 ParsedResource child_resource;
758 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800759 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 child_resource.id = next_id;
761 child_resource.comment = std::move(comment);
762 child_resource.source = item_source;
763 child_resource.symbol_state = SymbolState::kPublic;
764 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
769 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 error = true;
771 }
772 }
773 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800774}
775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
777 ParsedResource* out_resource) {
778 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
779 if (!maybe_type) {
780 diag_->Error(DiagMessage(out_resource->source)
781 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 << "> must have a 'type' attribute");
783 return false;
784 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800785
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
787 if (!parsed_type) {
788 diag_->Error(DiagMessage(out_resource->source)
789 << "invalid resource type '" << maybe_type.value() << "' in <"
790 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 return false;
792 }
793
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700794 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800796}
797
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700798bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
799 ParsedResource* out_resource) {
800 if (ParseSymbolImpl(parser, out_resource)) {
801 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700802 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 }
804 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800805}
806
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
808 ParsedResource* out_resource) {
809 if (ParseSymbolImpl(parser, out_resource)) {
810 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 return true;
812 }
813 return false;
814}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700815
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700816bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
817 ParsedResource* out_resource) {
818 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819}
820
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
822 ParsedResource* out_resource, bool weak) {
823 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824
825 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 if (out_resource->config != ConfigDescription::DefaultConfig()) {
827 diag_->Warn(DiagMessage(out_resource->source)
828 << "ignoring configuration '" << out_resource->config
829 << "' for attribute " << out_resource->name);
830 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 }
832
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
836 if (maybe_format) {
837 type_mask = ParseFormatAttribute(maybe_format.value());
838 if (type_mask == 0) {
839 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
840 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841 << "'");
842 return false;
843 }
844 }
845
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
849 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
850 if (!min_str.empty()) {
851 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700853 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800857 }
858
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700859 if (!maybe_min) {
860 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
861 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 return false;
863 }
864 }
865
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700866 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
867 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
868 if (!max_str.empty()) {
869 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800875 }
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 if (!maybe_max) {
878 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
879 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 return false;
881 }
882 }
883
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884 if ((maybe_min || maybe_max) &&
885 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
886 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 << "'min' and 'max' can only be used when format='integer'");
888 return false;
889 }
890
891 struct SymbolComparator {
892 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
893 return a.symbol.name.value() < b.symbol.name.value();
894 }
895 };
896
897 std::set<Attribute::Symbol, SymbolComparator> items;
898
899 std::string comment;
900 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700901 const size_t depth = parser->depth();
902 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
903 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800904 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 // Skip text.
908 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800909 }
910
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911 const Source item_source = source_.WithLine(parser->line_number());
912 const std::string& element_namespace = parser->element_namespace();
913 const std::string& element_name = parser->element_name();
914 if (element_namespace.empty() &&
915 (element_name == "flag" || element_name == "enum")) {
916 if (element_name == "enum") {
917 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
918 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700919 << "can not define an <enum>; already defined a <flag>");
920 error = true;
921 continue;
922 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700923 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 } else if (element_name == "flag") {
926 if (type_mask & android::ResTable_map::TYPE_ENUM) {
927 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 << "can not define a <flag>; already defined an <enum>");
929 error = true;
930 continue;
931 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700933 }
934
935 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700936 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700938 ParsedResource child_resource;
939 child_resource.name = symbol.symbol.name.value();
940 child_resource.source = item_source;
941 child_resource.value = util::make_unique<Id>();
942 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700944 symbol.symbol.SetComment(std::move(comment));
945 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700946
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 auto insert_result = items.insert(std::move(symbol));
948 if (!insert_result.second) {
949 const Attribute::Symbol& existing_symbol = *insert_result.first;
950 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700954 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 << "first defined here");
956 error = true;
957 }
958 } else {
959 error = true;
960 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
962 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963 error = true;
964 }
965
966 comment = {};
967 }
968
969 if (error) {
970 return false;
971 }
972
973 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
974 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700975 attr->type_mask =
976 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
977 if (maybe_min) {
978 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 }
980
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 if (maybe_max) {
982 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 return true;
986}
987
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700988Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
993 if (!maybe_name) {
994 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 << tag << ">");
996 return {};
997 }
998
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1000 if (!maybe_value) {
1001 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 << tag << ">");
1003 return {};
1004 }
1005
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 android::Res_value val;
1008 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 << "' for <" << tag
1011 << ">; must be an integer");
1012 return {};
1013 }
1014
1015 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001017 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001018}
1019
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001020bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1021 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001022
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1024 if (!maybe_name) {
1025 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 return false;
1027 }
1028
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029 Maybe<Reference> maybe_key =
1030 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1031 if (!maybe_key) {
1032 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1033 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001034 return false;
1035 }
1036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1038 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001042 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 return false;
1044 }
1045
1046 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001047 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 return true;
1049}
1050
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001051bool ResourceParser::ParseStyle(xml::XmlPullParser* parser,
1052 ParsedResource* out_resource) {
1053 out_resource->name.type = ResourceType::kStyle;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054
1055 std::unique_ptr<Style> style = util::make_unique<Style>();
1056
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1058 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001059 // If the parent is empty, we don't have a parent, but we also don't infer
1060 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 if (!maybe_parent.value().empty()) {
1062 std::string err_str;
1063 style->parent = ResourceUtils::ParseStyleParentReference(
1064 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001066 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001067 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001068 }
1069
1070 // Transform the namespace prefix to the actual package name, and mark the
1071 // reference as
1072 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001074 }
1075
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001076 } else {
1077 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001078 std::string style_name = out_resource->name.entry;
1079 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 }
1085 }
1086
1087 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 const size_t depth = parser->depth();
1089 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1090 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 // Skip text and comments.
1092 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001093 }
1094
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001095 const std::string& element_namespace = parser->element_namespace();
1096 const std::string& element_name = parser->element_name();
1097 if (element_namespace == "" && element_name == "item") {
1098 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001099
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001100 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1101 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1102 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001103 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001104 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001105 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001106
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 if (error) {
1108 return false;
1109 }
1110
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001111 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001113}
1114
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001115bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1116 ParsedResource* out_resource) {
1117 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001118}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001119
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001120bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1121 ParsedResource* out_resource) {
1122 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001124}
1125
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1127 ParsedResource* out_resource) {
1128 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001129 android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001130}
1131
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1133 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001134 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001135 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001136
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001137 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001138
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001139 bool translateable = options_.translatable;
1140 if (Maybe<StringPiece> translateable_attr =
1141 xml::FindAttribute(parser, "translatable")) {
1142 Maybe<bool> maybe_translateable =
1143 ResourceUtils::ParseBool(translateable_attr.value());
1144 if (!maybe_translateable) {
1145 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 << "invalid value for 'translatable'. Must be a boolean");
1147 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001148 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001149 translateable = maybe_translateable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001151 array->SetTranslateable(translateable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001152
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001153 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001154 const size_t depth = parser->depth();
1155 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1156 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 // Skip text and comments.
1158 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001159 }
1160
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001161 const Source item_source = source_.WithLine(parser->line_number());
1162 const std::string& element_namespace = parser->element_namespace();
1163 const std::string& element_name = parser->element_name();
1164 if (element_namespace.empty() && element_name == "item") {
1165 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001166 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001167 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 error = true;
1169 continue;
1170 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001171 item->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001172 array->items.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001173
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001174 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1175 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1176 << "unknown tag <" << element_namespace << ":"
1177 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 error = true;
1179 }
1180 }
1181
1182 if (error) {
1183 return false;
1184 }
1185
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001186 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001187 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001188}
1189
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1191 ParsedResource* out_resource) {
1192 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001193
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001195
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001197 const size_t depth = parser->depth();
1198 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1199 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200 // Skip text and comments.
1201 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001202 }
1203
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 const Source item_source = source_.WithLine(parser->line_number());
1205 const std::string& element_namespace = parser->element_namespace();
1206 const std::string& element_name = parser->element_name();
1207 if (element_namespace.empty() && element_name == "item") {
1208 Maybe<StringPiece> maybe_quantity =
1209 xml::FindNonEmptyAttribute(parser, "quantity");
1210 if (!maybe_quantity) {
1211 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 << "<item> in <plurals> requires attribute "
1213 << "'quantity'");
1214 error = true;
1215 continue;
1216 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001217
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001218 StringPiece trimmed_quantity =
1219 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001222 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001227 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 index = Plural::Other;
1233 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001236 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 error = true;
1238 continue;
1239 }
1240
1241 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1243 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001244 error = true;
1245 continue;
1246 }
1247
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001248 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001249 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1250 error = true;
1251 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001253
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1255 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1256 << element_namespace << ":"
1257 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 error = true;
1259 }
1260 }
1261
1262 if (error) {
1263 return false;
1264 }
1265
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001266 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001268}
1269
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1271 ParsedResource* out_resource) {
1272 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001273
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001274 // Declare-styleable is kPrivate by default, because it technically only
1275 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001276 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001277
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1280 diag_->Warn(DiagMessage(out_resource->source)
1281 << "ignoring configuration '" << out_resource->config
1282 << "' for styleable " << out_resource->name.entry);
1283 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001284 }
1285
1286 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1287
1288 std::string comment;
1289 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 const size_t depth = parser->depth();
1291 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1292 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001293 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001294 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001296 // Ignore text.
1297 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001298 }
1299
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001300 const Source item_source = source_.WithLine(parser->line_number());
1301 const std::string& element_namespace = parser->element_namespace();
1302 const std::string& element_name = parser->element_name();
1303 if (element_namespace.empty() && element_name == "attr") {
1304 Maybe<StringPiece> maybe_name =
1305 xml::FindNonEmptyAttribute(parser, "name");
1306 if (!maybe_name) {
1307 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 << "<attr> tag must have a 'name' attribute");
1309 error = true;
1310 continue;
1311 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001312
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001313 // If this is a declaration, the package name may be in the name. Separate
1314 // these out.
1315 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001316 Maybe<Reference> maybe_ref =
1317 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1318 if (!maybe_ref) {
1319 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1320 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321 error = true;
1322 continue;
1323 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001324
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001325 Reference& child_ref = maybe_ref.value();
1326 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001327
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 ParsedResource child_resource;
1330 child_resource.name = child_ref.name.value();
1331 child_resource.source = item_source;
1332 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001333
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 error = true;
1336 continue;
1337 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001338
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 child_ref.SetComment(child_resource.comment);
1341 child_ref.SetSource(item_source);
1342 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001343
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001345
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001346 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1347 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1348 << element_namespace << ":"
1349 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001351 }
1352
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353 comment = {};
1354 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001355
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001356 if (error) {
1357 return false;
1358 }
1359
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001362}
1363
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001364} // namespace aapt