blob: 931a14b1f650abd79ae924dcb9861a45b5725da1 [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 {
45
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070046constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070048// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
49static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080051}
52
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070053static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
54 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070068 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080072}
73
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070074static uint32_t ParseFormatType(const StringPiece& piece) {
75 if (piece == "enum") {
76 return android::ResTable_map::TYPE_ENUM;
77 } else if (piece == "flags") {
78 return android::ResTable_map::TYPE_FLAGS;
79 }
80 return ParseFormatTypeNoEnumsOrFlags(piece);
81}
82
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080085 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 StringPiece trimmed_part = util::TrimWhitespace(part);
87 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 if (type == 0) {
89 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 mask |= type;
92 }
93 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080094}
95
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070096// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080097struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 ResourceName name;
99 ConfigDescription config;
100 std::string product;
101 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800104 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700105 bool allow_new = false;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800106 Maybe<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 std::string comment;
109 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800111};
112
113// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700114static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
116 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800118 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800120
Adam Lesinski71be7052017-12-12 16:48:07 -0800121 if (res->visibility_level != Visibility::Level::kUndefined) {
122 Visibility visibility;
123 visibility.level = res->visibility_level;
124 visibility.source = res->source;
125 visibility.comment = res->comment;
126 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800128 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800130
Adam Lesinski71be7052017-12-12 16:48:07 -0800131 if (res->allow_new) {
132 AllowNew allow_new;
133 allow_new.source = res->source;
134 allow_new.comment = res->comment;
135 if (!table->SetAllowNew(res->name, allow_new, diag)) {
136 return false;
137 }
138 }
139
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800140 if (res->overlayable_item) {
141 if (!table->SetOverlayable(res->name, res->overlayable_item.value(), diag)) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800142 return false;
143 }
144 }
145
146 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 res->value->SetComment(std::move(res->comment));
149 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800150
Adam Lesinski71be7052017-12-12 16:48:07 -0800151 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
152 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800154 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 for (ParsedResource& child : res->child_resources) {
159 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 }
161 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800162}
163
164// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
168 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700169 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 : diag_(diag),
172 table_(table),
173 source_(source),
174 config_(config),
175 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800177// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
178// This will be used to traverse and flatten the XML string into a single std::string, with all
179// Span and Untranslatable data maintained in parallel, as indices into the string.
180class Node {
181 public:
182 virtual ~Node() = default;
183
184 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
185 // parent node as well.
186 // Returns a pointer to the child node that was added as a convenience.
187 template <typename T>
188 T* AddChild(std::unique_ptr<T> node) {
189 T* raw_ptr = node.get();
190 children.push_back(std::move(node));
191 return raw_ptr;
192 }
193
194 virtual void Build(StringBuilder* builder) const {
195 for (const auto& child : children) {
196 child->Build(builder);
197 }
198 }
199
200 std::vector<std::unique_ptr<Node>> children;
201};
202
203// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
204class SegmentNode : public Node {
205 public:
206 std::string data;
207
208 void Build(StringBuilder* builder) const override {
209 builder->AppendText(data);
210 }
211};
212
213// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
214class SpanNode : public Node {
215 public:
216 std::string name;
217
218 void Build(StringBuilder* builder) const override {
219 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
220 Node::Build(builder);
221 builder->EndSpan(span_handle);
222 }
223};
224
225// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
226class UntranslatableNode : public Node {
227 public:
228 void Build(StringBuilder* builder) const override {
229 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
230 Node::Build(builder);
231 builder->EndUntranslatable(handle);
232 }
233};
234
235// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800236bool ResourceParser::FlattenXmlSubtree(
237 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
238 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800239 std::string raw_string;
240 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800241
242 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
243 Maybe<size_t> untranslatable_start_depth;
244
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800245 Node root;
246 std::vector<Node*> node_stack;
247 node_stack.push_back(&root);
248
249 bool saw_span_node = false;
250 SegmentNode* first_segment = nullptr;
251 SegmentNode* last_segment = nullptr;
252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800254 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800256
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800257 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700258 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800259 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800260 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800261 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800262 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700263
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800264 last_segment = node_stack.back()->AddChild(std::move(segment_node));
265 if (first_segment == nullptr) {
266 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800267 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 current_text = {};
269 }
270 }
Adam Lesinski75421622017-01-06 15:20:04 -0800271
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800272 switch (event) {
273 case xml::XmlPullParser::Event::kText: {
274 current_text += parser->text();
275 raw_string += parser->text();
276 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800277
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800278 case xml::XmlPullParser::Event::kStartElement: {
279 if (parser->element_namespace().empty()) {
280 // This is an HTML tag which we encode as a span. Add it to the span stack.
281 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
282 span_node->name = parser->element_name();
283 const auto end_attr_iter = parser->end_attributes();
284 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
285 ++attr_iter) {
286 span_node->name += ";";
287 span_node->name += attr_iter->name;
288 span_node->name += "=";
289 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800290 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800291
292 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
293 saw_span_node = true;
294 } else if (parser->element_namespace() == sXliffNamespaceUri) {
295 // This is an XLIFF tag, which is not encoded as a span.
296 if (parser->element_name() == "g") {
297 // Check that an 'untranslatable' tag is not already being processed. Nested
298 // <xliff:g> tags are illegal.
299 if (untranslatable_start_depth) {
300 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
301 << "illegal nested XLIFF 'g' tag");
302 return false;
303 } else {
304 // Mark the beginning of an 'untranslatable' section.
305 untranslatable_start_depth = depth;
306 node_stack.push_back(
307 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
308 }
309 } else {
310 // Ignore unknown XLIFF tags, but don't warn.
311 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
312 }
313 } else {
314 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
315 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
316 << "ignoring element '" << parser->element_name()
317 << "' with unknown namespace '" << parser->element_namespace() << "'");
318 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800319 }
Adam Lesinski75421622017-01-06 15:20:04 -0800320
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800321 // Enter one level inside the element.
322 depth++;
323 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700324
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800325 case xml::XmlPullParser::Event::kEndElement: {
326 // Return one level from within the element.
327 depth--;
328 if (depth == 0) {
329 break;
330 }
331
332 node_stack.pop_back();
333 if (untranslatable_start_depth == make_value(depth)) {
334 // This is the end of an untranslatable section.
335 untranslatable_start_depth = {};
336 }
337 } break;
338
339 default:
340 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 }
343 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800345 // Sanity check to make sure we processed all the nodes.
346 CHECK(node_stack.size() == 1u);
347 CHECK(node_stack.back() == &root);
348
349 if (!saw_span_node) {
350 // If there were no spans, we must treat this string a little differently (according to AAPT).
351 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
352 // from the last segment.
353 if (first_segment != nullptr) {
354 // Trim leading whitespace.
355 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
356 if (trimmed.size() != first_segment->data.size()) {
357 first_segment->data = trimmed.to_string();
358 }
359 }
360
361 if (last_segment != nullptr) {
362 // Trim trailing whitespace.
363 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
364 if (trimmed.size() != last_segment->data.size()) {
365 last_segment->data = trimmed.to_string();
366 }
367 }
368 }
369
370 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
371 // care of recording the correctly adjusted Spans and UntranslatableSections.
372 StringBuilder builder;
373 root.Build(&builder);
374 if (!builder) {
375 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
376 return false;
377 }
378
379 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
380 *out_raw_string = std::move(raw_string);
381 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
382 out_style_string->str = std::move(flattened_string.text);
383 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800384 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800385}
386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 const size_t depth = parser->depth();
390 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
391 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 // Skip comments and text.
393 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800394 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395
Adam Lesinski060b53d2017-07-28 17:10:35 -0700396 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 << "root element must be <resources>");
399 return false;
400 }
401
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 break;
404 };
405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
407 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
408 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 return false;
410 }
411 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800412}
413
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
415 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700416
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 bool error = false;
418 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 const size_t depth = parser->depth();
420 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
421 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800425 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700426
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if (!util::TrimWhitespace(parser->text()).empty()) {
429 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 << "plain text not allowed here");
431 error = true;
432 }
433 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700434 }
435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 // Skip unknown namespace.
440 continue;
441 }
442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 std::string element_name = parser->element_name();
444 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 comment = "";
446 continue;
447 }
448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 ParsedResource parsed_resource;
450 parsed_resource.config = config_;
451 parsed_resource.source = source_.WithLine(parser->line_number());
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -0800452 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100454 if (options_.visibility) {
455 parsed_resource.visibility_level = options_.visibility.value();
456 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457
458 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700459 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800460 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 }
462
463 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 error = true;
466 continue;
467 }
468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 error = true;
471 }
472 }
473
474 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 for (const ResourceName& stripped_resource : stripped_resources) {
476 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700478 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
479 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 error = true;
481 }
482 }
483
484 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485}
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
488 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 struct ItemTypeFormat {
490 ResourceType type;
491 uint32_t format;
492 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800493
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
495 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800496
Adam Lesinski86d67df2017-01-31 13:47:27 -0800497 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
498 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
499 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
500 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
501 {"dimen",
502 {ResourceType::kDimen,
503 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
504 android::ResTable_map::TYPE_DIMENSION}},
505 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
506 {"fraction",
507 {ResourceType::kFraction,
508 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
509 android::ResTable_map::TYPE_DIMENSION}},
510 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
511 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
512 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800513
Adam Lesinski86d67df2017-01-31 13:47:27 -0800514 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
515 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
516 {"array", std::mem_fn(&ResourceParser::ParseArray)},
517 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
518 {"configVarying",
519 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
520 std::placeholders::_2, std::placeholders::_3)},
521 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
522 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
523 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700524 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800525 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
526 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
527 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
528 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
529 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
530 std::placeholders::_2, std::placeholders::_3)},
531 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
532 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800535
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800538
Adam Lesinski86d67df2017-01-31 13:47:27 -0800539 bool can_be_item = true;
540 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800542 can_be_bag = false;
543
Adam Lesinskie597d682017-06-01 17:16:44 -0700544 // The default format for <item> is any. If a format attribute is present, that one will
545 // override the default.
546 resource_format = android::ResTable_map::TYPE_ANY;
547
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700549 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800550 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 << "<item> must have a 'type' attribute");
554 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800555 }
556
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700557 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700559 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700561 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 if (!resource_format) {
563 diag_->Error(DiagMessage(out_resource->source)
564 << "'" << maybe_format.value()
565 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800566 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 }
568 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800569 } else if (resource_type == "bag") {
570 can_be_item = false;
571
572 // Bags have their type encoded in the type attribute.
573 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
574 resource_type = maybe_type.value().to_string();
575 } else {
576 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
577 << "<bag> must have a 'type' attribute");
578 return false;
579 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 }
581
582 // Get the name of the resource. This will be checked later, because not all
583 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 if (resource_type == "id") {
587 if (!maybe_name) {
588 diag_->Error(DiagMessage(out_resource->source)
589 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 << "> missing 'name' attribute");
591 return false;
592 }
593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800595 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700596
597 // Ids either represent a unique resource id or reference another resource id
598 auto item = ParseItem(parser, out_resource, resource_format);
599 if (!item) {
600 return false;
601 }
602
603 String* empty = ValueCast<String>(out_resource->value.get());
604 if (empty && *empty->value == "") {
605 // If no inner element exists, represent a unique identifier
606 out_resource->value = util::make_unique<Id>();
607 } else {
y9efbbef2018-04-18 11:29:09 -0700608 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700609 if (ref && !ref->name && !ref->id) {
610 // A null reference also means there is no inner element when ids are in the form:
611 // <id name="name"/>
612 out_resource->value = util::make_unique<Id>();
613 } else if (!ref || ref->name.value().type != ResourceType::kId) {
614 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700615 diag_->Error(DiagMessage(out_resource->source)
616 << "<" << parser->element_name()
617 << "> inner element must either be a resource reference or empty");
618 return false;
619 }
620 }
621
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 return true;
623 }
624
Adam Lesinski86d67df2017-01-31 13:47:27 -0800625 if (can_be_item) {
626 const auto item_iter = elToItemMap.find(resource_type);
627 if (item_iter != elToItemMap.end()) {
628 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629
Adam Lesinski86d67df2017-01-31 13:47:27 -0800630 if (!maybe_name) {
631 diag_->Error(DiagMessage(out_resource->source)
632 << "<" << parser->element_name() << "> missing 'name' attribute");
633 return false;
634 }
635
636 out_resource->name.type = item_iter->second.type;
637 out_resource->name.entry = maybe_name.value().to_string();
638
Adam Lesinskie597d682017-06-01 17:16:44 -0700639 // Only use the implied format of the type when there is no explicit format.
640 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800641 resource_format = item_iter->second.format;
642 }
643
644 if (!ParseItem(parser, out_resource, resource_format)) {
645 return false;
646 }
647 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 }
650
651 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800652 if (can_be_bag) {
653 const auto bag_iter = elToBagMap.find(resource_type);
654 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700655 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700656 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800657 if (!maybe_name) {
658 diag_->Error(DiagMessage(out_resource->source)
659 << "<" << parser->element_name() << "> missing 'name' attribute");
660 return false;
661 }
662
663 out_resource->name.entry = maybe_name.value().to_string();
664 }
665
666 // Call the associated parse method. The type will be filled in by the
667 // parse func.
668 if (!bag_iter->second(this, parser, out_resource)) {
669 return false;
670 }
671 return true;
672 }
673 }
674
675 if (can_be_item) {
676 // Try parsing the elementName (or type) as a resource. These shall only be
677 // resources like 'layout' or 'xml' and they can only be references.
678 const ResourceType* parsed_type = ParseResourceType(resource_type);
679 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 if (!maybe_name) {
681 diag_->Error(DiagMessage(out_resource->source)
682 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 << "> missing 'name' attribute");
684 return false;
685 }
686
Adam Lesinski86d67df2017-01-31 13:47:27 -0800687 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800688 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800689 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
690 if (!out_resource->value) {
691 diag_->Error(DiagMessage(out_resource->source)
692 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
693 return false;
694 }
695 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 }
698
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000699 // If the resource type was not recognized, write the error and return false.
700 diag_->Error(DiagMessage(out_resource->source)
Ryan Mitchell2e2c3b62019-04-26 01:16:52 -0700701 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 return false;
703}
704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
706 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 const uint32_t format) {
708 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700709 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 }
711
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 out_resource->value = ParseXml(parser, format, kNoRawString);
713 if (!out_resource->value) {
714 diag_->Error(DiagMessage(out_resource->source) << "invalid "
715 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 return false;
717 }
718 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800719}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800720
721/**
722 * Reads the entire XML subtree and attempts to parse it as some Item,
723 * with typeMask denoting which items it can be. If allowRawValue is
724 * true, a RawString is returned if the XML couldn't be parsed as
725 * an Item. If allowRawValue is false, nullptr is returned in this
726 * case.
727 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
729 const uint32_t type_mask,
730 const bool allow_raw_value) {
731 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 std::string raw_value;
734 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800735 std::vector<UntranslatableSection> untranslatable_sections;
736 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800737 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
739
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800742 std::unique_ptr<StyledString> styled_string =
743 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700744 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800745 styled_string->untranslatable_sections = std::move(untranslatable_sections);
746 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 }
748
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 // name.package can be empty here, as it will assume the package name of the
751 // table.
752 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 id->SetSource(source_.WithLine(begin_xml_line));
754 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 };
756
757 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800759 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700762 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700763 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 }
767
768 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700769 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800771 std::unique_ptr<String> string = util::make_unique<String>(
772 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
773 string->untranslatable_sections = std::move(untranslatable_sections);
774 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
776
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // We can't parse this so return a RawString if we are allowed.
779 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700780 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
781 StringPool::Context(config_)));
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700782 } else if (util::TrimWhitespace(raw_value).empty()) {
783 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
784 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 }
786 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800787}
788
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789bool ResourceParser::ParseString(xml::XmlPullParser* parser,
790 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700792 if (Maybe<StringPiece> formatted_attr =
793 xml::FindAttribute(parser, "formatted")) {
794 Maybe<bool> maybe_formatted =
795 ResourceUtils::ParseBool(formatted_attr.value());
796 if (!maybe_formatted) {
797 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 << "invalid value for 'formatted'. Must be a boolean");
799 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800800 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800803
Adam Lesinski75421622017-01-06 15:20:04 -0800804 bool translatable = options_.translatable;
805 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
806 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
807 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700808 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 << "invalid value for 'translatable'. Must be a boolean");
810 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800811 }
Adam Lesinski75421622017-01-06 15:20:04 -0800812 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800814
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 out_resource->value =
816 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
817 if (!out_resource->value) {
818 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 return false;
820 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800821
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800823 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800824
Adam Lesinski75421622017-01-06 15:20:04 -0800825 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700826 if (!util::VerifyJavaStringFormat(*string_value->value)) {
827 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 msg << "multiple substitutions specified in non-positional format; "
829 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830 if (options_.error_on_positional_arguments) {
831 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800833 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800837 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838
Adam Lesinski75421622017-01-06 15:20:04 -0800839 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
840 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841 }
842 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800843}
844
Adam Lesinski71be7052017-12-12 16:48:07 -0800845bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100846 if (options_.visibility) {
847 diag_->Error(DiagMessage(out_resource->source)
848 << "<public> tag not allowed with --visibility flag");
849 return false;
850 }
851
Adam Lesinski46c4d722017-08-23 13:03:56 -0700852 if (out_resource->config != ConfigDescription::DefaultConfig()) {
853 diag_->Warn(DiagMessage(out_resource->source)
854 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
855 }
856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
858 if (!maybe_type) {
859 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 << "<public> must have a 'type' attribute");
861 return false;
862 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800863
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
865 if (!parsed_type) {
866 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
867 << maybe_type.value()
868 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869 return false;
870 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800871
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800874 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
875 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800877 diag_->Error(DiagMessage(out_resource->source)
878 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800880 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800883
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700886 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700888
Adam Lesinski71be7052017-12-12 16:48:07 -0800889 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700890 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800891}
892
Adam Lesinski46c4d722017-08-23 13:03:56 -0700893bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100894 if (options_.visibility) {
895 diag_->Error(DiagMessage(out_resource->source)
896 << "<public-group> tag not allowed with --visibility flag");
897 return false;
898 }
899
Adam Lesinski46c4d722017-08-23 13:03:56 -0700900 if (out_resource->config != ConfigDescription::DefaultConfig()) {
901 diag_->Warn(DiagMessage(out_resource->source)
902 << "ignoring configuration '" << out_resource->config
903 << "' for <public-group> tag");
904 }
905
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
907 if (!maybe_type) {
908 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800910 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800912
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700913 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
914 if (!parsed_type) {
915 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
916 << maybe_type.value()
917 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800918 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700919 }
920
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 Maybe<StringPiece> maybe_id_str =
922 xml::FindNonEmptyAttribute(parser, "first-id");
923 if (!maybe_id_str) {
924 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700925 << "<public-group> must have a 'first-id' attribute");
926 return false;
927 }
928
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700929 Maybe<ResourceId> maybe_id =
930 ResourceUtils::ParseResourceId(maybe_id_str.value());
931 if (!maybe_id) {
932 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
933 << maybe_id_str.value()
934 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 return false;
936 }
937
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700938 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700939
940 std::string comment;
941 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 const size_t depth = parser->depth();
943 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
944 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800945 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700946 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 // Skip text.
949 continue;
950 }
951
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 const Source item_source = source_.WithLine(parser->line_number());
953 const std::string& element_namespace = parser->element_namespace();
954 const std::string& element_name = parser->element_name();
955 if (element_namespace.empty() && element_name == "public") {
956 Maybe<StringPiece> maybe_name =
957 xml::FindNonEmptyAttribute(parser, "name");
958 if (!maybe_name) {
959 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 << "<public> must have a 'name' attribute");
961 error = true;
962 continue;
963 }
964
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700965 if (xml::FindNonEmptyAttribute(parser, "id")) {
966 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 << "'id' is ignored within <public-group>");
968 error = true;
969 continue;
970 }
971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972 if (xml::FindNonEmptyAttribute(parser, "type")) {
973 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 << "'type' is ignored within <public-group>");
975 error = true;
976 continue;
977 }
978
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 ParsedResource child_resource;
980 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800981 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700982 child_resource.id = next_id;
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -0800983 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 child_resource.comment = std::move(comment);
985 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800986 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
992 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 error = true;
994 }
995 }
996 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800997}
998
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1000 ParsedResource* out_resource) {
1001 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1002 if (!maybe_type) {
1003 diag_->Error(DiagMessage(out_resource->source)
1004 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 << "> must have a 'type' attribute");
1006 return false;
1007 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001008
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1010 if (!parsed_type) {
1011 diag_->Error(DiagMessage(out_resource->source)
1012 << "invalid resource type '" << maybe_type.value() << "' in <"
1013 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return false;
1015 }
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001019}
1020
Adam Lesinski46c4d722017-08-23 13:03:56 -07001021bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001022 if (options_.visibility) {
1023 diag_->Error(DiagMessage(out_resource->source)
1024 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1025 return false;
1026 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001027 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1028 diag_->Warn(DiagMessage(out_resource->source)
1029 << "ignoring configuration '" << out_resource->config << "' for <"
1030 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001032
1033 if (!ParseSymbolImpl(parser, out_resource)) {
1034 return false;
1035 }
1036
Adam Lesinski71be7052017-12-12 16:48:07 -08001037 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001038 return true;
1039}
1040
1041bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1042 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1043 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001044 << "ignoring configuration '" << out_resource->config
1045 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001046 }
1047
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001048 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1049 if (!overlayable_name) {
1050 diag_->Error(DiagMessage(out_resource->source)
1051 << "<overlayable> tag must have a 'name' attribute");
1052 return false;
1053 }
1054
1055 const std::string kActorUriScheme =
1056 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1057 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1058 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1059 diag_->Error(DiagMessage(out_resource->source)
1060 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1061 << Overlayable::kActorScheme << "'");
1062 return false;
1063 }
1064
1065 // Create a overlayable entry grouping that represents this <overlayable>
1066 auto overlayable = std::make_shared<Overlayable>(
1067 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001068 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001069
Adam Lesinski46c4d722017-08-23 13:03:56 -07001070 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001071 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001072 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001073 const size_t start_depth = parser->depth();
1074 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1075 xml::XmlPullParser::Event event = parser->event();
1076 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001077 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001078 break;
1079 } else if (event == xml::XmlPullParser::Event::kEndElement
1080 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001081 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001082 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001083 continue;
1084 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001085 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001086 comment = parser->comment();
1087 continue;
1088 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001089 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001090 continue;
1091 }
1092
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001093 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001094 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001095 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001096 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001097 if (current_policies == PolicyFlags::NONE) {
Winsonb2d7f532019-02-04 16:32:43 -08001098 diag_->Error(DiagMessage(element_source)
1099 << "<item> within an <overlayable> must be inside a <policy> block");
1100 error = true;
1101 continue;
1102 }
1103
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001104 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001105 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1106 if (!item_name) {
1107 diag_->Error(DiagMessage(element_source)
1108 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001109 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001110 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001111 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001112
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001113 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1114 if (!item_type) {
1115 diag_->Error(DiagMessage(element_source)
1116 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001117 error = true;
1118 continue;
1119 }
1120
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001121 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001122 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001123 diag_->Error(DiagMessage(element_source)
1124 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001125 << "' in <item> within an <overlayable>");
1126 error = true;
1127 continue;
1128 }
1129
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001130 OverlayableItem overlayable_item(overlayable);
1131 overlayable_item.policies = current_policies;
1132 overlayable_item.comment = comment;
1133 overlayable_item.source = element_source;
1134
1135 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001136 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001137 child_resource.name.entry = item_name.value().to_string();
1138 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001139 out_resource->child_resources.push_back(std::move(child_resource));
1140
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001141 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001142 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001143 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001144 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001145 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001146 break;
1147 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1148 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001149 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchell939df092019-04-09 17:13:50 -07001150 for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001151 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001152 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1153 kPolicyStringToFlag.end(),
1154 [trimmed_part](const auto& it) {
1155 return trimmed_part == it.first;
1156 });
1157 if (policy == kPolicyStringToFlag.end()) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001158 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001159 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001160 error = true;
1161 continue;
1162 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001163
1164 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001165 }
Winsonb2d7f532019-02-04 16:32:43 -08001166 } else {
1167 diag_->Error(DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001168 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001169 error = true;
1170 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001171 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001172 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001173 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell939df092019-04-09 17:13:50 -07001174 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001175 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001176 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001177 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001178
1179 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001180 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001181
Adam Lesinski46c4d722017-08-23 13:03:56 -07001182 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001183}
1184
Adam Lesinski71be7052017-12-12 16:48:07 -08001185bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001186 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001187 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001188 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189 return true;
1190 }
1191 return false;
1192}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001193
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001194bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1195 ParsedResource* out_resource) {
1196 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197}
1198
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001199bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1200 ParsedResource* out_resource, bool weak) {
1201 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202
1203 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1205 diag_->Warn(DiagMessage(out_resource->source)
1206 << "ignoring configuration '" << out_resource->config
1207 << "' for attribute " << out_resource->name);
1208 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001209 }
1210
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001211 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001213 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1214 if (maybe_format) {
1215 type_mask = ParseFormatAttribute(maybe_format.value());
1216 if (type_mask == 0) {
1217 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001218 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 return false;
1220 }
1221 }
1222
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1226 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1227 if (!min_str.empty()) {
1228 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001229 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001230 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001233 }
1234
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 if (!maybe_min) {
1236 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1237 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 return false;
1239 }
1240 }
1241
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1243 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1244 if (!max_str.empty()) {
1245 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001247 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001248 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001249 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001250 }
1251
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 if (!maybe_max) {
1253 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1254 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255 return false;
1256 }
1257 }
1258
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001259 if ((maybe_min || maybe_max) &&
1260 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1261 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001262 << "'min' and 'max' can only be used when format='integer'");
1263 return false;
1264 }
1265
1266 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001267 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001268 return a.symbol.name.value() < b.symbol.name.value();
1269 }
1270 };
1271
1272 std::set<Attribute::Symbol, SymbolComparator> items;
1273
1274 std::string comment;
1275 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001276 const size_t depth = parser->depth();
1277 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1278 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001279 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001282 // Skip text.
1283 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001284 }
1285
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 const Source item_source = source_.WithLine(parser->line_number());
1287 const std::string& element_namespace = parser->element_namespace();
1288 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001289 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 if (element_name == "enum") {
1291 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1292 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001293 << "can not define an <enum>; already defined a <flag>");
1294 error = true;
1295 continue;
1296 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001297 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001298
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299 } else if (element_name == "flag") {
1300 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1301 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 << "can not define a <flag>; already defined an <enum>");
1303 error = true;
1304 continue;
1305 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 }
1308
1309 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312 ParsedResource child_resource;
1313 child_resource.name = symbol.symbol.name.value();
1314 child_resource.source = item_source;
1315 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001316 if (options_.visibility) {
1317 child_resource.visibility_level = options_.visibility.value();
1318 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 symbol.symbol.SetComment(std::move(comment));
1322 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001324 auto insert_result = items.insert(std::move(symbol));
1325 if (!insert_result.second) {
1326 const Attribute::Symbol& existing_symbol = *insert_result.first;
1327 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332 << "first defined here");
1333 error = true;
1334 }
1335 } else {
1336 error = true;
1337 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1339 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 error = true;
1341 }
1342
1343 comment = {};
1344 }
1345
1346 if (error) {
1347 return false;
1348 }
1349
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001350 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1351 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1352 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001354 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1355 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001357 return true;
1358}
1359
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1365 if (!maybe_name) {
1366 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001367 << tag << ">");
1368 return {};
1369 }
1370
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1372 if (!maybe_value) {
1373 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 << tag << ">");
1375 return {};
1376 }
1377
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 android::Res_value val;
1380 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 << "' for <" << tag
1383 << ">; must be an integer");
1384 return {};
1385 }
1386
1387 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001389 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001390}
1391
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001392bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1393 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001394
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001395 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1396 if (!maybe_name) {
1397 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398 return false;
1399 }
1400
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001401 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001403 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 return false;
1405 }
1406
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001407 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001408 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001412 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001413 return false;
1414 }
1415
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001416 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001417 return true;
1418}
1419
Adam Lesinski86d67df2017-01-31 13:47:27 -08001420bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001421 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001422 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001423
1424 std::unique_ptr<Style> style = util::make_unique<Style>();
1425
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1427 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001428 // 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 -07001429 if (!maybe_parent.value().empty()) {
1430 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001431 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001432 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001433 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001434 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001435 }
1436
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001437 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001438 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001439 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001440 }
1441
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001442 } else {
1443 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001444 std::string style_name = out_resource->name.entry;
1445 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001446 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001447 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001448 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001449 }
1450 }
1451
1452 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001453 const size_t depth = parser->depth();
1454 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1455 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 // Skip text and comments.
1457 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001458 }
1459
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001460 const std::string& element_namespace = parser->element_namespace();
1461 const std::string& element_name = parser->element_name();
1462 if (element_namespace == "" && element_name == "item") {
1463 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001464
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001465 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1466 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1467 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001469 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001471
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001472 if (error) {
1473 return false;
1474 }
1475
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001476 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001478}
1479
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001480bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1481 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1482 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1483 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1484 if (resource_format == 0u) {
1485 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1486 << "'" << format_attr.value() << "' is an invalid format");
1487 return false;
1488 }
1489 }
1490 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001491}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001492
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001493bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1494 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001495}
1496
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001497bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1498 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001499}
1500
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1502 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001503 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001504 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001505
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001506 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001507
Adam Lesinski75421622017-01-06 15:20:04 -08001508 bool translatable = options_.translatable;
1509 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1510 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1511 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001512 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 << "invalid value for 'translatable'. Must be a boolean");
1514 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001515 }
Adam Lesinski75421622017-01-06 15:20:04 -08001516 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517 }
Adam Lesinski75421622017-01-06 15:20:04 -08001518 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001519
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 const size_t depth = parser->depth();
1522 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1523 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 // Skip text and comments.
1525 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001526 }
1527
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001528 const Source item_source = source_.WithLine(parser->line_number());
1529 const std::string& element_namespace = parser->element_namespace();
1530 const std::string& element_name = parser->element_name();
1531 if (element_namespace.empty() && element_name == "item") {
1532 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001533 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 error = true;
1536 continue;
1537 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001539 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001540
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001541 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1542 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1543 << "unknown tag <" << element_namespace << ":"
1544 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 error = true;
1546 }
1547 }
1548
1549 if (error) {
1550 return false;
1551 }
1552
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001553 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001554 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001555}
1556
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1558 ParsedResource* out_resource) {
1559 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001560
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001562
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001563 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001564 const size_t depth = parser->depth();
1565 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1566 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001567 // Skip text and comments.
1568 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001569 }
1570
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001571 const Source item_source = source_.WithLine(parser->line_number());
1572 const std::string& element_namespace = parser->element_namespace();
1573 const std::string& element_name = parser->element_name();
1574 if (element_namespace.empty() && element_name == "item") {
1575 Maybe<StringPiece> maybe_quantity =
1576 xml::FindNonEmptyAttribute(parser, "quantity");
1577 if (!maybe_quantity) {
1578 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001579 << "<item> in <plurals> requires attribute "
1580 << "'quantity'");
1581 error = true;
1582 continue;
1583 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001584
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585 StringPiece trimmed_quantity =
1586 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001588 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001589 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001591 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001592 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001598 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 index = Plural::Other;
1600 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001604 error = true;
1605 continue;
1606 }
1607
1608 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1610 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 error = true;
1612 continue;
1613 }
1614
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001615 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1617 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001618 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001620
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001621 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001622
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1624 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1625 << element_namespace << ":"
1626 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001627 error = true;
1628 }
1629 }
1630
1631 if (error) {
1632 return false;
1633 }
1634
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001636 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001637}
1638
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001639bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1640 ParsedResource* out_resource) {
1641 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001642
Donald Chai94e4a012020-01-06 13:52:41 -08001643 if (!options_.preserve_visibility_of_styleables) {
1644 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1645 // knows exactly what for.
1646 //
1647 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1648 // package-private (to be used only by the view class); themes are a different story.
1649 out_resource->visibility_level = Visibility::Level::kPublic;
1650 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001651
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001652 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1654 diag_->Warn(DiagMessage(out_resource->source)
1655 << "ignoring configuration '" << out_resource->config
1656 << "' for styleable " << out_resource->name.entry);
1657 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 }
1659
1660 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1661
1662 std::string comment;
1663 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001664 const size_t depth = parser->depth();
1665 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1666 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001667 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 // Ignore text.
1671 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001672 }
1673
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 const Source item_source = source_.WithLine(parser->line_number());
1675 const std::string& element_namespace = parser->element_namespace();
1676 const std::string& element_name = parser->element_name();
1677 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001678 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001680 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001681 error = true;
1682 continue;
1683 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001684
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001685 // If this is a declaration, the package name may be in the name. Separate
1686 // these out.
1687 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001688 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 if (!maybe_ref) {
1690 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1691 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 error = true;
1693 continue;
1694 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001695
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001697 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001698
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001700 ParsedResource child_resource;
1701 child_resource.name = child_ref.name.value();
1702 child_resource.source = item_source;
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -08001703 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001704 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001705 if (options_.visibility) {
1706 child_resource.visibility_level = options_.visibility.value();
1707 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001708
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001709 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 error = true;
1711 continue;
1712 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001713
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001714 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001715 child_ref.SetComment(child_resource.comment);
1716 child_ref.SetSource(item_source);
1717 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001718
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001719 // Do not add referenced attributes that do not define a format to the table.
1720 CHECK(child_resource.value != nullptr);
1721 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1722
1723 CHECK(attr != nullptr);
1724 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1725 out_resource->child_resources.push_back(std::move(child_resource));
1726 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001727
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001728 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1729 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1730 << element_namespace << ":"
1731 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001733 }
1734
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 comment = {};
1736 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001737
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001738 if (error) {
1739 return false;
1740 }
1741
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001742 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001744}
1745
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001746} // namespace aapt