blob: a2383ac0285f6b9c675661c6a9612bf9a59b4c4c [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;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700110 std::optional<OverlayableItem> overlayable_item;
111 std::optional<StagedId> staged_alias;
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700112 std::optional<FeatureFlagAttribute> flag;
113 FlagStatus flag_status;
Adam Lesinski71be7052017-12-12 16:48:07 -0800114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 std::string comment;
116 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800118};
119
120// Recursively adds resources to the ResourceTable.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000121static bool AddResourcesToTable(ResourceTable* table, android::IDiagnostics* diag,
122 ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
124 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 // Only if there was a change do we re-assign.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700126 res->comment = std::string(trimmed_comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 }
Adam Lesinski76565542016-03-10 21:55:04 -0800128
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700129 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800130 if (res->visibility_level != Visibility::Level::kUndefined) {
131 Visibility visibility;
132 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700133 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800134 visibility.source = res->source;
135 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700136 res_builder.SetVisibility(visibility);
137 }
138
139 if (res->id.is_valid()) {
140 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800142
Adam Lesinski71be7052017-12-12 16:48:07 -0800143 if (res->allow_new) {
144 AllowNew allow_new;
145 allow_new.source = res->source;
146 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700147 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800148 }
149
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800150 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700151 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800152 }
153
154 if (res->value != nullptr) {
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700155 res->value->SetFlag(res->flag);
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700156 res->value->SetFlagStatus(res->flag_status);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 res->value->SetComment(std::move(res->comment));
159 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700160 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800162
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700163 if (res->staged_alias) {
164 res_builder.SetStagedId(res->staged_alias.value());
165 }
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
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700549 std::string_view resource_type = parser->element_name();
550 if (auto flag = GetFlag(parser)) {
551 if (options_.flag) {
552 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
553 << "Resource flag are not allowed both in the path and in the file");
554 return false;
555 }
556 out_resource->flag = std::move(flag);
557 std::string error;
558 auto flag_status = GetFlagStatus(out_resource->flag, options_.feature_flag_values, &error);
559 if (flag_status) {
560 out_resource->flag_status = flag_status.value();
561 } else {
562 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number())) << error);
563 return false;
564 }
565 } else if (options_.flag) {
566 out_resource->flag = options_.flag;
567 out_resource->flag_status = options_.flag_status;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700568 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800572
Adam Lesinski86d67df2017-01-31 13:47:27 -0800573 bool can_be_item = true;
574 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800576 can_be_bag = false;
577
Adam Lesinskie597d682017-06-01 17:16:44 -0700578 // The default format for <item> is any. If a format attribute is present, that one will
579 // override the default.
580 resource_format = android::ResTable_map::TYPE_ANY;
581
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700583 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700584 resource_type = maybe_type.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000586 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 << "<item> must have a 'type' attribute");
588 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800589 }
590
Ryan Mitchell4382e442021-07-14 12:53:01 -0700591 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700593 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700595 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 if (!resource_format) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000597 diag_->Error(android::DiagMessage(out_resource->source)
598 << "'" << maybe_format.value() << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800599 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
601 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800602 } else if (resource_type == "bag") {
603 can_be_item = false;
604
605 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700606 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700607 resource_type = maybe_type.value();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800608 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000609 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski86d67df2017-01-31 13:47:27 -0800610 << "<bag> must have a 'type' attribute");
611 return false;
612 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 }
614
615 // Get the name of the resource. This will be checked later, because not all
616 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700617 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 if (resource_type == "id") {
620 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000621 diag_->Error(android::DiagMessage(out_resource->source)
622 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 return false;
624 }
625
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000626 out_resource->name.type =
627 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700628 out_resource->name.entry = std::string(maybe_name.value());
y9efbbef2018-04-18 11:29:09 -0700629
630 // Ids either represent a unique resource id or reference another resource id
631 auto item = ParseItem(parser, out_resource, resource_format);
632 if (!item) {
633 return false;
634 }
635
636 String* empty = ValueCast<String>(out_resource->value.get());
637 if (empty && *empty->value == "") {
638 // If no inner element exists, represent a unique identifier
639 out_resource->value = util::make_unique<Id>();
640 } else {
y9efbbef2018-04-18 11:29:09 -0700641 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700642 if (ref && !ref->name && !ref->id) {
643 // A null reference also means there is no inner element when ids are in the form:
644 // <id name="name"/>
645 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000646 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700647 // If an inner element exists, the inner element must be a reference to another resource id
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000648 diag_->Error(android::DiagMessage(out_resource->source)
649 << "<" << parser->element_name()
650 << "> inner element must either be a resource reference or empty");
y9efbbef2018-04-18 11:29:09 -0700651 return false;
652 }
653 }
654
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700656 } else if (resource_type == "macro") {
657 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000658 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700659 << "<" << parser->element_name() << "> missing 'name' attribute");
660 return false;
661 }
662
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000663 out_resource->name.type =
664 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700665 out_resource->name.entry = std::string(maybe_name.value());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700666 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 }
668
Adam Lesinski86d67df2017-01-31 13:47:27 -0800669 if (can_be_item) {
670 const auto item_iter = elToItemMap.find(resource_type);
671 if (item_iter != elToItemMap.end()) {
672 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673
Adam Lesinski86d67df2017-01-31 13:47:27 -0800674 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000675 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800676 << "<" << parser->element_name() << "> missing 'name' attribute");
677 return false;
678 }
679
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000680 out_resource->name.type =
681 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700682 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800683
Adam Lesinskie597d682017-06-01 17:16:44 -0700684 // Only use the implied format of the type when there is no explicit format.
685 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800686 resource_format = item_iter->second.format;
687 }
688
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700689 if (!ParseItem(parser, out_resource, resource_format)) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800690 return false;
691 }
692 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 }
695
696 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800697 if (can_be_bag) {
698 const auto bag_iter = elToBagMap.find(resource_type);
699 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700700 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700701 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700702 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800703 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000704 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800705 << "<" << parser->element_name() << "> missing 'name' attribute");
706 return false;
707 }
708
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700709 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800710 }
711
712 // Call the associated parse method. The type will be filled in by the
713 // parse func.
714 if (!bag_iter->second(this, parser, out_resource)) {
715 return false;
716 }
717 return true;
718 }
719 }
720
721 if (can_be_item) {
722 // Try parsing the elementName (or type) as a resource. These shall only be
723 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000724 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800725 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000727 diag_->Error(android::DiagMessage(out_resource->source)
728 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 return false;
730 }
731
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000732 out_resource->name.type = parsed_type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700733 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800734 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
735 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000736 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800737 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
738 return false;
739 }
740 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
743
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000744 // If the resource type was not recognized, write the error and return false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000745 diag_->Error(android::DiagMessage(out_resource->source)
746 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 return false;
748}
749
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700750std::optional<FeatureFlagAttribute> ResourceParser::GetFlag(xml::XmlPullParser* parser) {
751 auto name = xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag");
752 if (name) {
753 FeatureFlagAttribute flag;
754 if (name->starts_with('!')) {
755 flag.negated = true;
756 flag.name = name->substr(1);
757 } else {
758 flag.name = name.value();
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700759 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700760 return flag;
761 } else {
762 return {};
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700763 }
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700764}
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
767 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 const uint32_t format) {
769 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 }
772
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700773 out_resource->value = ParseXml(parser, format, kNoRawString);
774 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000775 diag_->Error(android::DiagMessage(out_resource->source)
776 << "invalid " << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 return false;
778 }
779 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800780}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800781
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700782std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
783 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800785
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000787 android::StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800788 std::vector<UntranslatableSection> untranslatable_sections;
789 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800790 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 }
792
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700793 return FlattenedXmlSubTree{.raw_value = raw_value,
794 .style_string = style_string,
795 .untranslatable_sections = untranslatable_sections,
796 .namespace_resolver = parser,
797 .source = source_.WithLine(begin_xml_line)};
798}
799
800/**
801 * Reads the entire XML subtree and attempts to parse it as some Item,
802 * with typeMask denoting which items it can be. If allowRawValue is
803 * true, a RawString is returned if the XML couldn't be parsed as
804 * an Item. If allowRawValue is false, nullptr is returned in this
805 * case.
806 */
807std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
808 const bool allow_raw_value) {
809 auto sub_tree = CreateFlattenSubTree(parser);
810 if (!sub_tree.has_value()) {
811 return {};
812 }
813 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
814}
815
816std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
817 const uint32_t type_mask, const bool allow_raw_value,
818 ResourceTable& table,
819 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000820 android::IDiagnostics& diag) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700821 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800823 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700824 util::make_unique<StyledString>(table.string_pool.MakeRef(
825 xmlsub_tree.style_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000826 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700827 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800828 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 }
830
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 // name.package can be empty here, as it will assume the package name of the
833 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700834 auto id = util::make_unique<Id>();
835 id->SetSource(xmlsub_tree.source);
836 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 };
838
839 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700840 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000841 &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700844 if (auto ref = ValueCast<Reference>(processed_item.get())) {
845 ref->allow_raw = allow_raw_value;
846 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 }
850
851 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700852 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700853 // Use the trimmed, escaped string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000854 std::unique_ptr<String> string = util::make_unique<String>(table.string_pool.MakeRef(
855 xmlsub_tree.style_string.str, android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700856 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800857 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 }
859
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700860 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700862 return util::make_unique<RawString>(table.string_pool.MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000863 util::TrimWhitespace(xmlsub_tree.raw_value), android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700864 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700865 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
866 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867 }
868 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800869}
870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871bool ResourceParser::ParseString(xml::XmlPullParser* parser,
872 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700874 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
875 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 if (!maybe_formatted) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000877 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 << "invalid value for 'formatted'. Must be a boolean");
879 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800880 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800883
Adam Lesinski75421622017-01-06 15:20:04 -0800884 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700885 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
886 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800887 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000888 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 << "invalid value for 'translatable'. Must be a boolean");
890 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800891 }
Adam Lesinski75421622017-01-06 15:20:04 -0800892 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800894
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895 out_resource->value =
896 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
897 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000898 diag_->Error(android::DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 return false;
900 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800901
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700902 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800903 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800904
Adam Lesinski75421622017-01-06 15:20:04 -0800905 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 if (!util::VerifyJavaStringFormat(*string_value->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000907 android::DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 msg << "multiple substitutions specified in non-positional format; "
909 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 if (options_.error_on_positional_arguments) {
911 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800913 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800914
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700915 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800917 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918
Adam Lesinski75421622017-01-06 15:20:04 -0800919 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
920 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 }
922 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800923}
924
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700925bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
926 auto sub_tree = CreateFlattenSubTree(parser);
927 if (!sub_tree) {
928 return false;
929 }
930
931 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000932 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700933 << "<macro> tags cannot be declared in configurations other than the default "
934 "configuration'");
935 return false;
936 }
937
938 auto macro = std::make_unique<Macro>();
939 macro->raw_value = std::move(sub_tree->raw_value);
940 macro->style_string = std::move(sub_tree->style_string);
941 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
942
943 for (const auto& decl : parser->package_decls()) {
944 macro->alias_namespaces.emplace_back(
945 Macro::Namespace{.alias = decl.prefix,
946 .package_name = decl.package.package,
947 .is_private = decl.package.private_namespace});
948 }
949
950 out_resource->value = std::move(macro);
951 return true;
952}
953
Adam Lesinski71be7052017-12-12 16:48:07 -0800954bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100955 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000956 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100957 << "<public> tag not allowed with --visibility flag");
958 return false;
959 }
960
Adam Lesinski46c4d722017-08-23 13:03:56 -0700961 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000962 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -0700963 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
964 }
965
Ryan Mitchell4382e442021-07-14 12:53:01 -0700966 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000968 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969 << "<public> must have a 'type' attribute");
970 return false;
971 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800972
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000973 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000975 diag_->Error(android::DiagMessage(out_resource->source)
976 << "invalid resource type '" << maybe_type.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 return false;
978 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800979
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000980 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800981
Ryan Mitchell4382e442021-07-14 12:53:01 -0700982 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
983 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000985 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800986 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800988 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800991
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000992 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700994 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700996
Adam Lesinski71be7052017-12-12 16:48:07 -0800997 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800999}
1000
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001001template <typename Func>
1002bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001003 const char* tag_name, android::IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -07001004 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001005 diag->Warn(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001006 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
1007 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001008 }
1009
Ryan Mitchell4382e442021-07-14 12:53:01 -07001010 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001012 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001013 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001014 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001015 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001016
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001017 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
1018 ParseResourceNamedType(maybe_type.value());
1019 if (!maybe_parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001020 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001021 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001022 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001024 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001025
Ryan Mitchell4382e442021-07-14 12:53:01 -07001026 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 if (!maybe_id_str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001028 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001029 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 return false;
1031 }
1032
Ryan Mitchell4382e442021-07-14 12:53:01 -07001033 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001034 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001035 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001036 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 return false;
1038 }
1039
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001041 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001042 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 const size_t depth = parser->depth();
1044 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1045 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001046 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 // Skip text.
1050 continue;
1051 }
1052
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001053 const android::Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 const std::string& element_namespace = parser->element_namespace();
1055 const std::string& element_name = parser->element_name();
1056 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001057 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001058 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001059 diag->Error(android::DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 error = true;
1061 continue;
1062 }
1063
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 if (xml::FindNonEmptyAttribute(parser, "id")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001065 diag->Error(android::DiagMessage(item_source)
1066 << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001067 error = true;
1068 continue;
1069 }
1070
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 if (xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001072 diag->Error(android::DiagMessage(item_source)
1073 << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 error = true;
1075 continue;
1076 }
1077
Winson Chiuc84829d2022-03-04 20:35:58 +00001078 if (maybe_name.value().substr(0, std::strlen("removed_")) == "removed_") {
1079 // Skip resources that have been removed from the framework, but leave a hole so that
1080 // other staged resources don't shift and break apps previously compiled against them
1081 next_id.id++;
1082 continue;
1083 }
1084
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001085 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001086 .name = ResourceName{{}, parsed_type, std::string(maybe_name.value())},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001087 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001088 .comment = std::move(comment),
1089 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001090 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001092 // Execute group specific code.
1093 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001094
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001095 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001097 diag->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001098 error = true;
1099 }
1100 }
1101 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001102}
1103
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001104bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1105 ParsedResource* out_resource) {
1106 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1107 [](ParsedResource& parsed_entry, ResourceId id) {
1108 parsed_entry.id = id;
1109 parsed_entry.staged_api = true;
1110 parsed_entry.visibility_level = Visibility::Level::kPublic;
1111 });
1112}
1113
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001114bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1115 ParsedResource* out_resource) {
1116 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1117 [](ParsedResource& parsed_entry, ResourceId id) {
1118 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1119 });
1120}
1121
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001122bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1123 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001124 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001125 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1126 return false;
1127 }
1128
1129 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1130 [](ParsedResource& parsed_entry, ResourceId id) {
1131 parsed_entry.id = id;
1132 parsed_entry.visibility_level = Visibility::Level::kPublic;
1133 });
1134}
1135
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1137 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001138 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001139 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001140 diag_->Error(android::DiagMessage(out_resource->source)
1141 << "<" << parser->element_name() << "> must have a 'type' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001142 return false;
1143 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001144
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001145 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001146 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001147 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 << "invalid resource type '" << maybe_type.value() << "' in <"
1149 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 return false;
1151 }
1152
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001153 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001154 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001155}
1156
Adam Lesinski46c4d722017-08-23 13:03:56 -07001157bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001158 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001159 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001160 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1161 return false;
1162 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001163 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001164 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -07001165 << "ignoring configuration '" << out_resource->config << "' for <"
1166 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001167 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001168
1169 if (!ParseSymbolImpl(parser, out_resource)) {
1170 return false;
1171 }
1172
Adam Lesinski71be7052017-12-12 16:48:07 -08001173 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001174 return true;
1175}
1176
1177bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1178 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001179 diag_->Warn(android::DiagMessage(out_resource->source)
1180 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001181 }
1182
Ryan Mitchell4382e442021-07-14 12:53:01 -07001183 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001184 if (!overlayable_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001185 diag_->Error(android::DiagMessage(out_resource->source)
1186 << "<overlayable> tag must have a 'name' attribute");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001187 return false;
1188 }
1189
1190 const std::string kActorUriScheme =
1191 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001192 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001193 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001194 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001195 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1196 << Overlayable::kActorScheme << "'");
1197 return false;
1198 }
1199
1200 // Create a overlayable entry grouping that represents this <overlayable>
1201 auto overlayable = std::make_shared<Overlayable>(
1202 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001203 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001204
Adam Lesinski46c4d722017-08-23 13:03:56 -07001205 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001206 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001207 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001208 const size_t start_depth = parser->depth();
1209 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1210 xml::XmlPullParser::Event event = parser->event();
1211 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001212 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001213 break;
1214 } else if (event == xml::XmlPullParser::Event::kEndElement
1215 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001216 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001217 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001218 continue;
1219 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001220 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001221 comment = parser->comment();
1222 continue;
1223 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001224 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001225 continue;
1226 }
1227
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001228 const android::Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001229 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001230 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001231 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001232 if (current_policies == PolicyFlags::NONE) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001233 diag_->Error(android::DiagMessage(element_source)
1234 << "<item> within an <overlayable> must be inside a <policy> block");
Winsonb2d7f532019-02-04 16:32:43 -08001235 error = true;
1236 continue;
1237 }
1238
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001239 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001240 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001241 if (!item_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001242 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001243 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001244 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001245 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001246 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001247
Ryan Mitchell4382e442021-07-14 12:53:01 -07001248 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001249 if (!item_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001250 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001251 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001252 error = true;
1253 continue;
1254 }
1255
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001256 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1257 if (!type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001258 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001259 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001260 << "' in <item> within an <overlayable>");
1261 error = true;
1262 continue;
1263 }
1264
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001265 OverlayableItem overlayable_item(overlayable);
1266 overlayable_item.policies = current_policies;
1267 overlayable_item.comment = comment;
1268 overlayable_item.source = element_source;
1269
1270 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001271 child_resource.name.type = type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001272 child_resource.name.entry = std::string(item_name.value());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001273 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001274 out_resource->child_resources.push_back(std::move(child_resource));
1275
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001276 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001277 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001278 // If the policy list is not empty, then we are currently inside a policy element
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001279 diag_->Error(android::DiagMessage(element_source)
1280 << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001281 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001282 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001283 } else if (std::optional<StringPiece> maybe_type =
1284 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001285 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001286 // policies. Items within the policy tag will have the specified policy.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001287 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001288 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001289 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1290 kPolicyStringToFlag.end(),
1291 [trimmed_part](const auto& it) {
1292 return trimmed_part == it.first;
1293 });
1294 if (policy == kPolicyStringToFlag.end()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001295 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001296 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001297 error = true;
1298 continue;
1299 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001300
1301 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001302 }
Winsonb2d7f532019-02-04 16:32:43 -08001303 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001304 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001305 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001306 error = true;
1307 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001308 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001309 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001310 diag_->Error(android::DiagMessage(element_source)
1311 << "invalid element <" << element_name << "> "
1312 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001313 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001314 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001315 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001316
1317 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001318 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001319
Adam Lesinski46c4d722017-08-23 13:03:56 -07001320 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001321}
1322
Adam Lesinski71be7052017-12-12 16:48:07 -08001323bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001324 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001325 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001326 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 return true;
1328 }
1329 return false;
1330}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001331
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1333 ParsedResource* out_resource) {
1334 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335}
1336
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1338 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001339 out_resource->name.type =
1340 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341
1342 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001344 diag_->Warn(android::DiagMessage(out_resource->source)
1345 << "ignoring configuration '" << out_resource->config << "' for attribute "
1346 << out_resource->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001347 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348 }
1349
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001351
Ryan Mitchell4382e442021-07-14 12:53:01 -07001352 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 if (maybe_format) {
1354 type_mask = ParseFormatAttribute(maybe_format.value());
1355 if (type_mask == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001356 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001357 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 return false;
1359 }
1360 }
1361
Ryan Mitchell4382e442021-07-14 12:53:01 -07001362 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363
Ryan Mitchell4382e442021-07-14 12:53:01 -07001364 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001365 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1366 if (!min_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001367 std::u16string min_str16 = android::util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001368 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001369 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001372 }
1373
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001374 if (!maybe_min) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001375 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 return false;
1378 }
1379 }
1380
Ryan Mitchell4382e442021-07-14 12:53:01 -07001381 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1383 if (!max_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001384 std::u16string max_str16 = android::util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001386 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001389 }
1390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 if (!maybe_max) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001392 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394 return false;
1395 }
1396 }
1397
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398 if ((maybe_min || maybe_max) &&
1399 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001400 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401 << "'min' and 'max' can only be used when format='integer'");
1402 return false;
1403 }
1404
1405 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001406 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001407 return a.symbol.name.value() < b.symbol.name.value();
1408 }
1409 };
1410
1411 std::set<Attribute::Symbol, SymbolComparator> items;
1412
1413 std::string comment;
1414 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001415 const size_t depth = parser->depth();
1416 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1417 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001418 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001419 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 // Skip text.
1422 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001423 }
1424
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001425 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 const std::string& element_namespace = parser->element_namespace();
1427 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001428 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001429 if (element_name == "enum") {
1430 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001431 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001432 << "can not define an <enum>; already defined a <flag>");
1433 error = true;
1434 continue;
1435 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001436 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001437
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001438 } else if (element_name == "flag") {
1439 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001440 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441 << "can not define a <flag>; already defined an <enum>");
1442 error = true;
1443 continue;
1444 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001445 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001446 }
1447
Ryan Mitchell4382e442021-07-14 12:53:01 -07001448 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001449 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001450 ParsedResource child_resource;
1451 child_resource.name = symbol.symbol.name.value();
1452 child_resource.source = item_source;
1453 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001454 if (options_.visibility) {
1455 child_resource.visibility_level = options_.visibility.value();
1456 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001457 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001458
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 symbol.symbol.SetComment(std::move(comment));
1460 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001462 auto insert_result = items.insert(std::move(symbol));
1463 if (!insert_result.second) {
1464 const Attribute::Symbol& existing_symbol = *insert_result.first;
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001465 diag_->Error(android::DiagMessage(item_source)
1466 << "duplicate symbol '" << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001468 diag_->Note(android::DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001469 << "first defined here");
1470 error = true;
1471 }
1472 } else {
1473 error = true;
1474 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001475 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001476 diag_->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 error = true;
1478 }
1479
1480 comment = {};
1481 }
1482
1483 if (error) {
1484 return false;
1485 }
1486
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001487 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1488 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1489 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001491 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1492 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 return true;
1495}
1496
Ryan Mitchell4382e442021-07-14 12:53:01 -07001497std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001498 StringPiece tag) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001499 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500
Ryan Mitchell4382e442021-07-14 12:53:01 -07001501 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001503 diag_->Error(android::DiagMessage(source)
1504 << "no attribute 'name' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001505 return {};
1506 }
1507
Ryan Mitchell4382e442021-07-14 12:53:01 -07001508 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001509 if (!maybe_value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001510 diag_->Error(android::DiagMessage(source)
1511 << "no attribute 'value' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001512 return {};
1513 }
1514
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001515 std::u16string value16 = android::util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001516 android::Res_value val;
1517 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001518 diag_->Error(android::DiagMessage(source) << "invalid value '" << maybe_value.value()
1519 << "' for <" << tag << ">; must be an integer");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520 return {};
1521 }
1522
1523 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001524 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1525 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001526 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001527}
1528
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001530 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001531
Ryan Mitchell4382e442021-07-14 12:53:01 -07001532 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001533 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001534 diag_->Error(android::DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 return false;
1536 }
1537
Ryan Mitchell4382e442021-07-14 12:53:01 -07001538 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 if (!maybe_key) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001540 diag_->Error(android::DiagMessage(source)
1541 << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 return false;
1543 }
1544
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001545 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001546 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001548 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001549 if (!value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001550 diag_->Error(android::DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 return false;
1552 }
1553
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001554 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001555 return true;
1556}
1557
Adam Lesinski86d67df2017-01-31 13:47:27 -08001558bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001559 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001560 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561
1562 std::unique_ptr<Style> style = util::make_unique<Style>();
1563
Ryan Mitchell4382e442021-07-14 12:53:01 -07001564 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001565 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001566 // 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 -07001567 if (!maybe_parent.value().empty()) {
1568 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001569 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 if (!style->parent) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001571 diag_->Error(android::DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001572 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001573 }
1574
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001575 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001576 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001577 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001578 }
1579
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 } else {
1581 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 std::string style_name = out_resource->name.entry;
1583 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001584 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001586 style->parent = Reference(ResourceName(
1587 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588 }
1589 }
1590
1591 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001592 const size_t depth = parser->depth();
1593 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1594 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 // Skip text and comments.
1596 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001597 }
1598
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001599 const std::string& element_namespace = parser->element_namespace();
1600 const std::string& element_name = parser->element_name();
1601 if (element_namespace == "" && element_name == "item") {
1602 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001603
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001605 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001607 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001608 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001610
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 if (error) {
1612 return false;
1613 }
1614
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001615 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001617}
1618
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001619bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1620 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001621 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001622 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1623 if (resource_format == 0u) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001624 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001625 << "'" << format_attr.value() << "' is an invalid format");
1626 return false;
1627 }
1628 }
1629 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001630}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001631
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001632bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1633 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001634}
1635
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001636bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1637 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001638}
1639
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001640bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1641 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001642 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001643 out_resource->name.type =
1644 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001645
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001646 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001647
Adam Lesinski75421622017-01-06 15:20:04 -08001648 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001649 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1650 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001651 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001652 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 << "invalid value for 'translatable'. Must be a boolean");
1654 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001655 }
Adam Lesinski75421622017-01-06 15:20:04 -08001656 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 }
Adam Lesinski75421622017-01-06 15:20:04 -08001658 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001659
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001660 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001661 const size_t depth = parser->depth();
1662 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1663 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 // Skip text and comments.
1665 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001666 }
1667
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001668 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 const std::string& element_namespace = parser->element_namespace();
1670 const std::string& element_name = parser->element_name();
1671 if (element_namespace.empty() && element_name == "item") {
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -07001672 auto flag = GetFlag(parser);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001673 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 if (!item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001675 diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676 error = true;
1677 continue;
1678 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -07001679 item->SetFlag(flag);
1680 std::string err;
1681 auto status = GetFlagStatus(flag, options_.feature_flag_values, &err);
1682 if (status) {
1683 item->SetFlagStatus(status.value());
1684 } else {
1685 diag_->Error(android::DiagMessage(item_source) << err);
1686 error = true;
1687 continue;
1688 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001690 array->elements.emplace_back(std::move(item));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001691 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001692 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1693 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 error = true;
1695 }
1696 }
1697
1698 if (error) {
1699 return false;
1700 }
1701
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001702 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001704}
1705
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1707 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001708 out_resource->name.type =
1709 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001710
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001712
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 const size_t depth = parser->depth();
1715 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1716 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001717 // Skip text and comments.
1718 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001719 }
1720
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001721 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001722 const std::string& element_namespace = parser->element_namespace();
1723 const std::string& element_name = parser->element_name();
1724 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001725 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 if (!maybe_quantity) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001727 diag_->Error(android::DiagMessage(item_source) << "<item> in <plurals> requires attribute "
1728 << "'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729 error = true;
1730 continue;
1731 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001732
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001733 StringPiece trimmed_quantity =
1734 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001737 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001740 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001741 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001742 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001744 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001745 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001746 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001747 index = Plural::Other;
1748 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001749 diag_->Error(android::DiagMessage(item_source)
1750 << "<item> in <plural> has invalid value '" << trimmed_quantity
1751 << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001752 error = true;
1753 continue;
1754 }
1755
1756 if (plural->values[index]) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001757 diag_->Error(android::DiagMessage(item_source)
1758 << "duplicate quantity '" << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001759 error = true;
1760 continue;
1761 }
1762
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001763 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001764 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1765 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001766 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001767 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001768
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001769 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001771 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001772 diag_->Error(android::DiagMessage(item_source)
1773 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001774 error = true;
1775 }
1776 }
1777
1778 if (error) {
1779 return false;
1780 }
1781
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001783 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001784}
1785
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001786bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1787 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001788 out_resource->name.type =
1789 ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001790
Donald Chai94e4a012020-01-06 13:52:41 -08001791 if (!options_.preserve_visibility_of_styleables) {
1792 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1793 // knows exactly what for.
1794 //
1795 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1796 // package-private (to be used only by the view class); themes are a different story.
1797 out_resource->visibility_level = Visibility::Level::kPublic;
1798 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001799
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001802 diag_->Warn(android::DiagMessage(out_resource->source)
1803 << "ignoring configuration '" << out_resource->config << "' for styleable "
1804 << out_resource->name.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001805 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 }
1807
1808 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1809
1810 std::string comment;
1811 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001812 const size_t depth = parser->depth();
1813 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1814 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001815 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001816 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001817 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001818 // Ignore text.
1819 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001820 }
1821
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001822 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001823 const std::string& element_namespace = parser->element_namespace();
1824 const std::string& element_name = parser->element_name();
1825 if (element_namespace.empty() && element_name == "attr") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001826 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001827 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001828 diag_->Error(android::DiagMessage(item_source)
1829 << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001830 error = true;
1831 continue;
1832 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001833
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001834 // If this is a declaration, the package name may be in the name. Separate
1835 // these out.
1836 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001837 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001838 if (!maybe_ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001839 diag_->Error(android::DiagMessage(item_source)
1840 << "<attr> tag has invalid name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001841 error = true;
1842 continue;
1843 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001844
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001845 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001846 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001847
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001848 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001849 ParsedResource child_resource;
1850 child_resource.name = child_ref.name.value();
1851 child_resource.source = item_source;
1852 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001853 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001854 if (options_.visibility) {
1855 child_resource.visibility_level = options_.visibility.value();
1856 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001857
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001858 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001859 error = true;
1860 continue;
1861 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001862
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001863 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001864 child_ref.SetComment(child_resource.comment);
1865 child_ref.SetSource(item_source);
1866 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001867
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001868 // Do not add referenced attributes that do not define a format to the table.
1869 CHECK(child_resource.value != nullptr);
1870 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1871
1872 CHECK(attr != nullptr);
1873 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1874 out_resource->child_resources.push_back(std::move(child_resource));
1875 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001876
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001877 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001878 diag_->Error(android::DiagMessage(item_source)
1879 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001880 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001881 }
1882
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001883 comment = {};
1884 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001885
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001886 if (error) {
1887 return false;
1888 }
1889
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001890 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001891 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001892}
1893
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001894} // namespace aapt