blob: 96c03a6184cf0b9aebe2deff7fff20df2bb2c045 [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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 for (StringPiece part : util::Tokenize(str, '|')) {
82 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 Lesinski7656554f2016-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
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 diag_->Warn(DiagMessage(out_resource->source)
718 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 return false;
720}
721
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
723 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 const uint32_t format) {
725 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 }
728
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 out_resource->value = ParseXml(parser, format, kNoRawString);
730 if (!out_resource->value) {
731 diag_->Error(DiagMessage(out_resource->source) << "invalid "
732 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return false;
734 }
735 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800736}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800737
738/**
739 * Reads the entire XML subtree and attempts to parse it as some Item,
740 * with typeMask denoting which items it can be. If allowRawValue is
741 * true, a RawString is returned if the XML couldn't be parsed as
742 * an Item. If allowRawValue is false, nullptr is returned in this
743 * case.
744 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
746 const uint32_t type_mask,
747 const bool allow_raw_value) {
748 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 std::string raw_value;
751 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800752 std::vector<UntranslatableSection> untranslatable_sections;
753 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800754 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800759 std::unique_ptr<StyledString> styled_string =
760 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700761 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800762 styled_string->untranslatable_sections = std::move(untranslatable_sections);
763 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 }
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 // name.package can be empty here, as it will assume the package name of the
768 // table.
769 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 id->SetSource(source_.WithLine(begin_xml_line));
771 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 };
773
774 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800776 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700780 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 }
784
785 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800788 std::unique_ptr<String> string = util::make_unique<String>(
789 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
790 string->untranslatable_sections = std::move(untranslatable_sections);
791 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 }
793
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700794 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
795 if (util::TrimWhitespace(raw_value).empty()) {
796 return ResourceUtils::MakeNull();
797 }
798
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700799 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 // We can't parse this so return a RawString if we are allowed.
801 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700802 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
803 StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700804 }
805 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800806}
807
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700808bool ResourceParser::ParseString(xml::XmlPullParser* parser,
809 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811 if (Maybe<StringPiece> formatted_attr =
812 xml::FindAttribute(parser, "formatted")) {
813 Maybe<bool> maybe_formatted =
814 ResourceUtils::ParseBool(formatted_attr.value());
815 if (!maybe_formatted) {
816 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 << "invalid value for 'formatted'. Must be a boolean");
818 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800819 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800822
Adam Lesinski75421622017-01-06 15:20:04 -0800823 bool translatable = options_.translatable;
824 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
825 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
826 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 << "invalid value for 'translatable'. Must be a boolean");
829 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800830 }
Adam Lesinski75421622017-01-06 15:20:04 -0800831 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 out_resource->value =
835 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
836 if (!out_resource->value) {
837 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 return false;
839 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800840
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800842 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800843
Adam Lesinski75421622017-01-06 15:20:04 -0800844 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 if (!util::VerifyJavaStringFormat(*string_value->value)) {
846 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847 msg << "multiple substitutions specified in non-positional format; "
848 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700849 if (options_.error_on_positional_arguments) {
850 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800852 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800856 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857
Adam Lesinski75421622017-01-06 15:20:04 -0800858 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
859 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 }
861 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862}
863
Adam Lesinski71be7052017-12-12 16:48:07 -0800864bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100865 if (options_.visibility) {
866 diag_->Error(DiagMessage(out_resource->source)
867 << "<public> tag not allowed with --visibility flag");
868 return false;
869 }
870
Adam Lesinski46c4d722017-08-23 13:03:56 -0700871 if (out_resource->config != ConfigDescription::DefaultConfig()) {
872 diag_->Warn(DiagMessage(out_resource->source)
873 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
874 }
875
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
877 if (!maybe_type) {
878 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 << "<public> must have a 'type' attribute");
880 return false;
881 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800882
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
884 if (!parsed_type) {
885 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
886 << maybe_type.value()
887 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 return false;
889 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800892
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800893 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
894 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800896 diag_->Error(DiagMessage(out_resource->source)
897 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800899 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700907
Adam Lesinski71be7052017-12-12 16:48:07 -0800908 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800910}
911
Adam Lesinski46c4d722017-08-23 13:03:56 -0700912bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100913 if (options_.visibility) {
914 diag_->Error(DiagMessage(out_resource->source)
915 << "<public-group> tag not allowed with --visibility flag");
916 return false;
917 }
918
Adam Lesinski46c4d722017-08-23 13:03:56 -0700919 if (out_resource->config != ConfigDescription::DefaultConfig()) {
920 diag_->Warn(DiagMessage(out_resource->source)
921 << "ignoring configuration '" << out_resource->config
922 << "' for <public-group> tag");
923 }
924
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
926 if (!maybe_type) {
927 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800929 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
933 if (!parsed_type) {
934 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
935 << maybe_type.value()
936 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800937 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 Maybe<StringPiece> maybe_id_str =
941 xml::FindNonEmptyAttribute(parser, "first-id");
942 if (!maybe_id_str) {
943 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 << "<public-group> must have a 'first-id' attribute");
945 return false;
946 }
947
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 Maybe<ResourceId> maybe_id =
949 ResourceUtils::ParseResourceId(maybe_id_str.value());
950 if (!maybe_id) {
951 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
952 << maybe_id_str.value()
953 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 return false;
955 }
956
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958
959 std::string comment;
960 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 const size_t depth = parser->depth();
962 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
963 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800964 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 // Skip text.
968 continue;
969 }
970
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 const Source item_source = source_.WithLine(parser->line_number());
972 const std::string& element_namespace = parser->element_namespace();
973 const std::string& element_name = parser->element_name();
974 if (element_namespace.empty() && element_name == "public") {
975 Maybe<StringPiece> maybe_name =
976 xml::FindNonEmptyAttribute(parser, "name");
977 if (!maybe_name) {
978 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 << "<public> must have a 'name' attribute");
980 error = true;
981 continue;
982 }
983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 if (xml::FindNonEmptyAttribute(parser, "id")) {
985 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 << "'id' is ignored within <public-group>");
987 error = true;
988 continue;
989 }
990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 if (xml::FindNonEmptyAttribute(parser, "type")) {
992 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 << "'type' is ignored within <public-group>");
994 error = true;
995 continue;
996 }
997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 ParsedResource child_resource;
999 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -08001000 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 child_resource.id = next_id;
1002 child_resource.comment = std::move(comment);
1003 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -08001004 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1010 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 error = true;
1012 }
1013 }
1014 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001015}
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1018 ParsedResource* out_resource) {
1019 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1020 if (!maybe_type) {
1021 diag_->Error(DiagMessage(out_resource->source)
1022 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 << "> must have a 'type' attribute");
1024 return false;
1025 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1028 if (!parsed_type) {
1029 diag_->Error(DiagMessage(out_resource->source)
1030 << "invalid resource type '" << maybe_type.value() << "' in <"
1031 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 return false;
1033 }
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001037}
1038
Adam Lesinski46c4d722017-08-23 13:03:56 -07001039bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001040 if (options_.visibility) {
1041 diag_->Error(DiagMessage(out_resource->source)
1042 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1043 return false;
1044 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001045 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1046 diag_->Warn(DiagMessage(out_resource->source)
1047 << "ignoring configuration '" << out_resource->config << "' for <"
1048 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001050
1051 if (!ParseSymbolImpl(parser, out_resource)) {
1052 return false;
1053 }
1054
Adam Lesinski71be7052017-12-12 16:48:07 -08001055 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001056 return true;
1057}
1058
1059bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1060 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1061 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001062 << "ignoring configuration '" << out_resource->config
1063 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001064 }
1065
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001066 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1067 if (!overlayable_name) {
1068 diag_->Error(DiagMessage(out_resource->source)
1069 << "<overlayable> tag must have a 'name' attribute");
1070 return false;
1071 }
1072
1073 const std::string kActorUriScheme =
1074 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1075 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1076 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1077 diag_->Error(DiagMessage(out_resource->source)
1078 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1079 << Overlayable::kActorScheme << "'");
1080 return false;
1081 }
1082
1083 // Create a overlayable entry grouping that represents this <overlayable>
1084 auto overlayable = std::make_shared<Overlayable>(
1085 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
1086 out_resource->source);
1087
Adam Lesinski46c4d722017-08-23 13:03:56 -07001088 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001089 std::string comment;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001090 OverlayableItem::PolicyFlags current_policies = OverlayableItem::Policy::kNone;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001091 const size_t start_depth = parser->depth();
1092 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1093 xml::XmlPullParser::Event event = parser->event();
1094 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001095 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001096 break;
1097 } else if (event == xml::XmlPullParser::Event::kEndElement
1098 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001099 // Clear the current policies when exiting the <policy> tags
1100 current_policies = OverlayableItem::Policy::kNone;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001101 continue;
1102 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001103 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001104 comment = parser->comment();
1105 continue;
1106 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001107 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001108 continue;
1109 }
1110
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001111 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001112 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001113 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001114 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001115 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001116 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1117 if (!item_name) {
1118 diag_->Error(DiagMessage(element_source)
1119 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001120 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001121 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001122 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001123
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001124 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1125 if (!item_type) {
1126 diag_->Error(DiagMessage(element_source)
1127 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001128 error = true;
1129 continue;
1130 }
1131
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001132 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001133 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001134 diag_->Error(DiagMessage(element_source)
1135 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001136 << "' in <item> within an <overlayable>");
1137 error = true;
1138 continue;
1139 }
1140
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001141 OverlayableItem overlayable_item(overlayable);
1142 overlayable_item.policies = current_policies;
1143 overlayable_item.comment = comment;
1144 overlayable_item.source = element_source;
1145
1146 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001147 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001148 child_resource.name.entry = item_name.value().to_string();
1149 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001150 out_resource->child_resources.push_back(std::move(child_resource));
1151
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001152 } else if (element_namespace.empty() && element_name == "policy") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001153 if (current_policies != OverlayableItem::Policy::kNone) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001154 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001155 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001156 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001157 break;
1158 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1159 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001160 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001161 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
1162 StringPiece trimmed_part = util::TrimWhitespace(part);
1163 if (trimmed_part == "public") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001164 current_policies |= OverlayableItem::Policy::kPublic;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001165 } else if (trimmed_part == "product") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001166 current_policies |= OverlayableItem::Policy::kProduct;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001167 } else if (trimmed_part == "product_services") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001168 current_policies |= OverlayableItem::Policy::kProductServices;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001169 } else if (trimmed_part == "system") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001170 current_policies |= OverlayableItem::Policy::kSystem;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001171 } else if (trimmed_part == "vendor") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001172 current_policies |= OverlayableItem::Policy::kVendor;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001173 } else {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001174 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001175 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001176 error = true;
1177 continue;
1178 }
1179 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001180 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001181 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001182 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001183 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001184 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001185 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001186 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001187
1188 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001189 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001190
Adam Lesinski46c4d722017-08-23 13:03:56 -07001191 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001192}
1193
Adam Lesinski71be7052017-12-12 16:48:07 -08001194bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001196 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001197 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001198 return true;
1199 }
1200 return false;
1201}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001202
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001203bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1204 ParsedResource* out_resource) {
1205 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001206}
1207
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001208bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1209 ParsedResource* out_resource, bool weak) {
1210 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211
1212 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001213 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1214 diag_->Warn(DiagMessage(out_resource->source)
1215 << "ignoring configuration '" << out_resource->config
1216 << "' for attribute " << out_resource->name);
1217 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001218 }
1219
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001220 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001221
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1223 if (maybe_format) {
1224 type_mask = ParseFormatAttribute(maybe_format.value());
1225 if (type_mask == 0) {
1226 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001227 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 return false;
1229 }
1230 }
1231
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1235 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1236 if (!min_str.empty()) {
1237 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001239 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001241 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001242 }
1243
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 if (!maybe_min) {
1245 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1246 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 return false;
1248 }
1249 }
1250
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001251 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1252 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1253 if (!max_str.empty()) {
1254 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001256 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001259 }
1260
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 if (!maybe_max) {
1262 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1263 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 return false;
1265 }
1266 }
1267
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001268 if ((maybe_min || maybe_max) &&
1269 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1270 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271 << "'min' and 'max' can only be used when format='integer'");
1272 return false;
1273 }
1274
1275 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001276 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 return a.symbol.name.value() < b.symbol.name.value();
1278 }
1279 };
1280
1281 std::set<Attribute::Symbol, SymbolComparator> items;
1282
1283 std::string comment;
1284 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285 const size_t depth = parser->depth();
1286 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1287 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001288 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001289 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 // Skip text.
1292 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001293 }
1294
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 const Source item_source = source_.WithLine(parser->line_number());
1296 const std::string& element_namespace = parser->element_namespace();
1297 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001298 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299 if (element_name == "enum") {
1300 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1301 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 << "can not define an <enum>; already defined a <flag>");
1303 error = true;
1304 continue;
1305 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 } else if (element_name == "flag") {
1309 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1310 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311 << "can not define a <flag>; already defined an <enum>");
1312 error = true;
1313 continue;
1314 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316 }
1317
1318 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 ParsedResource child_resource;
1322 child_resource.name = symbol.symbol.name.value();
1323 child_resource.source = item_source;
1324 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001325 if (options_.visibility) {
1326 child_resource.visibility_level = options_.visibility.value();
1327 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 symbol.symbol.SetComment(std::move(comment));
1331 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001333 auto insert_result = items.insert(std::move(symbol));
1334 if (!insert_result.second) {
1335 const Attribute::Symbol& existing_symbol = *insert_result.first;
1336 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001337 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001341 << "first defined here");
1342 error = true;
1343 }
1344 } else {
1345 error = true;
1346 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001347 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1348 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001349 error = true;
1350 }
1351
1352 comment = {};
1353 }
1354
1355 if (error) {
1356 return false;
1357 }
1358
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001359 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1360 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1361 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001363 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1364 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001365 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001366 return true;
1367}
1368
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1374 if (!maybe_name) {
1375 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001376 << tag << ">");
1377 return {};
1378 }
1379
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1381 if (!maybe_value) {
1382 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 << tag << ">");
1384 return {};
1385 }
1386
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 android::Res_value val;
1389 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 << "' for <" << tag
1392 << ">; must be an integer");
1393 return {};
1394 }
1395
1396 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001397 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001399}
1400
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001401bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1402 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001403
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001404 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1405 if (!maybe_name) {
1406 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001407 return false;
1408 }
1409
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001410 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001411 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001412 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001413 return false;
1414 }
1415
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001416 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001417 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001419 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001421 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422 return false;
1423 }
1424
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001425 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001426 return true;
1427}
1428
Adam Lesinski86d67df2017-01-31 13:47:27 -08001429bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001430 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001431 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001432
1433 std::unique_ptr<Style> style = util::make_unique<Style>();
1434
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001435 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1436 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001437 // 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 -07001438 if (!maybe_parent.value().empty()) {
1439 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001440 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001442 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001443 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001444 }
1445
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001446 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001447 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001448 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001449 }
1450
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451 } else {
1452 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001453 std::string style_name = out_resource->name.entry;
1454 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001457 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001458 }
1459 }
1460
1461 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001462 const size_t depth = parser->depth();
1463 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1464 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001465 // Skip text and comments.
1466 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001467 }
1468
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001469 const std::string& element_namespace = parser->element_namespace();
1470 const std::string& element_name = parser->element_name();
1471 if (element_namespace == "" && element_name == "item") {
1472 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001473
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1475 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1476 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001478 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001479 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001480
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001481 if (error) {
1482 return false;
1483 }
1484
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001485 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001487}
1488
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001489bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1490 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1491 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1492 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1493 if (resource_format == 0u) {
1494 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1495 << "'" << format_attr.value() << "' is an invalid format");
1496 return false;
1497 }
1498 }
1499 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001500}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001501
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001502bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1503 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001504}
1505
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001506bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1507 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001508}
1509
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1511 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001512 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001514
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001516
Adam Lesinski75421622017-01-06 15:20:04 -08001517 bool translatable = options_.translatable;
1518 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1519 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1520 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001522 << "invalid value for 'translatable'. Must be a boolean");
1523 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001524 }
Adam Lesinski75421622017-01-06 15:20:04 -08001525 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001526 }
Adam Lesinski75421622017-01-06 15:20:04 -08001527 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001528
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001529 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 const size_t depth = parser->depth();
1531 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1532 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001533 // Skip text and comments.
1534 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001535 }
1536
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001537 const Source item_source = source_.WithLine(parser->line_number());
1538 const std::string& element_namespace = parser->element_namespace();
1539 const std::string& element_name = parser->element_name();
1540 if (element_namespace.empty() && element_name == "item") {
1541 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001542 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001544 error = true;
1545 continue;
1546 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001548 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001549
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001550 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1551 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1552 << "unknown tag <" << element_namespace << ":"
1553 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001554 error = true;
1555 }
1556 }
1557
1558 if (error) {
1559 return false;
1560 }
1561
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001562 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001563 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001564}
1565
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001566bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1567 ParsedResource* out_resource) {
1568 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001569
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001571
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001572 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001573 const size_t depth = parser->depth();
1574 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1575 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001576 // Skip text and comments.
1577 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001578 }
1579
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001580 const Source item_source = source_.WithLine(parser->line_number());
1581 const std::string& element_namespace = parser->element_namespace();
1582 const std::string& element_name = parser->element_name();
1583 if (element_namespace.empty() && element_name == "item") {
1584 Maybe<StringPiece> maybe_quantity =
1585 xml::FindNonEmptyAttribute(parser, "quantity");
1586 if (!maybe_quantity) {
1587 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588 << "<item> in <plurals> requires attribute "
1589 << "'quantity'");
1590 error = true;
1591 continue;
1592 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001593
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 StringPiece trimmed_quantity =
1595 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001596 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001597 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001599 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001600 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001601 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001603 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001604 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001605 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001606 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 index = Plural::Other;
1609 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001610 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 error = true;
1614 continue;
1615 }
1616
1617 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001618 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1619 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001620 error = true;
1621 continue;
1622 }
1623
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001624 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1626 error = true;
1627 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001628 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001629
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001630 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1631 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1632 << element_namespace << ":"
1633 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001634 error = true;
1635 }
1636 }
1637
1638 if (error) {
1639 return false;
1640 }
1641
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001642 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001643 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001644}
1645
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001646bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1647 ParsedResource* out_resource) {
1648 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001649
Adam Lesinski71be7052017-12-12 16:48:07 -08001650 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1651 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001652
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001654 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1655 diag_->Warn(DiagMessage(out_resource->source)
1656 << "ignoring configuration '" << out_resource->config
1657 << "' for styleable " << out_resource->name.entry);
1658 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001659 }
1660
1661 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1662
1663 std::string comment;
1664 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001665 const size_t depth = parser->depth();
1666 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1667 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001668 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001669 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001670 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 // Ignore text.
1672 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001673 }
1674
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001675 const Source item_source = source_.WithLine(parser->line_number());
1676 const std::string& element_namespace = parser->element_namespace();
1677 const std::string& element_name = parser->element_name();
1678 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001679 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001680 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001681 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001682 error = true;
1683 continue;
1684 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001685
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001686 // If this is a declaration, the package name may be in the name. Separate
1687 // these out.
1688 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001689 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001690 if (!maybe_ref) {
1691 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1692 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001693 error = true;
1694 continue;
1695 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001696
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001697 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001698 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001699
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001700 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001701 ParsedResource child_resource;
1702 child_resource.name = child_ref.name.value();
1703 child_resource.source = item_source;
1704 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001705 if (options_.visibility) {
1706 child_resource.visibility_level = options_.visibility.value();
1707 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001708
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001709 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001710 error = true;
1711 continue;
1712 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001713
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001714 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001715 child_ref.SetComment(child_resource.comment);
1716 child_ref.SetSource(item_source);
1717 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001718
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001719 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001720
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001721 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1722 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1723 << element_namespace << ":"
1724 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001725 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001726 }
1727
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728 comment = {};
1729 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001730
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001731 if (error) {
1732 return false;
1733 }
1734
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001735 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001736 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001737}
1738
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001739} // namespace aapt