blob: 773edc3e3c220dddffefa974dda1be803ea604c7 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 std::string resource_type = parser->element_name();
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700550 out_resource->flag = GetFlag(parser);
551 std::string error;
552 auto flag_status = GetFlagStatus(out_resource->flag, options_.feature_flag_values, &error);
553 if (flag_status) {
554 out_resource->flag_status = flag_status.value();
555 } else {
556 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number())) << error);
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700557 return false;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700558 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800559
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800562
Adam Lesinski86d67df2017-01-31 13:47:27 -0800563 bool can_be_item = true;
564 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800566 can_be_bag = false;
567
Adam Lesinskie597d682017-06-01 17:16:44 -0700568 // The default format for <item> is any. If a format attribute is present, that one will
569 // override the default.
570 resource_format = android::ResTable_map::TYPE_ANY;
571
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700573 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700574 resource_type = std::string(maybe_type.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000576 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 << "<item> must have a 'type' attribute");
578 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800579 }
580
Ryan Mitchell4382e442021-07-14 12:53:01 -0700581 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700583 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700585 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 if (!resource_format) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000587 diag_->Error(android::DiagMessage(out_resource->source)
588 << "'" << maybe_format.value() << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800589 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 }
591 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800592 } else if (resource_type == "bag") {
593 can_be_item = false;
594
595 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700596 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700597 resource_type = std::string(maybe_type.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800598 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000599 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski86d67df2017-01-31 13:47:27 -0800600 << "<bag> must have a 'type' attribute");
601 return false;
602 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 }
604
605 // Get the name of the resource. This will be checked later, because not all
606 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700607 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 if (resource_type == "id") {
610 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000611 diag_->Error(android::DiagMessage(out_resource->source)
612 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 return false;
614 }
615
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000616 out_resource->name.type =
617 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700618 out_resource->name.entry = std::string(maybe_name.value());
y9efbbef2018-04-18 11:29:09 -0700619
620 // Ids either represent a unique resource id or reference another resource id
621 auto item = ParseItem(parser, out_resource, resource_format);
622 if (!item) {
623 return false;
624 }
625
626 String* empty = ValueCast<String>(out_resource->value.get());
627 if (empty && *empty->value == "") {
628 // If no inner element exists, represent a unique identifier
629 out_resource->value = util::make_unique<Id>();
630 } else {
y9efbbef2018-04-18 11:29:09 -0700631 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700632 if (ref && !ref->name && !ref->id) {
633 // A null reference also means there is no inner element when ids are in the form:
634 // <id name="name"/>
635 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000636 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700637 // If an inner element exists, the inner element must be a reference to another resource id
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000638 diag_->Error(android::DiagMessage(out_resource->source)
639 << "<" << parser->element_name()
640 << "> inner element must either be a resource reference or empty");
y9efbbef2018-04-18 11:29:09 -0700641 return false;
642 }
643 }
644
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700646 } else if (resource_type == "macro") {
647 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000648 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700649 << "<" << parser->element_name() << "> missing 'name' attribute");
650 return false;
651 }
652
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000653 out_resource->name.type =
654 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700655 out_resource->name.entry = std::string(maybe_name.value());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700656 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 }
658
Adam Lesinski86d67df2017-01-31 13:47:27 -0800659 if (can_be_item) {
660 const auto item_iter = elToItemMap.find(resource_type);
661 if (item_iter != elToItemMap.end()) {
662 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663
Adam Lesinski86d67df2017-01-31 13:47:27 -0800664 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000665 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800666 << "<" << parser->element_name() << "> missing 'name' attribute");
667 return false;
668 }
669
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000670 out_resource->name.type =
671 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700672 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800673
Adam Lesinskie597d682017-06-01 17:16:44 -0700674 // Only use the implied format of the type when there is no explicit format.
675 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800676 resource_format = item_iter->second.format;
677 }
678
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700679 if (!ParseItem(parser, out_resource, resource_format)) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800680 return false;
681 }
682 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 }
685
686 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800687 if (can_be_bag) {
688 const auto bag_iter = elToBagMap.find(resource_type);
689 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700690 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700691 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700692 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800693 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000694 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800695 << "<" << parser->element_name() << "> missing 'name' attribute");
696 return false;
697 }
698
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700699 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800700 }
701
702 // Call the associated parse method. The type will be filled in by the
703 // parse func.
704 if (!bag_iter->second(this, parser, out_resource)) {
705 return false;
706 }
707 return true;
708 }
709 }
710
711 if (can_be_item) {
712 // Try parsing the elementName (or type) as a resource. These shall only be
713 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000714 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800715 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000717 diag_->Error(android::DiagMessage(out_resource->source)
718 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 return false;
720 }
721
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000722 out_resource->name.type = parsed_type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700723 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800724 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
725 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000726 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800727 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
728 return false;
729 }
730 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 }
733
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000734 // If the resource type was not recognized, write the error and return false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000735 diag_->Error(android::DiagMessage(out_resource->source)
736 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 return false;
738}
739
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700740std::optional<FeatureFlagAttribute> ResourceParser::GetFlag(xml::XmlPullParser* parser) {
741 auto name = xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag");
742 if (name) {
743 FeatureFlagAttribute flag;
744 if (name->starts_with('!')) {
745 flag.negated = true;
746 flag.name = name->substr(1);
747 } else {
748 flag.name = name.value();
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700749 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700750 return flag;
751 } else {
752 return {};
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700753 }
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700754}
755
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
757 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 const uint32_t format) {
759 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 out_resource->value = ParseXml(parser, format, kNoRawString);
764 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000765 diag_->Error(android::DiagMessage(out_resource->source)
766 << "invalid " << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 return false;
768 }
769 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800770}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800771
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700772std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
773 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000777 android::StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800778 std::vector<UntranslatableSection> untranslatable_sections;
779 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800780 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 }
782
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700783 return FlattenedXmlSubTree{.raw_value = raw_value,
784 .style_string = style_string,
785 .untranslatable_sections = untranslatable_sections,
786 .namespace_resolver = parser,
787 .source = source_.WithLine(begin_xml_line)};
788}
789
790/**
791 * Reads the entire XML subtree and attempts to parse it as some Item,
792 * with typeMask denoting which items it can be. If allowRawValue is
793 * true, a RawString is returned if the XML couldn't be parsed as
794 * an Item. If allowRawValue is false, nullptr is returned in this
795 * case.
796 */
797std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
798 const bool allow_raw_value) {
799 auto sub_tree = CreateFlattenSubTree(parser);
800 if (!sub_tree.has_value()) {
801 return {};
802 }
803 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
804}
805
806std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
807 const uint32_t type_mask, const bool allow_raw_value,
808 ResourceTable& table,
809 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000810 android::IDiagnostics& diag) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700811 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800813 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700814 util::make_unique<StyledString>(table.string_pool.MakeRef(
815 xmlsub_tree.style_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000816 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700817 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800818 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 }
820
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 // name.package can be empty here, as it will assume the package name of the
823 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700824 auto id = util::make_unique<Id>();
825 id->SetSource(xmlsub_tree.source);
826 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 };
828
829 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700830 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000831 &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700834 if (auto ref = ValueCast<Reference>(processed_item.get())) {
835 ref->allow_raw = allow_raw_value;
836 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 }
840
841 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700843 // Use the trimmed, escaped string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000844 std::unique_ptr<String> string = util::make_unique<String>(table.string_pool.MakeRef(
845 xmlsub_tree.style_string.str, android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700846 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800847 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 }
849
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700852 return util::make_unique<RawString>(table.string_pool.MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000853 util::TrimWhitespace(xmlsub_tree.raw_value), android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700854 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700855 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
856 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 }
858 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800859}
860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861bool ResourceParser::ParseString(xml::XmlPullParser* parser,
862 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700864 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
865 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700866 if (!maybe_formatted) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000867 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 << "invalid value for 'formatted'. Must be a boolean");
869 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800870 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800873
Adam Lesinski75421622017-01-06 15:20:04 -0800874 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700875 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
876 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800877 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000878 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 << "invalid value for 'translatable'. Must be a boolean");
880 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800881 }
Adam Lesinski75421622017-01-06 15:20:04 -0800882 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800884
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700885 out_resource->value =
886 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
887 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000888 diag_->Error(android::DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 return false;
890 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800893 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800894
Adam Lesinski75421622017-01-06 15:20:04 -0800895 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 if (!util::VerifyJavaStringFormat(*string_value->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000897 android::DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 msg << "multiple substitutions specified in non-positional format; "
899 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (options_.error_on_positional_arguments) {
901 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800903 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800904
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800907 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908
Adam Lesinski75421622017-01-06 15:20:04 -0800909 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
910 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911 }
912 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800913}
914
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700915bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
916 auto sub_tree = CreateFlattenSubTree(parser);
917 if (!sub_tree) {
918 return false;
919 }
920
921 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000922 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700923 << "<macro> tags cannot be declared in configurations other than the default "
924 "configuration'");
925 return false;
926 }
927
928 auto macro = std::make_unique<Macro>();
929 macro->raw_value = std::move(sub_tree->raw_value);
930 macro->style_string = std::move(sub_tree->style_string);
931 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
932
933 for (const auto& decl : parser->package_decls()) {
934 macro->alias_namespaces.emplace_back(
935 Macro::Namespace{.alias = decl.prefix,
936 .package_name = decl.package.package,
937 .is_private = decl.package.private_namespace});
938 }
939
940 out_resource->value = std::move(macro);
941 return true;
942}
943
Adam Lesinski71be7052017-12-12 16:48:07 -0800944bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100945 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000946 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100947 << "<public> tag not allowed with --visibility flag");
948 return false;
949 }
950
Adam Lesinski46c4d722017-08-23 13:03:56 -0700951 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000952 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -0700953 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
954 }
955
Ryan Mitchell4382e442021-07-14 12:53:01 -0700956 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000958 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 << "<public> must have a 'type' attribute");
960 return false;
961 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800962
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000963 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000965 diag_->Error(android::DiagMessage(out_resource->source)
966 << "invalid resource type '" << maybe_type.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 return false;
968 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800969
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000970 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800971
Ryan Mitchell4382e442021-07-14 12:53:01 -0700972 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
973 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000975 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800976 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800978 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800981
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000982 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700986
Adam Lesinski71be7052017-12-12 16:48:07 -0800987 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800989}
990
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700991template <typename Func>
992bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000993 const char* tag_name, android::IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700994 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000995 diag->Warn(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700996 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
997 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700998 }
999
Ryan Mitchell4382e442021-07-14 12:53:01 -07001000 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001002 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001003 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001004 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001006
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001007 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
1008 ParseResourceNamedType(maybe_type.value());
1009 if (!maybe_parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001010 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001011 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001012 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001013 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001014 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001015
Ryan Mitchell4382e442021-07-14 12:53:01 -07001016 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 if (!maybe_id_str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001018 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001019 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 return false;
1021 }
1022
Ryan Mitchell4382e442021-07-14 12:53:01 -07001023 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001024 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001025 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001026 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 return false;
1028 }
1029
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001031 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 const size_t depth = parser->depth();
1034 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1035 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001036 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 // Skip text.
1040 continue;
1041 }
1042
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001043 const android::Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001044 const std::string& element_namespace = parser->element_namespace();
1045 const std::string& element_name = parser->element_name();
1046 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001047 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001049 diag->Error(android::DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001050 error = true;
1051 continue;
1052 }
1053
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 if (xml::FindNonEmptyAttribute(parser, "id")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001055 diag->Error(android::DiagMessage(item_source)
1056 << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001057 error = true;
1058 continue;
1059 }
1060
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 if (xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001062 diag->Error(android::DiagMessage(item_source)
1063 << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001064 error = true;
1065 continue;
1066 }
1067
Winson Chiuc84829d2022-03-04 20:35:58 +00001068 if (maybe_name.value().substr(0, std::strlen("removed_")) == "removed_") {
1069 // Skip resources that have been removed from the framework, but leave a hole so that
1070 // other staged resources don't shift and break apps previously compiled against them
1071 next_id.id++;
1072 continue;
1073 }
1074
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001075 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001076 .name = ResourceName{{}, parsed_type, std::string(maybe_name.value())},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001077 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001078 .comment = std::move(comment),
1079 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001080 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001081
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001082 // Execute group specific code.
1083 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001085 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001087 diag->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088 error = true;
1089 }
1090 }
1091 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001092}
1093
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001094bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1095 ParsedResource* out_resource) {
1096 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1097 [](ParsedResource& parsed_entry, ResourceId id) {
1098 parsed_entry.id = id;
1099 parsed_entry.staged_api = true;
1100 parsed_entry.visibility_level = Visibility::Level::kPublic;
1101 });
1102}
1103
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001104bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1105 ParsedResource* out_resource) {
1106 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1107 [](ParsedResource& parsed_entry, ResourceId id) {
1108 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1109 });
1110}
1111
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001112bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1113 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001114 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001115 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1116 return false;
1117 }
1118
1119 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1120 [](ParsedResource& parsed_entry, ResourceId id) {
1121 parsed_entry.id = id;
1122 parsed_entry.visibility_level = Visibility::Level::kPublic;
1123 });
1124}
1125
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1127 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001128 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001129 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001130 diag_->Error(android::DiagMessage(out_resource->source)
1131 << "<" << parser->element_name() << "> must have a 'type' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 return false;
1133 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001134
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001135 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001137 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001138 << "invalid resource type '" << maybe_type.value() << "' in <"
1139 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001140 return false;
1141 }
1142
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001143 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001145}
1146
Adam Lesinski46c4d722017-08-23 13:03:56 -07001147bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001148 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001149 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001150 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1151 return false;
1152 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001153 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001154 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -07001155 << "ignoring configuration '" << out_resource->config << "' for <"
1156 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001158
1159 if (!ParseSymbolImpl(parser, out_resource)) {
1160 return false;
1161 }
1162
Adam Lesinski71be7052017-12-12 16:48:07 -08001163 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001164 return true;
1165}
1166
1167bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1168 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001169 diag_->Warn(android::DiagMessage(out_resource->source)
1170 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001171 }
1172
Ryan Mitchell4382e442021-07-14 12:53:01 -07001173 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001174 if (!overlayable_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001175 diag_->Error(android::DiagMessage(out_resource->source)
1176 << "<overlayable> tag must have a 'name' attribute");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001177 return false;
1178 }
1179
1180 const std::string kActorUriScheme =
1181 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001182 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001183 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001184 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001185 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1186 << Overlayable::kActorScheme << "'");
1187 return false;
1188 }
1189
1190 // Create a overlayable entry grouping that represents this <overlayable>
1191 auto overlayable = std::make_shared<Overlayable>(
1192 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001193 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001194
Adam Lesinski46c4d722017-08-23 13:03:56 -07001195 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001196 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001197 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001198 const size_t start_depth = parser->depth();
1199 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1200 xml::XmlPullParser::Event event = parser->event();
1201 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001202 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001203 break;
1204 } else if (event == xml::XmlPullParser::Event::kEndElement
1205 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001206 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001207 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001208 continue;
1209 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001210 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001211 comment = parser->comment();
1212 continue;
1213 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001214 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001215 continue;
1216 }
1217
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001218 const android::Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001219 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001220 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001221 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001222 if (current_policies == PolicyFlags::NONE) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001223 diag_->Error(android::DiagMessage(element_source)
1224 << "<item> within an <overlayable> must be inside a <policy> block");
Winsonb2d7f532019-02-04 16:32:43 -08001225 error = true;
1226 continue;
1227 }
1228
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001229 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001230 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001231 if (!item_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001232 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001233 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001234 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001235 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001236 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001237
Ryan Mitchell4382e442021-07-14 12:53:01 -07001238 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001239 if (!item_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001240 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001241 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001242 error = true;
1243 continue;
1244 }
1245
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001246 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1247 if (!type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001248 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001249 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001250 << "' in <item> within an <overlayable>");
1251 error = true;
1252 continue;
1253 }
1254
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001255 OverlayableItem overlayable_item(overlayable);
1256 overlayable_item.policies = current_policies;
1257 overlayable_item.comment = comment;
1258 overlayable_item.source = element_source;
1259
1260 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001261 child_resource.name.type = type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001262 child_resource.name.entry = std::string(item_name.value());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001263 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001264 out_resource->child_resources.push_back(std::move(child_resource));
1265
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001266 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001267 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001268 // If the policy list is not empty, then we are currently inside a policy element
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001269 diag_->Error(android::DiagMessage(element_source)
1270 << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001271 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001272 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001273 } else if (std::optional<StringPiece> maybe_type =
1274 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001275 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001276 // policies. Items within the policy tag will have the specified policy.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001277 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001278 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001279 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1280 kPolicyStringToFlag.end(),
1281 [trimmed_part](const auto& it) {
1282 return trimmed_part == it.first;
1283 });
1284 if (policy == kPolicyStringToFlag.end()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001285 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001286 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001287 error = true;
1288 continue;
1289 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001290
1291 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001292 }
Winsonb2d7f532019-02-04 16:32:43 -08001293 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001294 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001295 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001296 error = true;
1297 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001298 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001299 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001300 diag_->Error(android::DiagMessage(element_source)
1301 << "invalid element <" << element_name << "> "
1302 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001303 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001304 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001305 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001306
1307 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001308 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001309
Adam Lesinski46c4d722017-08-23 13:03:56 -07001310 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001311}
1312
Adam Lesinski71be7052017-12-12 16:48:07 -08001313bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001314 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001315 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001316 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 return true;
1318 }
1319 return false;
1320}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001321
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001322bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1323 ParsedResource* out_resource) {
1324 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325}
1326
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1328 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001329 out_resource->name.type =
1330 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331
1332 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001333 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001334 diag_->Warn(android::DiagMessage(out_resource->source)
1335 << "ignoring configuration '" << out_resource->config << "' for attribute "
1336 << out_resource->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 }
1339
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341
Ryan Mitchell4382e442021-07-14 12:53:01 -07001342 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 if (maybe_format) {
1344 type_mask = ParseFormatAttribute(maybe_format.value());
1345 if (type_mask == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001346 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001347 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348 return false;
1349 }
1350 }
1351
Ryan Mitchell4382e442021-07-14 12:53:01 -07001352 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353
Ryan Mitchell4382e442021-07-14 12:53:01 -07001354 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1356 if (!min_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001357 std::u16string min_str16 = android::util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001359 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001362 }
1363
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 if (!maybe_min) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001365 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001367 return false;
1368 }
1369 }
1370
Ryan Mitchell4382e442021-07-14 12:53:01 -07001371 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1373 if (!max_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001374 std::u16string max_str16 = android::util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001376 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001379 }
1380
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 if (!maybe_max) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001382 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001383 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001384 return false;
1385 }
1386 }
1387
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 if ((maybe_min || maybe_max) &&
1389 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001390 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 << "'min' and 'max' can only be used when format='integer'");
1392 return false;
1393 }
1394
1395 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001396 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 return a.symbol.name.value() < b.symbol.name.value();
1398 }
1399 };
1400
1401 std::set<Attribute::Symbol, SymbolComparator> items;
1402
1403 std::string comment;
1404 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001405 const size_t depth = parser->depth();
1406 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1407 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001408 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 // Skip text.
1412 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001413 }
1414
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001415 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 const std::string& element_namespace = parser->element_namespace();
1417 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001418 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 if (element_name == "enum") {
1420 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001421 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422 << "can not define an <enum>; already defined a <flag>");
1423 error = true;
1424 continue;
1425 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001427
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001428 } else if (element_name == "flag") {
1429 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001430 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431 << "can not define a <flag>; already defined an <enum>");
1432 error = true;
1433 continue;
1434 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001435 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436 }
1437
Ryan Mitchell4382e442021-07-14 12:53:01 -07001438 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001440 ParsedResource child_resource;
1441 child_resource.name = symbol.symbol.name.value();
1442 child_resource.source = item_source;
1443 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001444 if (options_.visibility) {
1445 child_resource.visibility_level = options_.visibility.value();
1446 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001447 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 symbol.symbol.SetComment(std::move(comment));
1450 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 auto insert_result = items.insert(std::move(symbol));
1453 if (!insert_result.second) {
1454 const Attribute::Symbol& existing_symbol = *insert_result.first;
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001455 diag_->Error(android::DiagMessage(item_source)
1456 << "duplicate symbol '" << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001458 diag_->Note(android::DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 << "first defined here");
1460 error = true;
1461 }
1462 } else {
1463 error = true;
1464 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001466 diag_->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467 error = true;
1468 }
1469
1470 comment = {};
1471 }
1472
1473 if (error) {
1474 return false;
1475 }
1476
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001477 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1478 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1479 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001480 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001481 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1482 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001483 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001484 return true;
1485}
1486
Ryan Mitchell4382e442021-07-14 12:53:01 -07001487std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001488 StringPiece tag) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001489 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490
Ryan Mitchell4382e442021-07-14 12:53:01 -07001491 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001492 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001493 diag_->Error(android::DiagMessage(source)
1494 << "no attribute 'name' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001495 return {};
1496 }
1497
Ryan Mitchell4382e442021-07-14 12:53:01 -07001498 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 if (!maybe_value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001500 diag_->Error(android::DiagMessage(source)
1501 << "no attribute 'value' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return {};
1503 }
1504
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001505 std::u16string value16 = android::util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001506 android::Res_value val;
1507 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001508 diag_->Error(android::DiagMessage(source) << "invalid value '" << maybe_value.value()
1509 << "' for <" << tag << ">; must be an integer");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 return {};
1511 }
1512
1513 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001514 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1515 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001516 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001517}
1518
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001520 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001521
Ryan Mitchell4382e442021-07-14 12:53:01 -07001522 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001524 diag_->Error(android::DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001525 return false;
1526 }
1527
Ryan Mitchell4382e442021-07-14 12:53:01 -07001528 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529 if (!maybe_key) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001530 diag_->Error(android::DiagMessage(source)
1531 << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 return false;
1533 }
1534
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001535 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001537
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 if (!value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001540 diag_->Error(android::DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 return false;
1542 }
1543
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001544 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 return true;
1546}
1547
Adam Lesinski86d67df2017-01-31 13:47:27 -08001548bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001550 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551
1552 std::unique_ptr<Style> style = util::make_unique<Style>();
1553
Ryan Mitchell4382e442021-07-14 12:53:01 -07001554 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001556 // If the parent is empty, we don't have a parent, but we also don't infer either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 if (!maybe_parent.value().empty()) {
1558 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001559 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 if (!style->parent) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001561 diag_->Error(android::DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001562 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001563 }
1564
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001565 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001567 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001568 }
1569
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 } else {
1571 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001572 std::string style_name = out_resource->name.entry;
1573 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001576 style->parent = Reference(ResourceName(
1577 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578 }
1579 }
1580
1581 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 const size_t depth = parser->depth();
1583 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1584 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 // Skip text and comments.
1586 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001587 }
1588
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 const std::string& element_namespace = parser->element_namespace();
1590 const std::string& element_name = parser->element_name();
1591 if (element_namespace == "" && element_name == "item") {
1592 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001593
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001595 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001598 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001600
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 if (error) {
1602 return false;
1603 }
1604
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001605 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001606 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001607}
1608
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001609bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1610 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001611 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001612 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1613 if (resource_format == 0u) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001614 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001615 << "'" << format_attr.value() << "' is an invalid format");
1616 return false;
1617 }
1618 }
1619 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001620}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001621
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001622bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1623 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001624}
1625
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001626bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1627 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001628}
1629
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001630bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1631 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001632 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001633 out_resource->name.type =
1634 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001635
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001636 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001637
Adam Lesinski75421622017-01-06 15:20:04 -08001638 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001639 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1640 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001641 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001642 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 << "invalid value for 'translatable'. Must be a boolean");
1644 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001645 }
Adam Lesinski75421622017-01-06 15:20:04 -08001646 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 }
Adam Lesinski75421622017-01-06 15:20:04 -08001648 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001649
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001650 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001651 const size_t depth = parser->depth();
1652 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1653 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001654 // Skip text and comments.
1655 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001656 }
1657
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001658 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001659 const std::string& element_namespace = parser->element_namespace();
1660 const std::string& element_name = parser->element_name();
1661 if (element_namespace.empty() && element_name == "item") {
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -07001662 auto flag = GetFlag(parser);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 if (!item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001665 diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 error = true;
1667 continue;
1668 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -07001669 item->SetFlag(flag);
1670 std::string err;
1671 auto status = GetFlagStatus(flag, options_.feature_flag_values, &err);
1672 if (status) {
1673 item->SetFlagStatus(status.value());
1674 } else {
1675 diag_->Error(android::DiagMessage(item_source) << err);
1676 error = true;
1677 continue;
1678 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001680 array->elements.emplace_back(std::move(item));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001681 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001682 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1683 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001684 error = true;
1685 }
1686 }
1687
1688 if (error) {
1689 return false;
1690 }
1691
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001692 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001693 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001694}
1695
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1697 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001698 out_resource->name.type =
1699 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001700
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001702
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001704 const size_t depth = parser->depth();
1705 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1706 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001707 // Skip text and comments.
1708 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001709 }
1710
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001711 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001712 const std::string& element_namespace = parser->element_namespace();
1713 const std::string& element_name = parser->element_name();
1714 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001715 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001716 if (!maybe_quantity) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001717 diag_->Error(android::DiagMessage(item_source) << "<item> in <plurals> requires attribute "
1718 << "'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001719 error = true;
1720 continue;
1721 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001722
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001723 StringPiece trimmed_quantity =
1724 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001726 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001727 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001728 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001730 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001731 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001732 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001733 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001737 index = Plural::Other;
1738 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001739 diag_->Error(android::DiagMessage(item_source)
1740 << "<item> in <plural> has invalid value '" << trimmed_quantity
1741 << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742 error = true;
1743 continue;
1744 }
1745
1746 if (plural->values[index]) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001747 diag_->Error(android::DiagMessage(item_source)
1748 << "duplicate quantity '" << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001749 error = true;
1750 continue;
1751 }
1752
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001753 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1755 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001756 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001757 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001758
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001759 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001760
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001761 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001762 diag_->Error(android::DiagMessage(item_source)
1763 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001764 error = true;
1765 }
1766 }
1767
1768 if (error) {
1769 return false;
1770 }
1771
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001772 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001773 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001774}
1775
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1777 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001778 out_resource->name.type =
1779 ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001780
Donald Chai94e4a012020-01-06 13:52:41 -08001781 if (!options_.preserve_visibility_of_styleables) {
1782 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1783 // knows exactly what for.
1784 //
1785 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1786 // package-private (to be used only by the view class); themes are a different story.
1787 out_resource->visibility_level = Visibility::Level::kPublic;
1788 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001789
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001790 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001791 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001792 diag_->Warn(android::DiagMessage(out_resource->source)
1793 << "ignoring configuration '" << out_resource->config << "' for styleable "
1794 << out_resource->name.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001795 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001796 }
1797
1798 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1799
1800 std::string comment;
1801 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001802 const size_t depth = parser->depth();
1803 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1804 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001805 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001806 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001807 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001808 // Ignore text.
1809 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001810 }
1811
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001812 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001813 const std::string& element_namespace = parser->element_namespace();
1814 const std::string& element_name = parser->element_name();
1815 if (element_namespace.empty() && element_name == "attr") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001816 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001817 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001818 diag_->Error(android::DiagMessage(item_source)
1819 << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001820 error = true;
1821 continue;
1822 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001823
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001824 // If this is a declaration, the package name may be in the name. Separate
1825 // these out.
1826 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001827 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001828 if (!maybe_ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001829 diag_->Error(android::DiagMessage(item_source)
1830 << "<attr> tag has invalid name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001831 error = true;
1832 continue;
1833 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001834
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001835 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001836 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001837
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001838 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001839 ParsedResource child_resource;
1840 child_resource.name = child_ref.name.value();
1841 child_resource.source = item_source;
1842 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001843 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001844 if (options_.visibility) {
1845 child_resource.visibility_level = options_.visibility.value();
1846 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001847
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001848 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001849 error = true;
1850 continue;
1851 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001852
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001853 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001854 child_ref.SetComment(child_resource.comment);
1855 child_ref.SetSource(item_source);
1856 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001857
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001858 // Do not add referenced attributes that do not define a format to the table.
1859 CHECK(child_resource.value != nullptr);
1860 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1861
1862 CHECK(attr != nullptr);
1863 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1864 out_resource->child_resources.push_back(std::move(child_resource));
1865 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001866
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001867 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001868 diag_->Error(android::DiagMessage(item_source)
1869 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001870 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001871 }
1872
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001873 comment = {};
1874 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001875
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001876 if (error) {
1877 return false;
1878 }
1879
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001880 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001881 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001882}
1883
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001884} // namespace aapt