blob: fb576df248be4e353fae8584de544196e4e3f9cb [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;
Jeremy Meyerfe92e8d2024-10-01 13:59:35 -0700113 FlagStatus flag_status = FlagStatus::NoFlag;
Adam Lesinski71be7052017-12-12 16:48:07 -0800114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 std::string comment;
116 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800118};
119
120// Recursively adds resources to the ResourceTable.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000121static bool AddResourcesToTable(ResourceTable* table, android::IDiagnostics* diag,
122 ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
124 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 // Only if there was a change do we re-assign.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700126 res->comment = std::string(trimmed_comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 }
Adam Lesinski76565542016-03-10 21:55:04 -0800128
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700129 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800130 if (res->visibility_level != Visibility::Level::kUndefined) {
131 Visibility visibility;
132 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700133 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800134 visibility.source = res->source;
135 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700136 res_builder.SetVisibility(visibility);
137 }
138
139 if (res->id.is_valid()) {
140 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800142
Adam Lesinski71be7052017-12-12 16:48:07 -0800143 if (res->allow_new) {
144 AllowNew allow_new;
145 allow_new.source = res->source;
146 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700147 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800148 }
149
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800150 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700151 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800152 }
153
154 if (res->value != nullptr) {
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -0700155 res->value->SetFlag(res->flag);
Jeremy Meyerd52bd682024-08-14 11:16:58 -0700156 res->value->SetFlagStatus(res->flag_status);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 res->value->SetComment(std::move(res->comment));
159 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700160 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800162
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700163 if (res->staged_alias) {
164 res_builder.SetStagedId(res->staged_alias.value());
165 }
166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700168 if (!res->name.entry.empty()) {
169 if (!table->AddResource(res_builder.Build(), diag)) {
170 return false;
171 }
172 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 for (ParsedResource& child : res->child_resources) {
174 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 }
176 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800177}
178
179// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800181
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000182ResourceParser::ResourceParser(android::IDiagnostics* diag, ResourceTable* table,
183 const android::Source& source, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 const ResourceParserOptions& options)
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000185 : diag_(diag), table_(table), source_(source), config_(config), options_(options) {
186}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800188// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
189// This will be used to traverse and flatten the XML string into a single std::string, with all
190// Span and Untranslatable data maintained in parallel, as indices into the string.
191class Node {
192 public:
193 virtual ~Node() = default;
194
195 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
196 // parent node as well.
197 // Returns a pointer to the child node that was added as a convenience.
198 template <typename T>
199 T* AddChild(std::unique_ptr<T> node) {
200 T* raw_ptr = node.get();
201 children.push_back(std::move(node));
202 return raw_ptr;
203 }
204
205 virtual void Build(StringBuilder* builder) const {
206 for (const auto& child : children) {
207 child->Build(builder);
208 }
209 }
210
211 std::vector<std::unique_ptr<Node>> children;
212};
213
214// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
215class SegmentNode : public Node {
216 public:
217 std::string data;
218
219 void Build(StringBuilder* builder) const override {
220 builder->AppendText(data);
221 }
222};
223
224// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
225class SpanNode : public Node {
226 public:
227 std::string name;
228
229 void Build(StringBuilder* builder) const override {
230 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
231 Node::Build(builder);
232 builder->EndSpan(span_handle);
233 }
234};
235
236// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
237class UntranslatableNode : public Node {
238 public:
239 void Build(StringBuilder* builder) const override {
240 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
241 Node::Build(builder);
242 builder->EndUntranslatable(handle);
243 }
244};
245
246// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800247bool ResourceParser::FlattenXmlSubtree(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000248 xml::XmlPullParser* parser, std::string* out_raw_string, android::StyleString* out_style_string,
Adam Lesinski75421622017-01-06 15:20:04 -0800249 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800250 std::string raw_string;
251 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800252
253 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700254 std::optional<size_t> untranslatable_start_depth;
Adam Lesinski75421622017-01-06 15:20:04 -0800255
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800256 Node root;
257 std::vector<Node*> node_stack;
258 node_stack.push_back(&root);
259
260 bool saw_span_node = false;
261 SegmentNode* first_segment = nullptr;
262 SegmentNode* last_segment = nullptr;
263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800265 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800267
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800270 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800271 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800272 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800273 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700274
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800275 last_segment = node_stack.back()->AddChild(std::move(segment_node));
276 if (first_segment == nullptr) {
277 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800278 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800279 current_text = {};
280 }
281 }
Adam Lesinski75421622017-01-06 15:20:04 -0800282
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800283 switch (event) {
284 case xml::XmlPullParser::Event::kText: {
285 current_text += parser->text();
286 raw_string += parser->text();
287 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800288
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289 case xml::XmlPullParser::Event::kStartElement: {
290 if (parser->element_namespace().empty()) {
291 // This is an HTML tag which we encode as a span. Add it to the span stack.
292 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
293 span_node->name = parser->element_name();
294 const auto end_attr_iter = parser->end_attributes();
295 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
296 ++attr_iter) {
297 span_node->name += ";";
298 span_node->name += attr_iter->name;
299 span_node->name += "=";
300 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800301 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800302
303 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
304 saw_span_node = true;
305 } else if (parser->element_namespace() == sXliffNamespaceUri) {
306 // This is an XLIFF tag, which is not encoded as a span.
307 if (parser->element_name() == "g") {
308 // Check that an 'untranslatable' tag is not already being processed. Nested
309 // <xliff:g> tags are illegal.
310 if (untranslatable_start_depth) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000311 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800312 << "illegal nested XLIFF 'g' tag");
313 return false;
314 } else {
315 // Mark the beginning of an 'untranslatable' section.
316 untranslatable_start_depth = depth;
317 node_stack.push_back(
318 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
319 }
320 } else {
321 // Ignore unknown XLIFF tags, but don't warn.
322 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
323 }
324 } else {
325 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000326 diag_->Warn(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800327 << "ignoring element '" << parser->element_name()
328 << "' with unknown namespace '" << parser->element_namespace() << "'");
329 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800330 }
Adam Lesinski75421622017-01-06 15:20:04 -0800331
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800332 // Enter one level inside the element.
333 depth++;
334 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800336 case xml::XmlPullParser::Event::kEndElement: {
337 // Return one level from within the element.
338 depth--;
339 if (depth == 0) {
340 break;
341 }
342
343 node_stack.pop_back();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700344 if (untranslatable_start_depth == depth) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800345 // This is the end of an untranslatable section.
346 untranslatable_start_depth = {};
347 }
348 } break;
349
350 default:
351 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
354 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700356 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800357 CHECK(node_stack.size() == 1u);
358 CHECK(node_stack.back() == &root);
359
360 if (!saw_span_node) {
361 // If there were no spans, we must treat this string a little differently (according to AAPT).
362 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
363 // from the last segment.
364 if (first_segment != nullptr) {
365 // Trim leading whitespace.
366 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
367 if (trimmed.size() != first_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700368 first_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800369 }
370 }
371
372 if (last_segment != nullptr) {
373 // Trim trailing whitespace.
374 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
375 if (trimmed.size() != last_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700376 last_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800377 }
378 }
379 }
380
381 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
382 // care of recording the correctly adjusted Spans and UntranslatableSections.
383 StringBuilder builder;
384 root.Build(&builder);
385 if (!builder) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000386 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
387 << builder.GetError());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800388 return false;
389 }
390
391 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
392 *out_raw_string = std::move(raw_string);
393 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
394 out_style_string->str = std::move(flattened_string.text);
395 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800396 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800397}
398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 const size_t depth = parser->depth();
402 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
403 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 // Skip comments and text.
405 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407
Adam Lesinski060b53d2017-07-28 17:10:35 -0700408 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000409 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 << "root element must be <resources>");
411 return false;
412 }
413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 break;
416 };
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000419 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 return false;
422 }
423 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800424}
425
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
427 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700428
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 bool error = false;
430 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 const size_t depth = parser->depth();
432 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
433 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800437 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700438
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 if (!util::TrimWhitespace(parser->text()).empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000441 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 << "plain text not allowed here");
443 error = true;
444 }
445 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700446 }
447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 // Skip unknown namespace.
452 continue;
453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 std::string element_name = parser->element_name();
456 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 comment = "";
458 continue;
459 }
460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 ParsedResource parsed_resource;
462 parsed_resource.config = config_;
463 parsed_resource.source = source_.WithLine(parser->line_number());
464 parsed_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +0800465 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100466 if (options_.visibility) {
467 parsed_resource.visibility_level = options_.visibility.value();
468 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469
470 // Extract the product name if it exists.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700471 if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700472 parsed_resource.product = std::string(maybe_product.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
475 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 error = true;
478 continue;
479 }
480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 error = true;
483 }
484 }
485
486 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 for (const ResourceName& stripped_resource : stripped_resources) {
488 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 // Failed to find the resource.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000490 diag_->Error(android::DiagMessage(source_)
491 << "resource '" << stripped_resource
492 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 error = true;
494 }
495 }
496
497 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800498}
499
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
501 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 struct ItemTypeFormat {
503 ResourceType type;
504 uint32_t format;
505 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800506
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
508 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800509
Adam Lesinski86d67df2017-01-31 13:47:27 -0800510 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
511 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
512 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
513 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
514 {"dimen",
515 {ResourceType::kDimen,
516 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
517 android::ResTable_map::TYPE_DIMENSION}},
518 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
519 {"fraction",
520 {ResourceType::kFraction,
521 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
522 android::ResTable_map::TYPE_DIMENSION}},
523 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
524 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
525 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800526
Adam Lesinski86d67df2017-01-31 13:47:27 -0800527 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
528 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
529 {"array", std::mem_fn(&ResourceParser::ParseArray)},
530 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
531 {"configVarying",
532 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
533 std::placeholders::_2, std::placeholders::_3)},
534 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
535 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
536 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700537 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800538 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
539 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
540 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700541 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700542 {"staging-public-group-final", std::mem_fn(&ResourceParser::ParseStagingPublicGroupFinal)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800543 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
544 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
545 std::placeholders::_2, std::placeholders::_3)},
546 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
547 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800548
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700549 std::string_view resource_type = parser->element_name();
Jeremy Meyere08e0372024-09-17 12:45:26 -0700550 if (auto flag = ParseFlag(xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag"))) {
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700551 if (options_.flag) {
552 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
553 << "Resource flag are not allowed both in the path and in the file");
554 return false;
555 }
556 out_resource->flag = std::move(flag);
557 std::string error;
558 auto flag_status = GetFlagStatus(out_resource->flag, options_.feature_flag_values, &error);
559 if (flag_status) {
560 out_resource->flag_status = flag_status.value();
561 } else {
562 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number())) << error);
563 return false;
564 }
565 } else if (options_.flag) {
566 out_resource->flag = options_.flag;
567 out_resource->flag_status = options_.flag_status;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700568 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800572
Adam Lesinski86d67df2017-01-31 13:47:27 -0800573 bool can_be_item = true;
574 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800576 can_be_bag = false;
577
Adam Lesinskie597d682017-06-01 17:16:44 -0700578 // The default format for <item> is any. If a format attribute is present, that one will
579 // override the default.
580 resource_format = android::ResTable_map::TYPE_ANY;
581
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700583 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700584 resource_type = maybe_type.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000586 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 << "<item> must have a 'type' attribute");
588 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800589 }
590
Ryan Mitchell4382e442021-07-14 12:53:01 -0700591 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700593 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700595 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 if (!resource_format) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000597 diag_->Error(android::DiagMessage(out_resource->source)
598 << "'" << maybe_format.value() << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800599 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
601 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800602 } else if (resource_type == "bag") {
603 can_be_item = false;
604
605 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700606 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700607 resource_type = maybe_type.value();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800608 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000609 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski86d67df2017-01-31 13:47:27 -0800610 << "<bag> must have a 'type' attribute");
611 return false;
612 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 }
614
615 // Get the name of the resource. This will be checked later, because not all
616 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700617 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 if (resource_type == "id") {
620 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000621 diag_->Error(android::DiagMessage(out_resource->source)
622 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 return false;
624 }
625
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000626 out_resource->name.type =
627 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700628 out_resource->name.entry = std::string(maybe_name.value());
y9efbbef2018-04-18 11:29:09 -0700629
630 // Ids either represent a unique resource id or reference another resource id
631 auto item = ParseItem(parser, out_resource, resource_format);
632 if (!item) {
633 return false;
634 }
635
636 String* empty = ValueCast<String>(out_resource->value.get());
637 if (empty && *empty->value == "") {
638 // If no inner element exists, represent a unique identifier
639 out_resource->value = util::make_unique<Id>();
640 } else {
y9efbbef2018-04-18 11:29:09 -0700641 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700642 if (ref && !ref->name && !ref->id) {
643 // A null reference also means there is no inner element when ids are in the form:
644 // <id name="name"/>
645 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000646 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700647 // If an inner element exists, the inner element must be a reference to another resource id
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000648 diag_->Error(android::DiagMessage(out_resource->source)
649 << "<" << parser->element_name()
650 << "> inner element must either be a resource reference or empty");
y9efbbef2018-04-18 11:29:09 -0700651 return false;
652 }
653 }
654
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700656 } else if (resource_type == "macro") {
657 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000658 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700659 << "<" << parser->element_name() << "> missing 'name' attribute");
660 return false;
661 }
662
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000663 out_resource->name.type =
664 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700665 out_resource->name.entry = std::string(maybe_name.value());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700666 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 }
668
Adam Lesinski86d67df2017-01-31 13:47:27 -0800669 if (can_be_item) {
670 const auto item_iter = elToItemMap.find(resource_type);
671 if (item_iter != elToItemMap.end()) {
672 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673
Adam Lesinski86d67df2017-01-31 13:47:27 -0800674 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000675 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800676 << "<" << parser->element_name() << "> missing 'name' attribute");
677 return false;
678 }
679
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000680 out_resource->name.type =
681 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700682 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800683
Adam Lesinskie597d682017-06-01 17:16:44 -0700684 // Only use the implied format of the type when there is no explicit format.
685 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800686 resource_format = item_iter->second.format;
687 }
688
Jeremy Meyer16c83af2024-07-26 16:21:49 -0700689 if (!ParseItem(parser, out_resource, resource_format)) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800690 return false;
691 }
692 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 }
695
696 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800697 if (can_be_bag) {
698 const auto bag_iter = elToBagMap.find(resource_type);
699 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700700 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700701 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700702 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800703 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000704 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800705 << "<" << parser->element_name() << "> missing 'name' attribute");
706 return false;
707 }
708
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700709 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800710 }
711
712 // Call the associated parse method. The type will be filled in by the
713 // parse func.
714 if (!bag_iter->second(this, parser, out_resource)) {
715 return false;
716 }
717 return true;
718 }
719 }
720
721 if (can_be_item) {
722 // Try parsing the elementName (or type) as a resource. These shall only be
723 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000724 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800725 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000727 diag_->Error(android::DiagMessage(out_resource->source)
728 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 return false;
730 }
731
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000732 out_resource->name.type = parsed_type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700733 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800734 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
735 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000736 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800737 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
738 return false;
739 }
740 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
743
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000744 // If the resource type was not recognized, write the error and return false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000745 diag_->Error(android::DiagMessage(out_resource->source)
746 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 return false;
748}
749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
751 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 const uint32_t format) {
753 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 out_resource->value = ParseXml(parser, format, kNoRawString);
758 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000759 diag_->Error(android::DiagMessage(out_resource->source)
760 << "invalid " << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 return false;
762 }
763 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800764}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800765
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700766std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
767 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800769
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000771 android::StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800772 std::vector<UntranslatableSection> untranslatable_sections;
773 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800774 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
776
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700777 return FlattenedXmlSubTree{.raw_value = raw_value,
778 .style_string = style_string,
779 .untranslatable_sections = untranslatable_sections,
780 .namespace_resolver = parser,
781 .source = source_.WithLine(begin_xml_line)};
782}
783
784/**
785 * Reads the entire XML subtree and attempts to parse it as some Item,
786 * with typeMask denoting which items it can be. If allowRawValue is
787 * true, a RawString is returned if the XML couldn't be parsed as
788 * an Item. If allowRawValue is false, nullptr is returned in this
789 * case.
790 */
791std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
792 const bool allow_raw_value) {
793 auto sub_tree = CreateFlattenSubTree(parser);
794 if (!sub_tree.has_value()) {
795 return {};
796 }
797 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
798}
799
800std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
801 const uint32_t type_mask, const bool allow_raw_value,
802 ResourceTable& table,
803 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000804 android::IDiagnostics& diag) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700805 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800807 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700808 util::make_unique<StyledString>(table.string_pool.MakeRef(
809 xmlsub_tree.style_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000810 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700811 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800812 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 }
814
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 // name.package can be empty here, as it will assume the package name of the
817 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700818 auto id = util::make_unique<Id>();
819 id->SetSource(xmlsub_tree.source);
820 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 };
822
823 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700824 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000825 &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700828 if (auto ref = ValueCast<Reference>(processed_item.get())) {
829 ref->allow_raw = allow_raw_value;
830 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
834
835 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 // Use the trimmed, escaped string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000838 std::unique_ptr<String> string = util::make_unique<String>(table.string_pool.MakeRef(
839 xmlsub_tree.style_string.str, android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700840 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800841 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 }
843
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700845 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700846 return util::make_unique<RawString>(table.string_pool.MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000847 util::TrimWhitespace(xmlsub_tree.raw_value), android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700848 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700849 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
850 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 }
852 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800853}
854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855bool ResourceParser::ParseString(xml::XmlPullParser* parser,
856 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700858 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
859 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700860 if (!maybe_formatted) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000861 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700862 << "invalid value for 'formatted'. Must be a boolean");
863 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800864 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800867
Adam Lesinski75421622017-01-06 15:20:04 -0800868 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700869 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
870 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800871 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000872 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 << "invalid value for 'translatable'. Must be a boolean");
874 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800875 }
Adam Lesinski75421622017-01-06 15:20:04 -0800876 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800878
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 out_resource->value =
880 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
881 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000882 diag_->Error(android::DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 return false;
884 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800885
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700886 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800887 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800888
Adam Lesinski75421622017-01-06 15:20:04 -0800889 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700890 if (!util::VerifyJavaStringFormat(*string_value->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000891 android::DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892 msg << "multiple substitutions specified in non-positional format; "
893 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 if (options_.error_on_positional_arguments) {
895 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700896 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800897 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800898
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700900 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800901 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902
Adam Lesinski75421622017-01-06 15:20:04 -0800903 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
904 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 }
906 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800907}
908
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700909bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
910 auto sub_tree = CreateFlattenSubTree(parser);
911 if (!sub_tree) {
912 return false;
913 }
914
915 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000916 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700917 << "<macro> tags cannot be declared in configurations other than the default "
918 "configuration'");
919 return false;
920 }
921
922 auto macro = std::make_unique<Macro>();
923 macro->raw_value = std::move(sub_tree->raw_value);
924 macro->style_string = std::move(sub_tree->style_string);
925 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
926
927 for (const auto& decl : parser->package_decls()) {
928 macro->alias_namespaces.emplace_back(
929 Macro::Namespace{.alias = decl.prefix,
930 .package_name = decl.package.package,
931 .is_private = decl.package.private_namespace});
932 }
933
934 out_resource->value = std::move(macro);
935 return true;
936}
937
Adam Lesinski71be7052017-12-12 16:48:07 -0800938bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100939 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000940 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100941 << "<public> tag not allowed with --visibility flag");
942 return false;
943 }
944
Adam Lesinski46c4d722017-08-23 13:03:56 -0700945 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000946 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -0700947 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
948 }
949
Ryan Mitchell4382e442021-07-14 12:53:01 -0700950 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000952 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 << "<public> must have a 'type' attribute");
954 return false;
955 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800956
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000957 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000959 diag_->Error(android::DiagMessage(out_resource->source)
960 << "invalid resource type '" << maybe_type.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961 return false;
962 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800963
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000964 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800965
Ryan Mitchell4382e442021-07-14 12:53:01 -0700966 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
967 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000969 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800970 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800972 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800975
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000976 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700980
Adam Lesinski71be7052017-12-12 16:48:07 -0800981 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800983}
984
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700985template <typename Func>
986bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000987 const char* tag_name, android::IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700988 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000989 diag->Warn(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700990 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
991 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700992 }
993
Ryan Mitchell4382e442021-07-14 12:53:01 -0700994 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000996 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700997 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800998 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001000
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001001 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
1002 ParseResourceNamedType(maybe_type.value());
1003 if (!maybe_parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001004 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001005 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001006 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001008 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009
Ryan Mitchell4382e442021-07-14 12:53:01 -07001010 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001011 if (!maybe_id_str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001012 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001013 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return false;
1015 }
1016
Ryan Mitchell4382e442021-07-14 12:53:01 -07001017 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001019 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001020 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001021 return false;
1022 }
1023
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001025 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 const size_t depth = parser->depth();
1028 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1029 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001030 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001032 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 // Skip text.
1034 continue;
1035 }
1036
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001037 const android::Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 const std::string& element_namespace = parser->element_namespace();
1039 const std::string& element_name = parser->element_name();
1040 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001041 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001042 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001043 diag->Error(android::DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001044 error = true;
1045 continue;
1046 }
1047
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001048 if (xml::FindNonEmptyAttribute(parser, "id")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001049 diag->Error(android::DiagMessage(item_source)
1050 << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 error = true;
1052 continue;
1053 }
1054
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001055 if (xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001056 diag->Error(android::DiagMessage(item_source)
1057 << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 error = true;
1059 continue;
1060 }
1061
Winson Chiuc84829d2022-03-04 20:35:58 +00001062 if (maybe_name.value().substr(0, std::strlen("removed_")) == "removed_") {
1063 // Skip resources that have been removed from the framework, but leave a hole so that
1064 // other staged resources don't shift and break apps previously compiled against them
1065 next_id.id++;
1066 continue;
1067 }
1068
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001069 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001070 .name = ResourceName{{}, parsed_type, std::string(maybe_name.value())},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001071 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001072 .comment = std::move(comment),
1073 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001074 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001076 // Execute group specific code.
1077 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001079 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001080 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001081 diag->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 error = true;
1083 }
1084 }
1085 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001086}
1087
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001088bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1089 ParsedResource* out_resource) {
1090 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1091 [](ParsedResource& parsed_entry, ResourceId id) {
1092 parsed_entry.id = id;
1093 parsed_entry.staged_api = true;
1094 parsed_entry.visibility_level = Visibility::Level::kPublic;
1095 });
1096}
1097
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001098bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1099 ParsedResource* out_resource) {
1100 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1101 [](ParsedResource& parsed_entry, ResourceId id) {
1102 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1103 });
1104}
1105
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001106bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1107 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001108 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001109 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1110 return false;
1111 }
1112
1113 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1114 [](ParsedResource& parsed_entry, ResourceId id) {
1115 parsed_entry.id = id;
1116 parsed_entry.visibility_level = Visibility::Level::kPublic;
1117 });
1118}
1119
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001120bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1121 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001122 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001123 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001124 diag_->Error(android::DiagMessage(out_resource->source)
1125 << "<" << parser->element_name() << "> must have a 'type' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 return false;
1127 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001128
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001129 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001131 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 << "invalid resource type '" << maybe_type.value() << "' in <"
1133 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001134 return false;
1135 }
1136
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001137 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001139}
1140
Adam Lesinski46c4d722017-08-23 13:03:56 -07001141bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001142 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001143 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001144 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1145 return false;
1146 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001147 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001148 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -07001149 << "ignoring configuration '" << out_resource->config << "' for <"
1150 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001151 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001152
1153 if (!ParseSymbolImpl(parser, out_resource)) {
1154 return false;
1155 }
1156
Adam Lesinski71be7052017-12-12 16:48:07 -08001157 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001158 return true;
1159}
1160
1161bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1162 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001163 diag_->Warn(android::DiagMessage(out_resource->source)
1164 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001165 }
1166
Ryan Mitchell4382e442021-07-14 12:53:01 -07001167 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001168 if (!overlayable_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001169 diag_->Error(android::DiagMessage(out_resource->source)
1170 << "<overlayable> tag must have a 'name' attribute");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001171 return false;
1172 }
1173
1174 const std::string kActorUriScheme =
1175 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001176 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001177 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001178 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001179 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1180 << Overlayable::kActorScheme << "'");
1181 return false;
1182 }
1183
1184 // Create a overlayable entry grouping that represents this <overlayable>
1185 auto overlayable = std::make_shared<Overlayable>(
1186 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001187 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001188
Adam Lesinski46c4d722017-08-23 13:03:56 -07001189 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001190 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001191 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001192 const size_t start_depth = parser->depth();
1193 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1194 xml::XmlPullParser::Event event = parser->event();
1195 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001196 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001197 break;
1198 } else if (event == xml::XmlPullParser::Event::kEndElement
1199 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001200 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001201 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001202 continue;
1203 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001204 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001205 comment = parser->comment();
1206 continue;
1207 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001208 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001209 continue;
1210 }
1211
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001212 const android::Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001213 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001214 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001215 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001216 if (current_policies == PolicyFlags::NONE) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001217 diag_->Error(android::DiagMessage(element_source)
1218 << "<item> within an <overlayable> must be inside a <policy> block");
Winsonb2d7f532019-02-04 16:32:43 -08001219 error = true;
1220 continue;
1221 }
1222
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001223 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001224 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001225 if (!item_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001226 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001227 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001228 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001229 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001230 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001231
Ryan Mitchell4382e442021-07-14 12:53:01 -07001232 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001233 if (!item_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001234 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001235 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001236 error = true;
1237 continue;
1238 }
1239
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001240 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1241 if (!type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001242 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001243 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001244 << "' in <item> within an <overlayable>");
1245 error = true;
1246 continue;
1247 }
1248
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001249 OverlayableItem overlayable_item(overlayable);
1250 overlayable_item.policies = current_policies;
1251 overlayable_item.comment = comment;
1252 overlayable_item.source = element_source;
1253
1254 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001255 child_resource.name.type = type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001256 child_resource.name.entry = std::string(item_name.value());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001257 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001258 out_resource->child_resources.push_back(std::move(child_resource));
1259
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001260 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001261 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001262 // If the policy list is not empty, then we are currently inside a policy element
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001263 diag_->Error(android::DiagMessage(element_source)
1264 << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001265 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001266 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001267 } else if (std::optional<StringPiece> maybe_type =
1268 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001269 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001270 // policies. Items within the policy tag will have the specified policy.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001271 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001272 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001273 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1274 kPolicyStringToFlag.end(),
1275 [trimmed_part](const auto& it) {
1276 return trimmed_part == it.first;
1277 });
1278 if (policy == kPolicyStringToFlag.end()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001279 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001280 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001281 error = true;
1282 continue;
1283 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001284
1285 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001286 }
Winsonb2d7f532019-02-04 16:32:43 -08001287 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001288 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001289 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001290 error = true;
1291 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001292 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001293 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001294 diag_->Error(android::DiagMessage(element_source)
1295 << "invalid element <" << element_name << "> "
1296 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001297 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001298 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001299 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001300
1301 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001302 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001303
Adam Lesinski46c4d722017-08-23 13:03:56 -07001304 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001305}
1306
Adam Lesinski71be7052017-12-12 16:48:07 -08001307bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001309 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001310 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 return true;
1312 }
1313 return false;
1314}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001315
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001316bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1317 ParsedResource* out_resource) {
1318 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001319}
1320
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1322 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001323 out_resource->name.type =
1324 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325
1326 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001328 diag_->Warn(android::DiagMessage(out_resource->source)
1329 << "ignoring configuration '" << out_resource->config << "' for attribute "
1330 << out_resource->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332 }
1333
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335
Ryan Mitchell4382e442021-07-14 12:53:01 -07001336 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 if (maybe_format) {
1338 type_mask = ParseFormatAttribute(maybe_format.value());
1339 if (type_mask == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001340 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001341 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001342 return false;
1343 }
1344 }
1345
Ryan Mitchell4382e442021-07-14 12:53:01 -07001346 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001347
Ryan Mitchell4382e442021-07-14 12:53:01 -07001348 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1350 if (!min_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001351 std::u16string min_str16 = android::util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001353 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001356 }
1357
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 if (!maybe_min) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001359 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 return false;
1362 }
1363 }
1364
Ryan Mitchell4382e442021-07-14 12:53:01 -07001365 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1367 if (!max_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001368 std::u16string max_str16 = android::util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001370 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001373 }
1374
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 if (!maybe_max) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001376 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 return false;
1379 }
1380 }
1381
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 if ((maybe_min || maybe_max) &&
1383 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001384 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 << "'min' and 'max' can only be used when format='integer'");
1386 return false;
1387 }
1388
1389 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001390 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 return a.symbol.name.value() < b.symbol.name.value();
1392 }
1393 };
1394
1395 std::set<Attribute::Symbol, SymbolComparator> items;
1396
1397 std::string comment;
1398 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 const size_t depth = parser->depth();
1400 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1401 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001402 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001404 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001405 // Skip text.
1406 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001407 }
1408
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001409 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 const std::string& element_namespace = parser->element_namespace();
1411 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001412 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 if (element_name == "enum") {
1414 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001415 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 << "can not define an <enum>; already defined a <flag>");
1417 error = true;
1418 continue;
1419 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001422 } else if (element_name == "flag") {
1423 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001424 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001425 << "can not define a <flag>; already defined an <enum>");
1426 error = true;
1427 continue;
1428 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001429 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001430 }
1431
Ryan Mitchell4382e442021-07-14 12:53:01 -07001432 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001433 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 ParsedResource child_resource;
1435 child_resource.name = symbol.symbol.name.value();
1436 child_resource.source = item_source;
1437 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001438 if (options_.visibility) {
1439 child_resource.visibility_level = options_.visibility.value();
1440 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001441 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001442
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 symbol.symbol.SetComment(std::move(comment));
1444 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001446 auto insert_result = items.insert(std::move(symbol));
1447 if (!insert_result.second) {
1448 const Attribute::Symbol& existing_symbol = *insert_result.first;
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001449 diag_->Error(android::DiagMessage(item_source)
1450 << "duplicate symbol '" << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001452 diag_->Note(android::DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001453 << "first defined here");
1454 error = true;
1455 }
1456 } else {
1457 error = true;
1458 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001460 diag_->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 error = true;
1462 }
1463
1464 comment = {};
1465 }
1466
1467 if (error) {
1468 return false;
1469 }
1470
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001471 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1472 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1473 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001475 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1476 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001477 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 return true;
1479}
1480
Ryan Mitchell4382e442021-07-14 12:53:01 -07001481std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001482 StringPiece tag) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001483 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001484
Ryan Mitchell4382e442021-07-14 12:53:01 -07001485 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001486 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001487 diag_->Error(android::DiagMessage(source)
1488 << "no attribute 'name' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001489 return {};
1490 }
1491
Ryan Mitchell4382e442021-07-14 12:53:01 -07001492 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 if (!maybe_value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001494 diag_->Error(android::DiagMessage(source)
1495 << "no attribute 'value' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001496 return {};
1497 }
1498
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001499 std::u16string value16 = android::util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500 android::Res_value val;
1501 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001502 diag_->Error(android::DiagMessage(source) << "invalid value '" << maybe_value.value()
1503 << "' for <" << tag << ">; must be an integer");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 return {};
1505 }
1506
1507 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001508 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1509 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001510 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001511}
1512
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001514 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001515
Ryan Mitchell4382e442021-07-14 12:53:01 -07001516 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001517 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001518 diag_->Error(android::DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 return false;
1520 }
1521
Ryan Mitchell4382e442021-07-14 12:53:01 -07001522 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523 if (!maybe_key) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001524 diag_->Error(android::DiagMessage(source)
1525 << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001526 return false;
1527 }
1528
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001529 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531
Jeremy Meyerf53a0272024-09-19 09:47:47 -07001532 auto flag = ParseFlag(xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag"));
1533
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 if (!value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001536 diag_->Error(android::DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001537 return false;
1538 }
1539
Jeremy Meyerf53a0272024-09-19 09:47:47 -07001540 if (flag) {
1541 if (options_.flag) {
1542 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1543 << "Resource flag are not allowed both in the path and in the file");
1544 return false;
1545 }
1546 std::string error;
1547 auto flag_status = GetFlagStatus(flag, options_.feature_flag_values, &error);
1548 if (flag_status) {
1549 value->SetFlagStatus(flag_status.value());
1550 value->SetFlag(std::move(flag));
1551 } else {
1552 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number())) << error);
1553 return false;
1554 }
1555 }
1556
1557 if (value->GetFlagStatus() != FlagStatus::Disabled) {
1558 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
1559 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 return true;
1561}
1562
Adam Lesinski86d67df2017-01-31 13:47:27 -08001563bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001564 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001565 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566
1567 std::unique_ptr<Style> style = util::make_unique<Style>();
1568
Ryan Mitchell4382e442021-07-14 12:53:01 -07001569 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001570 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001571 // 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 -07001572 if (!maybe_parent.value().empty()) {
1573 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001574 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001575 if (!style->parent) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001576 diag_->Error(android::DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001577 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578 }
1579
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001580 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001581 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001582 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001583 }
1584
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 } else {
1586 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001587 std::string style_name = out_resource->name.entry;
1588 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001589 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001591 style->parent = Reference(ResourceName(
1592 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 }
1594 }
1595
1596 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001597 const size_t depth = parser->depth();
1598 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1599 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001600 // Skip text and comments.
1601 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001602 }
1603
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 const std::string& element_namespace = parser->element_namespace();
1605 const std::string& element_name = parser->element_name();
1606 if (element_namespace == "" && element_name == "item") {
1607 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001608
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001610 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001611 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001612 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001613 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001614 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001615
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 if (error) {
1617 return false;
1618 }
1619
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001622}
1623
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001624bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1625 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001626 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001627 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1628 if (resource_format == 0u) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001629 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001630 << "'" << format_attr.value() << "' is an invalid format");
1631 return false;
1632 }
1633 }
1634 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001635}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001636
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001637bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1638 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001639}
1640
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001641bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1642 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001643}
1644
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001645bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1646 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001647 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001648 out_resource->name.type =
1649 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001650
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001652
Adam Lesinski75421622017-01-06 15:20:04 -08001653 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001654 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1655 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001656 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001657 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 << "invalid value for 'translatable'. Must be a boolean");
1659 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001660 }
Adam Lesinski75421622017-01-06 15:20:04 -08001661 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001662 }
Adam Lesinski75421622017-01-06 15:20:04 -08001663 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001664
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001666 const size_t depth = parser->depth();
1667 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1668 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001669 // Skip text and comments.
1670 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001671 }
1672
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001673 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 const std::string& element_namespace = parser->element_namespace();
1675 const std::string& element_name = parser->element_name();
1676 if (element_namespace.empty() && element_name == "item") {
Jeremy Meyere08e0372024-09-17 12:45:26 -07001677 auto flag = ParseFlag(xml::FindAttribute(parser, xml::kSchemaAndroid, "featureFlag"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001678 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 if (!item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001680 diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001681 error = true;
1682 continue;
1683 }
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -07001684 item->SetFlag(flag);
1685 std::string err;
1686 auto status = GetFlagStatus(flag, options_.feature_flag_values, &err);
1687 if (status) {
1688 item->SetFlagStatus(status.value());
1689 } else {
1690 diag_->Error(android::DiagMessage(item_source) << err);
1691 error = true;
1692 continue;
1693 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001695 array->elements.emplace_back(std::move(item));
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001697 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1698 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 error = true;
1700 }
1701 }
1702
1703 if (error) {
1704 return false;
1705 }
1706
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001707 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001709}
1710
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1712 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001713 out_resource->name.type =
1714 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001715
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001716 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001717
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001719 const size_t depth = parser->depth();
1720 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1721 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722 // Skip text and comments.
1723 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001724 }
1725
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001726 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 const std::string& element_namespace = parser->element_namespace();
1728 const std::string& element_name = parser->element_name();
1729 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001730 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 if (!maybe_quantity) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001732 diag_->Error(android::DiagMessage(item_source) << "<item> in <plurals> requires attribute "
1733 << "'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001734 error = true;
1735 continue;
1736 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001737
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738 StringPiece trimmed_quantity =
1739 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001741 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001742 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001743 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001744 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001745 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001746 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001748 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001749 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001751 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001752 index = Plural::Other;
1753 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001754 diag_->Error(android::DiagMessage(item_source)
1755 << "<item> in <plural> has invalid value '" << trimmed_quantity
1756 << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001757 error = true;
1758 continue;
1759 }
1760
1761 if (plural->values[index]) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001762 diag_->Error(android::DiagMessage(item_source)
1763 << "duplicate quantity '" << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001764 error = true;
1765 continue;
1766 }
1767
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001768 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001769 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1770 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001771 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001772 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001773
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001775
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001776 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001777 diag_->Error(android::DiagMessage(item_source)
1778 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001779 error = true;
1780 }
1781 }
1782
1783 if (error) {
1784 return false;
1785 }
1786
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001787 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001788 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001789}
1790
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001791bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1792 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001793 out_resource->name.type =
1794 ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001795
Donald Chai94e4a012020-01-06 13:52:41 -08001796 if (!options_.preserve_visibility_of_styleables) {
1797 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1798 // knows exactly what for.
1799 //
1800 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1801 // package-private (to be used only by the view class); themes are a different story.
1802 out_resource->visibility_level = Visibility::Level::kPublic;
1803 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001804
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001805 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001806 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001807 diag_->Warn(android::DiagMessage(out_resource->source)
1808 << "ignoring configuration '" << out_resource->config << "' for styleable "
1809 << out_resource->name.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001810 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 }
1812
1813 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1814
1815 std::string comment;
1816 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001817 const size_t depth = parser->depth();
1818 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1819 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001820 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001821 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001822 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001823 // Ignore text.
1824 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001825 }
1826
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001827 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001828 const std::string& element_namespace = parser->element_namespace();
1829 const std::string& element_name = parser->element_name();
1830 if (element_namespace.empty() && element_name == "attr") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001831 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001832 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001833 diag_->Error(android::DiagMessage(item_source)
1834 << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001835 error = true;
1836 continue;
1837 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001838
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001839 // If this is a declaration, the package name may be in the name. Separate
1840 // these out.
1841 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001842 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001843 if (!maybe_ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001844 diag_->Error(android::DiagMessage(item_source)
1845 << "<attr> tag has invalid name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001846 error = true;
1847 continue;
1848 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001849
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001850 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001851 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001852
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001853 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001854 ParsedResource child_resource;
1855 child_resource.name = child_ref.name.value();
1856 child_resource.source = item_source;
1857 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001858 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001859 if (options_.visibility) {
1860 child_resource.visibility_level = options_.visibility.value();
1861 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001862
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001863 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001864 error = true;
1865 continue;
1866 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001867
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001868 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001869 child_ref.SetComment(child_resource.comment);
1870 child_ref.SetSource(item_source);
1871 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001872
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001873 // Do not add referenced attributes that do not define a format to the table.
1874 CHECK(child_resource.value != nullptr);
1875 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1876
1877 CHECK(attr != nullptr);
1878 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1879 out_resource->child_resources.push_back(std::move(child_resource));
1880 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001881
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001882 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001883 diag_->Error(android::DiagMessage(item_source)
1884 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001885 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001886 }
1887
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001888 comment = {};
1889 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001890
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001891 if (error) {
1892 return false;
1893 }
1894
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001895 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001896 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001897}
1898
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001899} // namespace aapt