blob: 6af39b739e9b0edeb3e7d21cbe25e08537974419 [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 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
Ryan Mitchell4382e442021-07-14 12:53:01 -070023#include <android-base/logging.h>
24#include <idmap2/Policies.h>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ResourceTable.h"
27#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080030#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080031#include "util/ImmutableMap.h"
Ryan Mitchell4382e442021-07-14 12:53:01 -070032
Adam Lesinski9e10ac72015-10-16 14:37:48 -070033#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080034#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070035
Adam Lesinski2eed52e2018-02-21 15:55:58 -080036using ::aapt::ResourceUtils::StringBuilder;
37using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020038using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080039using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080040
Winson62ac8b52019-12-04 08:36:48 -080041using android::idmap2::policy::kPolicyStringToFlag;
42
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043namespace aapt {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070044namespace {
45constexpr const char* kPublicGroupTag = "public-group";
46constexpr const char* kStagingPublicGroupTag = "staging-public-group";
Ryan Mitchell2fedba92021-04-23 07:47:38 -070047constexpr const char* kStagingPublicGroupFinalTag = "staging-public-group-final";
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070048} // namespace
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070050constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070053static bool ShouldIgnoreElement(StringPiece ns, StringPiece name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080055}
56
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070057static uint32_t ParseFormatTypeNoEnumsOrFlags(StringPiece piece) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070068 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070072 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070074 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080076}
77
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070078static uint32_t ParseFormatType(StringPiece piece) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070079 if (piece == "enum") {
80 return android::ResTable_map::TYPE_ENUM;
81 } else if (piece == "flags") {
82 return android::ResTable_map::TYPE_FLAGS;
83 }
84 return ParseFormatTypeNoEnumsOrFlags(piece);
85}
86
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070087static uint32_t ParseFormatAttribute(StringPiece str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 uint32_t mask = 0;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070089 for (StringPiece part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 StringPiece trimmed_part = util::TrimWhitespace(part);
91 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 if (type == 0) {
93 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080094 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 mask |= type;
96 }
97 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080098}
99
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700100// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800101struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 ResourceName name;
103 ConfigDescription config;
104 std::string product;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000105 android::Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800108 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700109 bool staged_api = false;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700110 bool allow_new = false;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700111 std::optional<OverlayableItem> overlayable_item;
112 std::optional<StagedId> staged_alias;
Adam Lesinski71be7052017-12-12 16:48:07 -0800113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 std::string comment;
115 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800117};
118
119// Recursively adds resources to the ResourceTable.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000120static bool AddResourcesToTable(ResourceTable* table, android::IDiagnostics* diag,
121 ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
123 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 // Only if there was a change do we re-assign.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700125 res->comment = std::string(trimmed_comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
Adam Lesinski76565542016-03-10 21:55:04 -0800127
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700128 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800129 if (res->visibility_level != Visibility::Level::kUndefined) {
130 Visibility visibility;
131 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700132 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800133 visibility.source = res->source;
134 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700135 res_builder.SetVisibility(visibility);
136 }
137
138 if (res->id.is_valid()) {
139 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800141
Adam Lesinski71be7052017-12-12 16:48:07 -0800142 if (res->allow_new) {
143 AllowNew allow_new;
144 allow_new.source = res->source;
145 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700146 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800147 }
148
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800149 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700150 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800151 }
152
153 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 res->value->SetComment(std::move(res->comment));
156 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700157 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800159
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700160 if (res->staged_alias) {
161 res_builder.SetStagedId(res->staged_alias.value());
162 }
163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700165 if (!res->name.entry.empty()) {
166 if (!table->AddResource(res_builder.Build(), diag)) {
167 return false;
168 }
169 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 for (ParsedResource& child : res->child_resources) {
171 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
173 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800174}
175
176// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800178
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000179ResourceParser::ResourceParser(android::IDiagnostics* diag, ResourceTable* table,
180 const android::Source& source, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 const ResourceParserOptions& options)
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000182 : diag_(diag), table_(table), source_(source), config_(config), options_(options) {
183}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800185// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
186// This will be used to traverse and flatten the XML string into a single std::string, with all
187// Span and Untranslatable data maintained in parallel, as indices into the string.
188class Node {
189 public:
190 virtual ~Node() = default;
191
192 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
193 // parent node as well.
194 // Returns a pointer to the child node that was added as a convenience.
195 template <typename T>
196 T* AddChild(std::unique_ptr<T> node) {
197 T* raw_ptr = node.get();
198 children.push_back(std::move(node));
199 return raw_ptr;
200 }
201
202 virtual void Build(StringBuilder* builder) const {
203 for (const auto& child : children) {
204 child->Build(builder);
205 }
206 }
207
208 std::vector<std::unique_ptr<Node>> children;
209};
210
211// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
212class SegmentNode : public Node {
213 public:
214 std::string data;
215
216 void Build(StringBuilder* builder) const override {
217 builder->AppendText(data);
218 }
219};
220
221// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
222class SpanNode : public Node {
223 public:
224 std::string name;
225
226 void Build(StringBuilder* builder) const override {
227 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
228 Node::Build(builder);
229 builder->EndSpan(span_handle);
230 }
231};
232
233// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
234class UntranslatableNode : public Node {
235 public:
236 void Build(StringBuilder* builder) const override {
237 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
238 Node::Build(builder);
239 builder->EndUntranslatable(handle);
240 }
241};
242
243// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800244bool ResourceParser::FlattenXmlSubtree(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000245 xml::XmlPullParser* parser, std::string* out_raw_string, android::StyleString* out_style_string,
Adam Lesinski75421622017-01-06 15:20:04 -0800246 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800247 std::string raw_string;
248 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800249
250 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700251 std::optional<size_t> untranslatable_start_depth;
Adam Lesinski75421622017-01-06 15:20:04 -0800252
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800253 Node root;
254 std::vector<Node*> node_stack;
255 node_stack.push_back(&root);
256
257 bool saw_span_node = false;
258 SegmentNode* first_segment = nullptr;
259 SegmentNode* last_segment = nullptr;
260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800262 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800264
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800265 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700266 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800267 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800269 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800270 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700271
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800272 last_segment = node_stack.back()->AddChild(std::move(segment_node));
273 if (first_segment == nullptr) {
274 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800275 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800276 current_text = {};
277 }
278 }
Adam Lesinski75421622017-01-06 15:20:04 -0800279
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800280 switch (event) {
281 case xml::XmlPullParser::Event::kText: {
282 current_text += parser->text();
283 raw_string += parser->text();
284 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800285
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800286 case xml::XmlPullParser::Event::kStartElement: {
287 if (parser->element_namespace().empty()) {
288 // This is an HTML tag which we encode as a span. Add it to the span stack.
289 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
290 span_node->name = parser->element_name();
291 const auto end_attr_iter = parser->end_attributes();
292 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
293 ++attr_iter) {
294 span_node->name += ";";
295 span_node->name += attr_iter->name;
296 span_node->name += "=";
297 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800298 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800299
300 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
301 saw_span_node = true;
302 } else if (parser->element_namespace() == sXliffNamespaceUri) {
303 // This is an XLIFF tag, which is not encoded as a span.
304 if (parser->element_name() == "g") {
305 // Check that an 'untranslatable' tag is not already being processed. Nested
306 // <xliff:g> tags are illegal.
307 if (untranslatable_start_depth) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000308 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800309 << "illegal nested XLIFF 'g' tag");
310 return false;
311 } else {
312 // Mark the beginning of an 'untranslatable' section.
313 untranslatable_start_depth = depth;
314 node_stack.push_back(
315 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
316 }
317 } else {
318 // Ignore unknown XLIFF tags, but don't warn.
319 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
320 }
321 } else {
322 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000323 diag_->Warn(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800324 << "ignoring element '" << parser->element_name()
325 << "' with unknown namespace '" << parser->element_namespace() << "'");
326 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800327 }
Adam Lesinski75421622017-01-06 15:20:04 -0800328
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800329 // Enter one level inside the element.
330 depth++;
331 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700332
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800333 case xml::XmlPullParser::Event::kEndElement: {
334 // Return one level from within the element.
335 depth--;
336 if (depth == 0) {
337 break;
338 }
339
340 node_stack.pop_back();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700341 if (untranslatable_start_depth == depth) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800342 // This is the end of an untranslatable section.
343 untranslatable_start_depth = {};
344 }
345 } break;
346
347 default:
348 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 }
351 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700353 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800354 CHECK(node_stack.size() == 1u);
355 CHECK(node_stack.back() == &root);
356
357 if (!saw_span_node) {
358 // If there were no spans, we must treat this string a little differently (according to AAPT).
359 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
360 // from the last segment.
361 if (first_segment != nullptr) {
362 // Trim leading whitespace.
363 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
364 if (trimmed.size() != first_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700365 first_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800366 }
367 }
368
369 if (last_segment != nullptr) {
370 // Trim trailing whitespace.
371 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
372 if (trimmed.size() != last_segment->data.size()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700373 last_segment->data = std::string(trimmed);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800374 }
375 }
376 }
377
378 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
379 // care of recording the correctly adjusted Spans and UntranslatableSections.
380 StringBuilder builder;
381 root.Build(&builder);
382 if (!builder) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000383 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
384 << builder.GetError());
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800385 return false;
386 }
387
388 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
389 *out_raw_string = std::move(raw_string);
390 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
391 out_style_string->str = std::move(flattened_string.text);
392 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800393 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394}
395
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 const size_t depth = parser->depth();
399 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
400 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 // Skip comments and text.
402 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800403 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404
Adam Lesinski060b53d2017-07-28 17:10:35 -0700405 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000406 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 << "root element must be <resources>");
408 return false;
409 }
410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 break;
413 };
414
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000416 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 return false;
419 }
420 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421}
422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
424 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700425
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 bool error = false;
427 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 const size_t depth = parser->depth();
429 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
430 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700435
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 if (!util::TrimWhitespace(parser->text()).empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000438 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 << "plain text not allowed here");
440 error = true;
441 }
442 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700443 }
444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 // Skip unknown namespace.
449 continue;
450 }
451
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 std::string element_name = parser->element_name();
453 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 comment = "";
455 continue;
456 }
457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 ParsedResource parsed_resource;
459 parsed_resource.config = config_;
460 parsed_resource.source = source_.WithLine(parser->line_number());
461 parsed_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +0800462 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100463 if (options_.visibility) {
464 parsed_resource.visibility_level = options_.visibility.value();
465 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466
467 // Extract the product name if it exists.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700468 if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700469 parsed_resource.product = std::string(maybe_product.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 }
471
472 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 error = true;
475 continue;
476 }
477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 error = true;
480 }
481 }
482
483 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 for (const ResourceName& stripped_resource : stripped_resources) {
485 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 // Failed to find the resource.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000487 diag_->Error(android::DiagMessage(source_)
488 << "resource '" << stripped_resource
489 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 error = true;
491 }
492 }
493
494 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800495}
496
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700497bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
498 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 struct ItemTypeFormat {
500 ResourceType type;
501 uint32_t format;
502 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
505 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800506
Adam Lesinski86d67df2017-01-31 13:47:27 -0800507 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
508 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
509 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
510 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
511 {"dimen",
512 {ResourceType::kDimen,
513 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
514 android::ResTable_map::TYPE_DIMENSION}},
515 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
516 {"fraction",
517 {ResourceType::kFraction,
518 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
519 android::ResTable_map::TYPE_DIMENSION}},
520 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
521 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
522 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800523
Adam Lesinski86d67df2017-01-31 13:47:27 -0800524 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
525 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
526 {"array", std::mem_fn(&ResourceParser::ParseArray)},
527 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
528 {"configVarying",
529 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
530 std::placeholders::_2, std::placeholders::_3)},
531 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
532 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
533 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700534 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800535 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
536 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
537 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700538 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700539 {"staging-public-group-final", std::mem_fn(&ResourceParser::ParseStagingPublicGroupFinal)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800540 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
541 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
542 std::placeholders::_2, std::placeholders::_3)},
543 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
544 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800545
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800547
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800550
Adam Lesinski86d67df2017-01-31 13:47:27 -0800551 bool can_be_item = true;
552 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800554 can_be_bag = false;
555
Adam Lesinskie597d682017-06-01 17:16:44 -0700556 // The default format for <item> is any. If a format attribute is present, that one will
557 // override the default.
558 resource_format = android::ResTable_map::TYPE_ANY;
559
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700561 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700562 resource_type = std::string(maybe_type.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000564 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 << "<item> must have a 'type' attribute");
566 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800567 }
568
Ryan Mitchell4382e442021-07-14 12:53:01 -0700569 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700571 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700573 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 if (!resource_format) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000575 diag_->Error(android::DiagMessage(out_resource->source)
576 << "'" << maybe_format.value() << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800577 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 }
579 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800580 } else if (resource_type == "bag") {
581 can_be_item = false;
582
583 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700584 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700585 resource_type = std::string(maybe_type.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800586 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000587 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski86d67df2017-01-31 13:47:27 -0800588 << "<bag> must have a 'type' attribute");
589 return false;
590 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 }
592
593 // Get the name of the resource. This will be checked later, because not all
594 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700595 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 if (resource_type == "id") {
598 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000599 diag_->Error(android::DiagMessage(out_resource->source)
600 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 return false;
602 }
603
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000604 out_resource->name.type =
605 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700606 out_resource->name.entry = std::string(maybe_name.value());
y9efbbef2018-04-18 11:29:09 -0700607
608 // Ids either represent a unique resource id or reference another resource id
609 auto item = ParseItem(parser, out_resource, resource_format);
610 if (!item) {
611 return false;
612 }
613
614 String* empty = ValueCast<String>(out_resource->value.get());
615 if (empty && *empty->value == "") {
616 // If no inner element exists, represent a unique identifier
617 out_resource->value = util::make_unique<Id>();
618 } else {
y9efbbef2018-04-18 11:29:09 -0700619 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700620 if (ref && !ref->name && !ref->id) {
621 // A null reference also means there is no inner element when ids are in the form:
622 // <id name="name"/>
623 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000624 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700625 // If an inner element exists, the inner element must be a reference to another resource id
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000626 diag_->Error(android::DiagMessage(out_resource->source)
627 << "<" << parser->element_name()
628 << "> inner element must either be a resource reference or empty");
y9efbbef2018-04-18 11:29:09 -0700629 return false;
630 }
631 }
632
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700634 } else if (resource_type == "macro") {
635 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000636 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700637 << "<" << parser->element_name() << "> missing 'name' attribute");
638 return false;
639 }
640
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000641 out_resource->name.type =
642 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700643 out_resource->name.entry = std::string(maybe_name.value());
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700644 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 }
646
Adam Lesinski86d67df2017-01-31 13:47:27 -0800647 if (can_be_item) {
648 const auto item_iter = elToItemMap.find(resource_type);
649 if (item_iter != elToItemMap.end()) {
650 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651
Adam Lesinski86d67df2017-01-31 13:47:27 -0800652 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000653 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800654 << "<" << parser->element_name() << "> missing 'name' attribute");
655 return false;
656 }
657
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000658 out_resource->name.type =
659 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700660 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800661
Adam Lesinskie597d682017-06-01 17:16:44 -0700662 // Only use the implied format of the type when there is no explicit format.
663 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800664 resource_format = item_iter->second.format;
665 }
666
667 if (!ParseItem(parser, out_resource, resource_format)) {
668 return false;
669 }
670 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 }
673
674 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800675 if (can_be_bag) {
676 const auto bag_iter = elToBagMap.find(resource_type);
677 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700678 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700679 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700680 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800681 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000682 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800683 << "<" << parser->element_name() << "> missing 'name' attribute");
684 return false;
685 }
686
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700687 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800688 }
689
690 // Call the associated parse method. The type will be filled in by the
691 // parse func.
692 if (!bag_iter->second(this, parser, out_resource)) {
693 return false;
694 }
695 return true;
696 }
697 }
698
699 if (can_be_item) {
700 // Try parsing the elementName (or type) as a resource. These shall only be
701 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000702 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800703 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000705 diag_->Error(android::DiagMessage(out_resource->source)
706 << "<" << parser->element_name() << "> missing 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 return false;
708 }
709
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000710 out_resource->name.type = parsed_type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700711 out_resource->name.entry = std::string(maybe_name.value());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800712 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
713 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000714 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinski86d67df2017-01-31 13:47:27 -0800715 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
716 return false;
717 }
718 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 }
721
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000722 // If the resource type was not recognized, write the error and return false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000723 diag_->Error(android::DiagMessage(out_resource->source)
724 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 return false;
726}
727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
729 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 const uint32_t format) {
731 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 }
734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 out_resource->value = ParseXml(parser, format, kNoRawString);
736 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000737 diag_->Error(android::DiagMessage(out_resource->source)
738 << "invalid " << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739 return false;
740 }
741 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800742}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800743
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700744std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
745 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000749 android::StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800750 std::vector<UntranslatableSection> untranslatable_sections;
751 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800752 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 }
754
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700755 return FlattenedXmlSubTree{.raw_value = raw_value,
756 .style_string = style_string,
757 .untranslatable_sections = untranslatable_sections,
758 .namespace_resolver = parser,
759 .source = source_.WithLine(begin_xml_line)};
760}
761
762/**
763 * Reads the entire XML subtree and attempts to parse it as some Item,
764 * with typeMask denoting which items it can be. If allowRawValue is
765 * true, a RawString is returned if the XML couldn't be parsed as
766 * an Item. If allowRawValue is false, nullptr is returned in this
767 * case.
768 */
769std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
770 const bool allow_raw_value) {
771 auto sub_tree = CreateFlattenSubTree(parser);
772 if (!sub_tree.has_value()) {
773 return {};
774 }
775 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
776}
777
778std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
779 const uint32_t type_mask, const bool allow_raw_value,
780 ResourceTable& table,
781 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000782 android::IDiagnostics& diag) {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700783 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800785 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700786 util::make_unique<StyledString>(table.string_pool.MakeRef(
787 xmlsub_tree.style_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000788 android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700789 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800790 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 }
792
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700793 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794 // name.package can be empty here, as it will assume the package name of the
795 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700796 auto id = util::make_unique<Id>();
797 id->SetSource(xmlsub_tree.source);
798 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 };
800
801 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700802 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
Brandon Liu48d229d2023-05-04 23:54:03 +0000803 &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700804 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700806 if (auto ref = ValueCast<Reference>(processed_item.get())) {
807 ref->allow_raw = allow_raw_value;
808 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 }
812
813 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 // Use the trimmed, escaped string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000816 std::unique_ptr<String> string = util::make_unique<String>(table.string_pool.MakeRef(
817 xmlsub_tree.style_string.str, android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700818 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800819 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 }
821
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700824 return util::make_unique<RawString>(table.string_pool.MakeRef(
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000825 util::TrimWhitespace(xmlsub_tree.raw_value), android::StringPool::Context(config)));
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700826 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700827 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
828 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 }
830 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800831}
832
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833bool ResourceParser::ParseString(xml::XmlPullParser* parser,
834 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700836 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
837 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 if (!maybe_formatted) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000839 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 << "invalid value for 'formatted'. Must be a boolean");
841 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800842 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800845
Adam Lesinski75421622017-01-06 15:20:04 -0800846 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700847 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
848 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800849 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000850 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 << "invalid value for 'translatable'. Must be a boolean");
852 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800853 }
Adam Lesinski75421622017-01-06 15:20:04 -0800854 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 out_resource->value =
858 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
859 if (!out_resource->value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000860 diag_->Error(android::DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 return false;
862 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800863
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800865 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800866
Adam Lesinski75421622017-01-06 15:20:04 -0800867 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 if (!util::VerifyJavaStringFormat(*string_value->value)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000869 android::DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 msg << "multiple substitutions specified in non-positional format; "
871 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 if (options_.error_on_positional_arguments) {
873 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800875 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800879 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880
Adam Lesinski75421622017-01-06 15:20:04 -0800881 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
882 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700883 }
884 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800885}
886
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700887bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
888 auto sub_tree = CreateFlattenSubTree(parser);
889 if (!sub_tree) {
890 return false;
891 }
892
893 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000894 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700895 << "<macro> tags cannot be declared in configurations other than the default "
896 "configuration'");
897 return false;
898 }
899
900 auto macro = std::make_unique<Macro>();
901 macro->raw_value = std::move(sub_tree->raw_value);
902 macro->style_string = std::move(sub_tree->style_string);
903 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
904
905 for (const auto& decl : parser->package_decls()) {
906 macro->alias_namespaces.emplace_back(
907 Macro::Namespace{.alias = decl.prefix,
908 .package_name = decl.package.package,
909 .is_private = decl.package.private_namespace});
910 }
911
912 out_resource->value = std::move(macro);
913 return true;
914}
915
Adam Lesinski71be7052017-12-12 16:48:07 -0800916bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100917 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000918 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100919 << "<public> tag not allowed with --visibility flag");
920 return false;
921 }
922
Adam Lesinski46c4d722017-08-23 13:03:56 -0700923 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000924 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -0700925 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
926 }
927
Ryan Mitchell4382e442021-07-14 12:53:01 -0700928 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700929 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000930 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 << "<public> must have a 'type' attribute");
932 return false;
933 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800934
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000935 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700936 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000937 diag_->Error(android::DiagMessage(out_resource->source)
938 << "invalid resource type '" << maybe_type.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700939 return false;
940 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800941
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000942 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800943
Ryan Mitchell4382e442021-07-14 12:53:01 -0700944 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
945 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000947 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800948 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800950 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700952 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800953
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000954 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700958
Adam Lesinski71be7052017-12-12 16:48:07 -0800959 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800961}
962
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700963template <typename Func>
964bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000965 const char* tag_name, android::IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700966 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000967 diag->Warn(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700968 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
969 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700970 }
971
Ryan Mitchell4382e442021-07-14 12:53:01 -0700972 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000974 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700975 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800976 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800978
Iurii Makhnoaeac0d02022-02-22 06:41:58 +0000979 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
980 ParseResourceNamedType(maybe_type.value());
981 if (!maybe_parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000982 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700983 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800984 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +0000986 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987
Ryan Mitchell4382e442021-07-14 12:53:01 -0700988 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 if (!maybe_id_str) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000990 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700991 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 return false;
993 }
994
Ryan Mitchell4382e442021-07-14 12:53:01 -0700995 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 if (!maybe_id) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000997 diag->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700998 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 return false;
1000 }
1001
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001003 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 const size_t depth = parser->depth();
1006 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1007 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001008 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 // Skip text.
1012 continue;
1013 }
1014
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001015 const android::Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 const std::string& element_namespace = parser->element_namespace();
1017 const std::string& element_name = parser->element_name();
1018 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001019 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001020 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001021 diag->Error(android::DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 error = true;
1023 continue;
1024 }
1025
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001026 if (xml::FindNonEmptyAttribute(parser, "id")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001027 diag->Error(android::DiagMessage(item_source)
1028 << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 error = true;
1030 continue;
1031 }
1032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 if (xml::FindNonEmptyAttribute(parser, "type")) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001034 diag->Error(android::DiagMessage(item_source)
1035 << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 error = true;
1037 continue;
1038 }
1039
Winson Chiuc84829d2022-03-04 20:35:58 +00001040 if (maybe_name.value().substr(0, std::strlen("removed_")) == "removed_") {
1041 // Skip resources that have been removed from the framework, but leave a hole so that
1042 // other staged resources don't shift and break apps previously compiled against them
1043 next_id.id++;
1044 continue;
1045 }
1046
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001047 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001048 .name = ResourceName{{}, parsed_type, std::string(maybe_name.value())},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001049 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001050 .comment = std::move(comment),
1051 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001052 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001054 // Execute group specific code.
1055 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001057 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001058 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001059 diag->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001060 error = true;
1061 }
1062 }
1063 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001064}
1065
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001066bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1067 ParsedResource* out_resource) {
1068 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1069 [](ParsedResource& parsed_entry, ResourceId id) {
1070 parsed_entry.id = id;
1071 parsed_entry.staged_api = true;
1072 parsed_entry.visibility_level = Visibility::Level::kPublic;
1073 });
1074}
1075
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001076bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1077 ParsedResource* out_resource) {
1078 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1079 [](ParsedResource& parsed_entry, ResourceId id) {
1080 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1081 });
1082}
1083
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001084bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1085 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001086 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001087 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1088 return false;
1089 }
1090
1091 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1092 [](ParsedResource& parsed_entry, ResourceId id) {
1093 parsed_entry.id = id;
1094 parsed_entry.visibility_level = Visibility::Level::kPublic;
1095 });
1096}
1097
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1099 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001100 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 if (!maybe_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001102 diag_->Error(android::DiagMessage(out_resource->source)
1103 << "<" << parser->element_name() << "> must have a 'type' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 return false;
1105 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001106
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001107 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 if (!parsed_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001109 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 << "invalid resource type '" << maybe_type.value() << "' in <"
1111 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112 return false;
1113 }
1114
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001115 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001117}
1118
Adam Lesinski46c4d722017-08-23 13:03:56 -07001119bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001120 if (options_.visibility) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001121 diag_->Error(android::DiagMessage(out_resource->source)
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001122 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1123 return false;
1124 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001125 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001126 diag_->Warn(android::DiagMessage(out_resource->source)
Adam Lesinski46c4d722017-08-23 13:03:56 -07001127 << "ignoring configuration '" << out_resource->config << "' for <"
1128 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001129 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001130
1131 if (!ParseSymbolImpl(parser, out_resource)) {
1132 return false;
1133 }
1134
Adam Lesinski71be7052017-12-12 16:48:07 -08001135 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001136 return true;
1137}
1138
1139bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1140 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001141 diag_->Warn(android::DiagMessage(out_resource->source)
1142 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001143 }
1144
Ryan Mitchell4382e442021-07-14 12:53:01 -07001145 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001146 if (!overlayable_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001147 diag_->Error(android::DiagMessage(out_resource->source)
1148 << "<overlayable> tag must have a 'name' attribute");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001149 return false;
1150 }
1151
1152 const std::string kActorUriScheme =
1153 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001154 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001155 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001156 diag_->Error(android::DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001157 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1158 << Overlayable::kActorScheme << "'");
1159 return false;
1160 }
1161
1162 // Create a overlayable entry grouping that represents this <overlayable>
1163 auto overlayable = std::make_shared<Overlayable>(
1164 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001165 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001166
Adam Lesinski46c4d722017-08-23 13:03:56 -07001167 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001168 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001169 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001170 const size_t start_depth = parser->depth();
1171 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1172 xml::XmlPullParser::Event event = parser->event();
1173 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001174 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001175 break;
1176 } else if (event == xml::XmlPullParser::Event::kEndElement
1177 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001178 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001179 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001180 continue;
1181 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001182 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001183 comment = parser->comment();
1184 continue;
1185 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001186 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001187 continue;
1188 }
1189
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001190 const android::Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001191 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001192 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001193 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001194 if (current_policies == PolicyFlags::NONE) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001195 diag_->Error(android::DiagMessage(element_source)
1196 << "<item> within an <overlayable> must be inside a <policy> block");
Winsonb2d7f532019-02-04 16:32:43 -08001197 error = true;
1198 continue;
1199 }
1200
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001201 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001202 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001203 if (!item_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001204 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001205 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001206 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001207 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001208 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001209
Ryan Mitchell4382e442021-07-14 12:53:01 -07001210 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001211 if (!item_type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001212 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001213 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001214 error = true;
1215 continue;
1216 }
1217
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001218 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1219 if (!type) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001220 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001221 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001222 << "' in <item> within an <overlayable>");
1223 error = true;
1224 continue;
1225 }
1226
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001227 OverlayableItem overlayable_item(overlayable);
1228 overlayable_item.policies = current_policies;
1229 overlayable_item.comment = comment;
1230 overlayable_item.source = element_source;
1231
1232 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001233 child_resource.name.type = type->ToResourceNamedType();
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001234 child_resource.name.entry = std::string(item_name.value());
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001235 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001236 out_resource->child_resources.push_back(std::move(child_resource));
1237
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001238 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001239 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001240 // If the policy list is not empty, then we are currently inside a policy element
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001241 diag_->Error(android::DiagMessage(element_source)
1242 << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001243 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001244 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001245 } else if (std::optional<StringPiece> maybe_type =
1246 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001247 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001248 // policies. Items within the policy tag will have the specified policy.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001249 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001250 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001251 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1252 kPolicyStringToFlag.end(),
1253 [trimmed_part](const auto& it) {
1254 return trimmed_part == it.first;
1255 });
1256 if (policy == kPolicyStringToFlag.end()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001257 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001258 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001259 error = true;
1260 continue;
1261 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001262
1263 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001264 }
Winsonb2d7f532019-02-04 16:32:43 -08001265 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001266 diag_->Error(android::DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001267 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001268 error = true;
1269 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001270 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001271 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001272 diag_->Error(android::DiagMessage(element_source)
1273 << "invalid element <" << element_name << "> "
1274 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001275 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001276 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001277 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001278
1279 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001280 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001281
Adam Lesinski46c4d722017-08-23 13:03:56 -07001282 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001283}
1284
Adam Lesinski71be7052017-12-12 16:48:07 -08001285bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001287 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001288 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 return true;
1290 }
1291 return false;
1292}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001293
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1295 ParsedResource* out_resource) {
1296 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297}
1298
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1300 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001301 out_resource->name.type =
1302 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001303
1304 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001306 diag_->Warn(android::DiagMessage(out_resource->source)
1307 << "ignoring configuration '" << out_resource->config << "' for attribute "
1308 << out_resource->name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 }
1311
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001313
Ryan Mitchell4382e442021-07-14 12:53:01 -07001314 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 if (maybe_format) {
1316 type_mask = ParseFormatAttribute(maybe_format.value());
1317 if (type_mask == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001318 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001319 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320 return false;
1321 }
1322 }
1323
Ryan Mitchell4382e442021-07-14 12:53:01 -07001324 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325
Ryan Mitchell4382e442021-07-14 12:53:01 -07001326 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1328 if (!min_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001329 std::u16string min_str16 = android::util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001331 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001334 }
1335
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001336 if (!maybe_min) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001337 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 return false;
1340 }
1341 }
1342
Ryan Mitchell4382e442021-07-14 12:53:01 -07001343 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1345 if (!max_str.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001346 std::u16string max_str16 = android::util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001347 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001348 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001351 }
1352
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 if (!maybe_max) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001354 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001356 return false;
1357 }
1358 }
1359
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 if ((maybe_min || maybe_max) &&
1361 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001362 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 << "'min' and 'max' can only be used when format='integer'");
1364 return false;
1365 }
1366
1367 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001368 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 return a.symbol.name.value() < b.symbol.name.value();
1370 }
1371 };
1372
1373 std::set<Attribute::Symbol, SymbolComparator> items;
1374
1375 std::string comment;
1376 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 const size_t depth = parser->depth();
1378 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1379 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001380 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 // Skip text.
1384 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001385 }
1386
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001387 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 const std::string& element_namespace = parser->element_namespace();
1389 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001390 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 if (element_name == "enum") {
1392 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001393 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394 << "can not define an <enum>; already defined a <flag>");
1395 error = true;
1396 continue;
1397 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 } else if (element_name == "flag") {
1401 if (type_mask & android::ResTable_map::TYPE_ENUM) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001402 diag_->Error(android::DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 << "can not define a <flag>; already defined an <enum>");
1404 error = true;
1405 continue;
1406 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001407 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408 }
1409
Ryan Mitchell4382e442021-07-14 12:53:01 -07001410 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001412 ParsedResource child_resource;
1413 child_resource.name = symbol.symbol.name.value();
1414 child_resource.source = item_source;
1415 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001416 if (options_.visibility) {
1417 child_resource.visibility_level = options_.visibility.value();
1418 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001421 symbol.symbol.SetComment(std::move(comment));
1422 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001423
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001424 auto insert_result = items.insert(std::move(symbol));
1425 if (!insert_result.second) {
1426 const Attribute::Symbol& existing_symbol = *insert_result.first;
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001427 diag_->Error(android::DiagMessage(item_source)
1428 << "duplicate symbol '" << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001429
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001430 diag_->Note(android::DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431 << "first defined here");
1432 error = true;
1433 }
1434 } else {
1435 error = true;
1436 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001437 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001438 diag_->Error(android::DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 error = true;
1440 }
1441
1442 comment = {};
1443 }
1444
1445 if (error) {
1446 return false;
1447 }
1448
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001449 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1450 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1451 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001452 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001453 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1454 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001455 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 return true;
1457}
1458
Ryan Mitchell4382e442021-07-14 12:53:01 -07001459std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001460 StringPiece tag) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001461 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001462
Ryan Mitchell4382e442021-07-14 12:53:01 -07001463 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001464 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001465 diag_->Error(android::DiagMessage(source)
1466 << "no attribute 'name' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467 return {};
1468 }
1469
Ryan Mitchell4382e442021-07-14 12:53:01 -07001470 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001471 if (!maybe_value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001472 diag_->Error(android::DiagMessage(source)
1473 << "no attribute 'value' found for tag <" << tag << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 return {};
1475 }
1476
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001477 std::u16string value16 = android::util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 android::Res_value val;
1479 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001480 diag_->Error(android::DiagMessage(source) << "invalid value '" << maybe_value.value()
1481 << "' for <" << tag << ">; must be an integer");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001482 return {};
1483 }
1484
1485 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001486 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1487 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001488 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001489}
1490
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001491bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001492 const android::Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001493
Ryan Mitchell4382e442021-07-14 12:53:01 -07001494 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001495 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001496 diag_->Error(android::DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497 return false;
1498 }
1499
Ryan Mitchell4382e442021-07-14 12:53:01 -07001500 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 if (!maybe_key) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001502 diag_->Error(android::DiagMessage(source)
1503 << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 return false;
1505 }
1506
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001507 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001508 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001511 if (!value) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001512 diag_->Error(android::DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 return false;
1514 }
1515
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001516 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 return true;
1518}
1519
Adam Lesinski86d67df2017-01-31 13:47:27 -08001520bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001522 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001523
1524 std::unique_ptr<Style> style = util::make_unique<Style>();
1525
Ryan Mitchell4382e442021-07-14 12:53:01 -07001526 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001527 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001528 // 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 -07001529 if (!maybe_parent.value().empty()) {
1530 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001531 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 if (!style->parent) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001533 diag_->Error(android::DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001534 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 }
1536
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001537 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001538 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001539 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001540 }
1541
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 } else {
1543 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 std::string style_name = out_resource->name.entry;
1545 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001548 style->parent = Reference(ResourceName(
1549 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 }
1551 }
1552
1553 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001554 const size_t depth = parser->depth();
1555 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1556 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557 // Skip text and comments.
1558 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001559 }
1560
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001561 const std::string& element_namespace = parser->element_namespace();
1562 const std::string& element_name = parser->element_name();
1563 if (element_namespace == "" && element_name == "item") {
1564 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001565
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001566 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001567 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001568 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001569 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001570 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001571 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001572
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001573 if (error) {
1574 return false;
1575 }
1576
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001577 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001579}
1580
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001581bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1582 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001583 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001584 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1585 if (resource_format == 0u) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001586 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001587 << "'" << format_attr.value() << "' is an invalid format");
1588 return false;
1589 }
1590 }
1591 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001592}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001593
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001594bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1595 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001596}
1597
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001598bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1599 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001600}
1601
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1603 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001604 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001605 out_resource->name.type =
1606 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001607
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001609
Adam Lesinski75421622017-01-06 15:20:04 -08001610 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001611 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1612 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001613 if (!maybe_translatable) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001614 diag_->Error(android::DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 << "invalid value for 'translatable'. Must be a boolean");
1616 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001617 }
Adam Lesinski75421622017-01-06 15:20:04 -08001618 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 }
Adam Lesinski75421622017-01-06 15:20:04 -08001620 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001621
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001622 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 const size_t depth = parser->depth();
1624 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1625 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001626 // Skip text and comments.
1627 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001628 }
1629
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001630 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001631 const std::string& element_namespace = parser->element_namespace();
1632 const std::string& element_name = parser->element_name();
1633 if (element_namespace.empty() && element_name == "item") {
1634 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635 if (!item) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001636 diag_->Error(android::DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637 error = true;
1638 continue;
1639 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001640 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001641 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001642
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001643 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001644 diag_->Error(android::DiagMessage(source_.WithLine(parser->line_number()))
1645 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001646 error = true;
1647 }
1648 }
1649
1650 if (error) {
1651 return false;
1652 }
1653
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001654 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001655 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001656}
1657
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1659 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001660 out_resource->name.type =
1661 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001662
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001664
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 Lesinski6f6ceb72014-11-14 14:48:12 -08001671 }
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") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001677 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001678 if (!maybe_quantity) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001679 diag_->Error(android::DiagMessage(item_source) << "<item> in <plurals> requires attribute "
1680 << "'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001681 error = true;
1682 continue;
1683 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001684
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001685 StringPiece trimmed_quantity =
1686 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001687 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001688 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001689 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001691 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001692 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001693 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001695 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001698 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 index = Plural::Other;
1700 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001701 diag_->Error(android::DiagMessage(item_source)
1702 << "<item> in <plural> has invalid value '" << trimmed_quantity
1703 << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001704 error = true;
1705 continue;
1706 }
1707
1708 if (plural->values[index]) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001709 diag_->Error(android::DiagMessage(item_source)
1710 << "duplicate quantity '" << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711 error = true;
1712 continue;
1713 }
1714
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001715 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001716 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1717 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001718 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001719 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001720
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001721 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001722
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001723 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001724 diag_->Error(android::DiagMessage(item_source)
1725 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001726 error = true;
1727 }
1728 }
1729
1730 if (error) {
1731 return false;
1732 }
1733
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001736}
1737
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001738bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1739 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001740 out_resource->name.type =
1741 ResourceNamedTypeWithDefaultName(ResourceType::kStyleable).ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001742
Donald Chai94e4a012020-01-06 13:52:41 -08001743 if (!options_.preserve_visibility_of_styleables) {
1744 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1745 // knows exactly what for.
1746 //
1747 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1748 // package-private (to be used only by the view class); themes are a different story.
1749 out_resource->visibility_level = Visibility::Level::kPublic;
1750 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001751
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001752 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001753 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001754 diag_->Warn(android::DiagMessage(out_resource->source)
1755 << "ignoring configuration '" << out_resource->config << "' for styleable "
1756 << out_resource->name.entry);
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001757 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001758 }
1759
1760 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1761
1762 std::string comment;
1763 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001764 const size_t depth = parser->depth();
1765 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1766 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001767 comment = std::string(util::TrimWhitespace(parser->comment()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001768 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001769 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770 // Ignore text.
1771 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001772 }
1773
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001774 const android::Source item_source = source_.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001775 const std::string& element_namespace = parser->element_namespace();
1776 const std::string& element_name = parser->element_name();
1777 if (element_namespace.empty() && element_name == "attr") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001778 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001779 if (!maybe_name) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001780 diag_->Error(android::DiagMessage(item_source)
1781 << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001782 error = true;
1783 continue;
1784 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001785
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001786 // If this is a declaration, the package name may be in the name. Separate
1787 // these out.
1788 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001789 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001790 if (!maybe_ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001791 diag_->Error(android::DiagMessage(item_source)
1792 << "<attr> tag has invalid name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001793 error = true;
1794 continue;
1795 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001796
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001797 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001798 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001799
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001800 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001801 ParsedResource child_resource;
1802 child_resource.name = child_ref.name.value();
1803 child_resource.source = item_source;
1804 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001805 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001806 if (options_.visibility) {
1807 child_resource.visibility_level = options_.visibility.value();
1808 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001809
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001810 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001811 error = true;
1812 continue;
1813 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001814
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001815 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001816 child_ref.SetComment(child_resource.comment);
1817 child_ref.SetSource(item_source);
1818 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001819
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001820 // Do not add referenced attributes that do not define a format to the table.
1821 CHECK(child_resource.value != nullptr);
1822 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1823
1824 CHECK(attr != nullptr);
1825 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1826 out_resource->child_resources.push_back(std::move(child_resource));
1827 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001828
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001829 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +00001830 diag_->Error(android::DiagMessage(item_source)
1831 << "unknown tag <" << element_namespace << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001832 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001833 }
1834
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001835 comment = {};
1836 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001837
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001838 if (error) {
1839 return false;
1840 }
1841
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001842 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001843 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001844}
1845
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001846} // namespace aapt