blob: 234cbc4b37e080a70c3243862923726f77f4b131 [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 Lesinski76565542016-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());
452 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100453 if (options_.visibility) {
454 parsed_resource.visibility_level = options_.visibility.value();
455 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456
457 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700458 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800459 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 }
461
462 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 error = true;
465 continue;
466 }
467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 error = true;
470 }
471 }
472
473 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 for (const ResourceName& stripped_resource : stripped_resources) {
475 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700477 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
478 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 error = true;
480 }
481 }
482
483 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800484}
485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
487 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 struct ItemTypeFormat {
489 ResourceType type;
490 uint32_t format;
491 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800492
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
494 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800495
Adam Lesinski86d67df2017-01-31 13:47:27 -0800496 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
497 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
498 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
499 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
500 {"dimen",
501 {ResourceType::kDimen,
502 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
503 android::ResTable_map::TYPE_DIMENSION}},
504 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
505 {"fraction",
506 {ResourceType::kFraction,
507 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
508 android::ResTable_map::TYPE_DIMENSION}},
509 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
510 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
511 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800512
Adam Lesinski86d67df2017-01-31 13:47:27 -0800513 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
514 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
515 {"array", std::mem_fn(&ResourceParser::ParseArray)},
516 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
517 {"configVarying",
518 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
519 std::placeholders::_2, std::placeholders::_3)},
520 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
521 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
522 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700523 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800524 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
525 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
526 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
527 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
528 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
529 std::placeholders::_2, std::placeholders::_3)},
530 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
531 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800534
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800537
Adam Lesinski86d67df2017-01-31 13:47:27 -0800538 bool can_be_item = true;
539 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800541 can_be_bag = false;
542
Adam Lesinskie597d682017-06-01 17:16:44 -0700543 // The default format for <item> is any. If a format attribute is present, that one will
544 // override the default.
545 resource_format = android::ResTable_map::TYPE_ANY;
546
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700548 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800549 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 << "<item> must have a 'type' attribute");
553 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800554 }
555
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700556 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700558 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700560 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 if (!resource_format) {
562 diag_->Error(DiagMessage(out_resource->source)
563 << "'" << maybe_format.value()
564 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800565 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 }
567 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800568 } else if (resource_type == "bag") {
569 can_be_item = false;
570
571 // Bags have their type encoded in the type attribute.
572 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
573 resource_type = maybe_type.value().to_string();
574 } else {
575 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
576 << "<bag> must have a 'type' attribute");
577 return false;
578 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 }
580
581 // Get the name of the resource. This will be checked later, because not all
582 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 if (resource_type == "id") {
586 if (!maybe_name) {
587 diag_->Error(DiagMessage(out_resource->source)
588 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 << "> missing 'name' attribute");
590 return false;
591 }
592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800594 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700595
596 // Ids either represent a unique resource id or reference another resource id
597 auto item = ParseItem(parser, out_resource, resource_format);
598 if (!item) {
599 return false;
600 }
601
602 String* empty = ValueCast<String>(out_resource->value.get());
603 if (empty && *empty->value == "") {
604 // If no inner element exists, represent a unique identifier
605 out_resource->value = util::make_unique<Id>();
606 } else {
y9efbbef2018-04-18 11:29:09 -0700607 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700608 if (ref && !ref->name && !ref->id) {
609 // A null reference also means there is no inner element when ids are in the form:
610 // <id name="name"/>
611 out_resource->value = util::make_unique<Id>();
612 } else if (!ref || ref->name.value().type != ResourceType::kId) {
613 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700614 diag_->Error(DiagMessage(out_resource->source)
615 << "<" << parser->element_name()
616 << "> inner element must either be a resource reference or empty");
617 return false;
618 }
619 }
620
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 return true;
622 }
623
Adam Lesinski86d67df2017-01-31 13:47:27 -0800624 if (can_be_item) {
625 const auto item_iter = elToItemMap.find(resource_type);
626 if (item_iter != elToItemMap.end()) {
627 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628
Adam Lesinski86d67df2017-01-31 13:47:27 -0800629 if (!maybe_name) {
630 diag_->Error(DiagMessage(out_resource->source)
631 << "<" << parser->element_name() << "> missing 'name' attribute");
632 return false;
633 }
634
635 out_resource->name.type = item_iter->second.type;
636 out_resource->name.entry = maybe_name.value().to_string();
637
Adam Lesinskie597d682017-06-01 17:16:44 -0700638 // Only use the implied format of the type when there is no explicit format.
639 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800640 resource_format = item_iter->second.format;
641 }
642
643 if (!ParseItem(parser, out_resource, resource_format)) {
644 return false;
645 }
646 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
649
650 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800651 if (can_be_bag) {
652 const auto bag_iter = elToBagMap.find(resource_type);
653 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700654 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700655 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800656 if (!maybe_name) {
657 diag_->Error(DiagMessage(out_resource->source)
658 << "<" << parser->element_name() << "> missing 'name' attribute");
659 return false;
660 }
661
662 out_resource->name.entry = maybe_name.value().to_string();
663 }
664
665 // Call the associated parse method. The type will be filled in by the
666 // parse func.
667 if (!bag_iter->second(this, parser, out_resource)) {
668 return false;
669 }
670 return true;
671 }
672 }
673
674 if (can_be_item) {
675 // Try parsing the elementName (or type) as a resource. These shall only be
676 // resources like 'layout' or 'xml' and they can only be references.
677 const ResourceType* parsed_type = ParseResourceType(resource_type);
678 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 if (!maybe_name) {
680 diag_->Error(DiagMessage(out_resource->source)
681 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 << "> missing 'name' attribute");
683 return false;
684 }
685
Adam Lesinski86d67df2017-01-31 13:47:27 -0800686 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800687 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800688 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
689 if (!out_resource->value) {
690 diag_->Error(DiagMessage(out_resource->source)
691 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
692 return false;
693 }
694 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 }
697
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000698 // If the resource type was not recognized, write the error and return false.
699 diag_->Error(DiagMessage(out_resource->source)
Ryan Mitchell2e2c3b62019-04-26 01:16:52 -0700700 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 return false;
702}
703
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
705 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700706 const uint32_t format) {
707 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 }
710
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 out_resource->value = ParseXml(parser, format, kNoRawString);
712 if (!out_resource->value) {
713 diag_->Error(DiagMessage(out_resource->source) << "invalid "
714 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 return false;
716 }
717 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800718}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800719
720/**
721 * Reads the entire XML subtree and attempts to parse it as some Item,
722 * with typeMask denoting which items it can be. If allowRawValue is
723 * true, a RawString is returned if the XML couldn't be parsed as
724 * an Item. If allowRawValue is false, nullptr is returned in this
725 * case.
726 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
728 const uint32_t type_mask,
729 const bool allow_raw_value) {
730 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 std::string raw_value;
733 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800734 std::vector<UntranslatableSection> untranslatable_sections;
735 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800736 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700737 }
738
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800741 std::unique_ptr<StyledString> styled_string =
742 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700743 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800744 styled_string->untranslatable_sections = std::move(untranslatable_sections);
745 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 }
747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 // name.package can be empty here, as it will assume the package name of the
750 // table.
751 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 id->SetSource(source_.WithLine(begin_xml_line));
753 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 };
755
756 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800758 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700762 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 }
766
767 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800770 std::unique_ptr<String> string = util::make_unique<String>(
771 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
772 string->untranslatable_sections = std::move(untranslatable_sections);
773 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 }
775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700777 // We can't parse this so return a RawString if we are allowed.
778 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700779 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
780 StringPool::Context(config_)));
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700781 } else if (util::TrimWhitespace(raw_value).empty()) {
782 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
783 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 }
785 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800786}
787
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700788bool ResourceParser::ParseString(xml::XmlPullParser* parser,
789 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791 if (Maybe<StringPiece> formatted_attr =
792 xml::FindAttribute(parser, "formatted")) {
793 Maybe<bool> maybe_formatted =
794 ResourceUtils::ParseBool(formatted_attr.value());
795 if (!maybe_formatted) {
796 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 << "invalid value for 'formatted'. Must be a boolean");
798 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800799 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800802
Adam Lesinski75421622017-01-06 15:20:04 -0800803 bool translatable = options_.translatable;
804 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
805 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
806 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700808 << "invalid value for 'translatable'. Must be a boolean");
809 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800810 }
Adam Lesinski75421622017-01-06 15:20:04 -0800811 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800813
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 out_resource->value =
815 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
816 if (!out_resource->value) {
817 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 return false;
819 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800820
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800822 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800823
Adam Lesinski75421622017-01-06 15:20:04 -0800824 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700825 if (!util::VerifyJavaStringFormat(*string_value->value)) {
826 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 msg << "multiple substitutions specified in non-positional format; "
828 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700829 if (options_.error_on_positional_arguments) {
830 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800832 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800836 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837
Adam Lesinski75421622017-01-06 15:20:04 -0800838 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
839 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 }
841 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800842}
843
Adam Lesinski71be7052017-12-12 16:48:07 -0800844bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100845 if (options_.visibility) {
846 diag_->Error(DiagMessage(out_resource->source)
847 << "<public> tag not allowed with --visibility flag");
848 return false;
849 }
850
Adam Lesinski46c4d722017-08-23 13:03:56 -0700851 if (out_resource->config != ConfigDescription::DefaultConfig()) {
852 diag_->Warn(DiagMessage(out_resource->source)
853 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
854 }
855
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700856 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
857 if (!maybe_type) {
858 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 << "<public> must have a 'type' attribute");
860 return false;
861 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700863 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
864 if (!parsed_type) {
865 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
866 << maybe_type.value()
867 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 return false;
869 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800872
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800873 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
874 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700875 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800876 diag_->Error(DiagMessage(out_resource->source)
877 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800879 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700880 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700881 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800882
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700885 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700886 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700887
Adam Lesinski71be7052017-12-12 16:48:07 -0800888 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800890}
891
Adam Lesinski46c4d722017-08-23 13:03:56 -0700892bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100893 if (options_.visibility) {
894 diag_->Error(DiagMessage(out_resource->source)
895 << "<public-group> tag not allowed with --visibility flag");
896 return false;
897 }
898
Adam Lesinski46c4d722017-08-23 13:03:56 -0700899 if (out_resource->config != ConfigDescription::DefaultConfig()) {
900 diag_->Warn(DiagMessage(out_resource->source)
901 << "ignoring configuration '" << out_resource->config
902 << "' for <public-group> tag");
903 }
904
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
906 if (!maybe_type) {
907 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800909 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800911
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
913 if (!parsed_type) {
914 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
915 << maybe_type.value()
916 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800917 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 }
919
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700920 Maybe<StringPiece> maybe_id_str =
921 xml::FindNonEmptyAttribute(parser, "first-id");
922 if (!maybe_id_str) {
923 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 << "<public-group> must have a 'first-id' attribute");
925 return false;
926 }
927
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 Maybe<ResourceId> maybe_id =
929 ResourceUtils::ParseResourceId(maybe_id_str.value());
930 if (!maybe_id) {
931 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
932 << maybe_id_str.value()
933 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700934 return false;
935 }
936
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700937 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700938
939 std::string comment;
940 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700941 const size_t depth = parser->depth();
942 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
943 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800944 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 // Skip text.
948 continue;
949 }
950
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 const Source item_source = source_.WithLine(parser->line_number());
952 const std::string& element_namespace = parser->element_namespace();
953 const std::string& element_name = parser->element_name();
954 if (element_namespace.empty() && element_name == "public") {
955 Maybe<StringPiece> maybe_name =
956 xml::FindNonEmptyAttribute(parser, "name");
957 if (!maybe_name) {
958 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959 << "<public> must have a 'name' attribute");
960 error = true;
961 continue;
962 }
963
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 if (xml::FindNonEmptyAttribute(parser, "id")) {
965 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966 << "'id' is ignored within <public-group>");
967 error = true;
968 continue;
969 }
970
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 if (xml::FindNonEmptyAttribute(parser, "type")) {
972 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700973 << "'type' is ignored within <public-group>");
974 error = true;
975 continue;
976 }
977
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700978 ParsedResource child_resource;
979 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800980 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 child_resource.id = next_id;
982 child_resource.comment = std::move(comment);
983 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800984 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
990 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991 error = true;
992 }
993 }
994 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800995}
996
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
998 ParsedResource* out_resource) {
999 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1000 if (!maybe_type) {
1001 diag_->Error(DiagMessage(out_resource->source)
1002 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 << "> must have a 'type' attribute");
1004 return false;
1005 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1008 if (!parsed_type) {
1009 diag_->Error(DiagMessage(out_resource->source)
1010 << "invalid resource type '" << maybe_type.value() << "' in <"
1011 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 return false;
1013 }
1014
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001016 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001017}
1018
Adam Lesinski46c4d722017-08-23 13:03:56 -07001019bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001020 if (options_.visibility) {
1021 diag_->Error(DiagMessage(out_resource->source)
1022 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1023 return false;
1024 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001025 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1026 diag_->Warn(DiagMessage(out_resource->source)
1027 << "ignoring configuration '" << out_resource->config << "' for <"
1028 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001030
1031 if (!ParseSymbolImpl(parser, out_resource)) {
1032 return false;
1033 }
1034
Adam Lesinski71be7052017-12-12 16:48:07 -08001035 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001036 return true;
1037}
1038
1039bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1040 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1041 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001042 << "ignoring configuration '" << out_resource->config
1043 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001044 }
1045
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001046 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1047 if (!overlayable_name) {
1048 diag_->Error(DiagMessage(out_resource->source)
1049 << "<overlayable> tag must have a 'name' attribute");
1050 return false;
1051 }
1052
1053 const std::string kActorUriScheme =
1054 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1055 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1056 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1057 diag_->Error(DiagMessage(out_resource->source)
1058 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1059 << Overlayable::kActorScheme << "'");
1060 return false;
1061 }
1062
1063 // Create a overlayable entry grouping that represents this <overlayable>
1064 auto overlayable = std::make_shared<Overlayable>(
1065 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001066 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001067
Adam Lesinski46c4d722017-08-23 13:03:56 -07001068 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001069 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001070 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001071 const size_t start_depth = parser->depth();
1072 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1073 xml::XmlPullParser::Event event = parser->event();
1074 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001075 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001076 break;
1077 } else if (event == xml::XmlPullParser::Event::kEndElement
1078 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001079 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001080 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001081 continue;
1082 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001083 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001084 comment = parser->comment();
1085 continue;
1086 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001087 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001088 continue;
1089 }
1090
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001091 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001092 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001093 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001094 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001095 if (current_policies == PolicyFlags::NONE) {
Winsonb2d7f532019-02-04 16:32:43 -08001096 diag_->Error(DiagMessage(element_source)
1097 << "<item> within an <overlayable> must be inside a <policy> block");
1098 error = true;
1099 continue;
1100 }
1101
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001102 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001103 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1104 if (!item_name) {
1105 diag_->Error(DiagMessage(element_source)
1106 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001107 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001108 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001109 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001110
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001111 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1112 if (!item_type) {
1113 diag_->Error(DiagMessage(element_source)
1114 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001115 error = true;
1116 continue;
1117 }
1118
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001119 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001120 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001121 diag_->Error(DiagMessage(element_source)
1122 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001123 << "' in <item> within an <overlayable>");
1124 error = true;
1125 continue;
1126 }
1127
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001128 OverlayableItem overlayable_item(overlayable);
1129 overlayable_item.policies = current_policies;
1130 overlayable_item.comment = comment;
1131 overlayable_item.source = element_source;
1132
1133 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001134 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001135 child_resource.name.entry = item_name.value().to_string();
1136 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001137 out_resource->child_resources.push_back(std::move(child_resource));
1138
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001139 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001140 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001141 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001142 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001143 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001144 break;
1145 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1146 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001147 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchell939df092019-04-09 17:13:50 -07001148 for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001149 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001150 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1151 kPolicyStringToFlag.end(),
1152 [trimmed_part](const auto& it) {
1153 return trimmed_part == it.first;
1154 });
1155 if (policy == kPolicyStringToFlag.end()) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001156 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001157 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001158 error = true;
1159 continue;
1160 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001161
1162 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001163 }
Winsonb2d7f532019-02-04 16:32:43 -08001164 } else {
1165 diag_->Error(DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001166 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001167 error = true;
1168 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001169 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001170 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001171 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell939df092019-04-09 17:13:50 -07001172 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001173 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001174 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001175 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001176
1177 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001178 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001179
Adam Lesinski46c4d722017-08-23 13:03:56 -07001180 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001181}
1182
Adam Lesinski71be7052017-12-12 16:48:07 -08001183bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001184 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001185 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001186 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001187 return true;
1188 }
1189 return false;
1190}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001191
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001192bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1193 ParsedResource* out_resource) {
1194 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001195}
1196
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001197bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1198 ParsedResource* out_resource, bool weak) {
1199 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200
1201 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1203 diag_->Warn(DiagMessage(out_resource->source)
1204 << "ignoring configuration '" << out_resource->config
1205 << "' for attribute " << out_resource->name);
1206 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 }
1208
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001209 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001211 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1212 if (maybe_format) {
1213 type_mask = ParseFormatAttribute(maybe_format.value());
1214 if (type_mask == 0) {
1215 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001216 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001217 return false;
1218 }
1219 }
1220
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001222
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1224 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1225 if (!min_str.empty()) {
1226 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001228 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001231 }
1232
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233 if (!maybe_min) {
1234 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1235 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 return false;
1237 }
1238 }
1239
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1241 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1242 if (!max_str.empty()) {
1243 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001244 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001245 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001248 }
1249
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001250 if (!maybe_max) {
1251 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1252 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001253 return false;
1254 }
1255 }
1256
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 if ((maybe_min || maybe_max) &&
1258 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1259 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001260 << "'min' and 'max' can only be used when format='integer'");
1261 return false;
1262 }
1263
1264 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001265 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001266 return a.symbol.name.value() < b.symbol.name.value();
1267 }
1268 };
1269
1270 std::set<Attribute::Symbol, SymbolComparator> items;
1271
1272 std::string comment;
1273 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001274 const size_t depth = parser->depth();
1275 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1276 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001277 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 // Skip text.
1281 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001282 }
1283
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001284 const Source item_source = source_.WithLine(parser->line_number());
1285 const std::string& element_namespace = parser->element_namespace();
1286 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001287 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001288 if (element_name == "enum") {
1289 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1290 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 << "can not define an <enum>; already defined a <flag>");
1292 error = true;
1293 continue;
1294 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001296
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001297 } else if (element_name == "flag") {
1298 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1299 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001300 << "can not define a <flag>; already defined an <enum>");
1301 error = true;
1302 continue;
1303 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305 }
1306
1307 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 ParsedResource child_resource;
1311 child_resource.name = symbol.symbol.name.value();
1312 child_resource.source = item_source;
1313 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001314 if (options_.visibility) {
1315 child_resource.visibility_level = options_.visibility.value();
1316 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001317 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001318
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 symbol.symbol.SetComment(std::move(comment));
1320 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001322 auto insert_result = items.insert(std::move(symbol));
1323 if (!insert_result.second) {
1324 const Attribute::Symbol& existing_symbol = *insert_result.first;
1325 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 << "first defined here");
1331 error = true;
1332 }
1333 } else {
1334 error = true;
1335 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001336 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1337 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 error = true;
1339 }
1340
1341 comment = {};
1342 }
1343
1344 if (error) {
1345 return false;
1346 }
1347
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001348 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1349 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1350 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001351 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001352 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1353 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 return true;
1356}
1357
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1363 if (!maybe_name) {
1364 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 << tag << ">");
1366 return {};
1367 }
1368
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1370 if (!maybe_value) {
1371 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 << tag << ">");
1373 return {};
1374 }
1375
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 android::Res_value val;
1378 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001380 << "' for <" << tag
1381 << ">; must be an integer");
1382 return {};
1383 }
1384
1385 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001387 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001388}
1389
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1391 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001392
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1394 if (!maybe_name) {
1395 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 return false;
1397 }
1398
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001399 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001401 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402 return false;
1403 }
1404
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001405 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001407
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001408 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001409 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 return false;
1412 }
1413
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001414 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001415 return true;
1416}
1417
Adam Lesinski86d67df2017-01-31 13:47:27 -08001418bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001420 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421
1422 std::unique_ptr<Style> style = util::make_unique<Style>();
1423
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001424 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1425 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001426 // 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 -07001427 if (!maybe_parent.value().empty()) {
1428 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001429 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001430 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001432 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001433 }
1434
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001435 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001437 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001438 }
1439
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440 } else {
1441 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001442 std::string style_name = out_resource->name.entry;
1443 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001444 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001445 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001446 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001447 }
1448 }
1449
1450 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001451 const size_t depth = parser->depth();
1452 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1453 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 // Skip text and comments.
1455 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001456 }
1457
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 const std::string& element_namespace = parser->element_namespace();
1459 const std::string& element_name = parser->element_name();
1460 if (element_namespace == "" && element_name == "item") {
1461 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001462
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001463 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1464 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1465 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001467 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001469
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 if (error) {
1471 return false;
1472 }
1473
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001475 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001476}
1477
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001478bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1479 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1480 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1481 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1482 if (resource_format == 0u) {
1483 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1484 << "'" << format_attr.value() << "' is an invalid format");
1485 return false;
1486 }
1487 }
1488 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001489}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001490
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001491bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1492 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001493}
1494
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001495bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1496 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001497}
1498
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1500 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001501 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001502 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001503
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001504 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001505
Adam Lesinski75421622017-01-06 15:20:04 -08001506 bool translatable = options_.translatable;
1507 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1508 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1509 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001511 << "invalid value for 'translatable'. Must be a boolean");
1512 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001513 }
Adam Lesinski75421622017-01-06 15:20:04 -08001514 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515 }
Adam Lesinski75421622017-01-06 15:20:04 -08001516 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001517
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001518 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519 const size_t depth = parser->depth();
1520 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1521 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001522 // Skip text and comments.
1523 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001524 }
1525
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 const Source item_source = source_.WithLine(parser->line_number());
1527 const std::string& element_namespace = parser->element_namespace();
1528 const std::string& element_name = parser->element_name();
1529 if (element_namespace.empty() && element_name == "item") {
1530 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001532 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001533 error = true;
1534 continue;
1535 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001537 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001538
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001539 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1540 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1541 << "unknown tag <" << element_namespace << ":"
1542 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543 error = true;
1544 }
1545 }
1546
1547 if (error) {
1548 return false;
1549 }
1550
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001551 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001552 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001553}
1554
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1556 ParsedResource* out_resource) {
1557 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001558
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001560
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001561 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001562 const size_t depth = parser->depth();
1563 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1564 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001565 // Skip text and comments.
1566 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001567 }
1568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 const Source item_source = source_.WithLine(parser->line_number());
1570 const std::string& element_namespace = parser->element_namespace();
1571 const std::string& element_name = parser->element_name();
1572 if (element_namespace.empty() && element_name == "item") {
1573 Maybe<StringPiece> maybe_quantity =
1574 xml::FindNonEmptyAttribute(parser, "quantity");
1575 if (!maybe_quantity) {
1576 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001577 << "<item> in <plurals> requires attribute "
1578 << "'quantity'");
1579 error = true;
1580 continue;
1581 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001582
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001583 StringPiece trimmed_quantity =
1584 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001588 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001589 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001591 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001592 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001593 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 index = Plural::Other;
1598 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001599 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001600 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602 error = true;
1603 continue;
1604 }
1605
1606 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1608 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 error = true;
1610 continue;
1611 }
1612
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001613 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001614 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1615 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001616 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001617 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001618
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001619 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001620
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001621 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1622 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1623 << element_namespace << ":"
1624 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625 error = true;
1626 }
1627 }
1628
1629 if (error) {
1630 return false;
1631 }
1632
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001634 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001635}
1636
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001637bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1638 ParsedResource* out_resource) {
1639 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001640
Donald Chai94e4a012020-01-06 13:52:41 -08001641 if (!options_.preserve_visibility_of_styleables) {
1642 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1643 // knows exactly what for.
1644 //
1645 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1646 // package-private (to be used only by the view class); themes are a different story.
1647 out_resource->visibility_level = Visibility::Level::kPublic;
1648 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001649
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001650 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001651 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1652 diag_->Warn(DiagMessage(out_resource->source)
1653 << "ignoring configuration '" << out_resource->config
1654 << "' for styleable " << out_resource->name.entry);
1655 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001656 }
1657
1658 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1659
1660 std::string comment;
1661 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 const size_t depth = parser->depth();
1663 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1664 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001665 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001666 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001667 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 // Ignore text.
1669 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001670 }
1671
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001672 const Source item_source = source_.WithLine(parser->line_number());
1673 const std::string& element_namespace = parser->element_namespace();
1674 const std::string& element_name = parser->element_name();
1675 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001676 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001677 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001678 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001679 error = true;
1680 continue;
1681 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001682
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001683 // If this is a declaration, the package name may be in the name. Separate
1684 // these out.
1685 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001686 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001687 if (!maybe_ref) {
1688 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1689 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001690 error = true;
1691 continue;
1692 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001693
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001694 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001695 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001696
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001698 ParsedResource child_resource;
1699 child_resource.name = child_ref.name.value();
1700 child_resource.source = item_source;
1701 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001702 if (options_.visibility) {
1703 child_resource.visibility_level = options_.visibility.value();
1704 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001705
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001706 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001707 error = true;
1708 continue;
1709 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001710
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001711 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001712 child_ref.SetComment(child_resource.comment);
1713 child_ref.SetSource(item_source);
1714 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001715
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001716 // Do not add referenced attributes that do not define a format to the table.
1717 CHECK(child_resource.value != nullptr);
1718 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1719
1720 CHECK(attr != nullptr);
1721 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1722 out_resource->child_resources.push_back(std::move(child_resource));
1723 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001724
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001725 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1726 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1727 << element_namespace << ":"
1728 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001730 }
1731
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001732 comment = {};
1733 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001734
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 if (error) {
1736 return false;
1737 }
1738
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001739 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001741}
1742
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743} // namespace aapt