blob: a5aecc8557070cb1e90fc962617415f6ae7b73c0 [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 Meyerd7f86e52024-07-24 16:00:44 -0700110 FlagStatus flag_status = FlagStatus::NoFlag;
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) {
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700154 res->value->SetFlagStatus(res->flag_status);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 res->value->SetComment(std::move(res->comment));
157 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700158 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800160
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700161 if (res->staged_alias) {
162 res_builder.SetStagedId(res->staged_alias.value());
163 }
164
Jeremy Meyer211bec22024-06-04 14:22:03 -0700165 res_builder.SetFlagStatus(res->flag_status);
166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700168 if (!res->name.entry.empty()) {
169 if (!table->AddResource(res_builder.Build(), diag)) {
170 return false;
171 }
172 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 for (ParsedResource& child : res->child_resources) {
174 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 }
176 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800177}
178
179// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800181
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000182ResourceParser::ResourceParser(android::IDiagnostics* diag, ResourceTable* table,
183 const android::Source& source, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 const ResourceParserOptions& options)
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000185 : diag_(diag), table_(table), source_(source), config_(config), options_(options) {
186}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800188// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
189// This will be used to traverse and flatten the XML string into a single std::string, with all
190// Span and Untranslatable data maintained in parallel, as indices into the string.
191class Node {
192 public:
193 virtual ~Node() = default;
194
195 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
196 // parent node as well.
197 // Returns a pointer to the child node that was added as a convenience.
198 template <typename T>
199 T* AddChild(std::unique_ptr<T> node) {
200 T* raw_ptr = node.get();
201 children.push_back(std::move(node));
202 return raw_ptr;
203 }
204
205 virtual void Build(StringBuilder* builder) const {
206 for (const auto& child : children) {
207 child->Build(builder);
208 }
209 }
210
211 std::vector<std::unique_ptr<Node>> children;
212};
213
214// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
215class SegmentNode : public Node {
216 public:
217 std::string data;
218
219 void Build(StringBuilder* builder) const override {
220 builder->AppendText(data);
221 }
222};
223
224// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
225class SpanNode : public Node {
226 public:
227 std::string name;
228
229 void Build(StringBuilder* builder) const override {
230 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
231 Node::Build(builder);
232 builder->EndSpan(span_handle);
233 }
234};
235
236// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
237class UntranslatableNode : public Node {
238 public:
239 void Build(StringBuilder* builder) const override {
240 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
241 Node::Build(builder);
242 builder->EndUntranslatable(handle);
243 }
244};
245
246// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800247bool ResourceParser::FlattenXmlSubtree(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000248 xml::XmlPullParser* parser, std::string* out_raw_string, android::StyleString* out_style_string,
Adam Lesinski75421622017-01-06 15:20:04 -0800249 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800250 std::string raw_string;
251 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800252
253 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700254 std::optional<size_t> untranslatable_start_depth;
Adam Lesinski75421622017-01-06 15:20:04 -0800255
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800256 Node root;
257 std::vector<Node*> node_stack;
258 node_stack.push_back(&root);
259
260 bool saw_span_node = false;
261 SegmentNode* first_segment = nullptr;
262 SegmentNode* last_segment = nullptr;
263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800265 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800267
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800270 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800271 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800272 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800273 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700274
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800275 last_segment = node_stack.back()->AddChild(std::move(segment_node));
276 if (first_segment == nullptr) {
277 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800278 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800279 current_text = {};
280 }
281 }
Adam Lesinski75421622017-01-06 15:20:04 -0800282
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800283 switch (event) {
284 case xml::XmlPullParser::Event::kText: {
285 current_text += parser->text();
286 raw_string += parser->text();
287 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800288
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289 case xml::XmlPullParser::Event::kStartElement: {
290 if (parser->element_namespace().empty()) {
291 // This is an HTML tag which we encode as a span. Add it to the span stack.
292 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
293 span_node->name = parser->element_name();
294 const auto end_attr_iter = parser->end_attributes();
295 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
296 ++attr_iter) {
297 span_node->name += ";";
298 span_node->name += attr_iter->name;
299 span_node->name += "=";
300 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800301 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800302
303 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
304 saw_span_node = true;
305 } else if (parser->element_namespace() == sXliffNamespaceUri) {
306 // This is an XLIFF tag, which is not encoded as a span.
307 if (parser->element_name() == "g") {
308 // Check that an 'untranslatable' tag is not already being processed. Nested
309 // <xliff:g> tags are illegal.
310 if (untranslatable_start_depth) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000311 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800312 << "illegal nested XLIFF 'g' tag");
313 return false;
314 } else {
315 // Mark the beginning of an 'untranslatable' section.
316 untranslatable_start_depth = depth;
317 node_stack.push_back(
318 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
319 }
320 } else {
321 // Ignore unknown XLIFF tags, but don't warn.
322 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
323 }
324 } else {
325 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000326 diag_->Warn(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800327 << "ignoring element '" << parser->element_name()
328 << "' with unknown namespace '" << parser->element_namespace() << "'");
329 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800330 }
Adam Lesinski75421622017-01-06 15:20:04 -0800331
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800332 // Enter one level inside the element.
333 depth++;
334 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800336 case xml::XmlPullParser::Event::kEndElement: {
337 // Return one level from within the element.
338 depth--;
339 if (depth == 0) {
340 break;
341 }
342
343 node_stack.pop_back();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700344 if (untranslatable_start_depth == depth) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800345 // This is the end of an untranslatable section.
346 untranslatable_start_depth = {};
347 }
348 } break;
349
350 default:
351 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
354 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700356 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800357 CHECK(node_stack.size() == 1u);
358 CHECK(node_stack.back() == &root);
359
360 if (!saw_span_node) {
361 // If there were no spans, we must treat this string a little differently (according to AAPT).
362 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
363 // from the last segment.
364 if (first_segment != nullptr) {
365 // Trim leading whitespace.
366 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
367 if (trimmed.size() != first_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700368 first_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800369 }
370 }
371
372 if (last_segment != nullptr) {
373 // Trim trailing whitespace.
374 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
375 if (trimmed.size() != last_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700376 last_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800377 }
378 }
379 }
380
381 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
382 // care of recording the correctly adjusted Spans and UntranslatableSections.
383 StringBuilder builder;
384 root.Build(&builder);
385 if (!builder) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000386 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
387 << builder.GetError());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800388 return false;
389 }
390
391 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
392 *out_raw_string = std::move(raw_string);
393 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
394 out_style_string->str = std::move(flattened_string.text);
395 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800396 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397}
398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 const size_t depth = parser->depth();
402 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
403 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 // Skip comments and text.
405 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407
Adam Lesinski060b53d2017-07-28 17:10:35 -0700408 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000409 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 << "root element must be <resources>");
411 return false;
412 }
413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 break;
416 };
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000419 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 return false;
422 }
423 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424}
425
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
427 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700428
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 bool error = false;
430 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 const size_t depth = parser->depth();
432 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
433 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700438
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 if (!util::TrimWhitespace(parser->text()).empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000441 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 << "plain text not allowed here");
443 error = true;
444 }
445 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700446 }
447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 // Skip unknown namespace.
452 continue;
453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 std::string element_name = parser->element_name();
456 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 comment = "";
458 continue;
459 }
460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 ParsedResource parsed_resource;
462 parsed_resource.config = config_;
463 parsed_resource.source = source_.WithLine(parser->line_number());
464 parsed_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +0800465 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100466 if (options_.visibility) {
467 parsed_resource.visibility_level = options_.visibility.value();
468 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469
470 // Extract the product name if it exists.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700471 if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700472 parsed_resource.product = std::string(maybe_product.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
475 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 error = true;
478 continue;
479 }
480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 error = true;
483 }
484 }
485
486 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 for (const ResourceName& stripped_resource : stripped_resources) {
488 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 // Failed to find the resource.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000490 diag_->Error(android::DiagMessage(source_)
491 << "resource '" << stripped_resource
492 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 error = true;
494 }
495 }
496
497 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498}
499
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
501 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 struct ItemTypeFormat {
503 ResourceType type;
504 uint32_t format;
505 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800506
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
508 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800509
Adam Lesinski86d67df2017-01-31 13:47:27 -0800510 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
511 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
512 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
513 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
514 {"dimen",
515 {ResourceType::kDimen,
516 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
517 android::ResTable_map::TYPE_DIMENSION}},
518 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
519 {"fraction",
520 {ResourceType::kFraction,
521 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
522 android::ResTable_map::TYPE_DIMENSION}},
523 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
524 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
525 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800526
Adam Lesinski86d67df2017-01-31 13:47:27 -0800527 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
528 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
529 {"array", std::mem_fn(&ResourceParser::ParseArray)},
530 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
531 {"configVarying",
532 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
533 std::placeholders::_2, std::placeholders::_3)},
534 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
535 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
536 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700537 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800538 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
539 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
540 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700541 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700542 {"staging-public-group-final", std::mem_fn(&ResourceParser::ParseStagingPublicGroupFinal)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800543 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
544 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
545 std::placeholders::_2, std::placeholders::_3)},
546 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
547 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 std::string resource_type = parser->element_name();
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700550 auto flag_status = GetFlagStatus(parser);
551 if (!flag_status) {
552 return false;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700553 }
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700554 out_resource->flag_status = flag_status.value();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800555
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800558
Adam Lesinski86d67df2017-01-31 13:47:27 -0800559 bool can_be_item = true;
560 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800562 can_be_bag = false;
563
Adam Lesinskie597d682017-06-01 17:16:44 -0700564 // The default format for <item> is any. If a format attribute is present, that one will
565 // override the default.
566 resource_format = android::ResTable_map::TYPE_ANY;
567
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700569 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700570 resource_type = std::string(maybe_type.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000572 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 << "<item> must have a 'type' attribute");
574 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800575 }
576
Ryan Mitchell4382e442021-07-14 12:53:01 -0700577 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700579 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700581 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if (!resource_format) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000583 diag_->Error(android::DiagMessage(out_resource->source)
584 << "'" << maybe_format.value() << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800585 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 }
587 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800588 } else if (resource_type == "bag") {
589 can_be_item = false;
590
591 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700592 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700593 resource_type = std::string(maybe_type.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800594 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000595 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski86d67df2017-01-31 13:47:27 -0800596 << "<bag> must have a 'type' attribute");
597 return false;
598 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 }
600
601 // Get the name of the resource. This will be checked later, because not all
602 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700603 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 if (resource_type == "id") {
606 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000607 diag_->Error(android::DiagMessage(out_resource->source)
608 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 return false;
610 }
611
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000612 out_resource->name.type =
613 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700614 out_resource->name.entry = std::string(maybe_name.value());
y9efbbef2018-04-18 11:29:09 -0700615
616 // Ids either represent a unique resource id or reference another resource id
617 auto item = ParseItem(parser, out_resource, resource_format);
618 if (!item) {
619 return false;
620 }
621
622 String* empty = ValueCast<String>(out_resource->value.get());
623 if (empty && *empty->value == "") {
624 // If no inner element exists, represent a unique identifier
625 out_resource->value = util::make_unique<Id>();
626 } else {
y9efbbef2018-04-18 11:29:09 -0700627 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700628 if (ref && !ref->name && !ref->id) {
629 // A null reference also means there is no inner element when ids are in the form:
630 // <id name="name"/>
631 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000632 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700633 // If an inner element exists, the inner element must be a reference to another resource id
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000634 diag_->Error(android::DiagMessage(out_resource->source)
635 << "<" << parser->element_name()
636 << "> inner element must either be a resource reference or empty");
y9efbbef2018-04-18 11:29:09 -0700637 return false;
638 }
639 }
640
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700642 } else if (resource_type == "macro") {
643 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000644 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700645 << "<" << parser->element_name() << "> missing 'name' attribute");
646 return false;
647 }
648
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000649 out_resource->name.type =
650 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700651 out_resource->name.entry = std::string(maybe_name.value());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700652 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 }
654
Adam Lesinski86d67df2017-01-31 13:47:27 -0800655 if (can_be_item) {
656 const auto item_iter = elToItemMap.find(resource_type);
657 if (item_iter != elToItemMap.end()) {
658 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659
Adam Lesinski86d67df2017-01-31 13:47:27 -0800660 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000661 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800662 << "<" << parser->element_name() << "> missing 'name' attribute");
663 return false;
664 }
665
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000666 out_resource->name.type =
667 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700668 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800669
Adam Lesinskie597d682017-06-01 17:16:44 -0700670 // Only use the implied format of the type when there is no explicit format.
671 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800672 resource_format = item_iter->second.format;
673 }
674
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700675 if (!ParseItem(parser, out_resource, resource_format)) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800676 return false;
677 }
678 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 }
681
682 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800683 if (can_be_bag) {
684 const auto bag_iter = elToBagMap.find(resource_type);
685 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700686 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700687 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700688 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800689 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000690 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800691 << "<" << parser->element_name() << "> missing 'name' attribute");
692 return false;
693 }
694
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700695 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800696 }
697
698 // Call the associated parse method. The type will be filled in by the
699 // parse func.
700 if (!bag_iter->second(this, parser, out_resource)) {
701 return false;
702 }
703 return true;
704 }
705 }
706
707 if (can_be_item) {
708 // Try parsing the elementName (or type) as a resource. These shall only be
709 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000710 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800711 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000713 diag_->Error(android::DiagMessage(out_resource->source)
714 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 return false;
716 }
717
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000718 out_resource->name.type = parsed_type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700719 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800720 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
721 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000722 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800723 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
724 return false;
725 }
726 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 }
729
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000730 // If the resource type was not recognized, write the error and return false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000731 diag_->Error(android::DiagMessage(out_resource->source)
732 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return false;
734}
735
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700736std::optional<FlagStatus> ResourceParser::GetFlagStatus(xml::XmlPullParser* parser) {
737 auto flag_status = FlagStatus::NoFlag;
738
739 std::optional<StringPiece> flag = xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag");
740 if (flag) {
741 auto flag_it = options_.feature_flag_values.find(flag.value());
742 if (flag_it == options_.feature_flag_values.end()) {
743 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
744 << "Resource flag value undefined");
745 return {};
746 }
747 const auto& flag_properties = flag_it->second;
748 if (!flag_properties.read_only) {
749 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
750 << "Only read only flags may be used with resources");
751 return {};
752 }
753 if (!flag_properties.enabled.has_value()) {
754 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
755 << "Only flags with a value may be used with resources");
756 return {};
757 }
758 flag_status = flag_properties.enabled.value() ? FlagStatus::Enabled : FlagStatus::Disabled;
759 }
760 return flag_status;
761}
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
764 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 const uint32_t format) {
766 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 }
769
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 out_resource->value = ParseXml(parser, format, kNoRawString);
771 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000772 diag_->Error(android::DiagMessage(out_resource->source)
773 << "invalid " << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 return false;
775 }
776 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800777}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800778
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700779std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
780 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800782
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000784 android::StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800785 std::vector<UntranslatableSection> untranslatable_sections;
786 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800787 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 }
789
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700790 return FlattenedXmlSubTree{.raw_value = raw_value,
791 .style_string = style_string,
792 .untranslatable_sections = untranslatable_sections,
793 .namespace_resolver = parser,
794 .source = source_.WithLine(begin_xml_line)};
795}
796
797/**
798 * Reads the entire XML subtree and attempts to parse it as some Item,
799 * with typeMask denoting which items it can be. If allowRawValue is
800 * true, a RawString is returned if the XML couldn't be parsed as
801 * an Item. If allowRawValue is false, nullptr is returned in this
802 * case.
803 */
804std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
805 const bool allow_raw_value) {
806 auto sub_tree = CreateFlattenSubTree(parser);
807 if (!sub_tree.has_value()) {
808 return {};
809 }
810 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
811}
812
813std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
814 const uint32_t type_mask, const bool allow_raw_value,
815 ResourceTable& table,
816 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000817 android::IDiagnostics& diag) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700818 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800820 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700821 util::make_unique<StyledString>(table.string_pool.MakeRef(
822 xmlsub_tree.style_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000823 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700824 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800825 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 }
827
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 // name.package can be empty here, as it will assume the package name of the
830 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700831 auto id = util::make_unique<Id>();
832 id->SetSource(xmlsub_tree.source);
833 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 };
835
836 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700837 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000838 &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700839 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700841 if (auto ref = ValueCast<Reference>(processed_item.get())) {
842 ref->allow_raw = allow_raw_value;
843 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846 }
847
848 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700849 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 // Use the trimmed, escaped string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000851 std::unique_ptr<String> string = util::make_unique<String>(table.string_pool.MakeRef(
852 xmlsub_tree.style_string.str, android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700853 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800854 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 }
856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700859 return util::make_unique<RawString>(table.string_pool.MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000860 util::TrimWhitespace(xmlsub_tree.raw_value), android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700861 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700862 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
863 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 }
865 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800866}
867
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868bool ResourceParser::ParseString(xml::XmlPullParser* parser,
869 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700871 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
872 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873 if (!maybe_formatted) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000874 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 << "invalid value for 'formatted'. Must be a boolean");
876 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800877 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800880
Adam Lesinski75421622017-01-06 15:20:04 -0800881 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700882 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
883 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800884 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000885 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700886 << "invalid value for 'translatable'. Must be a boolean");
887 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800888 }
Adam Lesinski75421622017-01-06 15:20:04 -0800889 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 out_resource->value =
893 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
894 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000895 diag_->Error(android::DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700896 return false;
897 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800898
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800900 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800901
Adam Lesinski75421622017-01-06 15:20:04 -0800902 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 if (!util::VerifyJavaStringFormat(*string_value->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000904 android::DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 msg << "multiple substitutions specified in non-positional format; "
906 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700907 if (options_.error_on_positional_arguments) {
908 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800910 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800911
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700913 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800914 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915
Adam Lesinski75421622017-01-06 15:20:04 -0800916 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
917 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 }
919 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800920}
921
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700922bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
923 auto sub_tree = CreateFlattenSubTree(parser);
924 if (!sub_tree) {
925 return false;
926 }
927
928 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000929 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700930 << "<macro> tags cannot be declared in configurations other than the default "
931 "configuration'");
932 return false;
933 }
934
935 auto macro = std::make_unique<Macro>();
936 macro->raw_value = std::move(sub_tree->raw_value);
937 macro->style_string = std::move(sub_tree->style_string);
938 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
939
940 for (const auto& decl : parser->package_decls()) {
941 macro->alias_namespaces.emplace_back(
942 Macro::Namespace{.alias = decl.prefix,
943 .package_name = decl.package.package,
944 .is_private = decl.package.private_namespace});
945 }
946
947 out_resource->value = std::move(macro);
948 return true;
949}
950
Adam Lesinski71be7052017-12-12 16:48:07 -0800951bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100952 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000953 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100954 << "<public> tag not allowed with --visibility flag");
955 return false;
956 }
957
Adam Lesinski46c4d722017-08-23 13:03:56 -0700958 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000959 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -0700960 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
961 }
962
Ryan Mitchell4382e442021-07-14 12:53:01 -0700963 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000965 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966 << "<public> must have a 'type' attribute");
967 return false;
968 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800969
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000970 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000972 diag_->Error(android::DiagMessage(out_resource->source)
973 << "invalid resource type '" << maybe_type.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 return false;
975 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800976
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000977 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800978
Ryan Mitchell4382e442021-07-14 12:53:01 -0700979 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
980 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000982 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800983 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700984 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800985 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800988
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000989 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700993
Adam Lesinski71be7052017-12-12 16:48:07 -0800994 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800996}
997
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700998template <typename Func>
999bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001000 const char* tag_name, android::IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -07001001 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001002 diag->Warn(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001003 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
1004 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001005 }
1006
Ryan Mitchell4382e442021-07-14 12:53:01 -07001007 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001009 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001010 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001011 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001013
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001014 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
1015 ParseResourceNamedType(maybe_type.value());
1016 if (!maybe_parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001017 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001018 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001019 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001021 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022
Ryan Mitchell4382e442021-07-14 12:53:01 -07001023 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 if (!maybe_id_str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001025 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001026 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 return false;
1028 }
1029
Ryan Mitchell4382e442021-07-14 12:53:01 -07001030 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001031 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001032 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001033 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001034 return false;
1035 }
1036
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001038 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040 const size_t depth = parser->depth();
1041 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1042 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001043 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001045 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046 // Skip text.
1047 continue;
1048 }
1049
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001050 const android::Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001051 const std::string& element_namespace = parser->element_namespace();
1052 const std::string& element_name = parser->element_name();
1053 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001054 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001056 diag->Error(android::DiagMessage(item_source) << "<public> must have a 'name' attribute");
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, "id")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001062 diag->Error(android::DiagMessage(item_source)
1063 << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 error = true;
1065 continue;
1066 }
1067
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001068 if (xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001069 diag->Error(android::DiagMessage(item_source)
1070 << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071 error = true;
1072 continue;
1073 }
1074
Winson Chiuc84829d2022-03-04 20:35:58 +00001075 if (maybe_name.value().substr(0, std::strlen("removed_")) == "removed_") {
1076 // Skip resources that have been removed from the framework, but leave a hole so that
1077 // other staged resources don't shift and break apps previously compiled against them
1078 next_id.id++;
1079 continue;
1080 }
1081
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001082 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001083 .name = ResourceName{{}, parsed_type, std::string(maybe_name.value())},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001084 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001085 .comment = std::move(comment),
1086 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001087 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001089 // Execute group specific code.
1090 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001092 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001094 diag->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095 error = true;
1096 }
1097 }
1098 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001099}
1100
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001101bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1102 ParsedResource* out_resource) {
1103 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1104 [](ParsedResource& parsed_entry, ResourceId id) {
1105 parsed_entry.id = id;
1106 parsed_entry.staged_api = true;
1107 parsed_entry.visibility_level = Visibility::Level::kPublic;
1108 });
1109}
1110
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001111bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1112 ParsedResource* out_resource) {
1113 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1114 [](ParsedResource& parsed_entry, ResourceId id) {
1115 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1116 });
1117}
1118
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001119bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1120 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001121 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001122 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1123 return false;
1124 }
1125
1126 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1127 [](ParsedResource& parsed_entry, ResourceId id) {
1128 parsed_entry.id = id;
1129 parsed_entry.visibility_level = Visibility::Level::kPublic;
1130 });
1131}
1132
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001133bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1134 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001135 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001137 diag_->Error(android::DiagMessage(out_resource->source)
1138 << "<" << parser->element_name() << "> must have a 'type' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 return false;
1140 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001141
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001142 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001143 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001144 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001145 << "invalid resource type '" << maybe_type.value() << "' in <"
1146 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001147 return false;
1148 }
1149
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001150 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001151 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001152}
1153
Adam Lesinski46c4d722017-08-23 13:03:56 -07001154bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001155 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001156 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001157 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1158 return false;
1159 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001160 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001161 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -07001162 << "ignoring configuration '" << out_resource->config << "' for <"
1163 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001164 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001165
1166 if (!ParseSymbolImpl(parser, out_resource)) {
1167 return false;
1168 }
1169
Adam Lesinski71be7052017-12-12 16:48:07 -08001170 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001171 return true;
1172}
1173
1174bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1175 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001176 diag_->Warn(android::DiagMessage(out_resource->source)
1177 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001178 }
1179
Ryan Mitchell4382e442021-07-14 12:53:01 -07001180 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001181 if (!overlayable_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001182 diag_->Error(android::DiagMessage(out_resource->source)
1183 << "<overlayable> tag must have a 'name' attribute");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001184 return false;
1185 }
1186
1187 const std::string kActorUriScheme =
1188 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001189 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001190 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001191 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001192 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1193 << Overlayable::kActorScheme << "'");
1194 return false;
1195 }
1196
1197 // Create a overlayable entry grouping that represents this <overlayable>
1198 auto overlayable = std::make_shared<Overlayable>(
1199 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001200 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001201
Adam Lesinski46c4d722017-08-23 13:03:56 -07001202 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001203 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001204 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001205 const size_t start_depth = parser->depth();
1206 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1207 xml::XmlPullParser::Event event = parser->event();
1208 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001209 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001210 break;
1211 } else if (event == xml::XmlPullParser::Event::kEndElement
1212 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001213 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001214 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001215 continue;
1216 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001217 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001218 comment = parser->comment();
1219 continue;
1220 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001221 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001222 continue;
1223 }
1224
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001225 const android::Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001226 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001227 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001228 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001229 if (current_policies == PolicyFlags::NONE) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001230 diag_->Error(android::DiagMessage(element_source)
1231 << "<item> within an <overlayable> must be inside a <policy> block");
Winsonb2d7f532019-02-04 16:32:43 -08001232 error = true;
1233 continue;
1234 }
1235
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001236 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001237 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001238 if (!item_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001239 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001240 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001241 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001242 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001243 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001244
Ryan Mitchell4382e442021-07-14 12:53:01 -07001245 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001246 if (!item_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001247 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001248 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001249 error = true;
1250 continue;
1251 }
1252
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001253 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1254 if (!type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001255 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001256 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001257 << "' in <item> within an <overlayable>");
1258 error = true;
1259 continue;
1260 }
1261
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001262 OverlayableItem overlayable_item(overlayable);
1263 overlayable_item.policies = current_policies;
1264 overlayable_item.comment = comment;
1265 overlayable_item.source = element_source;
1266
1267 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001268 child_resource.name.type = type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001269 child_resource.name.entry = std::string(item_name.value());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001270 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001271 out_resource->child_resources.push_back(std::move(child_resource));
1272
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001273 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001274 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001275 // If the policy list is not empty, then we are currently inside a policy element
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001276 diag_->Error(android::DiagMessage(element_source)
1277 << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001278 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001279 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001280 } else if (std::optional<StringPiece> maybe_type =
1281 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001282 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001283 // policies. Items within the policy tag will have the specified policy.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001284 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001285 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001286 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1287 kPolicyStringToFlag.end(),
1288 [trimmed_part](const auto& it) {
1289 return trimmed_part == it.first;
1290 });
1291 if (policy == kPolicyStringToFlag.end()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001292 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001293 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001294 error = true;
1295 continue;
1296 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001297
1298 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001299 }
Winsonb2d7f532019-02-04 16:32:43 -08001300 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001301 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001302 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001303 error = true;
1304 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001305 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001306 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001307 diag_->Error(android::DiagMessage(element_source)
1308 << "invalid element <" << element_name << "> "
1309 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001310 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001311 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001312 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001313
1314 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001315 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001316
Adam Lesinski46c4d722017-08-23 13:03:56 -07001317 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001318}
1319
Adam Lesinski71be7052017-12-12 16:48:07 -08001320bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001322 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001323 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 return true;
1325 }
1326 return false;
1327}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001328
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1330 ParsedResource* out_resource) {
1331 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332}
1333
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1335 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001336 out_resource->name.type =
1337 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338
1339 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001341 diag_->Warn(android::DiagMessage(out_resource->source)
1342 << "ignoring configuration '" << out_resource->config << "' for attribute "
1343 << out_resource->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001345 }
1346
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001347 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348
Ryan Mitchell4382e442021-07-14 12:53:01 -07001349 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 if (maybe_format) {
1351 type_mask = ParseFormatAttribute(maybe_format.value());
1352 if (type_mask == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001353 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001354 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 return false;
1356 }
1357 }
1358
Ryan Mitchell4382e442021-07-14 12:53:01 -07001359 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001360
Ryan Mitchell4382e442021-07-14 12:53:01 -07001361 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1363 if (!min_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001364 std::u16string min_str16 = android::util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001366 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001369 }
1370
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 if (!maybe_min) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001372 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 return false;
1375 }
1376 }
1377
Ryan Mitchell4382e442021-07-14 12:53:01 -07001378 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1380 if (!max_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001381 std::u16string max_str16 = android::util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001383 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001386 }
1387
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 if (!maybe_max) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001389 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 return false;
1392 }
1393 }
1394
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001395 if ((maybe_min || maybe_max) &&
1396 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001397 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398 << "'min' and 'max' can only be used when format='integer'");
1399 return false;
1400 }
1401
1402 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001403 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 return a.symbol.name.value() < b.symbol.name.value();
1405 }
1406 };
1407
1408 std::set<Attribute::Symbol, SymbolComparator> items;
1409
1410 std::string comment;
1411 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001412 const size_t depth = parser->depth();
1413 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1414 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001415 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001417 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 // Skip text.
1419 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001420 }
1421
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001422 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001423 const std::string& element_namespace = parser->element_namespace();
1424 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001425 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 if (element_name == "enum") {
1427 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001428 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001429 << "can not define an <enum>; already defined a <flag>");
1430 error = true;
1431 continue;
1432 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001433 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001434
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001435 } else if (element_name == "flag") {
1436 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001437 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001438 << "can not define a <flag>; already defined an <enum>");
1439 error = true;
1440 continue;
1441 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001442 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001443 }
1444
Ryan Mitchell4382e442021-07-14 12:53:01 -07001445 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001446 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001447 ParsedResource child_resource;
1448 child_resource.name = symbol.symbol.name.value();
1449 child_resource.source = item_source;
1450 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001451 if (options_.visibility) {
1452 child_resource.visibility_level = options_.visibility.value();
1453 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 symbol.symbol.SetComment(std::move(comment));
1457 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001458
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 auto insert_result = items.insert(std::move(symbol));
1460 if (!insert_result.second) {
1461 const Attribute::Symbol& existing_symbol = *insert_result.first;
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001462 diag_->Error(android::DiagMessage(item_source)
1463 << "duplicate symbol '" << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001465 diag_->Note(android::DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 << "first defined here");
1467 error = true;
1468 }
1469 } else {
1470 error = true;
1471 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001472 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001473 diag_->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 error = true;
1475 }
1476
1477 comment = {};
1478 }
1479
1480 if (error) {
1481 return false;
1482 }
1483
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001484 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1485 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1486 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001487 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001488 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1489 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001490 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 return true;
1492}
1493
Ryan Mitchell4382e442021-07-14 12:53:01 -07001494std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001495 StringPiece tag) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001496 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497
Ryan Mitchell4382e442021-07-14 12:53:01 -07001498 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001500 diag_->Error(android::DiagMessage(source)
1501 << "no attribute 'name' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return {};
1503 }
1504
Ryan Mitchell4382e442021-07-14 12:53:01 -07001505 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001506 if (!maybe_value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001507 diag_->Error(android::DiagMessage(source)
1508 << "no attribute 'value' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509 return {};
1510 }
1511
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001512 std::u16string value16 = android::util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 android::Res_value val;
1514 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001515 diag_->Error(android::DiagMessage(source) << "invalid value '" << maybe_value.value()
1516 << "' for <" << tag << ">; must be an integer");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 return {};
1518 }
1519
1520 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001521 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1522 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001523 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001524}
1525
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001527 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001528
Ryan Mitchell4382e442021-07-14 12:53:01 -07001529 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001531 diag_->Error(android::DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 return false;
1533 }
1534
Ryan Mitchell4382e442021-07-14 12:53:01 -07001535 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 if (!maybe_key) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001537 diag_->Error(android::DiagMessage(source)
1538 << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 return false;
1540 }
1541
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001542 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001544
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001545 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 if (!value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001547 diag_->Error(android::DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001548 return false;
1549 }
1550
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001551 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001552 return true;
1553}
1554
Adam Lesinski86d67df2017-01-31 13:47:27 -08001555bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001557 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001558
1559 std::unique_ptr<Style> style = util::make_unique<Style>();
1560
Ryan Mitchell4382e442021-07-14 12:53:01 -07001561 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001562 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001563 // 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 -07001564 if (!maybe_parent.value().empty()) {
1565 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001566 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001567 if (!style->parent) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001568 diag_->Error(android::DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001569 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 }
1571
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001572 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001573 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001574 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001575 }
1576
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001577 } else {
1578 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 std::string style_name = out_resource->name.entry;
1580 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001581 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001583 style->parent = Reference(ResourceName(
1584 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 }
1586 }
1587
1588 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 const size_t depth = parser->depth();
1590 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1591 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 // Skip text and comments.
1593 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001594 }
1595
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 const std::string& element_namespace = parser->element_namespace();
1597 const std::string& element_name = parser->element_name();
1598 if (element_namespace == "" && element_name == "item") {
1599 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001600
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001602 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001604 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001605 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001606 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001607
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 if (error) {
1609 return false;
1610 }
1611
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001614}
1615
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001616bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1617 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001618 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001619 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1620 if (resource_format == 0u) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001621 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001622 << "'" << format_attr.value() << "' is an invalid format");
1623 return false;
1624 }
1625 }
1626 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001627}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001628
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001629bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1630 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001631}
1632
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001633bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1634 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001635}
1636
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001637bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1638 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001639 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001640 out_resource->name.type =
1641 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001642
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001644
Adam Lesinski75421622017-01-06 15:20:04 -08001645 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001646 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1647 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001648 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001649 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001650 << "invalid value for 'translatable'. Must be a boolean");
1651 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001652 }
Adam Lesinski75421622017-01-06 15:20:04 -08001653 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001654 }
Adam Lesinski75421622017-01-06 15:20:04 -08001655 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001656
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658 const size_t depth = parser->depth();
1659 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1660 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661 // Skip text and comments.
1662 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001663 }
1664
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001665 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001666 const std::string& element_namespace = parser->element_namespace();
1667 const std::string& element_name = parser->element_name();
1668 if (element_namespace.empty() && element_name == "item") {
Jeremy Meyerd52bd682024-08-14 11:16:58 -07001669 auto flag_status = GetFlagStatus(parser);
1670 if (!flag_status) {
1671 error = true;
1672 continue;
1673 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001675 if (!item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001676 diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001677 error = true;
1678 continue;
1679 }
Jeremy Meyerd52bd682024-08-14 11:16:58 -07001680 item->SetFlagStatus(flag_status.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001681 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001682 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001683
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001685 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1686 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001687 error = true;
1688 }
1689 }
1690
1691 if (error) {
1692 return false;
1693 }
1694
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001696 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001697}
1698
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001699bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1700 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001701 out_resource->name.type =
1702 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001703
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001704 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001705
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001706 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001707 const size_t depth = parser->depth();
1708 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1709 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 // Skip text and comments.
1711 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001712 }
1713
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001714 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001715 const std::string& element_namespace = parser->element_namespace();
1716 const std::string& element_name = parser->element_name();
1717 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001718 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001719 if (!maybe_quantity) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001720 diag_->Error(android::DiagMessage(item_source) << "<item> in <plurals> requires attribute "
1721 << "'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722 error = true;
1723 continue;
1724 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001725
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 StringPiece trimmed_quantity =
1727 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001729 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001730 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001733 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001734 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001735 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001736 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001737 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001738 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001739 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 index = Plural::Other;
1741 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001742 diag_->Error(android::DiagMessage(item_source)
1743 << "<item> in <plural> has invalid value '" << trimmed_quantity
1744 << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001745 error = true;
1746 continue;
1747 }
1748
1749 if (plural->values[index]) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001750 diag_->Error(android::DiagMessage(item_source)
1751 << "duplicate quantity '" << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001752 error = true;
1753 continue;
1754 }
1755
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001756 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001757 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1758 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001759 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001761
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001762 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001763
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001764 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001765 diag_->Error(android::DiagMessage(item_source)
1766 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001767 error = true;
1768 }
1769 }
1770
1771 if (error) {
1772 return false;
1773 }
1774
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001775 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001776 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001777}
1778
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001779bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1780 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001781 out_resource->name.type =
1782 ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001783
Donald Chai94e4a012020-01-06 13:52:41 -08001784 if (!options_.preserve_visibility_of_styleables) {
1785 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1786 // knows exactly what for.
1787 //
1788 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1789 // package-private (to be used only by the view class); themes are a different story.
1790 out_resource->visibility_level = Visibility::Level::kPublic;
1791 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001792
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001793 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001794 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001795 diag_->Warn(android::DiagMessage(out_resource->source)
1796 << "ignoring configuration '" << out_resource->config << "' for styleable "
1797 << out_resource->name.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001798 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001799 }
1800
1801 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1802
1803 std::string comment;
1804 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001805 const size_t depth = parser->depth();
1806 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1807 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001808 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001809 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001810 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 // Ignore text.
1812 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001813 }
1814
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001815 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001816 const std::string& element_namespace = parser->element_namespace();
1817 const std::string& element_name = parser->element_name();
1818 if (element_namespace.empty() && element_name == "attr") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001819 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001821 diag_->Error(android::DiagMessage(item_source)
1822 << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001823 error = true;
1824 continue;
1825 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001826
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001827 // If this is a declaration, the package name may be in the name. Separate
1828 // these out.
1829 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001830 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001831 if (!maybe_ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001832 diag_->Error(android::DiagMessage(item_source)
1833 << "<attr> tag has invalid name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001834 error = true;
1835 continue;
1836 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001837
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001838 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001839 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001840
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001841 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001842 ParsedResource child_resource;
1843 child_resource.name = child_ref.name.value();
1844 child_resource.source = item_source;
1845 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001846 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001847 if (options_.visibility) {
1848 child_resource.visibility_level = options_.visibility.value();
1849 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001850
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001851 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001852 error = true;
1853 continue;
1854 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001855
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001856 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001857 child_ref.SetComment(child_resource.comment);
1858 child_ref.SetSource(item_source);
1859 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001860
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001861 // Do not add referenced attributes that do not define a format to the table.
1862 CHECK(child_resource.value != nullptr);
1863 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1864
1865 CHECK(attr != nullptr);
1866 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1867 out_resource->child_resources.push_back(std::move(child_resource));
1868 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001869
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001870 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001871 diag_->Error(android::DiagMessage(item_source)
1872 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001873 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001874 }
1875
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001876 comment = {};
1877 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001878
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001879 if (error) {
1880 return false;
1881 }
1882
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001883 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001884 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001885}
1886
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001887} // namespace aapt