blob: 45bf8e38c5ce6de8a200b9022fb605bd39d5e237 [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 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080016#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017
18#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080019#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <sstream>
21
Ryan Mitchell4382e442021-07-14 12:53:01 -070022#include <android-base/logging.h>
23#include <idmap2/Policies.h>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
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"
Ryan Mitchell4382e442021-07-14 12:53:01 -070031
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
Adam Lesinski2eed52e2018-02-21 15:55:58 -080035using ::aapt::ResourceUtils::StringBuilder;
36using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Winson62ac8b52019-12-04 08:36:48 -080040using android::idmap2::policy::kPolicyStringToFlag;
41
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042namespace aapt {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070043namespace {
44constexpr const char* kPublicGroupTag = "public-group";
45constexpr const char* kStagingPublicGroupTag = "staging-public-group";
Ryan Mitchell2fedba92021-04-23 07:47:38 -070046constexpr const char* kStagingPublicGroupFinalTag = "staging-public-group-final";
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070047} // namespace
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070051// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070052static bool ShouldIgnoreElement(StringPiece ns, StringPiece name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080054}
55
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070056static uint32_t ParseFormatTypeNoEnumsOrFlags(StringPiece piece) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070057 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070059 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070061 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070063 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070065 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070067 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070069 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070071 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070073 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080075}
76
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070077static uint32_t ParseFormatType(StringPiece piece) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070078 if (piece == "enum") {
79 return android::ResTable_map::TYPE_ENUM;
80 } else if (piece == "flags") {
81 return android::ResTable_map::TYPE_FLAGS;
82 }
83 return ParseFormatTypeNoEnumsOrFlags(piece);
84}
85
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070086static uint32_t ParseFormatAttribute(StringPiece str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 uint32_t mask = 0;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070088 for (StringPiece part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 StringPiece trimmed_part = util::TrimWhitespace(part);
90 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 if (type == 0) {
92 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080093 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 mask |= type;
95 }
96 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080097}
98
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070099// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800100struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 ResourceName name;
102 ConfigDescription config;
103 std::string product;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000104 android::Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800107 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700108 bool staged_api = false;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700109 bool allow_new = false;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700110 FlagStatus flag_status;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700111 std::optional<OverlayableItem> overlayable_item;
112 std::optional<StagedId> staged_alias;
Adam Lesinski71be7052017-12-12 16:48:07 -0800113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 std::string comment;
115 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800117};
118
119// Recursively adds resources to the ResourceTable.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000120static bool AddResourcesToTable(ResourceTable* table, android::IDiagnostics* diag,
121 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.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700125 res->comment = std::string(trimmed_comment);
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
Jeremy Meyer211bec22024-06-04 14:22:03 -0700164 res_builder.SetFlagStatus(res->flag_status);
165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700167 if (!res->name.entry.empty()) {
168 if (!table->AddResource(res_builder.Build(), diag)) {
169 return false;
170 }
171 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 for (ParsedResource& child : res->child_resources) {
173 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
175 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800176}
177
178// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800180
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000181ResourceParser::ResourceParser(android::IDiagnostics* diag, ResourceTable* table,
182 const android::Source& source, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 const ResourceParserOptions& options)
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000184 : diag_(diag), table_(table), source_(source), config_(config), options_(options) {
185}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800187// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
188// This will be used to traverse and flatten the XML string into a single std::string, with all
189// Span and Untranslatable data maintained in parallel, as indices into the string.
190class Node {
191 public:
192 virtual ~Node() = default;
193
194 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
195 // parent node as well.
196 // Returns a pointer to the child node that was added as a convenience.
197 template <typename T>
198 T* AddChild(std::unique_ptr<T> node) {
199 T* raw_ptr = node.get();
200 children.push_back(std::move(node));
201 return raw_ptr;
202 }
203
204 virtual void Build(StringBuilder* builder) const {
205 for (const auto& child : children) {
206 child->Build(builder);
207 }
208 }
209
210 std::vector<std::unique_ptr<Node>> children;
211};
212
213// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
214class SegmentNode : public Node {
215 public:
216 std::string data;
217
218 void Build(StringBuilder* builder) const override {
219 builder->AppendText(data);
220 }
221};
222
223// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
224class SpanNode : public Node {
225 public:
226 std::string name;
227
228 void Build(StringBuilder* builder) const override {
229 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
230 Node::Build(builder);
231 builder->EndSpan(span_handle);
232 }
233};
234
235// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
236class UntranslatableNode : public Node {
237 public:
238 void Build(StringBuilder* builder) const override {
239 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
240 Node::Build(builder);
241 builder->EndUntranslatable(handle);
242 }
243};
244
245// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800246bool ResourceParser::FlattenXmlSubtree(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000247 xml::XmlPullParser* parser, std::string* out_raw_string, android::StyleString* out_style_string,
Adam Lesinski75421622017-01-06 15:20:04 -0800248 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800249 std::string raw_string;
250 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800251
252 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700253 std::optional<size_t> untranslatable_start_depth;
Adam Lesinski75421622017-01-06 15:20:04 -0800254
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800255 Node root;
256 std::vector<Node*> node_stack;
257 node_stack.push_back(&root);
258
259 bool saw_span_node = false;
260 SegmentNode* first_segment = nullptr;
261 SegmentNode* last_segment = nullptr;
262
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800264 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800266
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800267 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700268 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800269 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800270 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800271 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800272 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700273
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 last_segment = node_stack.back()->AddChild(std::move(segment_node));
275 if (first_segment == nullptr) {
276 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800277 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800278 current_text = {};
279 }
280 }
Adam Lesinski75421622017-01-06 15:20:04 -0800281
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800282 switch (event) {
283 case xml::XmlPullParser::Event::kText: {
284 current_text += parser->text();
285 raw_string += parser->text();
286 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800287
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800288 case xml::XmlPullParser::Event::kStartElement: {
289 if (parser->element_namespace().empty()) {
290 // This is an HTML tag which we encode as a span. Add it to the span stack.
291 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
292 span_node->name = parser->element_name();
293 const auto end_attr_iter = parser->end_attributes();
294 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
295 ++attr_iter) {
296 span_node->name += ";";
297 span_node->name += attr_iter->name;
298 span_node->name += "=";
299 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800300 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800301
302 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
303 saw_span_node = true;
304 } else if (parser->element_namespace() == sXliffNamespaceUri) {
305 // This is an XLIFF tag, which is not encoded as a span.
306 if (parser->element_name() == "g") {
307 // Check that an 'untranslatable' tag is not already being processed. Nested
308 // <xliff:g> tags are illegal.
309 if (untranslatable_start_depth) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000310 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800311 << "illegal nested XLIFF 'g' tag");
312 return false;
313 } else {
314 // Mark the beginning of an 'untranslatable' section.
315 untranslatable_start_depth = depth;
316 node_stack.push_back(
317 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
318 }
319 } else {
320 // Ignore unknown XLIFF tags, but don't warn.
321 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
322 }
323 } else {
324 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000325 diag_->Warn(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800326 << "ignoring element '" << parser->element_name()
327 << "' with unknown namespace '" << parser->element_namespace() << "'");
328 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800329 }
Adam Lesinski75421622017-01-06 15:20:04 -0800330
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800331 // Enter one level inside the element.
332 depth++;
333 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700334
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800335 case xml::XmlPullParser::Event::kEndElement: {
336 // Return one level from within the element.
337 depth--;
338 if (depth == 0) {
339 break;
340 }
341
342 node_stack.pop_back();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700343 if (untranslatable_start_depth == depth) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800344 // This is the end of an untranslatable section.
345 untranslatable_start_depth = {};
346 }
347 } break;
348
349 default:
350 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 }
353 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700355 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800356 CHECK(node_stack.size() == 1u);
357 CHECK(node_stack.back() == &root);
358
359 if (!saw_span_node) {
360 // If there were no spans, we must treat this string a little differently (according to AAPT).
361 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
362 // from the last segment.
363 if (first_segment != nullptr) {
364 // Trim leading whitespace.
365 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
366 if (trimmed.size() != first_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700367 first_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800368 }
369 }
370
371 if (last_segment != nullptr) {
372 // Trim trailing whitespace.
373 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
374 if (trimmed.size() != last_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700375 last_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800376 }
377 }
378 }
379
380 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
381 // care of recording the correctly adjusted Spans and UntranslatableSections.
382 StringBuilder builder;
383 root.Build(&builder);
384 if (!builder) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000385 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
386 << builder.GetError());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800387 return false;
388 }
389
390 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
391 *out_raw_string = std::move(raw_string);
392 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
393 out_style_string->str = std::move(flattened_string.text);
394 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800395 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800396}
397
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 const size_t depth = parser->depth();
401 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
402 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 // Skip comments and text.
404 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800405 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406
Adam Lesinski060b53d2017-07-28 17:10:35 -0700407 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000408 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 << "root element must be <resources>");
410 return false;
411 }
412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 break;
415 };
416
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000418 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 return false;
421 }
422 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800423}
424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
426 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700427
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 bool error = false;
429 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 const size_t depth = parser->depth();
431 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
432 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700437
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 if (!util::TrimWhitespace(parser->text()).empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000440 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 << "plain text not allowed here");
442 error = true;
443 }
444 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700445 }
446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 // Skip unknown namespace.
451 continue;
452 }
453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 std::string element_name = parser->element_name();
455 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 comment = "";
457 continue;
458 }
459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 ParsedResource parsed_resource;
461 parsed_resource.config = config_;
462 parsed_resource.source = source_.WithLine(parser->line_number());
463 parsed_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +0800464 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100465 if (options_.visibility) {
466 parsed_resource.visibility_level = options_.visibility.value();
467 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468
469 // Extract the product name if it exists.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700470 if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700471 parsed_resource.product = std::string(maybe_product.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 }
473
474 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 error = true;
477 continue;
478 }
479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 error = true;
482 }
483 }
484
485 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 for (const ResourceName& stripped_resource : stripped_resources) {
487 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 // Failed to find the resource.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000489 diag_->Error(android::DiagMessage(source_)
490 << "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();
Jeremy Meyer211bec22024-06-04 14:22:03 -0700549 std::optional<StringPiece> flag =
550 xml::FindAttribute(parser, "http://schemas.android.com/apk/res/android", "featureFlag");
551 out_resource->flag_status = FlagStatus::NoFlag;
552 if (flag) {
553 auto flag_it = options_.feature_flag_values.find(flag.value());
554 if (flag_it == options_.feature_flag_values.end()) {
555 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
556 << "Resource flag value undefined");
557 return false;
558 }
559 const auto& flag_properties = flag_it->second;
560 if (!flag_properties.read_only) {
561 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
562 << "Only read only flags may be used with resources");
563 return false;
564 }
565 if (!flag_properties.enabled.has_value()) {
566 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
567 << "Only flags with a value may be used with resources");
568 return false;
569 }
570 out_resource->flag_status =
571 flag_properties.enabled.value() ? FlagStatus::Enabled : FlagStatus::Disabled;
572 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800573
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800576
Adam Lesinski86d67df2017-01-31 13:47:27 -0800577 bool can_be_item = true;
578 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800580 can_be_bag = false;
581
Adam Lesinskie597d682017-06-01 17:16:44 -0700582 // The default format for <item> is any. If a format attribute is present, that one will
583 // override the default.
584 resource_format = android::ResTable_map::TYPE_ANY;
585
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700587 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700588 resource_type = std::string(maybe_type.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000590 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 << "<item> must have a 'type' attribute");
592 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800593 }
594
Ryan Mitchell4382e442021-07-14 12:53:01 -0700595 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700597 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700599 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 if (!resource_format) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000601 diag_->Error(android::DiagMessage(out_resource->source)
602 << "'" << maybe_format.value() << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800603 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 }
605 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800606 } else if (resource_type == "bag") {
607 can_be_item = false;
608
609 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700610 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700611 resource_type = std::string(maybe_type.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800612 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000613 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski86d67df2017-01-31 13:47:27 -0800614 << "<bag> must have a 'type' attribute");
615 return false;
616 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 }
618
619 // Get the name of the resource. This will be checked later, because not all
620 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700621 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 if (resource_type == "id") {
624 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000625 diag_->Error(android::DiagMessage(out_resource->source)
626 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 return false;
628 }
629
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000630 out_resource->name.type =
631 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700632 out_resource->name.entry = std::string(maybe_name.value());
y9efbbef2018-04-18 11:29:09 -0700633
634 // Ids either represent a unique resource id or reference another resource id
635 auto item = ParseItem(parser, out_resource, resource_format);
636 if (!item) {
637 return false;
638 }
639
640 String* empty = ValueCast<String>(out_resource->value.get());
641 if (empty && *empty->value == "") {
642 // If no inner element exists, represent a unique identifier
643 out_resource->value = util::make_unique<Id>();
644 } else {
y9efbbef2018-04-18 11:29:09 -0700645 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700646 if (ref && !ref->name && !ref->id) {
647 // A null reference also means there is no inner element when ids are in the form:
648 // <id name="name"/>
649 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000650 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700651 // If an inner element exists, the inner element must be a reference to another resource id
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000652 diag_->Error(android::DiagMessage(out_resource->source)
653 << "<" << parser->element_name()
654 << "> inner element must either be a resource reference or empty");
y9efbbef2018-04-18 11:29:09 -0700655 return false;
656 }
657 }
658
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700660 } else if (resource_type == "macro") {
661 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000662 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700663 << "<" << parser->element_name() << "> missing 'name' attribute");
664 return false;
665 }
666
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000667 out_resource->name.type =
668 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700669 out_resource->name.entry = std::string(maybe_name.value());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700670 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
672
Adam Lesinski86d67df2017-01-31 13:47:27 -0800673 if (can_be_item) {
674 const auto item_iter = elToItemMap.find(resource_type);
675 if (item_iter != elToItemMap.end()) {
676 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677
Adam Lesinski86d67df2017-01-31 13:47:27 -0800678 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000679 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800680 << "<" << parser->element_name() << "> missing 'name' attribute");
681 return false;
682 }
683
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000684 out_resource->name.type =
685 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700686 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800687
Adam Lesinskie597d682017-06-01 17:16:44 -0700688 // Only use the implied format of the type when there is no explicit format.
689 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800690 resource_format = item_iter->second.format;
691 }
692
Jeremy Meyerefa42b12024-07-17 14:57:33 -0700693 // Don't bother parsing the item if it is behind a disabled flag
694 if (out_resource->flag_status != FlagStatus::Disabled &&
695 !ParseItem(parser, out_resource, resource_format)) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800696 return false;
697 }
698 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 }
701
702 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800703 if (can_be_bag) {
704 const auto bag_iter = elToBagMap.find(resource_type);
705 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700706 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700707 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700708 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800709 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000710 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800711 << "<" << parser->element_name() << "> missing 'name' attribute");
712 return false;
713 }
714
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700715 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800716 }
717
718 // Call the associated parse method. The type will be filled in by the
719 // parse func.
720 if (!bag_iter->second(this, parser, out_resource)) {
721 return false;
722 }
723 return true;
724 }
725 }
726
727 if (can_be_item) {
728 // Try parsing the elementName (or type) as a resource. These shall only be
729 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000730 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800731 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000733 diag_->Error(android::DiagMessage(out_resource->source)
734 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 return false;
736 }
737
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000738 out_resource->name.type = parsed_type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700739 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800740 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
741 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000742 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800743 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
744 return false;
745 }
746 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 }
749
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000750 // If the resource type was not recognized, write the error and return false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000751 diag_->Error(android::DiagMessage(out_resource->source)
752 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 return false;
754}
755
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
757 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 const uint32_t format) {
759 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 out_resource->value = ParseXml(parser, format, kNoRawString);
764 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000765 diag_->Error(android::DiagMessage(out_resource->source)
766 << "invalid " << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 return false;
768 }
769 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800770}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800771
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700772std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
773 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000777 android::StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800778 std::vector<UntranslatableSection> untranslatable_sections;
779 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800780 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 }
782
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700783 return FlattenedXmlSubTree{.raw_value = raw_value,
784 .style_string = style_string,
785 .untranslatable_sections = untranslatable_sections,
786 .namespace_resolver = parser,
787 .source = source_.WithLine(begin_xml_line)};
788}
789
790/**
791 * Reads the entire XML subtree and attempts to parse it as some Item,
792 * with typeMask denoting which items it can be. If allowRawValue is
793 * true, a RawString is returned if the XML couldn't be parsed as
794 * an Item. If allowRawValue is false, nullptr is returned in this
795 * case.
796 */
797std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
798 const bool allow_raw_value) {
799 auto sub_tree = CreateFlattenSubTree(parser);
800 if (!sub_tree.has_value()) {
801 return {};
802 }
803 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
804}
805
806std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
807 const uint32_t type_mask, const bool allow_raw_value,
808 ResourceTable& table,
809 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000810 android::IDiagnostics& diag) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700811 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800813 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700814 util::make_unique<StyledString>(table.string_pool.MakeRef(
815 xmlsub_tree.style_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000816 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700817 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800818 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 }
820
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 // name.package can be empty here, as it will assume the package name of the
823 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700824 auto id = util::make_unique<Id>();
825 id->SetSource(xmlsub_tree.source);
826 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 };
828
829 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700830 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000831 &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700834 if (auto ref = ValueCast<Reference>(processed_item.get())) {
835 ref->allow_raw = allow_raw_value;
836 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 }
840
841 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843 // Use the trimmed, escaped string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000844 std::unique_ptr<String> string = util::make_unique<String>(table.string_pool.MakeRef(
845 xmlsub_tree.style_string.str, android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700846 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800847 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 }
849
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700852 return util::make_unique<RawString>(table.string_pool.MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000853 util::TrimWhitespace(xmlsub_tree.raw_value), android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700854 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700855 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
856 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 }
858 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800859}
860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861bool ResourceParser::ParseString(xml::XmlPullParser* parser,
862 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700864 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
865 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700866 if (!maybe_formatted) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000867 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 << "invalid value for 'formatted'. Must be a boolean");
869 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800870 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800873
Adam Lesinski75421622017-01-06 15:20:04 -0800874 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700875 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
876 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800877 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000878 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 << "invalid value for 'translatable'. Must be a boolean");
880 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800881 }
Adam Lesinski75421622017-01-06 15:20:04 -0800882 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800884
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700885 out_resource->value =
886 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
887 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000888 diag_->Error(android::DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 return false;
890 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800893 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800894
Adam Lesinski75421622017-01-06 15:20:04 -0800895 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 if (!util::VerifyJavaStringFormat(*string_value->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000897 android::DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 msg << "multiple substitutions specified in non-positional format; "
899 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (options_.error_on_positional_arguments) {
901 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800903 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800904
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800907 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908
Adam Lesinski75421622017-01-06 15:20:04 -0800909 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
910 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911 }
912 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800913}
914
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700915bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
916 auto sub_tree = CreateFlattenSubTree(parser);
917 if (!sub_tree) {
918 return false;
919 }
920
921 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000922 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700923 << "<macro> tags cannot be declared in configurations other than the default "
924 "configuration'");
925 return false;
926 }
927
928 auto macro = std::make_unique<Macro>();
929 macro->raw_value = std::move(sub_tree->raw_value);
930 macro->style_string = std::move(sub_tree->style_string);
931 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
932
933 for (const auto& decl : parser->package_decls()) {
934 macro->alias_namespaces.emplace_back(
935 Macro::Namespace{.alias = decl.prefix,
936 .package_name = decl.package.package,
937 .is_private = decl.package.private_namespace});
938 }
939
940 out_resource->value = std::move(macro);
941 return true;
942}
943
Adam Lesinski71be7052017-12-12 16:48:07 -0800944bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100945 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000946 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100947 << "<public> tag not allowed with --visibility flag");
948 return false;
949 }
950
Adam Lesinski46c4d722017-08-23 13:03:56 -0700951 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000952 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -0700953 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
954 }
955
Ryan Mitchell4382e442021-07-14 12:53:01 -0700956 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000958 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 << "<public> must have a 'type' attribute");
960 return false;
961 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800962
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000963 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000965 diag_->Error(android::DiagMessage(out_resource->source)
966 << "invalid resource type '" << maybe_type.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 return false;
968 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800969
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000970 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800971
Ryan Mitchell4382e442021-07-14 12:53:01 -0700972 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
973 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000975 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800976 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800978 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800981
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000982 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700986
Adam Lesinski71be7052017-12-12 16:48:07 -0800987 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800989}
990
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700991template <typename Func>
992bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000993 const char* tag_name, android::IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700994 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000995 diag->Warn(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700996 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
997 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700998 }
999
Ryan Mitchell4382e442021-07-14 12:53:01 -07001000 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001002 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001003 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001004 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001006
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001007 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
1008 ParseResourceNamedType(maybe_type.value());
1009 if (!maybe_parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001010 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001011 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001012 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001014 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001015
Ryan Mitchell4382e442021-07-14 12:53:01 -07001016 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 if (!maybe_id_str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001018 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001019 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 return false;
1021 }
1022
Ryan Mitchell4382e442021-07-14 12:53:01 -07001023 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001025 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001026 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 return false;
1028 }
1029
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001031 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 const size_t depth = parser->depth();
1034 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1035 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001036 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 // Skip text.
1040 continue;
1041 }
1042
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001043 const android::Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001044 const std::string& element_namespace = parser->element_namespace();
1045 const std::string& element_name = parser->element_name();
1046 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001047 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001049 diag->Error(android::DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001050 error = true;
1051 continue;
1052 }
1053
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 if (xml::FindNonEmptyAttribute(parser, "id")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001055 diag->Error(android::DiagMessage(item_source)
1056 << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 error = true;
1058 continue;
1059 }
1060
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 if (xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001062 diag->Error(android::DiagMessage(item_source)
1063 << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 error = true;
1065 continue;
1066 }
1067
Winson Chiuc84829d2022-03-04 20:35:58 +00001068 if (maybe_name.value().substr(0, std::strlen("removed_")) == "removed_") {
1069 // Skip resources that have been removed from the framework, but leave a hole so that
1070 // other staged resources don't shift and break apps previously compiled against them
1071 next_id.id++;
1072 continue;
1073 }
1074
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001075 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001076 .name = ResourceName{{}, parsed_type, std::string(maybe_name.value())},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001077 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001078 .comment = std::move(comment),
1079 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001080 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001082 // Execute group specific code.
1083 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001085 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001087 diag->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088 error = true;
1089 }
1090 }
1091 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001092}
1093
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001094bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1095 ParsedResource* out_resource) {
1096 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1097 [](ParsedResource& parsed_entry, ResourceId id) {
1098 parsed_entry.id = id;
1099 parsed_entry.staged_api = true;
1100 parsed_entry.visibility_level = Visibility::Level::kPublic;
1101 });
1102}
1103
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001104bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1105 ParsedResource* out_resource) {
1106 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1107 [](ParsedResource& parsed_entry, ResourceId id) {
1108 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1109 });
1110}
1111
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001112bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1113 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001114 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001115 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1116 return false;
1117 }
1118
1119 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1120 [](ParsedResource& parsed_entry, ResourceId id) {
1121 parsed_entry.id = id;
1122 parsed_entry.visibility_level = Visibility::Level::kPublic;
1123 });
1124}
1125
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1127 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001128 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001129 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001130 diag_->Error(android::DiagMessage(out_resource->source)
1131 << "<" << parser->element_name() << "> must have a 'type' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 return false;
1133 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001134
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001135 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001137 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001138 << "invalid resource type '" << maybe_type.value() << "' in <"
1139 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 return false;
1141 }
1142
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001143 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001145}
1146
Adam Lesinski46c4d722017-08-23 13:03:56 -07001147bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001148 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001149 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001150 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1151 return false;
1152 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001153 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001154 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -07001155 << "ignoring configuration '" << out_resource->config << "' for <"
1156 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001158
1159 if (!ParseSymbolImpl(parser, out_resource)) {
1160 return false;
1161 }
1162
Adam Lesinski71be7052017-12-12 16:48:07 -08001163 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001164 return true;
1165}
1166
1167bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1168 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001169 diag_->Warn(android::DiagMessage(out_resource->source)
1170 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001171 }
1172
Ryan Mitchell4382e442021-07-14 12:53:01 -07001173 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001174 if (!overlayable_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001175 diag_->Error(android::DiagMessage(out_resource->source)
1176 << "<overlayable> tag must have a 'name' attribute");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001177 return false;
1178 }
1179
1180 const std::string kActorUriScheme =
1181 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001182 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001183 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001184 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001185 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1186 << Overlayable::kActorScheme << "'");
1187 return false;
1188 }
1189
1190 // Create a overlayable entry grouping that represents this <overlayable>
1191 auto overlayable = std::make_shared<Overlayable>(
1192 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001193 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001194
Adam Lesinski46c4d722017-08-23 13:03:56 -07001195 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001196 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001197 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001198 const size_t start_depth = parser->depth();
1199 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1200 xml::XmlPullParser::Event event = parser->event();
1201 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001202 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001203 break;
1204 } else if (event == xml::XmlPullParser::Event::kEndElement
1205 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001206 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001207 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001208 continue;
1209 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001210 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001211 comment = parser->comment();
1212 continue;
1213 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001214 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001215 continue;
1216 }
1217
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001218 const android::Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001219 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001220 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001221 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001222 if (current_policies == PolicyFlags::NONE) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001223 diag_->Error(android::DiagMessage(element_source)
1224 << "<item> within an <overlayable> must be inside a <policy> block");
Winsonb2d7f532019-02-04 16:32:43 -08001225 error = true;
1226 continue;
1227 }
1228
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001229 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001230 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001231 if (!item_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001232 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001233 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001234 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001235 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001236 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001237
Ryan Mitchell4382e442021-07-14 12:53:01 -07001238 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001239 if (!item_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001240 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001241 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001242 error = true;
1243 continue;
1244 }
1245
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001246 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1247 if (!type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001248 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001249 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001250 << "' in <item> within an <overlayable>");
1251 error = true;
1252 continue;
1253 }
1254
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001255 OverlayableItem overlayable_item(overlayable);
1256 overlayable_item.policies = current_policies;
1257 overlayable_item.comment = comment;
1258 overlayable_item.source = element_source;
1259
1260 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001261 child_resource.name.type = type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001262 child_resource.name.entry = std::string(item_name.value());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001263 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001264 out_resource->child_resources.push_back(std::move(child_resource));
1265
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001266 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001267 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001268 // If the policy list is not empty, then we are currently inside a policy element
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001269 diag_->Error(android::DiagMessage(element_source)
1270 << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001271 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001272 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001273 } else if (std::optional<StringPiece> maybe_type =
1274 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001275 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001276 // policies. Items within the policy tag will have the specified policy.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001277 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001278 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001279 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1280 kPolicyStringToFlag.end(),
1281 [trimmed_part](const auto& it) {
1282 return trimmed_part == it.first;
1283 });
1284 if (policy == kPolicyStringToFlag.end()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001285 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001286 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001287 error = true;
1288 continue;
1289 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001290
1291 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001292 }
Winsonb2d7f532019-02-04 16:32:43 -08001293 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001294 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001295 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001296 error = true;
1297 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001298 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001299 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001300 diag_->Error(android::DiagMessage(element_source)
1301 << "invalid element <" << element_name << "> "
1302 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001303 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001304 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001305 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001306
1307 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001308 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001309
Adam Lesinski46c4d722017-08-23 13:03:56 -07001310 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001311}
1312
Adam Lesinski71be7052017-12-12 16:48:07 -08001313bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001314 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001315 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001316 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 return true;
1318 }
1319 return false;
1320}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001321
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001322bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1323 ParsedResource* out_resource) {
1324 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325}
1326
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1328 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001329 out_resource->name.type =
1330 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331
1332 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001333 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001334 diag_->Warn(android::DiagMessage(out_resource->source)
1335 << "ignoring configuration '" << out_resource->config << "' for attribute "
1336 << out_resource->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 }
1339
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341
Ryan Mitchell4382e442021-07-14 12:53:01 -07001342 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 if (maybe_format) {
1344 type_mask = ParseFormatAttribute(maybe_format.value());
1345 if (type_mask == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001346 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001347 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348 return false;
1349 }
1350 }
1351
Ryan Mitchell4382e442021-07-14 12:53:01 -07001352 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353
Ryan Mitchell4382e442021-07-14 12:53:01 -07001354 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1356 if (!min_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001357 std::u16string min_str16 = android::util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001359 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001362 }
1363
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 if (!maybe_min) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001365 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001367 return false;
1368 }
1369 }
1370
Ryan Mitchell4382e442021-07-14 12:53:01 -07001371 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1373 if (!max_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001374 std::u16string max_str16 = android::util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001376 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001379 }
1380
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 if (!maybe_max) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001382 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001383 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001384 return false;
1385 }
1386 }
1387
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 if ((maybe_min || maybe_max) &&
1389 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001390 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 << "'min' and 'max' can only be used when format='integer'");
1392 return false;
1393 }
1394
1395 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001396 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 return a.symbol.name.value() < b.symbol.name.value();
1398 }
1399 };
1400
1401 std::set<Attribute::Symbol, SymbolComparator> items;
1402
1403 std::string comment;
1404 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001405 const size_t depth = parser->depth();
1406 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1407 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001408 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 // Skip text.
1412 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001413 }
1414
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001415 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 const std::string& element_namespace = parser->element_namespace();
1417 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001418 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 if (element_name == "enum") {
1420 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001421 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422 << "can not define an <enum>; already defined a <flag>");
1423 error = true;
1424 continue;
1425 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001427
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001428 } else if (element_name == "flag") {
1429 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001430 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431 << "can not define a <flag>; already defined an <enum>");
1432 error = true;
1433 continue;
1434 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001435 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436 }
1437
Ryan Mitchell4382e442021-07-14 12:53:01 -07001438 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001440 ParsedResource child_resource;
1441 child_resource.name = symbol.symbol.name.value();
1442 child_resource.source = item_source;
1443 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001444 if (options_.visibility) {
1445 child_resource.visibility_level = options_.visibility.value();
1446 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001447 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 symbol.symbol.SetComment(std::move(comment));
1450 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 auto insert_result = items.insert(std::move(symbol));
1453 if (!insert_result.second) {
1454 const Attribute::Symbol& existing_symbol = *insert_result.first;
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001455 diag_->Error(android::DiagMessage(item_source)
1456 << "duplicate symbol '" << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001458 diag_->Note(android::DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 << "first defined here");
1460 error = true;
1461 }
1462 } else {
1463 error = true;
1464 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001466 diag_->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467 error = true;
1468 }
1469
1470 comment = {};
1471 }
1472
1473 if (error) {
1474 return false;
1475 }
1476
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001477 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1478 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1479 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001480 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001481 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1482 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001483 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001484 return true;
1485}
1486
Ryan Mitchell4382e442021-07-14 12:53:01 -07001487std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001488 StringPiece tag) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001489 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490
Ryan Mitchell4382e442021-07-14 12:53:01 -07001491 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001492 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001493 diag_->Error(android::DiagMessage(source)
1494 << "no attribute 'name' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001495 return {};
1496 }
1497
Ryan Mitchell4382e442021-07-14 12:53:01 -07001498 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 if (!maybe_value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001500 diag_->Error(android::DiagMessage(source)
1501 << "no attribute 'value' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return {};
1503 }
1504
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001505 std::u16string value16 = android::util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001506 android::Res_value val;
1507 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001508 diag_->Error(android::DiagMessage(source) << "invalid value '" << maybe_value.value()
1509 << "' for <" << tag << ">; must be an integer");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 return {};
1511 }
1512
1513 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001514 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1515 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001516 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001517}
1518
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001520 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001521
Ryan Mitchell4382e442021-07-14 12:53:01 -07001522 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001524 diag_->Error(android::DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001525 return false;
1526 }
1527
Ryan Mitchell4382e442021-07-14 12:53:01 -07001528 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529 if (!maybe_key) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001530 diag_->Error(android::DiagMessage(source)
1531 << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 return false;
1533 }
1534
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001535 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001537
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 if (!value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001540 diag_->Error(android::DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 return false;
1542 }
1543
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001544 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 return true;
1546}
1547
Adam Lesinski86d67df2017-01-31 13:47:27 -08001548bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001550 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551
1552 std::unique_ptr<Style> style = util::make_unique<Style>();
1553
Ryan Mitchell4382e442021-07-14 12:53:01 -07001554 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001556 // 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 -07001557 if (!maybe_parent.value().empty()) {
1558 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001559 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 if (!style->parent) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001561 diag_->Error(android::DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001562 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001563 }
1564
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001565 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001567 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001568 }
1569
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 } else {
1571 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001572 std::string style_name = out_resource->name.entry;
1573 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001576 style->parent = Reference(ResourceName(
1577 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578 }
1579 }
1580
1581 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 const size_t depth = parser->depth();
1583 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1584 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 // Skip text and comments.
1586 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001587 }
1588
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 const std::string& element_namespace = parser->element_namespace();
1590 const std::string& element_name = parser->element_name();
1591 if (element_namespace == "" && element_name == "item") {
1592 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001593
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001595 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001598 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001600
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 if (error) {
1602 return false;
1603 }
1604
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001605 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001606 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001607}
1608
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001609bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1610 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001611 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001612 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1613 if (resource_format == 0u) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001614 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001615 << "'" << format_attr.value() << "' is an invalid format");
1616 return false;
1617 }
1618 }
1619 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001620}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001621
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001622bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1623 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001624}
1625
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001626bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1627 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001628}
1629
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001630bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1631 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001632 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001633 out_resource->name.type =
1634 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001635
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001636 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001637
Adam Lesinski75421622017-01-06 15:20:04 -08001638 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001639 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1640 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001641 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001642 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 << "invalid value for 'translatable'. Must be a boolean");
1644 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001645 }
Adam Lesinski75421622017-01-06 15:20:04 -08001646 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 }
Adam Lesinski75421622017-01-06 15:20:04 -08001648 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001649
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001650 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001651 const size_t depth = parser->depth();
1652 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1653 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001654 // Skip text and comments.
1655 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001656 }
1657
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001658 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 const std::string& element_namespace = parser->element_namespace();
1660 const std::string& element_name = parser->element_name();
1661 if (element_namespace.empty() && element_name == "item") {
1662 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 if (!item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001664 diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 error = true;
1666 continue;
1667 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001669 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001670
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001671 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001672 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1673 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 error = true;
1675 }
1676 }
1677
1678 if (error) {
1679 return false;
1680 }
1681
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001683 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001684}
1685
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001686bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1687 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001688 out_resource->name.type =
1689 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001690
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001691 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001692
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001693 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 const size_t depth = parser->depth();
1695 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1696 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 // Skip text and comments.
1698 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001699 }
1700
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001701 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001702 const std::string& element_namespace = parser->element_namespace();
1703 const std::string& element_name = parser->element_name();
1704 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001705 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706 if (!maybe_quantity) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001707 diag_->Error(android::DiagMessage(item_source) << "<item> in <plurals> requires attribute "
1708 << "'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001709 error = true;
1710 continue;
1711 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001712
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001713 StringPiece trimmed_quantity =
1714 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001716 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001717 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001718 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001719 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001721 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001722 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001723 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001724 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001727 index = Plural::Other;
1728 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001729 diag_->Error(android::DiagMessage(item_source)
1730 << "<item> in <plural> has invalid value '" << trimmed_quantity
1731 << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 error = true;
1733 continue;
1734 }
1735
1736 if (plural->values[index]) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001737 diag_->Error(android::DiagMessage(item_source)
1738 << "duplicate quantity '" << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 error = true;
1740 continue;
1741 }
1742
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001744 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1745 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001746 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001747 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001748
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001749 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001751 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001752 diag_->Error(android::DiagMessage(item_source)
1753 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754 error = true;
1755 }
1756 }
1757
1758 if (error) {
1759 return false;
1760 }
1761
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001762 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001763 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001764}
1765
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001766bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1767 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001768 out_resource->name.type =
1769 ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001770
Donald Chai94e4a012020-01-06 13:52:41 -08001771 if (!options_.preserve_visibility_of_styleables) {
1772 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1773 // knows exactly what for.
1774 //
1775 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1776 // package-private (to be used only by the view class); themes are a different story.
1777 out_resource->visibility_level = Visibility::Level::kPublic;
1778 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001779
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001780 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001781 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001782 diag_->Warn(android::DiagMessage(out_resource->source)
1783 << "ignoring configuration '" << out_resource->config << "' for styleable "
1784 << out_resource->name.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001785 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001786 }
1787
1788 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1789
1790 std::string comment;
1791 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001792 const size_t depth = parser->depth();
1793 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1794 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001795 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001796 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001797 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001798 // Ignore text.
1799 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001800 }
1801
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001802 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001803 const std::string& element_namespace = parser->element_namespace();
1804 const std::string& element_name = parser->element_name();
1805 if (element_namespace.empty() && element_name == "attr") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001806 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001807 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001808 diag_->Error(android::DiagMessage(item_source)
1809 << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001810 error = true;
1811 continue;
1812 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001813
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001814 // If this is a declaration, the package name may be in the name. Separate
1815 // these out.
1816 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001817 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001818 if (!maybe_ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001819 diag_->Error(android::DiagMessage(item_source)
1820 << "<attr> tag has invalid name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001821 error = true;
1822 continue;
1823 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001824
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001825 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001826 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001827
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001828 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001829 ParsedResource child_resource;
1830 child_resource.name = child_ref.name.value();
1831 child_resource.source = item_source;
1832 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001833 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001834 if (options_.visibility) {
1835 child_resource.visibility_level = options_.visibility.value();
1836 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001837
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001838 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001839 error = true;
1840 continue;
1841 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001842
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001843 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001844 child_ref.SetComment(child_resource.comment);
1845 child_ref.SetSource(item_source);
1846 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001847
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001848 // Do not add referenced attributes that do not define a format to the table.
1849 CHECK(child_resource.value != nullptr);
1850 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1851
1852 CHECK(attr != nullptr);
1853 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1854 out_resource->child_resources.push_back(std::move(child_resource));
1855 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001856
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001858 diag_->Error(android::DiagMessage(item_source)
1859 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001860 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001861 }
1862
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001863 comment = {};
1864 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001865
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001866 if (error) {
1867 return false;
1868 }
1869
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001870 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001871 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001872}
1873
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001874} // namespace aapt