blob: f1e2da9f41e2106620d4eb97b425532c67a25d02 [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>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Winson62ac8b52019-12-04 08:36:48 -080035#include "idmap2/Policies.h"
36
Adam Lesinski2eed52e2018-02-21 15:55:58 -080037using ::aapt::ResourceUtils::StringBuilder;
38using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020039using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080040using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080041
Winson62ac8b52019-12-04 08:36:48 -080042using android::idmap2::policy::kPolicyStringToFlag;
43
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070045namespace {
46constexpr const char* kPublicGroupTag = "public-group";
47constexpr const char* kStagingPublicGroupTag = "staging-public-group";
Ryan Mitchell2fedba92021-04-23 07:47:38 -070048constexpr const char* kStagingPublicGroupFinalTag = "staging-public-group-final";
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070049} // namespace
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070051constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070053// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
54static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080056}
57
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
59 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070061 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070063 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070065 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070067 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070069 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070071 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070073 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070075 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080077}
78
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070079static uint32_t ParseFormatType(const StringPiece& piece) {
80 if (piece == "enum") {
81 return android::ResTable_map::TYPE_ENUM;
82 } else if (piece == "flags") {
83 return android::ResTable_map::TYPE_FLAGS;
84 }
85 return ParseFormatTypeNoEnumsOrFlags(piece);
86}
87
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080090 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 StringPiece trimmed_part = util::TrimWhitespace(part);
92 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 if (type == 0) {
94 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080095 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 mask |= type;
97 }
98 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080099}
100
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700101// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800102struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceName name;
104 ConfigDescription config;
105 std::string product;
106 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800109 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700110 bool staged_api = false;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700111 bool allow_new = false;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800112 Maybe<OverlayableItem> overlayable_item;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700113 Maybe<StagedId> staged_alias;
Adam Lesinski71be7052017-12-12 16:48:07 -0800114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 std::string comment;
116 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800118};
119
120// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700121static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
123 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800125 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
Adam Lesinski76565542016-03-10 21:55:04 -0800127
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700128 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800129 if (res->visibility_level != Visibility::Level::kUndefined) {
130 Visibility visibility;
131 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700132 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800133 visibility.source = res->source;
134 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700135 res_builder.SetVisibility(visibility);
136 }
137
138 if (res->id.is_valid()) {
139 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800141
Adam Lesinski71be7052017-12-12 16:48:07 -0800142 if (res->allow_new) {
143 AllowNew allow_new;
144 allow_new.source = res->source;
145 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700146 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800147 }
148
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800149 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700150 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800151 }
152
153 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 res->value->SetComment(std::move(res->comment));
156 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700157 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800159
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700160 if (res->staged_alias) {
161 res_builder.SetStagedId(res->staged_alias.value());
162 }
163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700165 if (!res->name.entry.empty()) {
166 if (!table->AddResource(res_builder.Build(), diag)) {
167 return false;
168 }
169 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 for (ParsedResource& child : res->child_resources) {
171 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
173 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800174}
175
176// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
180 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700181 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 : diag_(diag),
184 table_(table),
185 source_(source),
186 config_(config),
187 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800189// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
190// This will be used to traverse and flatten the XML string into a single std::string, with all
191// Span and Untranslatable data maintained in parallel, as indices into the string.
192class Node {
193 public:
194 virtual ~Node() = default;
195
196 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
197 // parent node as well.
198 // Returns a pointer to the child node that was added as a convenience.
199 template <typename T>
200 T* AddChild(std::unique_ptr<T> node) {
201 T* raw_ptr = node.get();
202 children.push_back(std::move(node));
203 return raw_ptr;
204 }
205
206 virtual void Build(StringBuilder* builder) const {
207 for (const auto& child : children) {
208 child->Build(builder);
209 }
210 }
211
212 std::vector<std::unique_ptr<Node>> children;
213};
214
215// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
216class SegmentNode : public Node {
217 public:
218 std::string data;
219
220 void Build(StringBuilder* builder) const override {
221 builder->AppendText(data);
222 }
223};
224
225// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
226class SpanNode : public Node {
227 public:
228 std::string name;
229
230 void Build(StringBuilder* builder) const override {
231 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
232 Node::Build(builder);
233 builder->EndSpan(span_handle);
234 }
235};
236
237// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
238class UntranslatableNode : public Node {
239 public:
240 void Build(StringBuilder* builder) const override {
241 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
242 Node::Build(builder);
243 builder->EndUntranslatable(handle);
244 }
245};
246
247// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800248bool ResourceParser::FlattenXmlSubtree(
249 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
250 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800251 std::string raw_string;
252 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800253
254 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
255 Maybe<size_t> untranslatable_start_depth;
256
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800257 Node root;
258 std::vector<Node*> node_stack;
259 node_stack.push_back(&root);
260
261 bool saw_span_node = false;
262 SegmentNode* first_segment = nullptr;
263 SegmentNode* last_segment = nullptr;
264
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800266 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800268
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800269 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700270 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800271 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800272 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800273 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700275
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800276 last_segment = node_stack.back()->AddChild(std::move(segment_node));
277 if (first_segment == nullptr) {
278 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800279 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800280 current_text = {};
281 }
282 }
Adam Lesinski75421622017-01-06 15:20:04 -0800283
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800284 switch (event) {
285 case xml::XmlPullParser::Event::kText: {
286 current_text += parser->text();
287 raw_string += parser->text();
288 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800289
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800290 case xml::XmlPullParser::Event::kStartElement: {
291 if (parser->element_namespace().empty()) {
292 // This is an HTML tag which we encode as a span. Add it to the span stack.
293 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
294 span_node->name = parser->element_name();
295 const auto end_attr_iter = parser->end_attributes();
296 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
297 ++attr_iter) {
298 span_node->name += ";";
299 span_node->name += attr_iter->name;
300 span_node->name += "=";
301 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800302 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800303
304 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
305 saw_span_node = true;
306 } else if (parser->element_namespace() == sXliffNamespaceUri) {
307 // This is an XLIFF tag, which is not encoded as a span.
308 if (parser->element_name() == "g") {
309 // Check that an 'untranslatable' tag is not already being processed. Nested
310 // <xliff:g> tags are illegal.
311 if (untranslatable_start_depth) {
312 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
313 << "illegal nested XLIFF 'g' tag");
314 return false;
315 } else {
316 // Mark the beginning of an 'untranslatable' section.
317 untranslatable_start_depth = depth;
318 node_stack.push_back(
319 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
320 }
321 } else {
322 // Ignore unknown XLIFF tags, but don't warn.
323 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
324 }
325 } else {
326 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
327 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
328 << "ignoring element '" << parser->element_name()
329 << "' with unknown namespace '" << parser->element_namespace() << "'");
330 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800331 }
Adam Lesinski75421622017-01-06 15:20:04 -0800332
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800333 // Enter one level inside the element.
334 depth++;
335 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800337 case xml::XmlPullParser::Event::kEndElement: {
338 // Return one level from within the element.
339 depth--;
340 if (depth == 0) {
341 break;
342 }
343
344 node_stack.pop_back();
345 if (untranslatable_start_depth == make_value(depth)) {
346 // This is the end of an untranslatable section.
347 untranslatable_start_depth = {};
348 }
349 } break;
350
351 default:
352 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 }
355 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700357 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800358 CHECK(node_stack.size() == 1u);
359 CHECK(node_stack.back() == &root);
360
361 if (!saw_span_node) {
362 // If there were no spans, we must treat this string a little differently (according to AAPT).
363 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
364 // from the last segment.
365 if (first_segment != nullptr) {
366 // Trim leading whitespace.
367 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
368 if (trimmed.size() != first_segment->data.size()) {
369 first_segment->data = trimmed.to_string();
370 }
371 }
372
373 if (last_segment != nullptr) {
374 // Trim trailing whitespace.
375 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
376 if (trimmed.size() != last_segment->data.size()) {
377 last_segment->data = trimmed.to_string();
378 }
379 }
380 }
381
382 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
383 // care of recording the correctly adjusted Spans and UntranslatableSections.
384 StringBuilder builder;
385 root.Build(&builder);
386 if (!builder) {
387 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
388 return false;
389 }
390
391 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
392 *out_raw_string = std::move(raw_string);
393 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
394 out_style_string->str = std::move(flattened_string.text);
395 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800396 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397}
398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 const size_t depth = parser->depth();
402 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
403 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 // Skip comments and text.
405 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407
Adam Lesinski060b53d2017-07-28 17:10:35 -0700408 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 << "root element must be <resources>");
411 return false;
412 }
413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 break;
416 };
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
419 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
420 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 return false;
422 }
423 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424}
425
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
427 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700428
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 bool error = false;
430 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 const size_t depth = parser->depth();
432 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
433 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700438
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 if (!util::TrimWhitespace(parser->text()).empty()) {
441 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 << "plain text not allowed here");
443 error = true;
444 }
445 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700446 }
447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 // Skip unknown namespace.
452 continue;
453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 std::string element_name = parser->element_name();
456 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 comment = "";
458 continue;
459 }
460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 ParsedResource parsed_resource;
462 parsed_resource.config = config_;
463 parsed_resource.source = source_.WithLine(parser->line_number());
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -0800464 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100466 if (options_.visibility) {
467 parsed_resource.visibility_level = options_.visibility.value();
468 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469
470 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700471 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800472 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
475 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 error = true;
478 continue;
479 }
480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 error = true;
483 }
484 }
485
486 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 for (const ResourceName& stripped_resource : stripped_resources) {
488 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700490 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
491 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 error = true;
493 }
494 }
495
496 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800497}
498
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
500 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 struct ItemTypeFormat {
502 ResourceType type;
503 uint32_t format;
504 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800505
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
507 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800508
Adam Lesinski86d67df2017-01-31 13:47:27 -0800509 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
510 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
511 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
512 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
513 {"dimen",
514 {ResourceType::kDimen,
515 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
516 android::ResTable_map::TYPE_DIMENSION}},
517 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
518 {"fraction",
519 {ResourceType::kFraction,
520 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
521 android::ResTable_map::TYPE_DIMENSION}},
522 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
523 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
524 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800525
Adam Lesinski86d67df2017-01-31 13:47:27 -0800526 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
527 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
528 {"array", std::mem_fn(&ResourceParser::ParseArray)},
529 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
530 {"configVarying",
531 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
532 std::placeholders::_2, std::placeholders::_3)},
533 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
534 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
535 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700536 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800537 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
538 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
539 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700540 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700541 {"staging-public-group-final", std::mem_fn(&ResourceParser::ParseStagingPublicGroupFinal)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800542 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
543 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
544 std::placeholders::_2, std::placeholders::_3)},
545 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
546 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800549
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800552
Adam Lesinski86d67df2017-01-31 13:47:27 -0800553 bool can_be_item = true;
554 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800556 can_be_bag = false;
557
Adam Lesinskie597d682017-06-01 17:16:44 -0700558 // The default format for <item> is any. If a format attribute is present, that one will
559 // override the default.
560 resource_format = android::ResTable_map::TYPE_ANY;
561
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700563 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800564 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 << "<item> must have a 'type' attribute");
568 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800569 }
570
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700571 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700573 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700575 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 if (!resource_format) {
577 diag_->Error(DiagMessage(out_resource->source)
578 << "'" << maybe_format.value()
579 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800580 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 }
582 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800583 } else if (resource_type == "bag") {
584 can_be_item = false;
585
586 // Bags have their type encoded in the type attribute.
587 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
588 resource_type = maybe_type.value().to_string();
589 } else {
590 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
591 << "<bag> must have a 'type' attribute");
592 return false;
593 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 }
595
596 // Get the name of the resource. This will be checked later, because not all
597 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 if (resource_type == "id") {
601 if (!maybe_name) {
602 diag_->Error(DiagMessage(out_resource->source)
603 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 << "> missing 'name' attribute");
605 return false;
606 }
607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800609 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700610
611 // Ids either represent a unique resource id or reference another resource id
612 auto item = ParseItem(parser, out_resource, resource_format);
613 if (!item) {
614 return false;
615 }
616
617 String* empty = ValueCast<String>(out_resource->value.get());
618 if (empty && *empty->value == "") {
619 // If no inner element exists, represent a unique identifier
620 out_resource->value = util::make_unique<Id>();
621 } else {
y9efbbef2018-04-18 11:29:09 -0700622 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700623 if (ref && !ref->name && !ref->id) {
624 // A null reference also means there is no inner element when ids are in the form:
625 // <id name="name"/>
626 out_resource->value = util::make_unique<Id>();
627 } else if (!ref || ref->name.value().type != ResourceType::kId) {
628 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700629 diag_->Error(DiagMessage(out_resource->source)
630 << "<" << parser->element_name()
631 << "> inner element must either be a resource reference or empty");
632 return false;
633 }
634 }
635
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700637 } else if (resource_type == "macro") {
638 if (!maybe_name) {
639 diag_->Error(DiagMessage(out_resource->source)
640 << "<" << parser->element_name() << "> missing 'name' attribute");
641 return false;
642 }
643
644 out_resource->name.type = ResourceType::kMacro;
645 out_resource->name.entry = maybe_name.value().to_string();
646 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 }
648
Adam Lesinski86d67df2017-01-31 13:47:27 -0800649 if (can_be_item) {
650 const auto item_iter = elToItemMap.find(resource_type);
651 if (item_iter != elToItemMap.end()) {
652 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653
Adam Lesinski86d67df2017-01-31 13:47:27 -0800654 if (!maybe_name) {
655 diag_->Error(DiagMessage(out_resource->source)
656 << "<" << parser->element_name() << "> missing 'name' attribute");
657 return false;
658 }
659
660 out_resource->name.type = item_iter->second.type;
661 out_resource->name.entry = maybe_name.value().to_string();
662
Adam Lesinskie597d682017-06-01 17:16:44 -0700663 // Only use the implied format of the type when there is no explicit format.
664 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800665 resource_format = item_iter->second.format;
666 }
667
668 if (!ParseItem(parser, out_resource, resource_format)) {
669 return false;
670 }
671 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 }
674
675 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800676 if (can_be_bag) {
677 const auto bag_iter = elToBagMap.find(resource_type);
678 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700679 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700680 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700681 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800682 if (!maybe_name) {
683 diag_->Error(DiagMessage(out_resource->source)
684 << "<" << parser->element_name() << "> missing 'name' attribute");
685 return false;
686 }
687
688 out_resource->name.entry = maybe_name.value().to_string();
689 }
690
691 // Call the associated parse method. The type will be filled in by the
692 // parse func.
693 if (!bag_iter->second(this, parser, out_resource)) {
694 return false;
695 }
696 return true;
697 }
698 }
699
700 if (can_be_item) {
701 // Try parsing the elementName (or type) as a resource. These shall only be
702 // resources like 'layout' or 'xml' and they can only be references.
703 const ResourceType* parsed_type = ParseResourceType(resource_type);
704 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 if (!maybe_name) {
706 diag_->Error(DiagMessage(out_resource->source)
707 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 << "> missing 'name' attribute");
709 return false;
710 }
711
Adam Lesinski86d67df2017-01-31 13:47:27 -0800712 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800713 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800714 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
715 if (!out_resource->value) {
716 diag_->Error(DiagMessage(out_resource->source)
717 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
718 return false;
719 }
720 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 }
723
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000724 // If the resource type was not recognized, write the error and return false.
725 diag_->Error(DiagMessage(out_resource->source)
Ryan Mitchell2e2c3b62019-04-26 01:16:52 -0700726 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 return false;
728}
729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
731 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 const uint32_t format) {
733 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 }
736
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700737 out_resource->value = ParseXml(parser, format, kNoRawString);
738 if (!out_resource->value) {
739 diag_->Error(DiagMessage(out_resource->source) << "invalid "
740 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 return false;
742 }
743 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800744}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800745
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700746std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
747 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 std::string raw_value;
751 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800752 std::vector<UntranslatableSection> untranslatable_sections;
753 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800754 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
756
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700757 return FlattenedXmlSubTree{.raw_value = raw_value,
758 .style_string = style_string,
759 .untranslatable_sections = untranslatable_sections,
760 .namespace_resolver = parser,
761 .source = source_.WithLine(begin_xml_line)};
762}
763
764/**
765 * Reads the entire XML subtree and attempts to parse it as some Item,
766 * with typeMask denoting which items it can be. If allowRawValue is
767 * true, a RawString is returned if the XML couldn't be parsed as
768 * an Item. If allowRawValue is false, nullptr is returned in this
769 * case.
770 */
771std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
772 const bool allow_raw_value) {
773 auto sub_tree = CreateFlattenSubTree(parser);
774 if (!sub_tree.has_value()) {
775 return {};
776 }
777 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
778}
779
780std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
781 const uint32_t type_mask, const bool allow_raw_value,
782 ResourceTable& table,
783 const android::ConfigDescription& config,
784 IDiagnostics& diag) {
785 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800787 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700788 util::make_unique<StyledString>(table.string_pool.MakeRef(
789 xmlsub_tree.style_string,
790 StringPool::Context(StringPool::Context::kNormalPriority, config)));
791 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800792 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
794
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700795 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 // name.package can be empty here, as it will assume the package name of the
797 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700798 auto id = util::make_unique<Id>();
799 id->SetSource(xmlsub_tree.source);
800 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 };
802
803 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700804 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
805 xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700806 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700808 if (auto ref = ValueCast<Reference>(processed_item.get())) {
809 ref->allow_raw = allow_raw_value;
810 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 }
814
815 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700816 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800818 std::unique_ptr<String> string = util::make_unique<String>(
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700819 table.string_pool.MakeRef(xmlsub_tree.style_string.str, StringPool::Context(config)));
820 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800821 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 }
823
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700826 return util::make_unique<RawString>(table.string_pool.MakeRef(
827 util::TrimWhitespace(xmlsub_tree.raw_value), StringPool::Context(config)));
828 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700829 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
830 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 }
832 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833}
834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835bool ResourceParser::ParseString(xml::XmlPullParser* parser,
836 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 if (Maybe<StringPiece> formatted_attr =
839 xml::FindAttribute(parser, "formatted")) {
840 Maybe<bool> maybe_formatted =
841 ResourceUtils::ParseBool(formatted_attr.value());
842 if (!maybe_formatted) {
843 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 << "invalid value for 'formatted'. Must be a boolean");
845 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800846 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800849
Adam Lesinski75421622017-01-06 15:20:04 -0800850 bool translatable = options_.translatable;
851 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
852 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
853 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 << "invalid value for 'translatable'. Must be a boolean");
856 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800857 }
Adam Lesinski75421622017-01-06 15:20:04 -0800858 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 out_resource->value =
862 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
863 if (!out_resource->value) {
864 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700865 return false;
866 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800867
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800869 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800870
Adam Lesinski75421622017-01-06 15:20:04 -0800871 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 if (!util::VerifyJavaStringFormat(*string_value->value)) {
873 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 msg << "multiple substitutions specified in non-positional format; "
875 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 if (options_.error_on_positional_arguments) {
877 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800879 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800880
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800883 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884
Adam Lesinski75421622017-01-06 15:20:04 -0800885 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
886 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 }
888 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800889}
890
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700891bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
892 auto sub_tree = CreateFlattenSubTree(parser);
893 if (!sub_tree) {
894 return false;
895 }
896
897 if (out_resource->config != ConfigDescription::DefaultConfig()) {
898 diag_->Error(DiagMessage(out_resource->source)
899 << "<macro> tags cannot be declared in configurations other than the default "
900 "configuration'");
901 return false;
902 }
903
904 auto macro = std::make_unique<Macro>();
905 macro->raw_value = std::move(sub_tree->raw_value);
906 macro->style_string = std::move(sub_tree->style_string);
907 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
908
909 for (const auto& decl : parser->package_decls()) {
910 macro->alias_namespaces.emplace_back(
911 Macro::Namespace{.alias = decl.prefix,
912 .package_name = decl.package.package,
913 .is_private = decl.package.private_namespace});
914 }
915
916 out_resource->value = std::move(macro);
917 return true;
918}
919
Adam Lesinski71be7052017-12-12 16:48:07 -0800920bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100921 if (options_.visibility) {
922 diag_->Error(DiagMessage(out_resource->source)
923 << "<public> tag not allowed with --visibility flag");
924 return false;
925 }
926
Adam Lesinski46c4d722017-08-23 13:03:56 -0700927 if (out_resource->config != ConfigDescription::DefaultConfig()) {
928 diag_->Warn(DiagMessage(out_resource->source)
929 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
930 }
931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
933 if (!maybe_type) {
934 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 << "<public> must have a 'type' attribute");
936 return false;
937 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800938
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700939 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
940 if (!parsed_type) {
941 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
942 << maybe_type.value()
943 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 return false;
945 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800946
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800949 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
950 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800952 diag_->Error(DiagMessage(out_resource->source)
953 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800955 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800958
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700959 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700962 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700963
Adam Lesinski71be7052017-12-12 16:48:07 -0800964 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800966}
967
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700968template <typename Func>
969bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
970 const char* tag_name, IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700971 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700972 diag->Warn(DiagMessage(out_resource->source)
973 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
974 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700975 }
976
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
978 if (!maybe_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700979 diag->Error(DiagMessage(out_resource->source)
980 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800981 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
985 if (!parsed_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700986 diag->Error(DiagMessage(out_resource->source)
987 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800988 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 }
990
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700991 Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 if (!maybe_id_str) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700993 diag->Error(DiagMessage(out_resource->source)
994 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 return false;
996 }
997
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700998 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 if (!maybe_id) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001000 diag->Error(DiagMessage(out_resource->source)
1001 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 return false;
1003 }
1004
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001006 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 const size_t depth = parser->depth();
1009 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1010 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001011 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001013 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 // Skip text.
1015 continue;
1016 }
1017
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001018 const Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 const std::string& element_namespace = parser->element_namespace();
1020 const std::string& element_name = parser->element_name();
1021 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001022 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 if (!maybe_name) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001024 diag->Error(DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001025 error = true;
1026 continue;
1027 }
1028
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029 if (xml::FindNonEmptyAttribute(parser, "id")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001030 diag->Error(DiagMessage(item_source) << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 error = true;
1032 continue;
1033 }
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 if (xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001036 diag->Error(DiagMessage(item_source) << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 error = true;
1038 continue;
1039 }
1040
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001041 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
1042 .name = ResourceName{{}, *parsed_type, maybe_name.value().to_string()},
1043 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001044 .comment = std::move(comment),
1045 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001047 // Execute group specific code.
1048 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001050 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001051 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001052 diag->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 error = true;
1054 }
1055 }
1056 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001057}
1058
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001059bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1060 ParsedResource* out_resource) {
1061 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1062 [](ParsedResource& parsed_entry, ResourceId id) {
1063 parsed_entry.id = id;
1064 parsed_entry.staged_api = true;
1065 parsed_entry.visibility_level = Visibility::Level::kPublic;
1066 });
1067}
1068
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001069bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1070 ParsedResource* out_resource) {
1071 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1072 [](ParsedResource& parsed_entry, ResourceId id) {
1073 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1074 });
1075}
1076
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001077bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1078 if (options_.visibility) {
1079 diag_->Error(DiagMessage(out_resource->source)
1080 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1081 return false;
1082 }
1083
1084 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1085 [](ParsedResource& parsed_entry, ResourceId id) {
1086 parsed_entry.id = id;
1087 parsed_entry.visibility_level = Visibility::Level::kPublic;
1088 });
1089}
1090
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001091bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1092 ParsedResource* out_resource) {
1093 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1094 if (!maybe_type) {
1095 diag_->Error(DiagMessage(out_resource->source)
1096 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097 << "> must have a 'type' attribute");
1098 return false;
1099 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001100
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1102 if (!parsed_type) {
1103 diag_->Error(DiagMessage(out_resource->source)
1104 << "invalid resource type '" << maybe_type.value() << "' in <"
1105 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 return false;
1107 }
1108
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001109 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001111}
1112
Adam Lesinski46c4d722017-08-23 13:03:56 -07001113bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001114 if (options_.visibility) {
1115 diag_->Error(DiagMessage(out_resource->source)
1116 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1117 return false;
1118 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001119 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1120 diag_->Warn(DiagMessage(out_resource->source)
1121 << "ignoring configuration '" << out_resource->config << "' for <"
1122 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001124
1125 if (!ParseSymbolImpl(parser, out_resource)) {
1126 return false;
1127 }
1128
Adam Lesinski71be7052017-12-12 16:48:07 -08001129 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001130 return true;
1131}
1132
1133bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1134 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1135 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001136 << "ignoring configuration '" << out_resource->config
1137 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001138 }
1139
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001140 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1141 if (!overlayable_name) {
1142 diag_->Error(DiagMessage(out_resource->source)
1143 << "<overlayable> tag must have a 'name' attribute");
1144 return false;
1145 }
1146
1147 const std::string kActorUriScheme =
1148 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1149 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1150 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1151 diag_->Error(DiagMessage(out_resource->source)
1152 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1153 << Overlayable::kActorScheme << "'");
1154 return false;
1155 }
1156
1157 // Create a overlayable entry grouping that represents this <overlayable>
1158 auto overlayable = std::make_shared<Overlayable>(
1159 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001160 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001161
Adam Lesinski46c4d722017-08-23 13:03:56 -07001162 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001163 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001164 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001165 const size_t start_depth = parser->depth();
1166 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1167 xml::XmlPullParser::Event event = parser->event();
1168 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001169 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001170 break;
1171 } else if (event == xml::XmlPullParser::Event::kEndElement
1172 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001173 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001174 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001175 continue;
1176 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001177 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001178 comment = parser->comment();
1179 continue;
1180 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001181 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001182 continue;
1183 }
1184
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001185 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001186 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001187 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001188 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001189 if (current_policies == PolicyFlags::NONE) {
Winsonb2d7f532019-02-04 16:32:43 -08001190 diag_->Error(DiagMessage(element_source)
1191 << "<item> within an <overlayable> must be inside a <policy> block");
1192 error = true;
1193 continue;
1194 }
1195
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001196 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001197 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1198 if (!item_name) {
1199 diag_->Error(DiagMessage(element_source)
1200 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001201 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001202 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001203 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001204
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001205 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1206 if (!item_type) {
1207 diag_->Error(DiagMessage(element_source)
1208 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001209 error = true;
1210 continue;
1211 }
1212
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001213 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001214 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001215 diag_->Error(DiagMessage(element_source)
1216 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001217 << "' in <item> within an <overlayable>");
1218 error = true;
1219 continue;
1220 }
1221
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001222 OverlayableItem overlayable_item(overlayable);
1223 overlayable_item.policies = current_policies;
1224 overlayable_item.comment = comment;
1225 overlayable_item.source = element_source;
1226
1227 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001228 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001229 child_resource.name.entry = item_name.value().to_string();
1230 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001231 out_resource->child_resources.push_back(std::move(child_resource));
1232
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001233 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001234 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001235 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001236 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001237 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001238 break;
1239 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1240 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001241 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchell939df092019-04-09 17:13:50 -07001242 for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001243 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001244 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1245 kPolicyStringToFlag.end(),
1246 [trimmed_part](const auto& it) {
1247 return trimmed_part == it.first;
1248 });
1249 if (policy == kPolicyStringToFlag.end()) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001250 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001251 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001252 error = true;
1253 continue;
1254 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001255
1256 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001257 }
Winsonb2d7f532019-02-04 16:32:43 -08001258 } else {
1259 diag_->Error(DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001260 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001261 error = true;
1262 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001263 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001264 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001265 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell939df092019-04-09 17:13:50 -07001266 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001267 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001268 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001269 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001270
1271 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001272 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001273
Adam Lesinski46c4d722017-08-23 13:03:56 -07001274 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001275}
1276
Adam Lesinski71be7052017-12-12 16:48:07 -08001277bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001278 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001279 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001280 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001281 return true;
1282 }
1283 return false;
1284}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001285
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1287 ParsedResource* out_resource) {
1288 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289}
1290
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1292 ParsedResource* out_resource, bool weak) {
1293 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001294
1295 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001296 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1297 diag_->Warn(DiagMessage(out_resource->source)
1298 << "ignoring configuration '" << out_resource->config
1299 << "' for attribute " << out_resource->name);
1300 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 }
1302
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001303 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001304
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1306 if (maybe_format) {
1307 type_mask = ParseFormatAttribute(maybe_format.value());
1308 if (type_mask == 0) {
1309 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001310 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 return false;
1312 }
1313 }
1314
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001317 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1318 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1319 if (!min_str.empty()) {
1320 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001322 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001325 }
1326
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 if (!maybe_min) {
1328 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1329 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 return false;
1331 }
1332 }
1333
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1335 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1336 if (!max_str.empty()) {
1337 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001339 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001342 }
1343
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 if (!maybe_max) {
1345 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1346 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001347 return false;
1348 }
1349 }
1350
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001351 if ((maybe_min || maybe_max) &&
1352 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1353 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 << "'min' and 'max' can only be used when format='integer'");
1355 return false;
1356 }
1357
1358 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001359 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001360 return a.symbol.name.value() < b.symbol.name.value();
1361 }
1362 };
1363
1364 std::set<Attribute::Symbol, SymbolComparator> items;
1365
1366 std::string comment;
1367 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 const size_t depth = parser->depth();
1369 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1370 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001371 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 // Skip text.
1375 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001376 }
1377
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 const Source item_source = source_.WithLine(parser->line_number());
1379 const std::string& element_namespace = parser->element_namespace();
1380 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001381 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 if (element_name == "enum") {
1383 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1384 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 << "can not define an <enum>; already defined a <flag>");
1386 error = true;
1387 continue;
1388 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 } else if (element_name == "flag") {
1392 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1393 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394 << "can not define a <flag>; already defined an <enum>");
1395 error = true;
1396 continue;
1397 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399 }
1400
1401 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001404 ParsedResource child_resource;
1405 child_resource.name = symbol.symbol.name.value();
1406 child_resource.source = item_source;
1407 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001408 if (options_.visibility) {
1409 child_resource.visibility_level = options_.visibility.value();
1410 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001411 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001412
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 symbol.symbol.SetComment(std::move(comment));
1414 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001415
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 auto insert_result = items.insert(std::move(symbol));
1417 if (!insert_result.second) {
1418 const Attribute::Symbol& existing_symbol = *insert_result.first;
1419 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001421 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001423 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001424 << "first defined here");
1425 error = true;
1426 }
1427 } else {
1428 error = true;
1429 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001430 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1431 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001432 error = true;
1433 }
1434
1435 comment = {};
1436 }
1437
1438 if (error) {
1439 return false;
1440 }
1441
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001442 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1443 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1444 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001446 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1447 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001448 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001449 return true;
1450}
1451
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001453 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1457 if (!maybe_name) {
1458 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 << tag << ">");
1460 return {};
1461 }
1462
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1464 if (!maybe_value) {
1465 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 << tag << ">");
1467 return {};
1468 }
1469
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001470 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 android::Res_value val;
1472 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001473 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 << "' for <" << tag
1475 << ">; must be an integer");
1476 return {};
1477 }
1478
1479 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001480 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001481 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001482}
1483
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001484bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1485 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001486
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1488 if (!maybe_name) {
1489 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 return false;
1491 }
1492
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001493 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001495 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001496 return false;
1497 }
1498
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001499 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001500 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001501
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001503 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001504 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001505 return false;
1506 }
1507
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001508 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509 return true;
1510}
1511
Adam Lesinski86d67df2017-01-31 13:47:27 -08001512bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001514 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515
1516 std::unique_ptr<Style> style = util::make_unique<Style>();
1517
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1519 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001520 // If the parent is empty, we don't have a parent, but we also don't infer either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 if (!maybe_parent.value().empty()) {
1522 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001523 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001525 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001526 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 }
1528
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001529 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001530 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001531 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001532 }
1533
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534 } else {
1535 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 std::string style_name = out_resource->name.entry;
1537 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001538 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001540 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 }
1542 }
1543
1544 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001545 const size_t depth = parser->depth();
1546 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1547 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001548 // Skip text and comments.
1549 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001550 }
1551
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001552 const std::string& element_namespace = parser->element_namespace();
1553 const std::string& element_name = parser->element_name();
1554 if (element_namespace == "" && element_name == "item") {
1555 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001556
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1558 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1559 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001561 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001563
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001564 if (error) {
1565 return false;
1566 }
1567
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001568 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001569 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001570}
1571
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001572bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1573 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1574 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1575 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1576 if (resource_format == 0u) {
1577 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1578 << "'" << format_attr.value() << "' is an invalid format");
1579 return false;
1580 }
1581 }
1582 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001583}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001584
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001585bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1586 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001587}
1588
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001589bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1590 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001591}
1592
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1594 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001595 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001597
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001599
Adam Lesinski75421622017-01-06 15:20:04 -08001600 bool translatable = options_.translatable;
1601 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1602 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1603 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 << "invalid value for 'translatable'. Must be a boolean");
1606 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001607 }
Adam Lesinski75421622017-01-06 15:20:04 -08001608 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 }
Adam Lesinski75421622017-01-06 15:20:04 -08001610 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001611
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001612 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001613 const size_t depth = parser->depth();
1614 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1615 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 // Skip text and comments.
1617 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001618 }
1619
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 const Source item_source = source_.WithLine(parser->line_number());
1621 const std::string& element_namespace = parser->element_namespace();
1622 const std::string& element_name = parser->element_name();
1623 if (element_namespace.empty() && element_name == "item") {
1624 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001626 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001627 error = true;
1628 continue;
1629 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001630 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001631 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001632
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1634 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1635 << "unknown tag <" << element_namespace << ":"
1636 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637 error = true;
1638 }
1639 }
1640
1641 if (error) {
1642 return false;
1643 }
1644
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001645 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001646 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001647}
1648
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1650 ParsedResource* out_resource) {
1651 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001652
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001654
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001655 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 const size_t depth = parser->depth();
1657 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1658 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001659 // Skip text and comments.
1660 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001661 }
1662
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 const Source item_source = source_.WithLine(parser->line_number());
1664 const std::string& element_namespace = parser->element_namespace();
1665 const std::string& element_name = parser->element_name();
1666 if (element_namespace.empty() && element_name == "item") {
1667 Maybe<StringPiece> maybe_quantity =
1668 xml::FindNonEmptyAttribute(parser, "quantity");
1669 if (!maybe_quantity) {
1670 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 << "<item> in <plurals> requires attribute "
1672 << "'quantity'");
1673 error = true;
1674 continue;
1675 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001676
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001677 StringPiece trimmed_quantity =
1678 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001680 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001681 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001683 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001685 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001686 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001687 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001688 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001689 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001691 index = Plural::Other;
1692 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001693 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001696 error = true;
1697 continue;
1698 }
1699
1700 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001701 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1702 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703 error = true;
1704 continue;
1705 }
1706
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001707 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1709 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001710 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001712
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001713 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001714
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001715 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1716 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1717 << element_namespace << ":"
1718 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001719 error = true;
1720 }
1721 }
1722
1723 if (error) {
1724 return false;
1725 }
1726
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001729}
1730
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1732 ParsedResource* out_resource) {
1733 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001734
Donald Chai94e4a012020-01-06 13:52:41 -08001735 if (!options_.preserve_visibility_of_styleables) {
1736 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1737 // knows exactly what for.
1738 //
1739 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1740 // package-private (to be used only by the view class); themes are a different story.
1741 out_resource->visibility_level = Visibility::Level::kPublic;
1742 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001743
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001744 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001745 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1746 diag_->Warn(DiagMessage(out_resource->source)
1747 << "ignoring configuration '" << out_resource->config
1748 << "' for styleable " << out_resource->name.entry);
1749 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750 }
1751
1752 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1753
1754 std::string comment;
1755 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001756 const size_t depth = parser->depth();
1757 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1758 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001759 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001761 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001762 // Ignore text.
1763 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001764 }
1765
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001766 const Source item_source = source_.WithLine(parser->line_number());
1767 const std::string& element_namespace = parser->element_namespace();
1768 const std::string& element_name = parser->element_name();
1769 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001770 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001771 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001772 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 error = true;
1774 continue;
1775 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001776
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001777 // If this is a declaration, the package name may be in the name. Separate
1778 // these out.
1779 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001780 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001781 if (!maybe_ref) {
1782 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1783 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001784 error = true;
1785 continue;
1786 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001787
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001788 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001789 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001790
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001791 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001792 ParsedResource child_resource;
1793 child_resource.name = child_ref.name.value();
1794 child_resource.source = item_source;
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -08001795 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001796 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001797 if (options_.visibility) {
1798 child_resource.visibility_level = options_.visibility.value();
1799 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001800
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001802 error = true;
1803 continue;
1804 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001805
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001807 child_ref.SetComment(child_resource.comment);
1808 child_ref.SetSource(item_source);
1809 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001810
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001811 // Do not add referenced attributes that do not define a format to the table.
1812 CHECK(child_resource.value != nullptr);
1813 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1814
1815 CHECK(attr != nullptr);
1816 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1817 out_resource->child_resources.push_back(std::move(child_resource));
1818 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001819
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1821 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1822 << element_namespace << ":"
1823 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001824 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001825 }
1826
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001827 comment = {};
1828 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001829
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001830 if (error) {
1831 return false;
1832 }
1833
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001834 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001835 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001836}
1837
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001838} // namespace aapt