blob: 49784e3a3491d1f4e6e39de189ce9482417eb5a7 [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.
53static bool ShouldIgnoreElement(const StringPiece& ns, const 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
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070057static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
58 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
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070078static uint32_t ParseFormatType(const StringPiece& piece) {
79 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080089 for (const 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;
105 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.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700120static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
122 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800124 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski76565542016-03-10 21:55:04 -0800126
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700127 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800128 if (res->visibility_level != Visibility::Level::kUndefined) {
129 Visibility visibility;
130 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700131 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800132 visibility.source = res->source;
133 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700134 res_builder.SetVisibility(visibility);
135 }
136
137 if (res->id.is_valid()) {
138 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800140
Adam Lesinski71be7052017-12-12 16:48:07 -0800141 if (res->allow_new) {
142 AllowNew allow_new;
143 allow_new.source = res->source;
144 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700145 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800146 }
147
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800148 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700149 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800150 }
151
152 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 res->value->SetComment(std::move(res->comment));
155 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700156 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800158
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700159 if (res->staged_alias) {
160 res_builder.SetStagedId(res->staged_alias.value());
161 }
162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700164 if (!res->name.entry.empty()) {
165 if (!table->AddResource(res_builder.Build(), diag)) {
166 return false;
167 }
168 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 for (ParsedResource& child : res->child_resources) {
170 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 }
172 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800173}
174
175// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800177
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
179 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700180 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 : diag_(diag),
183 table_(table),
184 source_(source),
185 config_(config),
186 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800188// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
189// This will be used to traverse and flatten the XML string into a single std::string, with all
190// Span and Untranslatable data maintained in parallel, as indices into the string.
191class Node {
192 public:
193 virtual ~Node() = default;
194
195 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
196 // parent node as well.
197 // Returns a pointer to the child node that was added as a convenience.
198 template <typename T>
199 T* AddChild(std::unique_ptr<T> node) {
200 T* raw_ptr = node.get();
201 children.push_back(std::move(node));
202 return raw_ptr;
203 }
204
205 virtual void Build(StringBuilder* builder) const {
206 for (const auto& child : children) {
207 child->Build(builder);
208 }
209 }
210
211 std::vector<std::unique_ptr<Node>> children;
212};
213
214// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
215class SegmentNode : public Node {
216 public:
217 std::string data;
218
219 void Build(StringBuilder* builder) const override {
220 builder->AppendText(data);
221 }
222};
223
224// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
225class SpanNode : public Node {
226 public:
227 std::string name;
228
229 void Build(StringBuilder* builder) const override {
230 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
231 Node::Build(builder);
232 builder->EndSpan(span_handle);
233 }
234};
235
236// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
237class UntranslatableNode : public Node {
238 public:
239 void Build(StringBuilder* builder) const override {
240 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
241 Node::Build(builder);
242 builder->EndUntranslatable(handle);
243 }
244};
245
246// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800247bool ResourceParser::FlattenXmlSubtree(
248 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
249 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800250 std::string raw_string;
251 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800252
253 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700254 std::optional<size_t> untranslatable_start_depth;
Adam Lesinski75421622017-01-06 15:20:04 -0800255
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800256 Node root;
257 std::vector<Node*> node_stack;
258 node_stack.push_back(&root);
259
260 bool saw_span_node = false;
261 SegmentNode* first_segment = nullptr;
262 SegmentNode* last_segment = nullptr;
263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800265 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800267
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800270 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800271 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800272 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800273 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700274
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800275 last_segment = node_stack.back()->AddChild(std::move(segment_node));
276 if (first_segment == nullptr) {
277 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800278 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800279 current_text = {};
280 }
281 }
Adam Lesinski75421622017-01-06 15:20:04 -0800282
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800283 switch (event) {
284 case xml::XmlPullParser::Event::kText: {
285 current_text += parser->text();
286 raw_string += parser->text();
287 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800288
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289 case xml::XmlPullParser::Event::kStartElement: {
290 if (parser->element_namespace().empty()) {
291 // This is an HTML tag which we encode as a span. Add it to the span stack.
292 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
293 span_node->name = parser->element_name();
294 const auto end_attr_iter = parser->end_attributes();
295 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
296 ++attr_iter) {
297 span_node->name += ";";
298 span_node->name += attr_iter->name;
299 span_node->name += "=";
300 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800301 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800302
303 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
304 saw_span_node = true;
305 } else if (parser->element_namespace() == sXliffNamespaceUri) {
306 // This is an XLIFF tag, which is not encoded as a span.
307 if (parser->element_name() == "g") {
308 // Check that an 'untranslatable' tag is not already being processed. Nested
309 // <xliff:g> tags are illegal.
310 if (untranslatable_start_depth) {
311 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
312 << "illegal nested XLIFF 'g' tag");
313 return false;
314 } else {
315 // Mark the beginning of an 'untranslatable' section.
316 untranslatable_start_depth = depth;
317 node_stack.push_back(
318 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
319 }
320 } else {
321 // Ignore unknown XLIFF tags, but don't warn.
322 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
323 }
324 } else {
325 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
326 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
327 << "ignoring element '" << parser->element_name()
328 << "' with unknown namespace '" << parser->element_namespace() << "'");
329 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800330 }
Adam Lesinski75421622017-01-06 15:20:04 -0800331
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800332 // Enter one level inside the element.
333 depth++;
334 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800336 case xml::XmlPullParser::Event::kEndElement: {
337 // Return one level from within the element.
338 depth--;
339 if (depth == 0) {
340 break;
341 }
342
343 node_stack.pop_back();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700344 if (untranslatable_start_depth == depth) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800345 // This is the end of an untranslatable section.
346 untranslatable_start_depth = {};
347 }
348 } break;
349
350 default:
351 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
354 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700356 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800357 CHECK(node_stack.size() == 1u);
358 CHECK(node_stack.back() == &root);
359
360 if (!saw_span_node) {
361 // If there were no spans, we must treat this string a little differently (according to AAPT).
362 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
363 // from the last segment.
364 if (first_segment != nullptr) {
365 // Trim leading whitespace.
366 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
367 if (trimmed.size() != first_segment->data.size()) {
368 first_segment->data = trimmed.to_string();
369 }
370 }
371
372 if (last_segment != nullptr) {
373 // Trim trailing whitespace.
374 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
375 if (trimmed.size() != last_segment->data.size()) {
376 last_segment->data = trimmed.to_string();
377 }
378 }
379 }
380
381 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
382 // care of recording the correctly adjusted Spans and UntranslatableSections.
383 StringBuilder builder;
384 root.Build(&builder);
385 if (!builder) {
386 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
387 return false;
388 }
389
390 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
391 *out_raw_string = std::move(raw_string);
392 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
393 out_style_string->str = std::move(flattened_string.text);
394 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800395 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800396}
397
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 const size_t depth = parser->depth();
401 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
402 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 // Skip comments and text.
404 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800405 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406
Adam Lesinski060b53d2017-07-28 17:10:35 -0700407 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 << "root element must be <resources>");
410 return false;
411 }
412
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 break;
415 };
416
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
418 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
419 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 return false;
421 }
422 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800423}
424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
426 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700427
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 bool error = false;
429 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 const size_t depth = parser->depth();
431 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
432 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700437
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 if (!util::TrimWhitespace(parser->text()).empty()) {
440 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 << "plain text not allowed here");
442 error = true;
443 }
444 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700445 }
446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 // Skip unknown namespace.
451 continue;
452 }
453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 std::string element_name = parser->element_name();
455 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 comment = "";
457 continue;
458 }
459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 ParsedResource parsed_resource;
461 parsed_resource.config = config_;
462 parsed_resource.source = source_.WithLine(parser->line_number());
463 parsed_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +0800464 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100465 if (options_.visibility) {
466 parsed_resource.visibility_level = options_.visibility.value();
467 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468
469 // Extract the product name if it exists.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700470 if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800471 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 }
473
474 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 error = true;
477 continue;
478 }
479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 error = true;
482 }
483 }
484
485 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 for (const ResourceName& stripped_resource : stripped_resources) {
487 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700489 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
490 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 error = true;
492 }
493 }
494
495 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496}
497
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
499 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 struct ItemTypeFormat {
501 ResourceType type;
502 uint32_t format;
503 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800504
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
506 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800507
Adam Lesinski86d67df2017-01-31 13:47:27 -0800508 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
509 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
510 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
511 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
512 {"dimen",
513 {ResourceType::kDimen,
514 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
515 android::ResTable_map::TYPE_DIMENSION}},
516 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
517 {"fraction",
518 {ResourceType::kFraction,
519 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
520 android::ResTable_map::TYPE_DIMENSION}},
521 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
522 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
523 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800524
Adam Lesinski86d67df2017-01-31 13:47:27 -0800525 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
526 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
527 {"array", std::mem_fn(&ResourceParser::ParseArray)},
528 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
529 {"configVarying",
530 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
531 std::placeholders::_2, std::placeholders::_3)},
532 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
533 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
534 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700535 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800536 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
537 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
538 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700539 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700540 {"staging-public-group-final", std::mem_fn(&ResourceParser::ParseStagingPublicGroupFinal)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800541 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
542 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
543 std::placeholders::_2, std::placeholders::_3)},
544 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
545 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800548
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700550 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800551
Adam Lesinski86d67df2017-01-31 13:47:27 -0800552 bool can_be_item = true;
553 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800555 can_be_bag = false;
556
Adam Lesinskie597d682017-06-01 17:16:44 -0700557 // The default format for <item> is any. If a format attribute is present, that one will
558 // override the default.
559 resource_format = android::ResTable_map::TYPE_ANY;
560
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 // Items have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700562 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800563 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 << "<item> must have a 'type' attribute");
567 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800568 }
569
Ryan Mitchell4382e442021-07-14 12:53:01 -0700570 if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700572 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700574 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 if (!resource_format) {
576 diag_->Error(DiagMessage(out_resource->source)
577 << "'" << maybe_format.value()
578 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800579 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 }
581 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800582 } else if (resource_type == "bag") {
583 can_be_item = false;
584
585 // Bags have their type encoded in the type attribute.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700586 if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800587 resource_type = maybe_type.value().to_string();
588 } else {
589 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
590 << "<bag> must have a 'type' attribute");
591 return false;
592 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 }
594
595 // Get the name of the resource. This will be checked later, because not all
596 // XML elements require a name.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700597 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 if (resource_type == "id") {
600 if (!maybe_name) {
601 diag_->Error(DiagMessage(out_resource->source)
602 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 << "> missing 'name' attribute");
604 return false;
605 }
606
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000607 out_resource->name.type =
608 ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType();
Adam Lesinskid5083f62017-01-16 15:07:21 -0800609 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700610
611 // Ids either represent a unique resource id or reference another resource id
612 auto item = ParseItem(parser, out_resource, resource_format);
613 if (!item) {
614 return false;
615 }
616
617 String* empty = ValueCast<String>(out_resource->value.get());
618 if (empty && *empty->value == "") {
619 // If no inner element exists, represent a unique identifier
620 out_resource->value = util::make_unique<Id>();
621 } else {
y9efbbef2018-04-18 11:29:09 -0700622 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700623 if (ref && !ref->name && !ref->id) {
624 // A null reference also means there is no inner element when ids are in the form:
625 // <id name="name"/>
626 out_resource->value = util::make_unique<Id>();
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000627 } else if (!ref || ref->name.value().type.type != ResourceType::kId) {
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700628 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700629 diag_->Error(DiagMessage(out_resource->source)
630 << "<" << parser->element_name()
631 << "> inner element must either be a resource reference or empty");
632 return false;
633 }
634 }
635
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700637 } else if (resource_type == "macro") {
638 if (!maybe_name) {
639 diag_->Error(DiagMessage(out_resource->source)
640 << "<" << parser->element_name() << "> missing 'name' attribute");
641 return false;
642 }
643
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000644 out_resource->name.type =
645 ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType();
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700646 out_resource->name.entry = maybe_name.value().to_string();
647 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
649
Adam Lesinski86d67df2017-01-31 13:47:27 -0800650 if (can_be_item) {
651 const auto item_iter = elToItemMap.find(resource_type);
652 if (item_iter != elToItemMap.end()) {
653 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654
Adam Lesinski86d67df2017-01-31 13:47:27 -0800655 if (!maybe_name) {
656 diag_->Error(DiagMessage(out_resource->source)
657 << "<" << parser->element_name() << "> missing 'name' attribute");
658 return false;
659 }
660
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000661 out_resource->name.type =
662 ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800663 out_resource->name.entry = maybe_name.value().to_string();
664
Adam Lesinskie597d682017-06-01 17:16:44 -0700665 // Only use the implied format of the type when there is no explicit format.
666 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800667 resource_format = item_iter->second.format;
668 }
669
670 if (!ParseItem(parser, out_resource, resource_format)) {
671 return false;
672 }
673 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 }
676
677 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800678 if (can_be_bag) {
679 const auto bag_iter = elToBagMap.find(resource_type);
680 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700681 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700682 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700683 resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800684 if (!maybe_name) {
685 diag_->Error(DiagMessage(out_resource->source)
686 << "<" << parser->element_name() << "> missing 'name' attribute");
687 return false;
688 }
689
690 out_resource->name.entry = maybe_name.value().to_string();
691 }
692
693 // Call the associated parse method. The type will be filled in by the
694 // parse func.
695 if (!bag_iter->second(this, parser, out_resource)) {
696 return false;
697 }
698 return true;
699 }
700 }
701
702 if (can_be_item) {
703 // Try parsing the elementName (or type) as a resource. These shall only be
704 // resources like 'layout' or 'xml' and they can only be references.
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000705 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(resource_type);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800706 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 if (!maybe_name) {
708 diag_->Error(DiagMessage(out_resource->source)
709 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 << "> missing 'name' attribute");
711 return false;
712 }
713
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000714 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskid5083f62017-01-16 15:07:21 -0800715 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800716 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
717 if (!out_resource->value) {
718 diag_->Error(DiagMessage(out_resource->source)
719 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
720 return false;
721 }
722 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 }
725
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000726 // If the resource type was not recognized, write the error and return false.
727 diag_->Error(DiagMessage(out_resource->source)
Ryan Mitchell2e2c3b62019-04-26 01:16:52 -0700728 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 return false;
730}
731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
733 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 const uint32_t format) {
735 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 }
738
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 out_resource->value = ParseXml(parser, format, kNoRawString);
740 if (!out_resource->value) {
741 diag_->Error(DiagMessage(out_resource->source) << "invalid "
742 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 return false;
744 }
745 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800746}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800747
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700748std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
749 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800751
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 std::string raw_value;
753 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800754 std::vector<UntranslatableSection> untranslatable_sections;
755 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800756 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757 }
758
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700759 return FlattenedXmlSubTree{.raw_value = raw_value,
760 .style_string = style_string,
761 .untranslatable_sections = untranslatable_sections,
762 .namespace_resolver = parser,
763 .source = source_.WithLine(begin_xml_line)};
764}
765
766/**
767 * Reads the entire XML subtree and attempts to parse it as some Item,
768 * with typeMask denoting which items it can be. If allowRawValue is
769 * true, a RawString is returned if the XML couldn't be parsed as
770 * an Item. If allowRawValue is false, nullptr is returned in this
771 * case.
772 */
773std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
774 const bool allow_raw_value) {
775 auto sub_tree = CreateFlattenSubTree(parser);
776 if (!sub_tree.has_value()) {
777 return {};
778 }
779 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
780}
781
782std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
783 const uint32_t type_mask, const bool allow_raw_value,
784 ResourceTable& table,
785 const android::ConfigDescription& config,
786 IDiagnostics& diag) {
787 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800789 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700790 util::make_unique<StyledString>(table.string_pool.MakeRef(
791 xmlsub_tree.style_string,
792 StringPool::Context(StringPool::Context::kNormalPriority, config)));
793 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800794 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 }
796
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 // name.package can be empty here, as it will assume the package name of the
799 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700800 auto id = util::make_unique<Id>();
801 id->SetSource(xmlsub_tree.source);
802 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 };
804
805 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700806 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
807 xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700808 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700810 if (auto ref = ValueCast<Reference>(processed_item.get())) {
811 ref->allow_raw = allow_raw_value;
812 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 }
816
817 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700818 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800820 std::unique_ptr<String> string = util::make_unique<String>(
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700821 table.string_pool.MakeRef(xmlsub_tree.style_string.str, StringPool::Context(config)));
822 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800823 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
825
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700828 return util::make_unique<RawString>(table.string_pool.MakeRef(
829 util::TrimWhitespace(xmlsub_tree.raw_value), StringPool::Context(config)));
830 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700831 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
832 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
834 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800835}
836
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837bool ResourceParser::ParseString(xml::XmlPullParser* parser,
838 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 bool formatted = true;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700840 if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) {
841 std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 if (!maybe_formatted) {
843 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 << "invalid value for 'formatted'. Must be a boolean");
845 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800846 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800849
Adam Lesinski75421622017-01-06 15:20:04 -0800850 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700851 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
852 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -0800853 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 << "invalid value for 'translatable'. Must be a boolean");
856 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800857 }
Adam Lesinski75421622017-01-06 15:20:04 -0800858 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 out_resource->value =
862 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
863 if (!out_resource->value) {
864 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700865 return false;
866 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800867
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700868 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800869 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800870
Adam Lesinski75421622017-01-06 15:20:04 -0800871 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 if (!util::VerifyJavaStringFormat(*string_value->value)) {
873 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700874 msg << "multiple substitutions specified in non-positional format; "
875 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 if (options_.error_on_positional_arguments) {
877 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800879 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800880
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800883 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884
Adam Lesinski75421622017-01-06 15:20:04 -0800885 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
886 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 }
888 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800889}
890
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700891bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
892 auto sub_tree = CreateFlattenSubTree(parser);
893 if (!sub_tree) {
894 return false;
895 }
896
897 if (out_resource->config != ConfigDescription::DefaultConfig()) {
898 diag_->Error(DiagMessage(out_resource->source)
899 << "<macro> tags cannot be declared in configurations other than the default "
900 "configuration'");
901 return false;
902 }
903
904 auto macro = std::make_unique<Macro>();
905 macro->raw_value = std::move(sub_tree->raw_value);
906 macro->style_string = std::move(sub_tree->style_string);
907 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
908
909 for (const auto& decl : parser->package_decls()) {
910 macro->alias_namespaces.emplace_back(
911 Macro::Namespace{.alias = decl.prefix,
912 .package_name = decl.package.package,
913 .is_private = decl.package.private_namespace});
914 }
915
916 out_resource->value = std::move(macro);
917 return true;
918}
919
Adam Lesinski71be7052017-12-12 16:48:07 -0800920bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100921 if (options_.visibility) {
922 diag_->Error(DiagMessage(out_resource->source)
923 << "<public> tag not allowed with --visibility flag");
924 return false;
925 }
926
Adam Lesinski46c4d722017-08-23 13:03:56 -0700927 if (out_resource->config != ConfigDescription::DefaultConfig()) {
928 diag_->Warn(DiagMessage(out_resource->source)
929 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
930 }
931
Ryan Mitchell4382e442021-07-14 12:53:01 -0700932 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700933 if (!maybe_type) {
934 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 << "<public> must have a 'type' attribute");
936 return false;
937 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800938
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000939 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 if (!parsed_type) {
941 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
942 << maybe_type.value()
943 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 return false;
945 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800946
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000947 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800948
Ryan Mitchell4382e442021-07-14 12:53:01 -0700949 if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
950 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800952 diag_->Error(DiagMessage(out_resource->source)
953 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800955 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800958
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000959 if (parsed_type->type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700962 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700963
Adam Lesinski71be7052017-12-12 16:48:07 -0800964 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800966}
967
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700968template <typename Func>
969bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
970 const char* tag_name, IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700971 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700972 diag->Warn(DiagMessage(out_resource->source)
973 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
974 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700975 }
976
Ryan Mitchell4382e442021-07-14 12:53:01 -0700977 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 if (!maybe_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700979 diag->Error(DiagMessage(out_resource->source)
980 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800981 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800983
Iurii Makhnoaeac0d02022-02-22 06:41:58 +0000984 std::optional<ResourceNamedTypeRef> maybe_parsed_type =
985 ParseResourceNamedType(maybe_type.value());
986 if (!maybe_parsed_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700987 diag->Error(DiagMessage(out_resource->source)
988 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800989 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990 }
Iurii Makhnoaeac0d02022-02-22 06:41:58 +0000991 auto parsed_type = maybe_parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992
Ryan Mitchell4382e442021-07-14 12:53:01 -0700993 std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700994 if (!maybe_id_str) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700995 diag->Error(DiagMessage(out_resource->source)
996 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997 return false;
998 }
999
Ryan Mitchell4382e442021-07-14 12:53:01 -07001000 std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 if (!maybe_id) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001002 diag->Error(DiagMessage(out_resource->source)
1003 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 return false;
1005 }
1006
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001008 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010 const size_t depth = parser->depth();
1011 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1012 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001013 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 // Skip text.
1017 continue;
1018 }
1019
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001020 const Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 const std::string& element_namespace = parser->element_namespace();
1022 const std::string& element_name = parser->element_name();
1023 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001024 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 if (!maybe_name) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001026 diag->Error(DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 error = true;
1028 continue;
1029 }
1030
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001031 if (xml::FindNonEmptyAttribute(parser, "id")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001032 diag->Error(DiagMessage(item_source) << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 error = true;
1034 continue;
1035 }
1036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 if (xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001038 diag->Error(DiagMessage(item_source) << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 error = true;
1040 continue;
1041 }
1042
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001043 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
Iurii Makhnoaeac0d02022-02-22 06:41:58 +00001044 .name = ResourceName{{}, parsed_type, maybe_name.value().to_string()},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001045 .source = item_source,
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001046 .comment = std::move(comment),
1047 });
Felka Chang5cf069b2021-10-27 00:06:04 +08001048 comment.clear();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001050 // Execute group specific code.
1051 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001052
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001053 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001055 diag->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001056 error = true;
1057 }
1058 }
1059 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001060}
1061
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001062bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1063 ParsedResource* out_resource) {
1064 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1065 [](ParsedResource& parsed_entry, ResourceId id) {
1066 parsed_entry.id = id;
1067 parsed_entry.staged_api = true;
1068 parsed_entry.visibility_level = Visibility::Level::kPublic;
1069 });
1070}
1071
Ryan Mitchell2fedba92021-04-23 07:47:38 -07001072bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser,
1073 ParsedResource* out_resource) {
1074 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_,
1075 [](ParsedResource& parsed_entry, ResourceId id) {
1076 parsed_entry.staged_alias = StagedId{id, parsed_entry.source};
1077 });
1078}
1079
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001080bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1081 if (options_.visibility) {
1082 diag_->Error(DiagMessage(out_resource->source)
1083 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1084 return false;
1085 }
1086
1087 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1088 [](ParsedResource& parsed_entry, ResourceId id) {
1089 parsed_entry.id = id;
1090 parsed_entry.visibility_level = Visibility::Level::kPublic;
1091 });
1092}
1093
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001094bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1095 ParsedResource* out_resource) {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001096 std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001097 if (!maybe_type) {
1098 diag_->Error(DiagMessage(out_resource->source)
1099 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 << "> must have a 'type' attribute");
1101 return false;
1102 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001103
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001104 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(maybe_type.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001105 if (!parsed_type) {
1106 diag_->Error(DiagMessage(out_resource->source)
1107 << "invalid resource type '" << maybe_type.value() << "' in <"
1108 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 return false;
1110 }
1111
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001112 out_resource->name.type = parsed_type->ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001114}
1115
Adam Lesinski46c4d722017-08-23 13:03:56 -07001116bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001117 if (options_.visibility) {
1118 diag_->Error(DiagMessage(out_resource->source)
1119 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1120 return false;
1121 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001122 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1123 diag_->Warn(DiagMessage(out_resource->source)
1124 << "ignoring configuration '" << out_resource->config << "' for <"
1125 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001127
1128 if (!ParseSymbolImpl(parser, out_resource)) {
1129 return false;
1130 }
1131
Adam Lesinski71be7052017-12-12 16:48:07 -08001132 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001133 return true;
1134}
1135
1136bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1137 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1138 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001139 << "ignoring configuration '" << out_resource->config
1140 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001141 }
1142
Ryan Mitchell4382e442021-07-14 12:53:01 -07001143 std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001144 if (!overlayable_name) {
1145 diag_->Error(DiagMessage(out_resource->source)
1146 << "<overlayable> tag must have a 'name' attribute");
1147 return false;
1148 }
1149
1150 const std::string kActorUriScheme =
1151 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
Ryan Mitchell4382e442021-07-14 12:53:01 -07001152 std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001153 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1154 diag_->Error(DiagMessage(out_resource->source)
1155 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1156 << Overlayable::kActorScheme << "'");
1157 return false;
1158 }
1159
1160 // Create a overlayable entry grouping that represents this <overlayable>
1161 auto overlayable = std::make_shared<Overlayable>(
1162 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001163 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001164
Adam Lesinski46c4d722017-08-23 13:03:56 -07001165 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001166 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001167 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001168 const size_t start_depth = parser->depth();
1169 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1170 xml::XmlPullParser::Event event = parser->event();
1171 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001172 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001173 break;
1174 } else if (event == xml::XmlPullParser::Event::kEndElement
1175 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001176 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001177 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001178 continue;
1179 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001180 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001181 comment = parser->comment();
1182 continue;
1183 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001184 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001185 continue;
1186 }
1187
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001188 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001189 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001190 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001191 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001192 if (current_policies == PolicyFlags::NONE) {
Winsonb2d7f532019-02-04 16:32:43 -08001193 diag_->Error(DiagMessage(element_source)
1194 << "<item> within an <overlayable> must be inside a <policy> block");
1195 error = true;
1196 continue;
1197 }
1198
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001199 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell4382e442021-07-14 12:53:01 -07001200 std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001201 if (!item_name) {
1202 diag_->Error(DiagMessage(element_source)
1203 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001204 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001205 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001206 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001207
Ryan Mitchell4382e442021-07-14 12:53:01 -07001208 std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001209 if (!item_type) {
1210 diag_->Error(DiagMessage(element_source)
1211 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001212 error = true;
1213 continue;
1214 }
1215
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001216 std::optional<ResourceNamedTypeRef> type = ParseResourceNamedType(item_type.value());
1217 if (!type) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001218 diag_->Error(DiagMessage(element_source)
1219 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001220 << "' in <item> within an <overlayable>");
1221 error = true;
1222 continue;
1223 }
1224
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001225 OverlayableItem overlayable_item(overlayable);
1226 overlayable_item.policies = current_policies;
1227 overlayable_item.comment = comment;
1228 overlayable_item.source = element_source;
1229
1230 ParsedResource child_resource{};
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001231 child_resource.name.type = type->ToResourceNamedType();
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001232 child_resource.name.entry = item_name.value().to_string();
1233 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001234 out_resource->child_resources.push_back(std::move(child_resource));
1235
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001236 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001237 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001238 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001239 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001240 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001241 break;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001242 } else if (std::optional<StringPiece> maybe_type =
1243 xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001244 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001245 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchell939df092019-04-09 17:13:50 -07001246 for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001247 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001248 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1249 kPolicyStringToFlag.end(),
1250 [trimmed_part](const auto& it) {
1251 return trimmed_part == it.first;
1252 });
1253 if (policy == kPolicyStringToFlag.end()) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001254 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001255 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001256 error = true;
1257 continue;
1258 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001259
1260 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001261 }
Winsonb2d7f532019-02-04 16:32:43 -08001262 } else {
1263 diag_->Error(DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001264 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001265 error = true;
1266 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001267 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001268 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001269 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell939df092019-04-09 17:13:50 -07001270 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001271 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001272 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001273 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001274
1275 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001276 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001277
Adam Lesinski46c4d722017-08-23 13:03:56 -07001278 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001279}
1280
Adam Lesinski71be7052017-12-12 16:48:07 -08001281bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001282 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001283 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001284 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001285 return true;
1286 }
1287 return false;
1288}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001289
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1291 ParsedResource* out_resource) {
1292 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293}
1294
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1296 ParsedResource* out_resource, bool weak) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001297 out_resource->name.type =
1298 ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299
1300 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1302 diag_->Warn(DiagMessage(out_resource->source)
1303 << "ignoring configuration '" << out_resource->config
1304 << "' for attribute " << out_resource->name);
1305 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 }
1307
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309
Ryan Mitchell4382e442021-07-14 12:53:01 -07001310 std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 if (maybe_format) {
1312 type_mask = ParseFormatAttribute(maybe_format.value());
1313 if (type_mask == 0) {
1314 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001315 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316 return false;
1317 }
1318 }
1319
Ryan Mitchell4382e442021-07-14 12:53:01 -07001320 std::optional<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321
Ryan Mitchell4382e442021-07-14 12:53:01 -07001322 if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1324 if (!min_str.empty()) {
1325 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001327 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001330 }
1331
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 if (!maybe_min) {
1333 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1334 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 return false;
1336 }
1337 }
1338
Ryan Mitchell4382e442021-07-14 12:53:01 -07001339 if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1341 if (!max_str.empty()) {
1342 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001344 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001345 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001347 }
1348
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 if (!maybe_max) {
1350 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1351 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352 return false;
1353 }
1354 }
1355
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 if ((maybe_min || maybe_max) &&
1357 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1358 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 << "'min' and 'max' can only be used when format='integer'");
1360 return false;
1361 }
1362
1363 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001364 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 return a.symbol.name.value() < b.symbol.name.value();
1366 }
1367 };
1368
1369 std::set<Attribute::Symbol, SymbolComparator> items;
1370
1371 std::string comment;
1372 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 const size_t depth = parser->depth();
1374 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1375 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001376 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 // Skip text.
1380 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001381 }
1382
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001383 const Source item_source = source_.WithLine(parser->line_number());
1384 const std::string& element_namespace = parser->element_namespace();
1385 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001386 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 if (element_name == "enum") {
1388 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1389 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390 << "can not define an <enum>; already defined a <flag>");
1391 error = true;
1392 continue;
1393 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001396 } else if (element_name == "flag") {
1397 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1398 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399 << "can not define a <flag>; already defined an <enum>");
1400 error = true;
1401 continue;
1402 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 }
1405
Ryan Mitchell4382e442021-07-14 12:53:01 -07001406 if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001407 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001408 ParsedResource child_resource;
1409 child_resource.name = symbol.symbol.name.value();
1410 child_resource.source = item_source;
1411 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001412 if (options_.visibility) {
1413 child_resource.visibility_level = options_.visibility.value();
1414 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001415 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001417 symbol.symbol.SetComment(std::move(comment));
1418 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001419
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 auto insert_result = items.insert(std::move(symbol));
1421 if (!insert_result.second) {
1422 const Attribute::Symbol& existing_symbol = *insert_result.first;
1423 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001424 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001426
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001427 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001428 << "first defined here");
1429 error = true;
1430 }
1431 } else {
1432 error = true;
1433 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1435 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436 error = true;
1437 }
1438
1439 comment = {};
1440 }
1441
1442 if (error) {
1443 return false;
1444 }
1445
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001446 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1447 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1448 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001449 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Ryan Mitchell4382e442021-07-14 12:53:01 -07001450 attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min());
1451 attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001453 return true;
1454}
1455
Ryan Mitchell4382e442021-07-14 12:53:01 -07001456std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser,
1457 const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459
Ryan Mitchell4382e442021-07-14 12:53:01 -07001460 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001461 if (!maybe_name) {
1462 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001463 << tag << ">");
1464 return {};
1465 }
1466
Ryan Mitchell4382e442021-07-14 12:53:01 -07001467 std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001468 if (!maybe_value) {
1469 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 << tag << ">");
1471 return {};
1472 }
1473
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001475 android::Res_value val;
1476 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001477 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 << "' for <" << tag
1479 << ">; must be an integer");
1480 return {};
1481 }
1482
1483 return Attribute::Symbol{
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001484 Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId),
1485 maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001486 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001487}
1488
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001489bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1490 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001491
Ryan Mitchell4382e442021-07-14 12:53:01 -07001492 std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 if (!maybe_name) {
1494 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001495 return false;
1496 }
1497
Ryan Mitchell4382e442021-07-14 12:53:01 -07001498 std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001500 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001501 return false;
1502 }
1503
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001504 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001506
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001507 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001508 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001509 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 return false;
1511 }
1512
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001513 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514 return true;
1515}
1516
Adam Lesinski86d67df2017-01-31 13:47:27 -08001517bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001519 out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520
1521 std::unique_ptr<Style> style = util::make_unique<Style>();
1522
Ryan Mitchell4382e442021-07-14 12:53:01 -07001523 std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001524 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001525 // 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 -07001526 if (!maybe_parent.value().empty()) {
1527 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001528 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001529 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001531 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 }
1533
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001534 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001536 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001537 }
1538
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 } else {
1540 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001541 std::string style_name = out_resource->name.entry;
1542 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 style->parent_inferred = true;
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001545 style->parent = Reference(ResourceName(
1546 {}, ResourceNamedTypeWithDefaultName(ResourceType::kStyle), style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547 }
1548 }
1549
1550 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001551 const size_t depth = parser->depth();
1552 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1553 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001554 // Skip text and comments.
1555 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001556 }
1557
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001558 const std::string& element_namespace = parser->element_namespace();
1559 const std::string& element_name = parser->element_name();
1560 if (element_namespace == "" && element_name == "item") {
1561 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001562
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001563 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1564 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1565 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001567 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001568 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001569
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 if (error) {
1571 return false;
1572 }
1573
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001574 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001575 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001576}
1577
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001578bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1579 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001580 if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001581 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1582 if (resource_format == 0u) {
1583 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1584 << "'" << format_attr.value() << "' is an invalid format");
1585 return false;
1586 }
1587 }
1588 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001589}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001590
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001591bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1592 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001593}
1594
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001595bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1596 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001597}
1598
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001599bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1600 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001601 const uint32_t typeMask) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001602 out_resource->name.type =
1603 ResourceNamedTypeWithDefaultName(ResourceType::kArray).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001604
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001606
Adam Lesinski75421622017-01-06 15:20:04 -08001607 bool translatable = options_.translatable;
Ryan Mitchell4382e442021-07-14 12:53:01 -07001608 if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1609 std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
Adam Lesinski75421622017-01-06 15:20:04 -08001610 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001611 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001612 << "invalid value for 'translatable'. Must be a boolean");
1613 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001614 }
Adam Lesinski75421622017-01-06 15:20:04 -08001615 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 }
Adam Lesinski75421622017-01-06 15:20:04 -08001617 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001618
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 const size_t depth = parser->depth();
1621 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1622 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 // Skip text and comments.
1624 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001625 }
1626
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 const Source item_source = source_.WithLine(parser->line_number());
1628 const std::string& element_namespace = parser->element_namespace();
1629 const std::string& element_name = parser->element_name();
1630 if (element_namespace.empty() && element_name == "item") {
1631 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001632 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001634 error = true;
1635 continue;
1636 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001637 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001638 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001639
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001640 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1641 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1642 << "unknown tag <" << element_namespace << ":"
1643 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001644 error = true;
1645 }
1646 }
1647
1648 if (error) {
1649 return false;
1650 }
1651
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001652 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001654}
1655
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1657 ParsedResource* out_resource) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +00001658 out_resource->name.type =
1659 ResourceNamedTypeWithDefaultName(ResourceType::kPlurals).ToResourceNamedType();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001660
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001662
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001664 const size_t depth = parser->depth();
1665 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1666 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001667 // Skip text and comments.
1668 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001669 }
1670
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001671 const Source item_source = source_.WithLine(parser->line_number());
1672 const std::string& element_namespace = parser->element_namespace();
1673 const std::string& element_name = parser->element_name();
1674 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell4382e442021-07-14 12:53:01 -07001675 std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001676 if (!maybe_quantity) {
1677 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001678 << "<item> in <plurals> requires attribute "
1679 << "'quantity'");
1680 error = true;
1681 continue;
1682 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001683
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001684 StringPiece trimmed_quantity =
1685 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001686 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001687 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001690 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001691 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001693 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001696 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001697 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001698 index = Plural::Other;
1699 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001700 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001702 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703 error = true;
1704 continue;
1705 }
1706
1707 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001708 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1709 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 error = true;
1711 continue;
1712 }
1713
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1716 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001717 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001718 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001719
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001721
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001722 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1723 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1724 << element_namespace << ":"
1725 << 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()) {
1754 diag_->Warn(DiagMessage(out_resource->source)
1755 << "ignoring configuration '" << out_resource->config
1756 << "' for styleable " << out_resource->name.entry);
1757 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) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001767 comment = util::TrimWhitespace(parser->comment()).to_string();
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 const Source item_source = source_.WithLine(parser->line_number());
1775 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) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001780 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001781 error = true;
1782 continue;
1783 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001784
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001785 // If this is a declaration, the package name may be in the name. Separate
1786 // these out.
1787 // Eg. <attr name="android:text" />
Ryan Mitchell4382e442021-07-14 12:53:01 -07001788 std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001789 if (!maybe_ref) {
1790 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1791 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001792 error = true;
1793 continue;
1794 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001795
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001796 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001797 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001798
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001799 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001800 ParsedResource child_resource;
1801 child_resource.name = child_ref.name.value();
1802 child_resource.source = item_source;
1803 child_resource.comment = std::move(comment);
Felka Chang5cf069b2021-10-27 00:06:04 +08001804 comment.clear();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001805 if (options_.visibility) {
1806 child_resource.visibility_level = options_.visibility.value();
1807 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001808
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001809 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001810 error = true;
1811 continue;
1812 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001813
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001814 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001815 child_ref.SetComment(child_resource.comment);
1816 child_ref.SetSource(item_source);
1817 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001818
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001819 // Do not add referenced attributes that do not define a format to the table.
1820 CHECK(child_resource.value != nullptr);
1821 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1822
1823 CHECK(attr != nullptr);
1824 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1825 out_resource->child_resources.push_back(std::move(child_resource));
1826 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001827
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001828 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1829 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1830 << element_namespace << ":"
1831 << 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