blob: d0237f80a8f0ea1c91ee57e37028673fdd342d5c [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
Adam Lesinski2eed52e2018-02-21 15:55:58 -080035using ::aapt::ResourceUtils::StringBuilder;
36using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070042constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070044// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
45static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080047}
48
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
50 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070054 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068}
69
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070static uint32_t ParseFormatType(const StringPiece& piece) {
71 if (piece == "enum") {
72 return android::ResTable_map::TYPE_ENUM;
73 } else if (piece == "flags") {
74 return android::ResTable_map::TYPE_FLAGS;
75 }
76 return ParseFormatTypeNoEnumsOrFlags(piece);
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080081 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 StringPiece trimmed_part = util::TrimWhitespace(part);
83 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 if (type == 0) {
85 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 mask |= type;
88 }
89 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090}
91
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070092// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080093struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 ResourceName name;
95 ConfigDescription config;
96 std::string product;
97 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800100 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700101 bool allow_new = false;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800102 Maybe<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::string comment;
105 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800107};
108
109// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700110static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
112 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800114 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski76565542016-03-10 21:55:04 -0800116
Adam Lesinski71be7052017-12-12 16:48:07 -0800117 if (res->visibility_level != Visibility::Level::kUndefined) {
118 Visibility visibility;
119 visibility.level = res->visibility_level;
120 visibility.source = res->source;
121 visibility.comment = res->comment;
122 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800126
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->allow_new) {
128 AllowNew allow_new;
129 allow_new.source = res->source;
130 allow_new.comment = res->comment;
131 if (!table->SetAllowNew(res->name, allow_new, diag)) {
132 return false;
133 }
134 }
135
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800136 if (res->overlayable_item) {
137 if (!table->SetOverlayable(res->name, res->overlayable_item.value(), diag)) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800138 return false;
139 }
140 }
141
142 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 res->value->SetComment(std::move(res->comment));
145 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800146
Adam Lesinski71be7052017-12-12 16:48:07 -0800147 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
148 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800150 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 for (ParsedResource& child : res->child_resources) {
155 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800158}
159
160// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
164 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700165 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 : diag_(diag),
168 table_(table),
169 source_(source),
170 config_(config),
171 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800173// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
174// This will be used to traverse and flatten the XML string into a single std::string, with all
175// Span and Untranslatable data maintained in parallel, as indices into the string.
176class Node {
177 public:
178 virtual ~Node() = default;
179
180 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
181 // parent node as well.
182 // Returns a pointer to the child node that was added as a convenience.
183 template <typename T>
184 T* AddChild(std::unique_ptr<T> node) {
185 T* raw_ptr = node.get();
186 children.push_back(std::move(node));
187 return raw_ptr;
188 }
189
190 virtual void Build(StringBuilder* builder) const {
191 for (const auto& child : children) {
192 child->Build(builder);
193 }
194 }
195
196 std::vector<std::unique_ptr<Node>> children;
197};
198
199// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
200class SegmentNode : public Node {
201 public:
202 std::string data;
203
204 void Build(StringBuilder* builder) const override {
205 builder->AppendText(data);
206 }
207};
208
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700209// A chunk of text in the XML string within a CDATA tags.
210class CdataSegmentNode : public SegmentNode {
211 public:
212
213 void Build(StringBuilder* builder) const override {
214 builder->AppendText(data, /* preserve_spaces */ true);
215 }
216};
217
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800218// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
219class SpanNode : public Node {
220 public:
221 std::string name;
222
223 void Build(StringBuilder* builder) const override {
224 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
225 Node::Build(builder);
226 builder->EndSpan(span_handle);
227 }
228};
229
230// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
231class UntranslatableNode : public Node {
232 public:
233 void Build(StringBuilder* builder) const override {
234 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
235 Node::Build(builder);
236 builder->EndUntranslatable(handle);
237 }
238};
239
240// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800241bool ResourceParser::FlattenXmlSubtree(
242 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
243 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800244 std::string raw_string;
245 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800246
247 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
248 Maybe<size_t> untranslatable_start_depth;
249
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800250 Node root;
251 std::vector<Node*> node_stack;
252 node_stack.push_back(&root);
253
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700254 bool cdata_block = false;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800255 bool saw_span_node = false;
256 SegmentNode* first_segment = nullptr;
257 SegmentNode* last_segment = nullptr;
258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800260 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800262
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800263 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700264 if (event == xml::XmlPullParser::Event::kStartElement
265 || event == xml::XmlPullParser::Event::kEndElement
266 || event == xml::XmlPullParser::Event::kCdataStart
267 || event == xml::XmlPullParser::Event::kCdataEnd) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 if (!current_text.empty()) {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269 std::unique_ptr<SegmentNode> segment_node = (cdata_block)
270 ? util::make_unique<CdataSegmentNode>() : util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800271 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700272
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800273 last_segment = node_stack.back()->AddChild(std::move(segment_node));
274 if (first_segment == nullptr) {
275 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800276 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800277 current_text = {};
278 }
279 }
Adam Lesinski75421622017-01-06 15:20:04 -0800280
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800281 switch (event) {
282 case xml::XmlPullParser::Event::kText: {
283 current_text += parser->text();
284 raw_string += parser->text();
285 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800286
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800287 case xml::XmlPullParser::Event::kStartElement: {
288 if (parser->element_namespace().empty()) {
289 // This is an HTML tag which we encode as a span. Add it to the span stack.
290 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
291 span_node->name = parser->element_name();
292 const auto end_attr_iter = parser->end_attributes();
293 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
294 ++attr_iter) {
295 span_node->name += ";";
296 span_node->name += attr_iter->name;
297 span_node->name += "=";
298 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800299 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800300
301 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
302 saw_span_node = true;
303 } else if (parser->element_namespace() == sXliffNamespaceUri) {
304 // This is an XLIFF tag, which is not encoded as a span.
305 if (parser->element_name() == "g") {
306 // Check that an 'untranslatable' tag is not already being processed. Nested
307 // <xliff:g> tags are illegal.
308 if (untranslatable_start_depth) {
309 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
310 << "illegal nested XLIFF 'g' tag");
311 return false;
312 } else {
313 // Mark the beginning of an 'untranslatable' section.
314 untranslatable_start_depth = depth;
315 node_stack.push_back(
316 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
317 }
318 } else {
319 // Ignore unknown XLIFF tags, but don't warn.
320 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
321 }
322 } else {
323 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
324 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
325 << "ignoring element '" << parser->element_name()
326 << "' with unknown namespace '" << parser->element_namespace() << "'");
327 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800328 }
Adam Lesinski75421622017-01-06 15:20:04 -0800329
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800330 // Enter one level inside the element.
331 depth++;
332 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700333
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800334 case xml::XmlPullParser::Event::kEndElement: {
335 // Return one level from within the element.
336 depth--;
337 if (depth == 0) {
338 break;
339 }
340
341 node_stack.pop_back();
342 if (untranslatable_start_depth == make_value(depth)) {
343 // This is the end of an untranslatable section.
344 untranslatable_start_depth = {};
345 }
346 } break;
347
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700348 case xml::XmlPullParser::Event::kCdataStart: {
349 cdata_block = true;
350 break;
351 }
352
353 case xml::XmlPullParser::Event::kCdataEnd: {
354 cdata_block = false;
355 break;
356 }
357
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800358 default:
359 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 }
362 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800364 // Sanity check to make sure we processed all the nodes.
365 CHECK(node_stack.size() == 1u);
366 CHECK(node_stack.back() == &root);
367
368 if (!saw_span_node) {
369 // If there were no spans, we must treat this string a little differently (according to AAPT).
370 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
371 // from the last segment.
372 if (first_segment != nullptr) {
373 // Trim leading whitespace.
374 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
375 if (trimmed.size() != first_segment->data.size()) {
376 first_segment->data = trimmed.to_string();
377 }
378 }
379
380 if (last_segment != nullptr) {
381 // Trim trailing whitespace.
382 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
383 if (trimmed.size() != last_segment->data.size()) {
384 last_segment->data = trimmed.to_string();
385 }
386 }
387 }
388
389 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
390 // care of recording the correctly adjusted Spans and UntranslatableSections.
391 StringBuilder builder;
392 root.Build(&builder);
393 if (!builder) {
394 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
395 return false;
396 }
397
398 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
399 *out_raw_string = std::move(raw_string);
400 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
401 out_style_string->str = std::move(flattened_string.text);
402 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800403 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404}
405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 const size_t depth = parser->depth();
409 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
410 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 // Skip comments and text.
412 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414
Adam Lesinski060b53d2017-07-28 17:10:35 -0700415 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 << "root element must be <resources>");
418 return false;
419 }
420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 break;
423 };
424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
426 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
427 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 return false;
429 }
430 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431}
432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
434 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700435
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 bool error = false;
437 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 const size_t depth = parser->depth();
439 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
440 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700445
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 if (!util::TrimWhitespace(parser->text()).empty()) {
448 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 << "plain text not allowed here");
450 error = true;
451 }
452 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 // Skip unknown namespace.
459 continue;
460 }
461
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 std::string element_name = parser->element_name();
463 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 comment = "";
465 continue;
466 }
467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 ParsedResource parsed_resource;
469 parsed_resource.config = config_;
470 parsed_resource.source = source_.WithLine(parser->line_number());
471 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100472 if (options_.visibility) {
473 parsed_resource.visibility_level = options_.visibility.value();
474 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475
476 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700477 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800478 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 }
480
481 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 error = true;
484 continue;
485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 error = true;
489 }
490 }
491
492 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 for (const ResourceName& stripped_resource : stripped_resources) {
494 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700496 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
497 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 error = true;
499 }
500 }
501
502 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503}
504
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
506 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 struct ItemTypeFormat {
508 ResourceType type;
509 uint32_t format;
510 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800511
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
513 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800514
Adam Lesinski86d67df2017-01-31 13:47:27 -0800515 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
516 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
517 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
518 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
519 {"dimen",
520 {ResourceType::kDimen,
521 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
522 android::ResTable_map::TYPE_DIMENSION}},
523 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
524 {"fraction",
525 {ResourceType::kFraction,
526 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
527 android::ResTable_map::TYPE_DIMENSION}},
528 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
529 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
530 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800531
Adam Lesinski86d67df2017-01-31 13:47:27 -0800532 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
533 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
534 {"array", std::mem_fn(&ResourceParser::ParseArray)},
535 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
536 {"configVarying",
537 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
538 std::placeholders::_2, std::placeholders::_3)},
539 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
540 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
541 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700542 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800543 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
544 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
545 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
546 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
547 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
548 std::placeholders::_2, std::placeholders::_3)},
549 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
550 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800553
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800556
Adam Lesinski86d67df2017-01-31 13:47:27 -0800557 bool can_be_item = true;
558 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800560 can_be_bag = false;
561
Adam Lesinskie597d682017-06-01 17:16:44 -0700562 // The default format for <item> is any. If a format attribute is present, that one will
563 // override the default.
564 resource_format = android::ResTable_map::TYPE_ANY;
565
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700567 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800568 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 << "<item> must have a 'type' attribute");
572 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800573 }
574
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700575 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700577 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700579 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 if (!resource_format) {
581 diag_->Error(DiagMessage(out_resource->source)
582 << "'" << maybe_format.value()
583 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800584 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 }
586 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800587 } else if (resource_type == "bag") {
588 can_be_item = false;
589
590 // Bags have their type encoded in the type attribute.
591 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
592 resource_type = maybe_type.value().to_string();
593 } else {
594 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
595 << "<bag> must have a 'type' attribute");
596 return false;
597 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 }
599
600 // Get the name of the resource. This will be checked later, because not all
601 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 if (resource_type == "id") {
605 if (!maybe_name) {
606 diag_->Error(DiagMessage(out_resource->source)
607 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 << "> missing 'name' attribute");
609 return false;
610 }
611
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800613 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700614
615 // Ids either represent a unique resource id or reference another resource id
616 auto item = ParseItem(parser, out_resource, resource_format);
617 if (!item) {
618 return false;
619 }
620
621 String* empty = ValueCast<String>(out_resource->value.get());
622 if (empty && *empty->value == "") {
623 // If no inner element exists, represent a unique identifier
624 out_resource->value = util::make_unique<Id>();
625 } else {
y9efbbef2018-04-18 11:29:09 -0700626 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700627 if (ref && !ref->name && !ref->id) {
628 // A null reference also means there is no inner element when ids are in the form:
629 // <id name="name"/>
630 out_resource->value = util::make_unique<Id>();
631 } else if (!ref || ref->name.value().type != ResourceType::kId) {
632 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700633 diag_->Error(DiagMessage(out_resource->source)
634 << "<" << parser->element_name()
635 << "> inner element must either be a resource reference or empty");
636 return false;
637 }
638 }
639
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 return true;
641 }
642
Adam Lesinski86d67df2017-01-31 13:47:27 -0800643 if (can_be_item) {
644 const auto item_iter = elToItemMap.find(resource_type);
645 if (item_iter != elToItemMap.end()) {
646 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647
Adam Lesinski86d67df2017-01-31 13:47:27 -0800648 if (!maybe_name) {
649 diag_->Error(DiagMessage(out_resource->source)
650 << "<" << parser->element_name() << "> missing 'name' attribute");
651 return false;
652 }
653
654 out_resource->name.type = item_iter->second.type;
655 out_resource->name.entry = maybe_name.value().to_string();
656
Adam Lesinskie597d682017-06-01 17:16:44 -0700657 // Only use the implied format of the type when there is no explicit format.
658 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800659 resource_format = item_iter->second.format;
660 }
661
662 if (!ParseItem(parser, out_resource, resource_format)) {
663 return false;
664 }
665 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 }
668
669 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800670 if (can_be_bag) {
671 const auto bag_iter = elToBagMap.find(resource_type);
672 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700673 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700674 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800675 if (!maybe_name) {
676 diag_->Error(DiagMessage(out_resource->source)
677 << "<" << parser->element_name() << "> missing 'name' attribute");
678 return false;
679 }
680
681 out_resource->name.entry = maybe_name.value().to_string();
682 }
683
684 // Call the associated parse method. The type will be filled in by the
685 // parse func.
686 if (!bag_iter->second(this, parser, out_resource)) {
687 return false;
688 }
689 return true;
690 }
691 }
692
693 if (can_be_item) {
694 // Try parsing the elementName (or type) as a resource. These shall only be
695 // resources like 'layout' or 'xml' and they can only be references.
696 const ResourceType* parsed_type = ParseResourceType(resource_type);
697 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 if (!maybe_name) {
699 diag_->Error(DiagMessage(out_resource->source)
700 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 << "> missing 'name' attribute");
702 return false;
703 }
704
Adam Lesinski86d67df2017-01-31 13:47:27 -0800705 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800706 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800707 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
708 if (!out_resource->value) {
709 diag_->Error(DiagMessage(out_resource->source)
710 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
711 return false;
712 }
713 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
716
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000717 // If the resource type was not recognized, write the error and return false.
718 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 return false;
721}
722
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
724 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 const uint32_t format) {
726 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 }
729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 out_resource->value = ParseXml(parser, format, kNoRawString);
731 if (!out_resource->value) {
732 diag_->Error(DiagMessage(out_resource->source) << "invalid "
733 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 return false;
735 }
736 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800737}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800738
739/**
740 * Reads the entire XML subtree and attempts to parse it as some Item,
741 * with typeMask denoting which items it can be. If allowRawValue is
742 * true, a RawString is returned if the XML couldn't be parsed as
743 * an Item. If allowRawValue is false, nullptr is returned in this
744 * case.
745 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
747 const uint32_t type_mask,
748 const bool allow_raw_value) {
749 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800750
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 std::string raw_value;
752 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800753 std::vector<UntranslatableSection> untranslatable_sections;
754 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800755 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 }
757
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800760 std::unique_ptr<StyledString> styled_string =
761 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700762 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800763 styled_string->untranslatable_sections = std::move(untranslatable_sections);
764 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 }
766
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 // name.package can be empty here, as it will assume the package name of the
769 // table.
770 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700771 id->SetSource(source_.WithLine(begin_xml_line));
772 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 };
774
775 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800777 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700780 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700781 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 }
785
786 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800789 std::unique_ptr<String> string = util::make_unique<String>(
790 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
791 string->untranslatable_sections = std::move(untranslatable_sections);
792 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 }
794
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700795 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
796 if (util::TrimWhitespace(raw_value).empty()) {
797 return ResourceUtils::MakeNull();
798 }
799
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 // We can't parse this so return a RawString if we are allowed.
802 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700803 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
804 StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 }
806 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800807}
808
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809bool ResourceParser::ParseString(xml::XmlPullParser* parser,
810 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 if (Maybe<StringPiece> formatted_attr =
813 xml::FindAttribute(parser, "formatted")) {
814 Maybe<bool> maybe_formatted =
815 ResourceUtils::ParseBool(formatted_attr.value());
816 if (!maybe_formatted) {
817 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 << "invalid value for 'formatted'. Must be a boolean");
819 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800820 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800823
Adam Lesinski75421622017-01-06 15:20:04 -0800824 bool translatable = options_.translatable;
825 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
826 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
827 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 << "invalid value for 'translatable'. Must be a boolean");
830 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800831 }
Adam Lesinski75421622017-01-06 15:20:04 -0800832 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 out_resource->value =
836 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
837 if (!out_resource->value) {
838 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 return false;
840 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800841
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800843 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800844
Adam Lesinski75421622017-01-06 15:20:04 -0800845 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 if (!util::VerifyJavaStringFormat(*string_value->value)) {
847 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 msg << "multiple substitutions specified in non-positional format; "
849 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 if (options_.error_on_positional_arguments) {
851 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800853 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800857 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858
Adam Lesinski75421622017-01-06 15:20:04 -0800859 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
860 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 }
862 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800863}
864
Adam Lesinski71be7052017-12-12 16:48:07 -0800865bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100866 if (options_.visibility) {
867 diag_->Error(DiagMessage(out_resource->source)
868 << "<public> tag not allowed with --visibility flag");
869 return false;
870 }
871
Adam Lesinski46c4d722017-08-23 13:03:56 -0700872 if (out_resource->config != ConfigDescription::DefaultConfig()) {
873 diag_->Warn(DiagMessage(out_resource->source)
874 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
875 }
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
878 if (!maybe_type) {
879 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 << "<public> must have a 'type' attribute");
881 return false;
882 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800883
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
885 if (!parsed_type) {
886 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
887 << maybe_type.value()
888 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 return false;
890 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800894 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
895 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800897 diag_->Error(DiagMessage(out_resource->source)
898 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800900 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700901 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800903
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700908
Adam Lesinski71be7052017-12-12 16:48:07 -0800909 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800911}
912
Adam Lesinski46c4d722017-08-23 13:03:56 -0700913bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100914 if (options_.visibility) {
915 diag_->Error(DiagMessage(out_resource->source)
916 << "<public-group> tag not allowed with --visibility flag");
917 return false;
918 }
919
Adam Lesinski46c4d722017-08-23 13:03:56 -0700920 if (out_resource->config != ConfigDescription::DefaultConfig()) {
921 diag_->Warn(DiagMessage(out_resource->source)
922 << "ignoring configuration '" << out_resource->config
923 << "' for <public-group> tag");
924 }
925
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700926 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
927 if (!maybe_type) {
928 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800930 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800932
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700933 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
934 if (!parsed_type) {
935 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
936 << maybe_type.value()
937 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800938 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700939 }
940
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700941 Maybe<StringPiece> maybe_id_str =
942 xml::FindNonEmptyAttribute(parser, "first-id");
943 if (!maybe_id_str) {
944 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 << "<public-group> must have a 'first-id' attribute");
946 return false;
947 }
948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 Maybe<ResourceId> maybe_id =
950 ResourceUtils::ParseResourceId(maybe_id_str.value());
951 if (!maybe_id) {
952 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
953 << maybe_id_str.value()
954 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 return false;
956 }
957
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959
960 std::string comment;
961 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 const size_t depth = parser->depth();
963 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
964 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800965 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 // Skip text.
969 continue;
970 }
971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972 const Source item_source = source_.WithLine(parser->line_number());
973 const std::string& element_namespace = parser->element_namespace();
974 const std::string& element_name = parser->element_name();
975 if (element_namespace.empty() && element_name == "public") {
976 Maybe<StringPiece> maybe_name =
977 xml::FindNonEmptyAttribute(parser, "name");
978 if (!maybe_name) {
979 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 << "<public> must have a 'name' attribute");
981 error = true;
982 continue;
983 }
984
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 if (xml::FindNonEmptyAttribute(parser, "id")) {
986 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 << "'id' is ignored within <public-group>");
988 error = true;
989 continue;
990 }
991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 if (xml::FindNonEmptyAttribute(parser, "type")) {
993 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 << "'type' is ignored within <public-group>");
995 error = true;
996 continue;
997 }
998
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 ParsedResource child_resource;
1000 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -08001001 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001002 child_resource.id = next_id;
1003 child_resource.comment = std::move(comment);
1004 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -08001005 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1011 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 error = true;
1013 }
1014 }
1015 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001016}
1017
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1019 ParsedResource* out_resource) {
1020 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1021 if (!maybe_type) {
1022 diag_->Error(DiagMessage(out_resource->source)
1023 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 << "> must have a 'type' attribute");
1025 return false;
1026 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001027
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1029 if (!parsed_type) {
1030 diag_->Error(DiagMessage(out_resource->source)
1031 << "invalid resource type '" << maybe_type.value() << "' in <"
1032 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 return false;
1034 }
1035
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001038}
1039
Adam Lesinski46c4d722017-08-23 13:03:56 -07001040bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001041 if (options_.visibility) {
1042 diag_->Error(DiagMessage(out_resource->source)
1043 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1044 return false;
1045 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001046 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1047 diag_->Warn(DiagMessage(out_resource->source)
1048 << "ignoring configuration '" << out_resource->config << "' for <"
1049 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001050 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001051
1052 if (!ParseSymbolImpl(parser, out_resource)) {
1053 return false;
1054 }
1055
Adam Lesinski71be7052017-12-12 16:48:07 -08001056 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001057 return true;
1058}
1059
1060bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1061 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1062 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001063 << "ignoring configuration '" << out_resource->config
1064 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001065 }
1066
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001067 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1068 if (!overlayable_name) {
1069 diag_->Error(DiagMessage(out_resource->source)
1070 << "<overlayable> tag must have a 'name' attribute");
1071 return false;
1072 }
1073
1074 const std::string kActorUriScheme =
1075 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1076 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1077 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1078 diag_->Error(DiagMessage(out_resource->source)
1079 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1080 << Overlayable::kActorScheme << "'");
1081 return false;
1082 }
1083
1084 // Create a overlayable entry grouping that represents this <overlayable>
1085 auto overlayable = std::make_shared<Overlayable>(
1086 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001087 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001088
Adam Lesinski46c4d722017-08-23 13:03:56 -07001089 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001090 std::string comment;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001091 OverlayableItem::PolicyFlags current_policies = OverlayableItem::Policy::kNone;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001092 const size_t start_depth = parser->depth();
1093 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1094 xml::XmlPullParser::Event event = parser->event();
1095 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001096 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001097 break;
1098 } else if (event == xml::XmlPullParser::Event::kEndElement
1099 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001100 // Clear the current policies when exiting the <policy> tags
1101 current_policies = OverlayableItem::Policy::kNone;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001102 continue;
1103 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001104 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001105 comment = parser->comment();
1106 continue;
1107 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001108 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001109 continue;
1110 }
1111
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001112 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001113 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001114 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001115 if (element_namespace.empty() && element_name == "item") {
Winsonb2d7f532019-02-04 16:32:43 -08001116 if (current_policies == OverlayableItem::Policy::kNone) {
1117 diag_->Error(DiagMessage(element_source)
1118 << "<item> within an <overlayable> must be inside a <policy> block");
1119 error = true;
1120 continue;
1121 }
1122
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001123 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001124 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1125 if (!item_name) {
1126 diag_->Error(DiagMessage(element_source)
1127 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001128 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001129 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001130 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001131
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001132 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1133 if (!item_type) {
1134 diag_->Error(DiagMessage(element_source)
1135 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001136 error = true;
1137 continue;
1138 }
1139
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001140 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001141 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001142 diag_->Error(DiagMessage(element_source)
1143 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001144 << "' in <item> within an <overlayable>");
1145 error = true;
1146 continue;
1147 }
1148
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001149 OverlayableItem overlayable_item(overlayable);
1150 overlayable_item.policies = current_policies;
1151 overlayable_item.comment = comment;
1152 overlayable_item.source = element_source;
1153
1154 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001155 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001156 child_resource.name.entry = item_name.value().to_string();
1157 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001158 out_resource->child_resources.push_back(std::move(child_resource));
1159
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001160 } else if (element_namespace.empty() && element_name == "policy") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001161 if (current_policies != OverlayableItem::Policy::kNone) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001162 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001163 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001164 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001165 break;
1166 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1167 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001168 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001169 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
1170 StringPiece trimmed_part = util::TrimWhitespace(part);
1171 if (trimmed_part == "public") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001172 current_policies |= OverlayableItem::Policy::kPublic;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001173 } else if (trimmed_part == "product") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001174 current_policies |= OverlayableItem::Policy::kProduct;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001175 } else if (trimmed_part == "system") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001176 current_policies |= OverlayableItem::Policy::kSystem;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001177 } else if (trimmed_part == "vendor") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001178 current_policies |= OverlayableItem::Policy::kVendor;
Winsonb2d7f532019-02-04 16:32:43 -08001179 } else if (trimmed_part == "signature") {
1180 current_policies |= OverlayableItem::Policy::kSignature;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001181 } else {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001182 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001183 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001184 error = true;
1185 continue;
1186 }
1187 }
Winsonb2d7f532019-02-04 16:32:43 -08001188 } else {
1189 diag_->Error(DiagMessage(element_source)
1190 << "<policy> must have a 'type' attribute");
1191 error = true;
1192 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001193 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001194 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001195 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001196 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001197 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001198 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001199 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001200
1201 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001202 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001203
Adam Lesinski46c4d722017-08-23 13:03:56 -07001204 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001205}
1206
Adam Lesinski71be7052017-12-12 16:48:07 -08001207bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001208 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001209 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001210 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211 return true;
1212 }
1213 return false;
1214}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001215
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1217 ParsedResource* out_resource) {
1218 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219}
1220
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1222 ParsedResource* out_resource, bool weak) {
1223 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001224
1225 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001226 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1227 diag_->Warn(DiagMessage(out_resource->source)
1228 << "ignoring configuration '" << out_resource->config
1229 << "' for attribute " << out_resource->name);
1230 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001231 }
1232
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001234
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1236 if (maybe_format) {
1237 type_mask = ParseFormatAttribute(maybe_format.value());
1238 if (type_mask == 0) {
1239 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001240 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001241 return false;
1242 }
1243 }
1244
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001245 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1248 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1249 if (!min_str.empty()) {
1250 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001251 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001252 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001253 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001254 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001255 }
1256
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 if (!maybe_min) {
1258 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1259 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001260 return false;
1261 }
1262 }
1263
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1265 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1266 if (!max_str.empty()) {
1267 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001268 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001269 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001272 }
1273
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001274 if (!maybe_max) {
1275 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1276 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 return false;
1278 }
1279 }
1280
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 if ((maybe_min || maybe_max) &&
1282 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1283 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001284 << "'min' and 'max' can only be used when format='integer'");
1285 return false;
1286 }
1287
1288 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001289 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290 return a.symbol.name.value() < b.symbol.name.value();
1291 }
1292 };
1293
1294 std::set<Attribute::Symbol, SymbolComparator> items;
1295
1296 std::string comment;
1297 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 const size_t depth = parser->depth();
1299 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1300 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001301 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001303 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001304 // Skip text.
1305 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001306 }
1307
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 const Source item_source = source_.WithLine(parser->line_number());
1309 const std::string& element_namespace = parser->element_namespace();
1310 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001311 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312 if (element_name == "enum") {
1313 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1314 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 << "can not define an <enum>; already defined a <flag>");
1316 error = true;
1317 continue;
1318 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 } else if (element_name == "flag") {
1322 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1323 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 << "can not define a <flag>; already defined an <enum>");
1325 error = true;
1326 continue;
1327 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329 }
1330
1331 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 ParsedResource child_resource;
1335 child_resource.name = symbol.symbol.name.value();
1336 child_resource.source = item_source;
1337 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001338 if (options_.visibility) {
1339 child_resource.visibility_level = options_.visibility.value();
1340 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001342
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 symbol.symbol.SetComment(std::move(comment));
1344 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001345
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001346 auto insert_result = items.insert(std::move(symbol));
1347 if (!insert_result.second) {
1348 const Attribute::Symbol& existing_symbol = *insert_result.first;
1349 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001351 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001353 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001354 << "first defined here");
1355 error = true;
1356 }
1357 } else {
1358 error = true;
1359 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1361 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362 error = true;
1363 }
1364
1365 comment = {};
1366 }
1367
1368 if (error) {
1369 return false;
1370 }
1371
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001372 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1373 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1374 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001376 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1377 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001378 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 return true;
1380}
1381
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1387 if (!maybe_name) {
1388 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 << tag << ">");
1390 return {};
1391 }
1392
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1394 if (!maybe_value) {
1395 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 << tag << ">");
1397 return {};
1398 }
1399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401 android::Res_value val;
1402 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 << "' for <" << tag
1405 << ">; must be an integer");
1406 return {};
1407 }
1408
1409 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001412}
1413
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001414bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1415 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001416
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001417 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1418 if (!maybe_name) {
1419 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 return false;
1421 }
1422
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001423 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001424 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001425 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001426 return false;
1427 }
1428
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001429 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001430 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001432 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001433 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001435 return false;
1436 }
1437
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001438 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 return true;
1440}
1441
Adam Lesinski86d67df2017-01-31 13:47:27 -08001442bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001444 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445
1446 std::unique_ptr<Style> style = util::make_unique<Style>();
1447
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001448 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1449 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001450 // 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 -07001451 if (!maybe_parent.value().empty()) {
1452 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001453 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001455 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001456 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457 }
1458
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001459 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001460 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001461 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001462 }
1463
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 } else {
1465 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001466 std::string style_name = out_resource->name.entry;
1467 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001469 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001470 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 }
1472 }
1473
1474 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001475 const size_t depth = parser->depth();
1476 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1477 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 // Skip text and comments.
1479 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001480 }
1481
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 const std::string& element_namespace = parser->element_namespace();
1483 const std::string& element_name = parser->element_name();
1484 if (element_namespace == "" && element_name == "item") {
1485 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001486
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1488 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1489 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001491 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001492 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001493
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 if (error) {
1495 return false;
1496 }
1497
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001498 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001499 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001500}
1501
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001502bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1503 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1504 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1505 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1506 if (resource_format == 0u) {
1507 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1508 << "'" << format_attr.value() << "' is an invalid format");
1509 return false;
1510 }
1511 }
1512 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001513}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001514
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001515bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1516 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001517}
1518
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001519bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1520 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001521}
1522
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001523bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1524 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001525 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001527
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001528 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001529
Adam Lesinski75421622017-01-06 15:20:04 -08001530 bool translatable = options_.translatable;
1531 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1532 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1533 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 << "invalid value for 'translatable'. Must be a boolean");
1536 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001537 }
Adam Lesinski75421622017-01-06 15:20:04 -08001538 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 }
Adam Lesinski75421622017-01-06 15:20:04 -08001540 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001541
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 const size_t depth = parser->depth();
1544 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1545 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 // Skip text and comments.
1547 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001548 }
1549
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001550 const Source item_source = source_.WithLine(parser->line_number());
1551 const std::string& element_namespace = parser->element_namespace();
1552 const std::string& element_name = parser->element_name();
1553 if (element_namespace.empty() && element_name == "item") {
1554 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001555 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557 error = true;
1558 continue;
1559 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001560 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001561 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001562
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001563 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1564 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1565 << "unknown tag <" << element_namespace << ":"
1566 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001567 error = true;
1568 }
1569 }
1570
1571 if (error) {
1572 return false;
1573 }
1574
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001575 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001576 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001577}
1578
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1580 ParsedResource* out_resource) {
1581 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001582
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001583 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001584
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001586 const size_t depth = parser->depth();
1587 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1588 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001589 // Skip text and comments.
1590 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001591 }
1592
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 const Source item_source = source_.WithLine(parser->line_number());
1594 const std::string& element_namespace = parser->element_namespace();
1595 const std::string& element_name = parser->element_name();
1596 if (element_namespace.empty() && element_name == "item") {
1597 Maybe<StringPiece> maybe_quantity =
1598 xml::FindNonEmptyAttribute(parser, "quantity");
1599 if (!maybe_quantity) {
1600 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 << "<item> in <plurals> requires attribute "
1602 << "'quantity'");
1603 error = true;
1604 continue;
1605 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001606
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 StringPiece trimmed_quantity =
1608 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001609 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001610 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001616 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001617 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001618 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 index = Plural::Other;
1622 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001625 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001626 error = true;
1627 continue;
1628 }
1629
1630 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001631 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1632 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001633 error = true;
1634 continue;
1635 }
1636
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001637 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001638 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1639 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001640 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001641 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001642
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001643 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001644
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001645 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1646 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1647 << element_namespace << ":"
1648 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001649 error = true;
1650 }
1651 }
1652
1653 if (error) {
1654 return false;
1655 }
1656
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001657 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001659}
1660
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001661bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1662 ParsedResource* out_resource) {
1663 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001664
Adam Lesinski71be7052017-12-12 16:48:07 -08001665 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1666 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001667
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1670 diag_->Warn(DiagMessage(out_resource->source)
1671 << "ignoring configuration '" << out_resource->config
1672 << "' for styleable " << out_resource->name.entry);
1673 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 }
1675
1676 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1677
1678 std::string comment;
1679 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001680 const size_t depth = parser->depth();
1681 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1682 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001683 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001684 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001685 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001686 // Ignore text.
1687 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001688 }
1689
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 const Source item_source = source_.WithLine(parser->line_number());
1691 const std::string& element_namespace = parser->element_namespace();
1692 const std::string& element_name = parser->element_name();
1693 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001694 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001695 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001696 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 error = true;
1698 continue;
1699 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001700
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001701 // If this is a declaration, the package name may be in the name. Separate
1702 // these out.
1703 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001704 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001705 if (!maybe_ref) {
1706 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1707 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001708 error = true;
1709 continue;
1710 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001711
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001712 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001713 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001714
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001715 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001716 ParsedResource child_resource;
1717 child_resource.name = child_ref.name.value();
1718 child_resource.source = item_source;
1719 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001720 if (options_.visibility) {
1721 child_resource.visibility_level = options_.visibility.value();
1722 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001723
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001724 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725 error = true;
1726 continue;
1727 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001728
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001729 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001730 child_ref.SetComment(child_resource.comment);
1731 child_ref.SetSource(item_source);
1732 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001733
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001735
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001736 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1737 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1738 << element_namespace << ":"
1739 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001740 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001741 }
1742
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001743 comment = {};
1744 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001745
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001746 if (error) {
1747 return false;
1748 }
1749
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001750 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001751 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001752}
1753
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001754} // namespace aapt