blob: 1c750c6748b9191025aa8377bb6685e7d5b7fb6b [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 Lesinski86d67df2017-01-31 13:47:27 -0800341 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
342 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
343 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
344 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
345 {"dimen",
346 {ResourceType::kDimen,
347 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
348 android::ResTable_map::TYPE_DIMENSION}},
349 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
350 {"fraction",
351 {ResourceType::kFraction,
352 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
353 android::ResTable_map::TYPE_DIMENSION}},
354 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
355 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
356 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800357
Adam Lesinski86d67df2017-01-31 13:47:27 -0800358 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
359 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
360 {"array", std::mem_fn(&ResourceParser::ParseArray)},
361 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
362 {"configVarying",
363 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
364 std::placeholders::_2, std::placeholders::_3)},
365 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
366 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
367 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
368 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
369 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
370 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
371 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
372 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
373 std::placeholders::_2, std::placeholders::_3)},
374 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
375 });
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 Lesinski86d67df2017-01-31 13:47:27 -0800382 bool can_be_item = true;
383 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800385 can_be_bag = false;
386
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 // Items have their type encoded in the type attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 if (Maybe<StringPiece> maybe_type =
389 xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800390 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 << "<item> must have a 'type' attribute");
394 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800395 }
396
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 if (Maybe<StringPiece> maybe_format =
398 xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 // An explicit format for this resource was specified. The resource will
400 // retain
401 // its type in its name, but the accepted value for this type is
402 // overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 resource_format = ParseFormatType(maybe_format.value());
404 if (!resource_format) {
405 diag_->Error(DiagMessage(out_resource->source)
406 << "'" << maybe_format.value()
407 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800408 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 }
410 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800411 } else if (resource_type == "bag") {
412 can_be_item = false;
413
414 // Bags have their type encoded in the type attribute.
415 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
416 resource_type = maybe_type.value().to_string();
417 } else {
418 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
419 << "<bag> must have a 'type' attribute");
420 return false;
421 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 }
423
424 // Get the name of the resource. This will be checked later, because not all
425 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if (resource_type == "id") {
429 if (!maybe_name) {
430 diag_->Error(DiagMessage(out_resource->source)
431 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 << "> missing 'name' attribute");
433 return false;
434 }
435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800437 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 return true;
440 }
441
Adam Lesinski86d67df2017-01-31 13:47:27 -0800442 if (can_be_item) {
443 const auto item_iter = elToItemMap.find(resource_type);
444 if (item_iter != elToItemMap.end()) {
445 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446
Adam Lesinski86d67df2017-01-31 13:47:27 -0800447 if (!maybe_name) {
448 diag_->Error(DiagMessage(out_resource->source)
449 << "<" << parser->element_name() << "> missing 'name' attribute");
450 return false;
451 }
452
453 out_resource->name.type = item_iter->second.type;
454 out_resource->name.entry = maybe_name.value().to_string();
455
456 // Only use the implicit format for this type if it wasn't overridden.
457 if (!resource_format) {
458 resource_format = item_iter->second.format;
459 }
460
461 if (!ParseItem(parser, out_resource, resource_format)) {
462 return false;
463 }
464 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 }
467
468 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800469 if (can_be_bag) {
470 const auto bag_iter = elToBagMap.find(resource_type);
471 if (bag_iter != elToBagMap.end()) {
472 // Ensure we have a name (unless this is a <public-group>).
473 if (resource_type != "public-group") {
474 if (!maybe_name) {
475 diag_->Error(DiagMessage(out_resource->source)
476 << "<" << parser->element_name() << "> missing 'name' attribute");
477 return false;
478 }
479
480 out_resource->name.entry = maybe_name.value().to_string();
481 }
482
483 // Call the associated parse method. The type will be filled in by the
484 // parse func.
485 if (!bag_iter->second(this, parser, out_resource)) {
486 return false;
487 }
488 return true;
489 }
490 }
491
492 if (can_be_item) {
493 // Try parsing the elementName (or type) as a resource. These shall only be
494 // resources like 'layout' or 'xml' and they can only be references.
495 const ResourceType* parsed_type = ParseResourceType(resource_type);
496 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497 if (!maybe_name) {
498 diag_->Error(DiagMessage(out_resource->source)
499 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 << "> missing 'name' attribute");
501 return false;
502 }
503
Adam Lesinski86d67df2017-01-31 13:47:27 -0800504 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800505 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800506 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
507 if (!out_resource->value) {
508 diag_->Error(DiagMessage(out_resource->source)
509 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
510 return false;
511 }
512 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 }
515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 diag_->Warn(DiagMessage(out_resource->source)
517 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 return false;
519}
520
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700521bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
522 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 const uint32_t format) {
524 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 }
527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 out_resource->value = ParseXml(parser, format, kNoRawString);
529 if (!out_resource->value) {
530 diag_->Error(DiagMessage(out_resource->source) << "invalid "
531 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 return false;
533 }
534 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800535}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800536
537/**
538 * Reads the entire XML subtree and attempts to parse it as some Item,
539 * with typeMask denoting which items it can be. If allowRawValue is
540 * true, a RawString is returned if the XML couldn't be parsed as
541 * an Item. If allowRawValue is false, nullptr is returned in this
542 * case.
543 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
545 const uint32_t type_mask,
546 const bool allow_raw_value) {
547 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 std::string raw_value;
550 StyleString style_string;
551 if (!FlattenXmlSubtree(parser, &raw_value, &style_string)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800552 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 // This can only be a StyledString.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 return util::make_unique<StyledString>(table_->string_pool.MakeRef(
558 style_string,
559 StringPool::Context(StringPool::Context::kStylePriority, config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 }
561
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 // name.package can be empty here, as it will assume the package name of the
564 // table.
565 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 id->SetSource(source_.WithLine(begin_xml_line));
567 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 };
569
570 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 std::unique_ptr<Item> processed_item =
572 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
573 on_create_reference);
574 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
577 TransformReferenceFromNamespace(parser, "", ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 }
581
582 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 // Use the trimmed, escaped string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 return util::make_unique<String>(table_->string_pool.MakeRef(
586 style_string.str, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 }
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 // We can't parse this so return a RawString if we are allowed.
591 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 }
594 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800595}
596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597bool ResourceParser::ParseString(xml::XmlPullParser* parser,
598 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 if (Maybe<StringPiece> formatted_attr =
601 xml::FindAttribute(parser, "formatted")) {
602 Maybe<bool> maybe_formatted =
603 ResourceUtils::ParseBool(formatted_attr.value());
604 if (!maybe_formatted) {
605 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 << "invalid value for 'formatted'. Must be a boolean");
607 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800608 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800611
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612 bool translateable = options_.translatable;
613 if (Maybe<StringPiece> translateable_attr =
614 xml::FindAttribute(parser, "translatable")) {
615 Maybe<bool> maybe_translateable =
616 ResourceUtils::ParseBool(translateable_attr.value());
617 if (!maybe_translateable) {
618 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 << "invalid value for 'translatable'. Must be a boolean");
620 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800621 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 translateable = maybe_translateable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 out_resource->value =
626 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
627 if (!out_resource->value) {
628 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 return false;
630 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800631
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
633 string_value->SetTranslateable(translateable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800634
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 if (formatted && translateable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if (!util::VerifyJavaStringFormat(*string_value->value)) {
637 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 msg << "multiple substitutions specified in non-positional format; "
639 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 if (options_.error_on_positional_arguments) {
641 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800643 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800644
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800647 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649 } else if (StyledString* string_value =
650 ValueCast<StyledString>(out_resource->value.get())) {
651 string_value->SetTranslateable(translateable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 }
653 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800654}
655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
657 ParsedResource* out_resource) {
658 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
659 if (!maybe_type) {
660 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 << "<public> must have a 'type' attribute");
662 return false;
663 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800664
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
666 if (!parsed_type) {
667 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
668 << maybe_type.value()
669 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 return false;
671 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800672
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 if (Maybe<StringPiece> maybe_id_str =
676 xml::FindNonEmptyAttribute(parser, "id")) {
677 Maybe<ResourceId> maybe_id =
678 ResourceUtils::ParseResourceId(maybe_id_str.value());
679 if (!maybe_id) {
680 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
681 << maybe_id.value()
682 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800684 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700692
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800695}
696
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
698 ParsedResource* out_resource) {
699 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
700 if (!maybe_type) {
701 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800703 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800705
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
707 if (!parsed_type) {
708 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
709 << maybe_type.value()
710 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800711 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 }
713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 Maybe<StringPiece> maybe_id_str =
715 xml::FindNonEmptyAttribute(parser, "first-id");
716 if (!maybe_id_str) {
717 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 << "<public-group> must have a 'first-id' attribute");
719 return false;
720 }
721
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 Maybe<ResourceId> maybe_id =
723 ResourceUtils::ParseResourceId(maybe_id_str.value());
724 if (!maybe_id) {
725 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
726 << maybe_id_str.value()
727 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 return false;
729 }
730
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732
733 std::string comment;
734 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 const size_t depth = parser->depth();
736 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
737 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800738 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 // Skip text.
742 continue;
743 }
744
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 const Source item_source = source_.WithLine(parser->line_number());
746 const std::string& element_namespace = parser->element_namespace();
747 const std::string& element_name = parser->element_name();
748 if (element_namespace.empty() && element_name == "public") {
749 Maybe<StringPiece> maybe_name =
750 xml::FindNonEmptyAttribute(parser, "name");
751 if (!maybe_name) {
752 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 << "<public> must have a 'name' attribute");
754 error = true;
755 continue;
756 }
757
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 if (xml::FindNonEmptyAttribute(parser, "id")) {
759 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 << "'id' is ignored within <public-group>");
761 error = true;
762 continue;
763 }
764
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 if (xml::FindNonEmptyAttribute(parser, "type")) {
766 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 << "'type' is ignored within <public-group>");
768 error = true;
769 continue;
770 }
771
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 ParsedResource child_resource;
773 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800774 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 child_resource.id = next_id;
776 child_resource.comment = std::move(comment);
777 child_resource.source = item_source;
778 child_resource.symbol_state = SymbolState::kPublic;
779 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
784 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 error = true;
786 }
787 }
788 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800789}
790
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
792 ParsedResource* out_resource) {
793 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
794 if (!maybe_type) {
795 diag_->Error(DiagMessage(out_resource->source)
796 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 << "> must have a 'type' attribute");
798 return false;
799 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800800
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
802 if (!parsed_type) {
803 diag_->Error(DiagMessage(out_resource->source)
804 << "invalid resource type '" << maybe_type.value() << "' in <"
805 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 return false;
807 }
808
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800811}
812
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700813bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
814 ParsedResource* out_resource) {
815 if (ParseSymbolImpl(parser, out_resource)) {
816 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700817 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 }
819 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800820}
821
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
823 ParsedResource* out_resource) {
824 if (ParseSymbolImpl(parser, out_resource)) {
825 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 return true;
827 }
828 return false;
829}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700830
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
832 ParsedResource* out_resource) {
833 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834}
835
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
837 ParsedResource* out_resource, bool weak) {
838 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839
840 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 if (out_resource->config != ConfigDescription::DefaultConfig()) {
842 diag_->Warn(DiagMessage(out_resource->source)
843 << "ignoring configuration '" << out_resource->config
844 << "' for attribute " << out_resource->name);
845 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 }
847
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
851 if (maybe_format) {
852 type_mask = ParseFormatAttribute(maybe_format.value());
853 if (type_mask == 0) {
854 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
855 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 << "'");
857 return false;
858 }
859 }
860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700863 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
864 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
865 if (!min_str.empty()) {
866 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700870 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700871 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800872 }
873
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700874 if (!maybe_min) {
875 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
876 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 return false;
878 }
879 }
880
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
882 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
883 if (!max_str.empty()) {
884 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700886 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700888 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800890 }
891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 if (!maybe_max) {
893 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
894 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895 return false;
896 }
897 }
898
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 if ((maybe_min || maybe_max) &&
900 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
901 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 << "'min' and 'max' can only be used when format='integer'");
903 return false;
904 }
905
906 struct SymbolComparator {
907 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
908 return a.symbol.name.value() < b.symbol.name.value();
909 }
910 };
911
912 std::set<Attribute::Symbol, SymbolComparator> items;
913
914 std::string comment;
915 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 const size_t depth = parser->depth();
917 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
918 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800919 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700920 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 // Skip text.
923 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800924 }
925
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700926 const Source item_source = source_.WithLine(parser->line_number());
927 const std::string& element_namespace = parser->element_namespace();
928 const std::string& element_name = parser->element_name();
929 if (element_namespace.empty() &&
930 (element_name == "flag" || element_name == "enum")) {
931 if (element_name == "enum") {
932 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
933 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700934 << "can not define an <enum>; already defined a <flag>");
935 error = true;
936 continue;
937 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700938 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 } else if (element_name == "flag") {
941 if (type_mask & android::ResTable_map::TYPE_ENUM) {
942 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 << "can not define a <flag>; already defined an <enum>");
944 error = true;
945 continue;
946 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 }
949
950 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700952 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700953 ParsedResource child_resource;
954 child_resource.name = symbol.symbol.name.value();
955 child_resource.source = item_source;
956 child_resource.value = util::make_unique<Id>();
957 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700959 symbol.symbol.SetComment(std::move(comment));
960 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 auto insert_result = items.insert(std::move(symbol));
963 if (!insert_result.second) {
964 const Attribute::Symbol& existing_symbol = *insert_result.first;
965 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700969 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970 << "first defined here");
971 error = true;
972 }
973 } else {
974 error = true;
975 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
977 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 error = true;
979 }
980
981 comment = {};
982 }
983
984 if (error) {
985 return false;
986 }
987
988 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
989 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 attr->type_mask =
991 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
992 if (maybe_min) {
993 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 }
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 if (maybe_max) {
997 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 return true;
1001}
1002
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001003Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1008 if (!maybe_name) {
1009 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 << tag << ">");
1011 return {};
1012 }
1013
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001014 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1015 if (!maybe_value) {
1016 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001017 << tag << ">");
1018 return {};
1019 }
1020
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 android::Res_value val;
1023 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001025 << "' for <" << tag
1026 << ">; must be an integer");
1027 return {};
1028 }
1029
1030 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001031 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001033}
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1036 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001037
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1039 if (!maybe_name) {
1040 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 return false;
1042 }
1043
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001044 Maybe<Reference> maybe_key =
1045 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1046 if (!maybe_key) {
1047 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1048 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 return false;
1050 }
1051
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001052 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1053 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 return false;
1059 }
1060
1061 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001062 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001063 return true;
1064}
1065
Adam Lesinski86d67df2017-01-31 13:47:27 -08001066bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001067 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001068 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001069
1070 std::unique_ptr<Style> style = util::make_unique<Style>();
1071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1073 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 // If the parent is empty, we don't have a parent, but we also don't infer
1075 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001076 if (!maybe_parent.value().empty()) {
1077 std::string err_str;
1078 style->parent = ResourceUtils::ParseStyleParentReference(
1079 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001082 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 }
1084
1085 // Transform the namespace prefix to the actual package name, and mark the
1086 // reference as
1087 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001089 }
1090
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 } else {
1092 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 std::string style_name = out_resource->name.entry;
1094 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099 }
1100 }
1101
1102 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001103 const size_t depth = parser->depth();
1104 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1105 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 // Skip text and comments.
1107 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001108 }
1109
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 const std::string& element_namespace = parser->element_namespace();
1111 const std::string& element_name = parser->element_name();
1112 if (element_namespace == "" && element_name == "item") {
1113 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001114
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001115 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1116 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1117 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001118 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001119 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001121
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001122 if (error) {
1123 return false;
1124 }
1125
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001128}
1129
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1131 ParsedResource* out_resource) {
1132 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001133}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001134
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001135bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1136 ParsedResource* out_resource) {
1137 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001139}
1140
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001141bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1142 ParsedResource* out_resource) {
1143 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001145}
1146
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001147bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1148 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001149 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001150 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001151
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001153
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001154 bool translateable = options_.translatable;
1155 if (Maybe<StringPiece> translateable_attr =
1156 xml::FindAttribute(parser, "translatable")) {
1157 Maybe<bool> maybe_translateable =
1158 ResourceUtils::ParseBool(translateable_attr.value());
1159 if (!maybe_translateable) {
1160 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001161 << "invalid value for 'translatable'. Must be a boolean");
1162 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001163 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001164 translateable = maybe_translateable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001165 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001166 array->SetTranslateable(translateable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001167
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001169 const size_t depth = parser->depth();
1170 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1171 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001172 // Skip text and comments.
1173 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001174 }
1175
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001176 const Source item_source = source_.WithLine(parser->line_number());
1177 const std::string& element_namespace = parser->element_namespace();
1178 const std::string& element_name = parser->element_name();
1179 if (element_namespace.empty() && element_name == "item") {
1180 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001183 error = true;
1184 continue;
1185 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001186 item->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001187 array->items.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001188
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001189 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1190 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1191 << "unknown tag <" << element_namespace << ":"
1192 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 error = true;
1194 }
1195 }
1196
1197 if (error) {
1198 return false;
1199 }
1200
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001201 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001203}
1204
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001205bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1206 ParsedResource* out_resource) {
1207 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001208
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001209 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001210
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 const size_t depth = parser->depth();
1213 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1214 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001215 // Skip text and comments.
1216 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001217 }
1218
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001219 const Source item_source = source_.WithLine(parser->line_number());
1220 const std::string& element_namespace = parser->element_namespace();
1221 const std::string& element_name = parser->element_name();
1222 if (element_namespace.empty() && element_name == "item") {
1223 Maybe<StringPiece> maybe_quantity =
1224 xml::FindNonEmptyAttribute(parser, "quantity");
1225 if (!maybe_quantity) {
1226 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227 << "<item> in <plurals> requires attribute "
1228 << "'quantity'");
1229 error = true;
1230 continue;
1231 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001232
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233 StringPiece trimmed_quantity =
1234 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001236 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001238 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001241 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001243 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001245 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 index = Plural::Other;
1248 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001249 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001251 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001252 error = true;
1253 continue;
1254 }
1255
1256 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1258 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001259 error = true;
1260 continue;
1261 }
1262
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001263 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1265 error = true;
1266 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001267 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001268
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001269 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1270 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1271 << element_namespace << ":"
1272 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001273 error = true;
1274 }
1275 }
1276
1277 if (error) {
1278 return false;
1279 }
1280
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001282 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001283}
1284
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1286 ParsedResource* out_resource) {
1287 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001288
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 // Declare-styleable is kPrivate by default, because it technically only
1290 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001292
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1295 diag_->Warn(DiagMessage(out_resource->source)
1296 << "ignoring configuration '" << out_resource->config
1297 << "' for styleable " << out_resource->name.entry);
1298 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299 }
1300
1301 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1302
1303 std::string comment;
1304 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 const size_t depth = parser->depth();
1306 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1307 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001308 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 // Ignore text.
1312 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001313 }
1314
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 const Source item_source = source_.WithLine(parser->line_number());
1316 const std::string& element_namespace = parser->element_namespace();
1317 const std::string& element_name = parser->element_name();
1318 if (element_namespace.empty() && element_name == "attr") {
1319 Maybe<StringPiece> maybe_name =
1320 xml::FindNonEmptyAttribute(parser, "name");
1321 if (!maybe_name) {
1322 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323 << "<attr> tag must have a 'name' attribute");
1324 error = true;
1325 continue;
1326 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001327
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 // If this is a declaration, the package name may be in the name. Separate
1329 // these out.
1330 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 Maybe<Reference> maybe_ref =
1332 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1333 if (!maybe_ref) {
1334 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1335 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001336 error = true;
1337 continue;
1338 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001339
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 Reference& child_ref = maybe_ref.value();
1341 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001342
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 ParsedResource child_resource;
1345 child_resource.name = child_ref.name.value();
1346 child_resource.source = item_source;
1347 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001348
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 error = true;
1351 continue;
1352 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001353
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 child_ref.SetComment(child_resource.comment);
1356 child_ref.SetSource(item_source);
1357 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001358
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001359 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001360
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001361 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1362 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1363 << element_namespace << ":"
1364 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001366 }
1367
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368 comment = {};
1369 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001370
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 if (error) {
1372 return false;
1373 }
1374
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001376 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001377}
1378
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379} // namespace aapt