blob: 24c60b740bc35ea4f341045b1b0514bbfe880161 [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
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Winson62ac8b52019-12-04 08:36:48 -080035#include "idmap2/Policies.h"
36
Adam Lesinski2eed52e2018-02-21 15:55:58 -080037using ::aapt::ResourceUtils::StringBuilder;
38using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020039using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080040using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080041
Winson62ac8b52019-12-04 08:36:48 -080042using android::idmap2::policy::kPolicyStringToFlag;
43
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070045namespace {
46constexpr const char* kPublicGroupTag = "public-group";
47constexpr const char* kStagingPublicGroupTag = "staging-public-group";
48} // 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 Mitchell54237ff2018-12-13 15:44:29 -0800111 Maybe<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 std::string comment;
114 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800116};
117
118// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700119static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
121 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800123 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 }
Adam Lesinski76565542016-03-10 21:55:04 -0800125
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700126 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->visibility_level != Visibility::Level::kUndefined) {
128 Visibility visibility;
129 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700130 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800131 visibility.source = res->source;
132 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700133 res_builder.SetVisibility(visibility);
134 }
135
136 if (res->id.is_valid()) {
137 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800139
Adam Lesinski71be7052017-12-12 16:48:07 -0800140 if (res->allow_new) {
141 AllowNew allow_new;
142 allow_new.source = res->source;
143 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700144 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800145 }
146
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800147 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700148 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800149 }
150
151 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 res->value->SetComment(std::move(res->comment));
154 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700155 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700159 if (!res->name.entry.empty()) {
160 if (!table->AddResource(res_builder.Build(), diag)) {
161 return false;
162 }
163 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 for (ParsedResource& child : res->child_resources) {
165 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 }
167 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800168}
169
170// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
174 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700175 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 : diag_(diag),
178 table_(table),
179 source_(source),
180 config_(config),
181 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800183// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
184// This will be used to traverse and flatten the XML string into a single std::string, with all
185// Span and Untranslatable data maintained in parallel, as indices into the string.
186class Node {
187 public:
188 virtual ~Node() = default;
189
190 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
191 // parent node as well.
192 // Returns a pointer to the child node that was added as a convenience.
193 template <typename T>
194 T* AddChild(std::unique_ptr<T> node) {
195 T* raw_ptr = node.get();
196 children.push_back(std::move(node));
197 return raw_ptr;
198 }
199
200 virtual void Build(StringBuilder* builder) const {
201 for (const auto& child : children) {
202 child->Build(builder);
203 }
204 }
205
206 std::vector<std::unique_ptr<Node>> children;
207};
208
209// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
210class SegmentNode : public Node {
211 public:
212 std::string data;
213
214 void Build(StringBuilder* builder) const override {
215 builder->AppendText(data);
216 }
217};
218
219// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
220class SpanNode : public Node {
221 public:
222 std::string name;
223
224 void Build(StringBuilder* builder) const override {
225 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
226 Node::Build(builder);
227 builder->EndSpan(span_handle);
228 }
229};
230
231// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
232class UntranslatableNode : public Node {
233 public:
234 void Build(StringBuilder* builder) const override {
235 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
236 Node::Build(builder);
237 builder->EndUntranslatable(handle);
238 }
239};
240
241// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800242bool ResourceParser::FlattenXmlSubtree(
243 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
244 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800245 std::string raw_string;
246 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800247
248 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
249 Maybe<size_t> untranslatable_start_depth;
250
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800251 Node root;
252 std::vector<Node*> node_stack;
253 node_stack.push_back(&root);
254
255 bool saw_span_node = false;
256 SegmentNode* first_segment = nullptr;
257 SegmentNode* last_segment = nullptr;
258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800260 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800262
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800263 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700264 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800265 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800266 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800267 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800270 last_segment = node_stack.back()->AddChild(std::move(segment_node));
271 if (first_segment == nullptr) {
272 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800273 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 current_text = {};
275 }
276 }
Adam Lesinski75421622017-01-06 15:20:04 -0800277
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800278 switch (event) {
279 case xml::XmlPullParser::Event::kText: {
280 current_text += parser->text();
281 raw_string += parser->text();
282 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800283
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800284 case xml::XmlPullParser::Event::kStartElement: {
285 if (parser->element_namespace().empty()) {
286 // This is an HTML tag which we encode as a span. Add it to the span stack.
287 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
288 span_node->name = parser->element_name();
289 const auto end_attr_iter = parser->end_attributes();
290 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
291 ++attr_iter) {
292 span_node->name += ";";
293 span_node->name += attr_iter->name;
294 span_node->name += "=";
295 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800296 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800297
298 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
299 saw_span_node = true;
300 } else if (parser->element_namespace() == sXliffNamespaceUri) {
301 // This is an XLIFF tag, which is not encoded as a span.
302 if (parser->element_name() == "g") {
303 // Check that an 'untranslatable' tag is not already being processed. Nested
304 // <xliff:g> tags are illegal.
305 if (untranslatable_start_depth) {
306 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
307 << "illegal nested XLIFF 'g' tag");
308 return false;
309 } else {
310 // Mark the beginning of an 'untranslatable' section.
311 untranslatable_start_depth = depth;
312 node_stack.push_back(
313 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
314 }
315 } else {
316 // Ignore unknown XLIFF tags, but don't warn.
317 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
318 }
319 } else {
320 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
321 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
322 << "ignoring element '" << parser->element_name()
323 << "' with unknown namespace '" << parser->element_namespace() << "'");
324 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800325 }
Adam Lesinski75421622017-01-06 15:20:04 -0800326
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800327 // Enter one level inside the element.
328 depth++;
329 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800331 case xml::XmlPullParser::Event::kEndElement: {
332 // Return one level from within the element.
333 depth--;
334 if (depth == 0) {
335 break;
336 }
337
338 node_stack.pop_back();
339 if (untranslatable_start_depth == make_value(depth)) {
340 // This is the end of an untranslatable section.
341 untranslatable_start_depth = {};
342 }
343 } break;
344
345 default:
346 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 }
349 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700351 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800352 CHECK(node_stack.size() == 1u);
353 CHECK(node_stack.back() == &root);
354
355 if (!saw_span_node) {
356 // If there were no spans, we must treat this string a little differently (according to AAPT).
357 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
358 // from the last segment.
359 if (first_segment != nullptr) {
360 // Trim leading whitespace.
361 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
362 if (trimmed.size() != first_segment->data.size()) {
363 first_segment->data = trimmed.to_string();
364 }
365 }
366
367 if (last_segment != nullptr) {
368 // Trim trailing whitespace.
369 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
370 if (trimmed.size() != last_segment->data.size()) {
371 last_segment->data = trimmed.to_string();
372 }
373 }
374 }
375
376 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
377 // care of recording the correctly adjusted Spans and UntranslatableSections.
378 StringBuilder builder;
379 root.Build(&builder);
380 if (!builder) {
381 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
382 return false;
383 }
384
385 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
386 *out_raw_string = std::move(raw_string);
387 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
388 out_style_string->str = std::move(flattened_string.text);
389 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800390 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391}
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 const size_t depth = parser->depth();
396 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
397 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 // Skip comments and text.
399 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401
Adam Lesinski060b53d2017-07-28 17:10:35 -0700402 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 << "root element must be <resources>");
405 return false;
406 }
407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 break;
410 };
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
413 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
414 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 return false;
416 }
417 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418}
419
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
421 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700422
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 bool error = false;
424 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 const size_t depth = parser->depth();
426 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
427 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700432
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (!util::TrimWhitespace(parser->text()).empty()) {
435 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 << "plain text not allowed here");
437 error = true;
438 }
439 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700440 }
441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 // Skip unknown namespace.
446 continue;
447 }
448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 std::string element_name = parser->element_name();
450 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 comment = "";
452 continue;
453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 ParsedResource parsed_resource;
456 parsed_resource.config = config_;
457 parsed_resource.source = source_.WithLine(parser->line_number());
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -0800458 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100460 if (options_.visibility) {
461 parsed_resource.visibility_level = options_.visibility.value();
462 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463
464 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700465 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800466 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 }
468
469 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 error = true;
472 continue;
473 }
474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 error = true;
477 }
478 }
479
480 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 for (const ResourceName& stripped_resource : stripped_resources) {
482 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700484 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
485 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 error = true;
487 }
488 }
489
490 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491}
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
494 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 struct ItemTypeFormat {
496 ResourceType type;
497 uint32_t format;
498 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800499
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
501 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800502
Adam Lesinski86d67df2017-01-31 13:47:27 -0800503 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
504 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
505 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
506 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
507 {"dimen",
508 {ResourceType::kDimen,
509 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
510 android::ResTable_map::TYPE_DIMENSION}},
511 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
512 {"fraction",
513 {ResourceType::kFraction,
514 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
515 android::ResTable_map::TYPE_DIMENSION}},
516 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
517 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
518 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800519
Adam Lesinski86d67df2017-01-31 13:47:27 -0800520 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
521 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
522 {"array", std::mem_fn(&ResourceParser::ParseArray)},
523 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
524 {"configVarying",
525 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
526 std::placeholders::_2, std::placeholders::_3)},
527 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
528 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
529 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700530 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800531 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
532 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
533 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700534 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800535 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
536 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
537 std::placeholders::_2, std::placeholders::_3)},
538 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
539 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800542
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800545
Adam Lesinski86d67df2017-01-31 13:47:27 -0800546 bool can_be_item = true;
547 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800549 can_be_bag = false;
550
Adam Lesinskie597d682017-06-01 17:16:44 -0700551 // The default format for <item> is any. If a format attribute is present, that one will
552 // override the default.
553 resource_format = android::ResTable_map::TYPE_ANY;
554
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700556 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800557 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 << "<item> must have a 'type' attribute");
561 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800562 }
563
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700564 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700566 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700568 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 if (!resource_format) {
570 diag_->Error(DiagMessage(out_resource->source)
571 << "'" << maybe_format.value()
572 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800573 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 }
575 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800576 } else if (resource_type == "bag") {
577 can_be_item = false;
578
579 // Bags have their type encoded in the type attribute.
580 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
581 resource_type = maybe_type.value().to_string();
582 } else {
583 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
584 << "<bag> must have a 'type' attribute");
585 return false;
586 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 }
588
589 // Get the name of the resource. This will be checked later, because not all
590 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 if (resource_type == "id") {
594 if (!maybe_name) {
595 diag_->Error(DiagMessage(out_resource->source)
596 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 << "> missing 'name' attribute");
598 return false;
599 }
600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800602 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700603
604 // Ids either represent a unique resource id or reference another resource id
605 auto item = ParseItem(parser, out_resource, resource_format);
606 if (!item) {
607 return false;
608 }
609
610 String* empty = ValueCast<String>(out_resource->value.get());
611 if (empty && *empty->value == "") {
612 // If no inner element exists, represent a unique identifier
613 out_resource->value = util::make_unique<Id>();
614 } else {
y9efbbef2018-04-18 11:29:09 -0700615 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700616 if (ref && !ref->name && !ref->id) {
617 // A null reference also means there is no inner element when ids are in the form:
618 // <id name="name"/>
619 out_resource->value = util::make_unique<Id>();
620 } else if (!ref || ref->name.value().type != ResourceType::kId) {
621 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700622 diag_->Error(DiagMessage(out_resource->source)
623 << "<" << parser->element_name()
624 << "> inner element must either be a resource reference or empty");
625 return false;
626 }
627 }
628
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 return true;
630 }
631
Adam Lesinski86d67df2017-01-31 13:47:27 -0800632 if (can_be_item) {
633 const auto item_iter = elToItemMap.find(resource_type);
634 if (item_iter != elToItemMap.end()) {
635 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636
Adam Lesinski86d67df2017-01-31 13:47:27 -0800637 if (!maybe_name) {
638 diag_->Error(DiagMessage(out_resource->source)
639 << "<" << parser->element_name() << "> missing 'name' attribute");
640 return false;
641 }
642
643 out_resource->name.type = item_iter->second.type;
644 out_resource->name.entry = maybe_name.value().to_string();
645
Adam Lesinskie597d682017-06-01 17:16:44 -0700646 // Only use the implied format of the type when there is no explicit format.
647 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800648 resource_format = item_iter->second.format;
649 }
650
651 if (!ParseItem(parser, out_resource, resource_format)) {
652 return false;
653 }
654 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 }
657
658 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800659 if (can_be_bag) {
660 const auto bag_iter = elToBagMap.find(resource_type);
661 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700662 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700663 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
664 resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800665 if (!maybe_name) {
666 diag_->Error(DiagMessage(out_resource->source)
667 << "<" << parser->element_name() << "> missing 'name' attribute");
668 return false;
669 }
670
671 out_resource->name.entry = maybe_name.value().to_string();
672 }
673
674 // Call the associated parse method. The type will be filled in by the
675 // parse func.
676 if (!bag_iter->second(this, parser, out_resource)) {
677 return false;
678 }
679 return true;
680 }
681 }
682
683 if (can_be_item) {
684 // Try parsing the elementName (or type) as a resource. These shall only be
685 // resources like 'layout' or 'xml' and they can only be references.
686 const ResourceType* parsed_type = ParseResourceType(resource_type);
687 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 if (!maybe_name) {
689 diag_->Error(DiagMessage(out_resource->source)
690 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 << "> missing 'name' attribute");
692 return false;
693 }
694
Adam Lesinski86d67df2017-01-31 13:47:27 -0800695 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800696 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800697 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
698 if (!out_resource->value) {
699 diag_->Error(DiagMessage(out_resource->source)
700 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
701 return false;
702 }
703 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 }
706
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000707 // If the resource type was not recognized, write the error and return false.
708 diag_->Error(DiagMessage(out_resource->source)
Ryan Mitchell2e2c3b62019-04-26 01:16:52 -0700709 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 return false;
711}
712
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700713bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
714 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 const uint32_t format) {
716 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 }
719
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 out_resource->value = ParseXml(parser, format, kNoRawString);
721 if (!out_resource->value) {
722 diag_->Error(DiagMessage(out_resource->source) << "invalid "
723 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 return false;
725 }
726 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800727}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800728
729/**
730 * Reads the entire XML subtree and attempts to parse it as some Item,
731 * with typeMask denoting which items it can be. If allowRawValue is
732 * true, a RawString is returned if the XML couldn't be parsed as
733 * an Item. If allowRawValue is false, nullptr is returned in this
734 * case.
735 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
737 const uint32_t type_mask,
738 const bool allow_raw_value) {
739 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800740
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 std::string raw_value;
742 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800743 std::vector<UntranslatableSection> untranslatable_sections;
744 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800745 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 }
747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800750 std::unique_ptr<StyledString> styled_string =
751 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700752 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800753 styled_string->untranslatable_sections = std::move(untranslatable_sections);
754 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 // name.package can be empty here, as it will assume the package name of the
759 // table.
760 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 id->SetSource(source_.WithLine(begin_xml_line));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700762 table_->AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763 };
764
765 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800767 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700771 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700773 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 }
775
776 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800779 std::unique_ptr<String> string = util::make_unique<String>(
780 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
781 string->untranslatable_sections = std::move(untranslatable_sections);
782 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 }
784
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700785 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 // We can't parse this so return a RawString if we are allowed.
787 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700788 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
789 StringPool::Context(config_)));
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700790 } else if (util::TrimWhitespace(raw_value).empty()) {
791 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
792 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
794 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800795}
796
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797bool ResourceParser::ParseString(xml::XmlPullParser* parser,
798 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 if (Maybe<StringPiece> formatted_attr =
801 xml::FindAttribute(parser, "formatted")) {
802 Maybe<bool> maybe_formatted =
803 ResourceUtils::ParseBool(formatted_attr.value());
804 if (!maybe_formatted) {
805 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 << "invalid value for 'formatted'. Must be a boolean");
807 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800808 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800811
Adam Lesinski75421622017-01-06 15:20:04 -0800812 bool translatable = options_.translatable;
813 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
814 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
815 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700816 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 << "invalid value for 'translatable'. Must be a boolean");
818 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800819 }
Adam Lesinski75421622017-01-06 15:20:04 -0800820 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800822
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 out_resource->value =
824 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
825 if (!out_resource->value) {
826 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 return false;
828 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800829
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800831 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800832
Adam Lesinski75421622017-01-06 15:20:04 -0800833 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 if (!util::VerifyJavaStringFormat(*string_value->value)) {
835 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 msg << "multiple substitutions specified in non-positional format; "
837 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 if (options_.error_on_positional_arguments) {
839 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800841 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800842
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800845 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700846
Adam Lesinski75421622017-01-06 15:20:04 -0800847 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
848 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700849 }
850 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800851}
852
Adam Lesinski71be7052017-12-12 16:48:07 -0800853bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100854 if (options_.visibility) {
855 diag_->Error(DiagMessage(out_resource->source)
856 << "<public> tag not allowed with --visibility flag");
857 return false;
858 }
859
Adam Lesinski46c4d722017-08-23 13:03:56 -0700860 if (out_resource->config != ConfigDescription::DefaultConfig()) {
861 diag_->Warn(DiagMessage(out_resource->source)
862 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
863 }
864
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
866 if (!maybe_type) {
867 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 << "<public> must have a 'type' attribute");
869 return false;
870 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800871
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
873 if (!parsed_type) {
874 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
875 << maybe_type.value()
876 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877 return false;
878 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800879
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700880 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800881
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800882 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
883 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800885 diag_->Error(DiagMessage(out_resource->source)
886 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800888 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700889 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700896
Adam Lesinski71be7052017-12-12 16:48:07 -0800897 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800899}
900
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700901template <typename Func>
902bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
903 const char* tag_name, IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700904 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700905 diag->Warn(DiagMessage(out_resource->source)
906 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
907 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700908 }
909
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
911 if (!maybe_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700912 diag->Error(DiagMessage(out_resource->source)
913 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800914 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800916
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
918 if (!parsed_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700919 diag->Error(DiagMessage(out_resource->source)
920 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800921 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 }
923
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700924 Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 if (!maybe_id_str) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700926 diag->Error(DiagMessage(out_resource->source)
927 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 return false;
929 }
930
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700931 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 if (!maybe_id) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700933 diag->Error(DiagMessage(out_resource->source)
934 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 return false;
936 }
937
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700938 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700939 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700940 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700941 const size_t depth = parser->depth();
942 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
943 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800944 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 // Skip text.
948 continue;
949 }
950
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700951 const Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 const std::string& element_namespace = parser->element_namespace();
953 const std::string& element_name = parser->element_name();
954 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700955 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 if (!maybe_name) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700957 diag->Error(DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958 error = true;
959 continue;
960 }
961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 if (xml::FindNonEmptyAttribute(parser, "id")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700963 diag->Error(DiagMessage(item_source) << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700964 error = true;
965 continue;
966 }
967
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 if (xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700969 diag->Error(DiagMessage(item_source) << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970 error = true;
971 continue;
972 }
973
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700974 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
975 .name = ResourceName{{}, *parsed_type, maybe_name.value().to_string()},
976 .source = item_source,
977 .id = next_id,
978 .comment = std::move(comment),
979 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700981 // Execute group specific code.
982 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700983
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700984 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700986 diag->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 error = true;
988 }
989 }
990 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800991}
992
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700993bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
994 ParsedResource* out_resource) {
995 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
996 [](ParsedResource& parsed_entry, ResourceId id) {
997 parsed_entry.id = id;
998 parsed_entry.staged_api = true;
999 parsed_entry.visibility_level = Visibility::Level::kPublic;
1000 });
1001}
1002
1003bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1004 if (options_.visibility) {
1005 diag_->Error(DiagMessage(out_resource->source)
1006 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1007 return false;
1008 }
1009
1010 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1011 [](ParsedResource& parsed_entry, ResourceId id) {
1012 parsed_entry.id = id;
1013 parsed_entry.visibility_level = Visibility::Level::kPublic;
1014 });
1015}
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1018 ParsedResource* out_resource) {
1019 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1020 if (!maybe_type) {
1021 diag_->Error(DiagMessage(out_resource->source)
1022 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 << "> must have a 'type' attribute");
1024 return false;
1025 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1028 if (!parsed_type) {
1029 diag_->Error(DiagMessage(out_resource->source)
1030 << "invalid resource type '" << maybe_type.value() << "' in <"
1031 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 return false;
1033 }
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001037}
1038
Adam Lesinski46c4d722017-08-23 13:03:56 -07001039bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001040 if (options_.visibility) {
1041 diag_->Error(DiagMessage(out_resource->source)
1042 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1043 return false;
1044 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001045 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1046 diag_->Warn(DiagMessage(out_resource->source)
1047 << "ignoring configuration '" << out_resource->config << "' for <"
1048 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001050
1051 if (!ParseSymbolImpl(parser, out_resource)) {
1052 return false;
1053 }
1054
Adam Lesinski71be7052017-12-12 16:48:07 -08001055 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001056 return true;
1057}
1058
1059bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1060 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1061 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001062 << "ignoring configuration '" << out_resource->config
1063 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001064 }
1065
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001066 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1067 if (!overlayable_name) {
1068 diag_->Error(DiagMessage(out_resource->source)
1069 << "<overlayable> tag must have a 'name' attribute");
1070 return false;
1071 }
1072
1073 const std::string kActorUriScheme =
1074 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1075 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1076 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1077 diag_->Error(DiagMessage(out_resource->source)
1078 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1079 << Overlayable::kActorScheme << "'");
1080 return false;
1081 }
1082
1083 // Create a overlayable entry grouping that represents this <overlayable>
1084 auto overlayable = std::make_shared<Overlayable>(
1085 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001086 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001087
Adam Lesinski46c4d722017-08-23 13:03:56 -07001088 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001089 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001090 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001091 const size_t start_depth = parser->depth();
1092 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1093 xml::XmlPullParser::Event event = parser->event();
1094 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001095 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001096 break;
1097 } else if (event == xml::XmlPullParser::Event::kEndElement
1098 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001099 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001100 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001101 continue;
1102 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001103 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001104 comment = parser->comment();
1105 continue;
1106 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001107 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001108 continue;
1109 }
1110
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001111 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001112 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001113 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001114 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001115 if (current_policies == PolicyFlags::NONE) {
Winsonb2d7f532019-02-04 16:32:43 -08001116 diag_->Error(DiagMessage(element_source)
1117 << "<item> within an <overlayable> must be inside a <policy> block");
1118 error = true;
1119 continue;
1120 }
1121
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001122 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001123 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1124 if (!item_name) {
1125 diag_->Error(DiagMessage(element_source)
1126 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001127 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001128 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001129 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001130
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001131 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1132 if (!item_type) {
1133 diag_->Error(DiagMessage(element_source)
1134 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001135 error = true;
1136 continue;
1137 }
1138
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001139 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001140 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001141 diag_->Error(DiagMessage(element_source)
1142 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001143 << "' in <item> within an <overlayable>");
1144 error = true;
1145 continue;
1146 }
1147
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001148 OverlayableItem overlayable_item(overlayable);
1149 overlayable_item.policies = current_policies;
1150 overlayable_item.comment = comment;
1151 overlayable_item.source = element_source;
1152
1153 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001154 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001155 child_resource.name.entry = item_name.value().to_string();
1156 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001157 out_resource->child_resources.push_back(std::move(child_resource));
1158
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001159 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001160 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001161 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001162 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001163 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001164 break;
1165 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1166 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001167 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchell939df092019-04-09 17:13:50 -07001168 for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001169 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001170 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1171 kPolicyStringToFlag.end(),
1172 [trimmed_part](const auto& it) {
1173 return trimmed_part == it.first;
1174 });
1175 if (policy == kPolicyStringToFlag.end()) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001176 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001177 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001178 error = true;
1179 continue;
1180 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001181
1182 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001183 }
Winsonb2d7f532019-02-04 16:32:43 -08001184 } else {
1185 diag_->Error(DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001186 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001187 error = true;
1188 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001189 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001190 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001191 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell939df092019-04-09 17:13:50 -07001192 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001193 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001194 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001195 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001196
1197 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001198 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001199
Adam Lesinski46c4d722017-08-23 13:03:56 -07001200 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001201}
1202
Adam Lesinski71be7052017-12-12 16:48:07 -08001203bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001205 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001206 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 return true;
1208 }
1209 return false;
1210}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001211
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1213 ParsedResource* out_resource) {
1214 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001215}
1216
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001217bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1218 ParsedResource* out_resource, bool weak) {
1219 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220
1221 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1223 diag_->Warn(DiagMessage(out_resource->source)
1224 << "ignoring configuration '" << out_resource->config
1225 << "' for attribute " << out_resource->name);
1226 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227 }
1228
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1232 if (maybe_format) {
1233 type_mask = ParseFormatAttribute(maybe_format.value());
1234 if (type_mask == 0) {
1235 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001236 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 return false;
1238 }
1239 }
1240
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001241 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001243 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1244 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1245 if (!min_str.empty()) {
1246 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001248 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001249 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001251 }
1252
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001253 if (!maybe_min) {
1254 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1255 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001256 return false;
1257 }
1258 }
1259
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001260 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1261 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1262 if (!max_str.empty()) {
1263 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001265 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001266 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001268 }
1269
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270 if (!maybe_max) {
1271 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1272 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001273 return false;
1274 }
1275 }
1276
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 if ((maybe_min || maybe_max) &&
1278 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1279 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 << "'min' and 'max' can only be used when format='integer'");
1281 return false;
1282 }
1283
1284 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001285 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001286 return a.symbol.name.value() < b.symbol.name.value();
1287 }
1288 };
1289
1290 std::set<Attribute::Symbol, SymbolComparator> items;
1291
1292 std::string comment;
1293 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 const size_t depth = parser->depth();
1295 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1296 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001297 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001298 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001300 // Skip text.
1301 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001302 }
1303
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 const Source item_source = source_.WithLine(parser->line_number());
1305 const std::string& element_namespace = parser->element_namespace();
1306 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001307 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 if (element_name == "enum") {
1309 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1310 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 << "can not define an <enum>; already defined a <flag>");
1312 error = true;
1313 continue;
1314 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001317 } else if (element_name == "flag") {
1318 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1319 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320 << "can not define a <flag>; already defined an <enum>");
1321 error = true;
1322 continue;
1323 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001324 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325 }
1326
1327 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 ParsedResource child_resource;
1331 child_resource.name = symbol.symbol.name.value();
1332 child_resource.source = item_source;
1333 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001334 if (options_.visibility) {
1335 child_resource.visibility_level = options_.visibility.value();
1336 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001339 symbol.symbol.SetComment(std::move(comment));
1340 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001342 auto insert_result = items.insert(std::move(symbol));
1343 if (!insert_result.second) {
1344 const Attribute::Symbol& existing_symbol = *insert_result.first;
1345 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001347 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 << "first defined here");
1351 error = true;
1352 }
1353 } else {
1354 error = true;
1355 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1357 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 error = true;
1359 }
1360
1361 comment = {};
1362 }
1363
1364 if (error) {
1365 return false;
1366 }
1367
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001368 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1369 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1370 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001372 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1373 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001374 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 return true;
1376}
1377
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1383 if (!maybe_name) {
1384 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 << tag << ">");
1386 return {};
1387 }
1388
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1390 if (!maybe_value) {
1391 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 << tag << ">");
1393 return {};
1394 }
1395
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001396 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 android::Res_value val;
1398 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001400 << "' for <" << tag
1401 << ">; must be an integer");
1402 return {};
1403 }
1404
1405 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001407 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001408}
1409
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1411 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001412
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1414 if (!maybe_name) {
1415 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 return false;
1417 }
1418
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001419 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001421 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422 return false;
1423 }
1424
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001425 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001427
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001428 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001429 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001430 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431 return false;
1432 }
1433
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001434 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001435 return true;
1436}
1437
Adam Lesinski86d67df2017-01-31 13:47:27 -08001438bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001439 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001440 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441
1442 std::unique_ptr<Style> style = util::make_unique<Style>();
1443
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001444 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1445 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001446 // 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 -07001447 if (!maybe_parent.value().empty()) {
1448 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001449 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001450 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001451 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001452 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001453 }
1454
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001455 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001457 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001458 }
1459
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001460 } else {
1461 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001462 std::string style_name = out_resource->name.entry;
1463 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001466 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467 }
1468 }
1469
1470 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001471 const size_t depth = parser->depth();
1472 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1473 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001474 // Skip text and comments.
1475 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001476 }
1477
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001478 const std::string& element_namespace = parser->element_namespace();
1479 const std::string& element_name = parser->element_name();
1480 if (element_namespace == "" && element_name == "item") {
1481 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001482
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001483 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1484 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1485 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001487 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001488 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001489
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 if (error) {
1491 return false;
1492 }
1493
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001495 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001496}
1497
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001498bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1499 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1500 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1501 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1502 if (resource_format == 0u) {
1503 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1504 << "'" << format_attr.value() << "' is an invalid format");
1505 return false;
1506 }
1507 }
1508 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001509}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001510
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001511bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1512 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001513}
1514
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001515bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1516 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001517}
1518
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1520 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001521 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001522 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001523
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001525
Adam Lesinski75421622017-01-06 15:20:04 -08001526 bool translatable = options_.translatable;
1527 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1528 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1529 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 << "invalid value for 'translatable'. Must be a boolean");
1532 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001533 }
Adam Lesinski75421622017-01-06 15:20:04 -08001534 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 }
Adam Lesinski75421622017-01-06 15:20:04 -08001536 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001537
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001538 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 const size_t depth = parser->depth();
1540 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1541 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 // Skip text and comments.
1543 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001544 }
1545
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001546 const Source item_source = source_.WithLine(parser->line_number());
1547 const std::string& element_namespace = parser->element_namespace();
1548 const std::string& element_name = parser->element_name();
1549 if (element_namespace.empty() && element_name == "item") {
1550 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001551 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001552 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001553 error = true;
1554 continue;
1555 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001557 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001558
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001559 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1560 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1561 << "unknown tag <" << element_namespace << ":"
1562 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001563 error = true;
1564 }
1565 }
1566
1567 if (error) {
1568 return false;
1569 }
1570
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001571 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001572 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001573}
1574
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1576 ParsedResource* out_resource) {
1577 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001578
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001579 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001580
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001581 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 const size_t depth = parser->depth();
1583 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1584 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 // Skip text and comments.
1586 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001587 }
1588
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 const Source item_source = source_.WithLine(parser->line_number());
1590 const std::string& element_namespace = parser->element_namespace();
1591 const std::string& element_name = parser->element_name();
1592 if (element_namespace.empty() && element_name == "item") {
1593 Maybe<StringPiece> maybe_quantity =
1594 xml::FindNonEmptyAttribute(parser, "quantity");
1595 if (!maybe_quantity) {
1596 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 << "<item> in <plurals> requires attribute "
1598 << "'quantity'");
1599 error = true;
1600 continue;
1601 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001602
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 StringPiece trimmed_quantity =
1604 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001607 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001608 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001610 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001616 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001617 index = Plural::Other;
1618 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001619 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001620 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001621 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001622 error = true;
1623 continue;
1624 }
1625
1626 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1628 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001629 error = true;
1630 continue;
1631 }
1632
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001634 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1635 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001636 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001638
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001639 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001640
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001641 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1642 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1643 << element_namespace << ":"
1644 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 error = true;
1646 }
1647 }
1648
1649 if (error) {
1650 return false;
1651 }
1652
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001654 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001655}
1656
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001657bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1658 ParsedResource* out_resource) {
1659 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001660
Donald Chai94e4a012020-01-06 13:52:41 -08001661 if (!options_.preserve_visibility_of_styleables) {
1662 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1663 // knows exactly what for.
1664 //
1665 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1666 // package-private (to be used only by the view class); themes are a different story.
1667 out_resource->visibility_level = Visibility::Level::kPublic;
1668 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001669
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001671 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1672 diag_->Warn(DiagMessage(out_resource->source)
1673 << "ignoring configuration '" << out_resource->config
1674 << "' for styleable " << out_resource->name.entry);
1675 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676 }
1677
1678 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1679
1680 std::string comment;
1681 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 const size_t depth = parser->depth();
1683 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1684 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001685 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001686 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001687 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001688 // Ignore text.
1689 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001690 }
1691
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001692 const Source item_source = source_.WithLine(parser->line_number());
1693 const std::string& element_namespace = parser->element_namespace();
1694 const std::string& element_name = parser->element_name();
1695 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001696 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001697 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001698 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 error = true;
1700 continue;
1701 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001702
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001703 // If this is a declaration, the package name may be in the name. Separate
1704 // these out.
1705 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001706 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001707 if (!maybe_ref) {
1708 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1709 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 error = true;
1711 continue;
1712 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001713
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001715 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001716
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001717 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001718 ParsedResource child_resource;
1719 child_resource.name = child_ref.name.value();
1720 child_resource.source = item_source;
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -08001721 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001722 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001723 if (options_.visibility) {
1724 child_resource.visibility_level = options_.visibility.value();
1725 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001726
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728 error = true;
1729 continue;
1730 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001731
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001733 child_ref.SetComment(child_resource.comment);
1734 child_ref.SetSource(item_source);
1735 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001736
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001737 // Do not add referenced attributes that do not define a format to the table.
1738 CHECK(child_resource.value != nullptr);
1739 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1740
1741 CHECK(attr != nullptr);
1742 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1743 out_resource->child_resources.push_back(std::move(child_resource));
1744 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001745
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001746 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1747 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1748 << element_namespace << ":"
1749 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001750 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001751 }
1752
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001753 comment = {};
1754 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001755
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001756 if (error) {
1757 return false;
1758 }
1759
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001760 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001761 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001762}
1763
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001764} // namespace aapt