blob: 5e8d870488c40cebc2dcd41f9ef74427999e525a [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Adam Lesinski2eed52e2018-02-21 15:55:58 -080035using ::aapt::ResourceUtils::StringBuilder;
36using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070042constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070044// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
45static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080047}
48
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
50 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070054 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068}
69
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070static uint32_t ParseFormatType(const StringPiece& piece) {
71 if (piece == "enum") {
72 return android::ResTable_map::TYPE_ENUM;
73 } else if (piece == "flags") {
74 return android::ResTable_map::TYPE_FLAGS;
75 }
76 return ParseFormatTypeNoEnumsOrFlags(piece);
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080081 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 StringPiece trimmed_part = util::TrimWhitespace(part);
83 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 if (type == 0) {
85 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 mask |= type;
88 }
89 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090}
91
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070092// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080093struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 ResourceName name;
95 ConfigDescription config;
96 std::string product;
97 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800100 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700101 bool allow_new = false;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800102 Maybe<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::string comment;
105 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800107};
108
109// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700110static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
112 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800114 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski76565542016-03-10 21:55:04 -0800116
Adam Lesinski71be7052017-12-12 16:48:07 -0800117 if (res->visibility_level != Visibility::Level::kUndefined) {
118 Visibility visibility;
119 visibility.level = res->visibility_level;
120 visibility.source = res->source;
121 visibility.comment = res->comment;
122 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800126
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->allow_new) {
128 AllowNew allow_new;
129 allow_new.source = res->source;
130 allow_new.comment = res->comment;
131 if (!table->SetAllowNew(res->name, allow_new, diag)) {
132 return false;
133 }
134 }
135
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800136 if (res->overlayable_item) {
137 if (!table->SetOverlayable(res->name, res->overlayable_item.value(), diag)) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800138 return false;
139 }
140 }
141
142 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 res->value->SetComment(std::move(res->comment));
145 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800146
Adam Lesinski71be7052017-12-12 16:48:07 -0800147 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
148 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800150 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 for (ParsedResource& child : res->child_resources) {
155 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800158}
159
160// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
164 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700165 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 : diag_(diag),
168 table_(table),
169 source_(source),
170 config_(config),
171 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800173// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
174// This will be used to traverse and flatten the XML string into a single std::string, with all
175// Span and Untranslatable data maintained in parallel, as indices into the string.
176class Node {
177 public:
178 virtual ~Node() = default;
179
180 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
181 // parent node as well.
182 // Returns a pointer to the child node that was added as a convenience.
183 template <typename T>
184 T* AddChild(std::unique_ptr<T> node) {
185 T* raw_ptr = node.get();
186 children.push_back(std::move(node));
187 return raw_ptr;
188 }
189
190 virtual void Build(StringBuilder* builder) const {
191 for (const auto& child : children) {
192 child->Build(builder);
193 }
194 }
195
196 std::vector<std::unique_ptr<Node>> children;
197};
198
199// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
200class SegmentNode : public Node {
201 public:
202 std::string data;
203
204 void Build(StringBuilder* builder) const override {
205 builder->AppendText(data);
206 }
207};
208
209// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
210class SpanNode : public Node {
211 public:
212 std::string name;
213
214 void Build(StringBuilder* builder) const override {
215 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
216 Node::Build(builder);
217 builder->EndSpan(span_handle);
218 }
219};
220
221// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
222class UntranslatableNode : public Node {
223 public:
224 void Build(StringBuilder* builder) const override {
225 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
226 Node::Build(builder);
227 builder->EndUntranslatable(handle);
228 }
229};
230
231// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800232bool ResourceParser::FlattenXmlSubtree(
233 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
234 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800235 std::string raw_string;
236 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800237
238 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
239 Maybe<size_t> untranslatable_start_depth;
240
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800241 Node root;
242 std::vector<Node*> node_stack;
243 node_stack.push_back(&root);
244
245 bool saw_span_node = false;
246 SegmentNode* first_segment = nullptr;
247 SegmentNode* last_segment = nullptr;
248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800250 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800252
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800253 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700254 if (event == xml::XmlPullParser::Event::kStartElement
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800255 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800256 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800257 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800258 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700259
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800260 last_segment = node_stack.back()->AddChild(std::move(segment_node));
261 if (first_segment == nullptr) {
262 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800263 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800264 current_text = {};
265 }
266 }
Adam Lesinski75421622017-01-06 15:20:04 -0800267
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 switch (event) {
269 case xml::XmlPullParser::Event::kText: {
270 current_text += parser->text();
271 raw_string += parser->text();
272 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800273
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 case xml::XmlPullParser::Event::kStartElement: {
275 if (parser->element_namespace().empty()) {
276 // This is an HTML tag which we encode as a span. Add it to the span stack.
277 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
278 span_node->name = parser->element_name();
279 const auto end_attr_iter = parser->end_attributes();
280 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
281 ++attr_iter) {
282 span_node->name += ";";
283 span_node->name += attr_iter->name;
284 span_node->name += "=";
285 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800286 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800287
288 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
289 saw_span_node = true;
290 } else if (parser->element_namespace() == sXliffNamespaceUri) {
291 // This is an XLIFF tag, which is not encoded as a span.
292 if (parser->element_name() == "g") {
293 // Check that an 'untranslatable' tag is not already being processed. Nested
294 // <xliff:g> tags are illegal.
295 if (untranslatable_start_depth) {
296 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
297 << "illegal nested XLIFF 'g' tag");
298 return false;
299 } else {
300 // Mark the beginning of an 'untranslatable' section.
301 untranslatable_start_depth = depth;
302 node_stack.push_back(
303 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
304 }
305 } else {
306 // Ignore unknown XLIFF tags, but don't warn.
307 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
308 }
309 } else {
310 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
311 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
312 << "ignoring element '" << parser->element_name()
313 << "' with unknown namespace '" << parser->element_namespace() << "'");
314 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800315 }
Adam Lesinski75421622017-01-06 15:20:04 -0800316
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800317 // Enter one level inside the element.
318 depth++;
319 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700320
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800321 case xml::XmlPullParser::Event::kEndElement: {
322 // Return one level from within the element.
323 depth--;
324 if (depth == 0) {
325 break;
326 }
327
328 node_stack.pop_back();
329 if (untranslatable_start_depth == make_value(depth)) {
330 // This is the end of an untranslatable section.
331 untranslatable_start_depth = {};
332 }
333 } break;
334
335 default:
336 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 }
339 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800341 // Sanity check to make sure we processed all the nodes.
342 CHECK(node_stack.size() == 1u);
343 CHECK(node_stack.back() == &root);
344
345 if (!saw_span_node) {
346 // If there were no spans, we must treat this string a little differently (according to AAPT).
347 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
348 // from the last segment.
349 if (first_segment != nullptr) {
350 // Trim leading whitespace.
351 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
352 if (trimmed.size() != first_segment->data.size()) {
353 first_segment->data = trimmed.to_string();
354 }
355 }
356
357 if (last_segment != nullptr) {
358 // Trim trailing whitespace.
359 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
360 if (trimmed.size() != last_segment->data.size()) {
361 last_segment->data = trimmed.to_string();
362 }
363 }
364 }
365
366 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
367 // care of recording the correctly adjusted Spans and UntranslatableSections.
368 StringBuilder builder;
369 root.Build(&builder);
370 if (!builder) {
371 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
372 return false;
373 }
374
375 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
376 *out_raw_string = std::move(raw_string);
377 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
378 out_style_string->str = std::move(flattened_string.text);
379 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800380 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800381}
382
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 const size_t depth = parser->depth();
386 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
387 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 // Skip comments and text.
389 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800390 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391
Adam Lesinski060b53d2017-07-28 17:10:35 -0700392 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 << "root element must be <resources>");
395 return false;
396 }
397
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 break;
400 };
401
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
403 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
404 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 return false;
406 }
407 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408}
409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
411 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700412
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 bool error = false;
414 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 const size_t depth = parser->depth();
416 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
417 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800421 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700422
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 if (!util::TrimWhitespace(parser->text()).empty()) {
425 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 << "plain text not allowed here");
427 error = true;
428 }
429 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700430 }
431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 // Skip unknown namespace.
436 continue;
437 }
438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 std::string element_name = parser->element_name();
440 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 comment = "";
442 continue;
443 }
444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 ParsedResource parsed_resource;
446 parsed_resource.config = config_;
447 parsed_resource.source = source_.WithLine(parser->line_number());
448 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100449 if (options_.visibility) {
450 parsed_resource.visibility_level = options_.visibility.value();
451 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452
453 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700454 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800455 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 }
457
458 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 error = true;
461 continue;
462 }
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 error = true;
466 }
467 }
468
469 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 for (const ResourceName& stripped_resource : stripped_resources) {
471 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700473 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
474 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 error = true;
476 }
477 }
478
479 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800480}
481
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
483 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 struct ItemTypeFormat {
485 ResourceType type;
486 uint32_t format;
487 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800488
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
490 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800491
Adam Lesinski86d67df2017-01-31 13:47:27 -0800492 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
493 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
494 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
495 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
496 {"dimen",
497 {ResourceType::kDimen,
498 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
499 android::ResTable_map::TYPE_DIMENSION}},
500 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
501 {"fraction",
502 {ResourceType::kFraction,
503 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
504 android::ResTable_map::TYPE_DIMENSION}},
505 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
506 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
507 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800508
Adam Lesinski86d67df2017-01-31 13:47:27 -0800509 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
510 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
511 {"array", std::mem_fn(&ResourceParser::ParseArray)},
512 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
513 {"configVarying",
514 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
515 std::placeholders::_2, std::placeholders::_3)},
516 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
517 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
518 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700519 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800520 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
521 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
522 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
523 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
524 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
525 std::placeholders::_2, std::placeholders::_3)},
526 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
527 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800530
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800533
Adam Lesinski86d67df2017-01-31 13:47:27 -0800534 bool can_be_item = true;
535 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800537 can_be_bag = false;
538
Adam Lesinskie597d682017-06-01 17:16:44 -0700539 // The default format for <item> is any. If a format attribute is present, that one will
540 // override the default.
541 resource_format = android::ResTable_map::TYPE_ANY;
542
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700544 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800545 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 << "<item> must have a 'type' attribute");
549 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800550 }
551
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700552 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700554 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700556 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 if (!resource_format) {
558 diag_->Error(DiagMessage(out_resource->source)
559 << "'" << maybe_format.value()
560 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800561 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 }
563 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800564 } else if (resource_type == "bag") {
565 can_be_item = false;
566
567 // Bags have their type encoded in the type attribute.
568 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
569 resource_type = maybe_type.value().to_string();
570 } else {
571 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
572 << "<bag> must have a 'type' attribute");
573 return false;
574 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 }
576
577 // Get the name of the resource. This will be checked later, because not all
578 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 if (resource_type == "id") {
582 if (!maybe_name) {
583 diag_->Error(DiagMessage(out_resource->source)
584 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 << "> missing 'name' attribute");
586 return false;
587 }
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800590 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700591
592 // Ids either represent a unique resource id or reference another resource id
593 auto item = ParseItem(parser, out_resource, resource_format);
594 if (!item) {
595 return false;
596 }
597
598 String* empty = ValueCast<String>(out_resource->value.get());
599 if (empty && *empty->value == "") {
600 // If no inner element exists, represent a unique identifier
601 out_resource->value = util::make_unique<Id>();
602 } else {
y9efbbef2018-04-18 11:29:09 -0700603 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700604 if (ref && !ref->name && !ref->id) {
605 // A null reference also means there is no inner element when ids are in the form:
606 // <id name="name"/>
607 out_resource->value = util::make_unique<Id>();
608 } else if (!ref || ref->name.value().type != ResourceType::kId) {
609 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700610 diag_->Error(DiagMessage(out_resource->source)
611 << "<" << parser->element_name()
612 << "> inner element must either be a resource reference or empty");
613 return false;
614 }
615 }
616
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 return true;
618 }
619
Adam Lesinski86d67df2017-01-31 13:47:27 -0800620 if (can_be_item) {
621 const auto item_iter = elToItemMap.find(resource_type);
622 if (item_iter != elToItemMap.end()) {
623 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624
Adam Lesinski86d67df2017-01-31 13:47:27 -0800625 if (!maybe_name) {
626 diag_->Error(DiagMessage(out_resource->source)
627 << "<" << parser->element_name() << "> missing 'name' attribute");
628 return false;
629 }
630
631 out_resource->name.type = item_iter->second.type;
632 out_resource->name.entry = maybe_name.value().to_string();
633
Adam Lesinskie597d682017-06-01 17:16:44 -0700634 // Only use the implied format of the type when there is no explicit format.
635 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800636 resource_format = item_iter->second.format;
637 }
638
639 if (!ParseItem(parser, out_resource, resource_format)) {
640 return false;
641 }
642 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 }
645
646 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800647 if (can_be_bag) {
648 const auto bag_iter = elToBagMap.find(resource_type);
649 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700650 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700651 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800652 if (!maybe_name) {
653 diag_->Error(DiagMessage(out_resource->source)
654 << "<" << parser->element_name() << "> missing 'name' attribute");
655 return false;
656 }
657
658 out_resource->name.entry = maybe_name.value().to_string();
659 }
660
661 // Call the associated parse method. The type will be filled in by the
662 // parse func.
663 if (!bag_iter->second(this, parser, out_resource)) {
664 return false;
665 }
666 return true;
667 }
668 }
669
670 if (can_be_item) {
671 // Try parsing the elementName (or type) as a resource. These shall only be
672 // resources like 'layout' or 'xml' and they can only be references.
673 const ResourceType* parsed_type = ParseResourceType(resource_type);
674 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 if (!maybe_name) {
676 diag_->Error(DiagMessage(out_resource->source)
677 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 << "> missing 'name' attribute");
679 return false;
680 }
681
Adam Lesinski86d67df2017-01-31 13:47:27 -0800682 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800683 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800684 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
685 if (!out_resource->value) {
686 diag_->Error(DiagMessage(out_resource->source)
687 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
688 return false;
689 }
690 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 }
693
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000694 // If the resource type was not recognized, write the error and return false.
695 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 return false;
698}
699
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
701 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 const uint32_t format) {
703 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 }
706
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 out_resource->value = ParseXml(parser, format, kNoRawString);
708 if (!out_resource->value) {
709 diag_->Error(DiagMessage(out_resource->source) << "invalid "
710 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 return false;
712 }
713 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800714}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800715
716/**
717 * Reads the entire XML subtree and attempts to parse it as some Item,
718 * with typeMask denoting which items it can be. If allowRawValue is
719 * true, a RawString is returned if the XML couldn't be parsed as
720 * an Item. If allowRawValue is false, nullptr is returned in this
721 * case.
722 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
724 const uint32_t type_mask,
725 const bool allow_raw_value) {
726 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 std::string raw_value;
729 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800730 std::vector<UntranslatableSection> untranslatable_sections;
731 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 }
734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800737 std::unique_ptr<StyledString> styled_string =
738 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700739 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800740 styled_string->untranslatable_sections = std::move(untranslatable_sections);
741 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
743
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 // name.package can be empty here, as it will assume the package name of the
746 // table.
747 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 id->SetSource(source_.WithLine(begin_xml_line));
749 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 };
751
752 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800754 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700758 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
762
763 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800766 std::unique_ptr<String> string = util::make_unique<String>(
767 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
768 string->untranslatable_sections = std::move(untranslatable_sections);
769 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 }
771
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700772 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
773 if (util::TrimWhitespace(raw_value).empty()) {
774 return ResourceUtils::MakeNull();
775 }
776
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // We can't parse this so return a RawString if we are allowed.
779 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700780 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
781 StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 }
783 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784}
785
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786bool ResourceParser::ParseString(xml::XmlPullParser* parser,
787 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 if (Maybe<StringPiece> formatted_attr =
790 xml::FindAttribute(parser, "formatted")) {
791 Maybe<bool> maybe_formatted =
792 ResourceUtils::ParseBool(formatted_attr.value());
793 if (!maybe_formatted) {
794 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 << "invalid value for 'formatted'. Must be a boolean");
796 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800797 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700798 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800800
Adam Lesinski75421622017-01-06 15:20:04 -0800801 bool translatable = options_.translatable;
802 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
803 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
804 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 << "invalid value for 'translatable'. Must be a boolean");
807 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800808 }
Adam Lesinski75421622017-01-06 15:20:04 -0800809 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800811
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 out_resource->value =
813 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
814 if (!out_resource->value) {
815 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 return false;
817 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800818
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800820 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800821
Adam Lesinski75421622017-01-06 15:20:04 -0800822 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 if (!util::VerifyJavaStringFormat(*string_value->value)) {
824 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 msg << "multiple substitutions specified in non-positional format; "
826 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 if (options_.error_on_positional_arguments) {
828 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800830 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800831
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800834 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835
Adam Lesinski75421622017-01-06 15:20:04 -0800836 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
837 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 }
839 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800840}
841
Adam Lesinski71be7052017-12-12 16:48:07 -0800842bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100843 if (options_.visibility) {
844 diag_->Error(DiagMessage(out_resource->source)
845 << "<public> tag not allowed with --visibility flag");
846 return false;
847 }
848
Adam Lesinski46c4d722017-08-23 13:03:56 -0700849 if (out_resource->config != ConfigDescription::DefaultConfig()) {
850 diag_->Warn(DiagMessage(out_resource->source)
851 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
852 }
853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
855 if (!maybe_type) {
856 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 << "<public> must have a 'type' attribute");
858 return false;
859 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
862 if (!parsed_type) {
863 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
864 << maybe_type.value()
865 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 return false;
867 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800870
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800871 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
872 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800874 diag_->Error(DiagMessage(out_resource->source)
875 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800877 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800880
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700885
Adam Lesinski71be7052017-12-12 16:48:07 -0800886 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800888}
889
Adam Lesinski46c4d722017-08-23 13:03:56 -0700890bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100891 if (options_.visibility) {
892 diag_->Error(DiagMessage(out_resource->source)
893 << "<public-group> tag not allowed with --visibility flag");
894 return false;
895 }
896
Adam Lesinski46c4d722017-08-23 13:03:56 -0700897 if (out_resource->config != ConfigDescription::DefaultConfig()) {
898 diag_->Warn(DiagMessage(out_resource->source)
899 << "ignoring configuration '" << out_resource->config
900 << "' for <public-group> tag");
901 }
902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
904 if (!maybe_type) {
905 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800907 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800909
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
911 if (!parsed_type) {
912 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
913 << maybe_type.value()
914 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800915 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 }
917
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700918 Maybe<StringPiece> maybe_id_str =
919 xml::FindNonEmptyAttribute(parser, "first-id");
920 if (!maybe_id_str) {
921 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 << "<public-group> must have a 'first-id' attribute");
923 return false;
924 }
925
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700926 Maybe<ResourceId> maybe_id =
927 ResourceUtils::ParseResourceId(maybe_id_str.value());
928 if (!maybe_id) {
929 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
930 << maybe_id_str.value()
931 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700932 return false;
933 }
934
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700935 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936
937 std::string comment;
938 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700939 const size_t depth = parser->depth();
940 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
941 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800942 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700944 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 // Skip text.
946 continue;
947 }
948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 const Source item_source = source_.WithLine(parser->line_number());
950 const std::string& element_namespace = parser->element_namespace();
951 const std::string& element_name = parser->element_name();
952 if (element_namespace.empty() && element_name == "public") {
953 Maybe<StringPiece> maybe_name =
954 xml::FindNonEmptyAttribute(parser, "name");
955 if (!maybe_name) {
956 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 << "<public> must have a 'name' attribute");
958 error = true;
959 continue;
960 }
961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 if (xml::FindNonEmptyAttribute(parser, "id")) {
963 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700964 << "'id' is ignored within <public-group>");
965 error = true;
966 continue;
967 }
968
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700969 if (xml::FindNonEmptyAttribute(parser, "type")) {
970 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971 << "'type' is ignored within <public-group>");
972 error = true;
973 continue;
974 }
975
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 ParsedResource child_resource;
977 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800978 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 child_resource.id = next_id;
980 child_resource.comment = std::move(comment);
981 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800982 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700984
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
988 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 error = true;
990 }
991 }
992 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800993}
994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
996 ParsedResource* out_resource) {
997 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
998 if (!maybe_type) {
999 diag_->Error(DiagMessage(out_resource->source)
1000 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001 << "> must have a 'type' attribute");
1002 return false;
1003 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001004
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1006 if (!parsed_type) {
1007 diag_->Error(DiagMessage(out_resource->source)
1008 << "invalid resource type '" << maybe_type.value() << "' in <"
1009 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 return false;
1011 }
1012
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001013 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001015}
1016
Adam Lesinski46c4d722017-08-23 13:03:56 -07001017bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001018 if (options_.visibility) {
1019 diag_->Error(DiagMessage(out_resource->source)
1020 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1021 return false;
1022 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001023 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1024 diag_->Warn(DiagMessage(out_resource->source)
1025 << "ignoring configuration '" << out_resource->config << "' for <"
1026 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001028
1029 if (!ParseSymbolImpl(parser, out_resource)) {
1030 return false;
1031 }
1032
Adam Lesinski71be7052017-12-12 16:48:07 -08001033 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001034 return true;
1035}
1036
1037bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1038 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1039 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001040 << "ignoring configuration '" << out_resource->config
1041 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001042 }
1043
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001044 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1045 if (!overlayable_name) {
1046 diag_->Error(DiagMessage(out_resource->source)
1047 << "<overlayable> tag must have a 'name' attribute");
1048 return false;
1049 }
1050
1051 const std::string kActorUriScheme =
1052 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1053 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1054 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1055 diag_->Error(DiagMessage(out_resource->source)
1056 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1057 << Overlayable::kActorScheme << "'");
1058 return false;
1059 }
1060
1061 // Create a overlayable entry grouping that represents this <overlayable>
1062 auto overlayable = std::make_shared<Overlayable>(
1063 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001064 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001065
Adam Lesinski46c4d722017-08-23 13:03:56 -07001066 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001067 std::string comment;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001068 OverlayableItem::PolicyFlags current_policies = OverlayableItem::Policy::kNone;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001069 const size_t start_depth = parser->depth();
1070 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1071 xml::XmlPullParser::Event event = parser->event();
1072 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001073 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001074 break;
1075 } else if (event == xml::XmlPullParser::Event::kEndElement
1076 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001077 // Clear the current policies when exiting the <policy> tags
1078 current_policies = OverlayableItem::Policy::kNone;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001079 continue;
1080 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001081 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001082 comment = parser->comment();
1083 continue;
1084 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001085 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001086 continue;
1087 }
1088
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001089 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001090 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001091 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001092 if (element_namespace.empty() && element_name == "item") {
Winsonb2d7f532019-02-04 16:32:43 -08001093 if (current_policies == OverlayableItem::Policy::kNone) {
1094 diag_->Error(DiagMessage(element_source)
1095 << "<item> within an <overlayable> must be inside a <policy> block");
1096 error = true;
1097 continue;
1098 }
1099
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001100 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001101 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1102 if (!item_name) {
1103 diag_->Error(DiagMessage(element_source)
1104 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001105 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001106 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001107 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001108
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001109 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1110 if (!item_type) {
1111 diag_->Error(DiagMessage(element_source)
1112 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001113 error = true;
1114 continue;
1115 }
1116
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001117 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001118 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001119 diag_->Error(DiagMessage(element_source)
1120 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001121 << "' in <item> within an <overlayable>");
1122 error = true;
1123 continue;
1124 }
1125
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001126 OverlayableItem overlayable_item(overlayable);
1127 overlayable_item.policies = current_policies;
1128 overlayable_item.comment = comment;
1129 overlayable_item.source = element_source;
1130
1131 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001132 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001133 child_resource.name.entry = item_name.value().to_string();
1134 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001135 out_resource->child_resources.push_back(std::move(child_resource));
1136
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001137 } else if (element_namespace.empty() && element_name == "policy") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001138 if (current_policies != OverlayableItem::Policy::kNone) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001139 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001140 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001141 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001142 break;
1143 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1144 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001145 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001146 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
1147 StringPiece trimmed_part = util::TrimWhitespace(part);
1148 if (trimmed_part == "public") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001149 current_policies |= OverlayableItem::Policy::kPublic;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001150 } else if (trimmed_part == "product") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001151 current_policies |= OverlayableItem::Policy::kProduct;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001152 } else if (trimmed_part == "system") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001153 current_policies |= OverlayableItem::Policy::kSystem;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001154 } else if (trimmed_part == "vendor") {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001155 current_policies |= OverlayableItem::Policy::kVendor;
Winsonb2d7f532019-02-04 16:32:43 -08001156 } else if (trimmed_part == "signature") {
1157 current_policies |= OverlayableItem::Policy::kSignature;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001158 } else {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001159 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001160 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001161 error = true;
1162 continue;
1163 }
1164 }
Winsonb2d7f532019-02-04 16:32:43 -08001165 } else {
1166 diag_->Error(DiagMessage(element_source)
1167 << "<policy> must have a 'type' attribute");
1168 error = true;
1169 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001170 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001171 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001172 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001173 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001174 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001175 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001176 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001177
1178 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001179 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001180
Adam Lesinski46c4d722017-08-23 13:03:56 -07001181 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001182}
1183
Adam Lesinski71be7052017-12-12 16:48:07 -08001184bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001185 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001186 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001187 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 return true;
1189 }
1190 return false;
1191}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001192
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001193bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1194 ParsedResource* out_resource) {
1195 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196}
1197
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001198bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1199 ParsedResource* out_resource, bool weak) {
1200 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201
1202 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001203 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1204 diag_->Warn(DiagMessage(out_resource->source)
1205 << "ignoring configuration '" << out_resource->config
1206 << "' for attribute " << out_resource->name);
1207 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208 }
1209
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001210 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1213 if (maybe_format) {
1214 type_mask = ParseFormatAttribute(maybe_format.value());
1215 if (type_mask == 0) {
1216 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001217 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001218 return false;
1219 }
1220 }
1221
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001223
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1225 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1226 if (!min_str.empty()) {
1227 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001229 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001230 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001231 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001232 }
1233
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 if (!maybe_min) {
1235 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1236 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 return false;
1238 }
1239 }
1240
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001241 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1242 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1243 if (!max_str.empty()) {
1244 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001245 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001246 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001248 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001249 }
1250
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001251 if (!maybe_max) {
1252 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1253 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001254 return false;
1255 }
1256 }
1257
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001258 if ((maybe_min || maybe_max) &&
1259 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1260 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001261 << "'min' and 'max' can only be used when format='integer'");
1262 return false;
1263 }
1264
1265 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001266 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 return a.symbol.name.value() < b.symbol.name.value();
1268 }
1269 };
1270
1271 std::set<Attribute::Symbol, SymbolComparator> items;
1272
1273 std::string comment;
1274 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001275 const size_t depth = parser->depth();
1276 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1277 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001278 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001279 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001280 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001281 // Skip text.
1282 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001283 }
1284
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285 const Source item_source = source_.WithLine(parser->line_number());
1286 const std::string& element_namespace = parser->element_namespace();
1287 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001288 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001289 if (element_name == "enum") {
1290 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1291 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292 << "can not define an <enum>; already defined a <flag>");
1293 error = true;
1294 continue;
1295 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001296 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 } else if (element_name == "flag") {
1299 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1300 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 << "can not define a <flag>; already defined an <enum>");
1302 error = true;
1303 continue;
1304 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 }
1307
1308 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 ParsedResource child_resource;
1312 child_resource.name = symbol.symbol.name.value();
1313 child_resource.source = item_source;
1314 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001315 if (options_.visibility) {
1316 child_resource.visibility_level = options_.visibility.value();
1317 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001319
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 symbol.symbol.SetComment(std::move(comment));
1321 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 auto insert_result = items.insert(std::move(symbol));
1324 if (!insert_result.second) {
1325 const Attribute::Symbol& existing_symbol = *insert_result.first;
1326 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001328 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331 << "first defined here");
1332 error = true;
1333 }
1334 } else {
1335 error = true;
1336 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1338 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 error = true;
1340 }
1341
1342 comment = {};
1343 }
1344
1345 if (error) {
1346 return false;
1347 }
1348
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001349 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1350 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1351 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001353 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1354 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001356 return true;
1357}
1358
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001359Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001360 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001361 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001362
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001363 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1364 if (!maybe_name) {
1365 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001366 << tag << ">");
1367 return {};
1368 }
1369
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1371 if (!maybe_value) {
1372 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001373 << tag << ">");
1374 return {};
1375 }
1376
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 android::Res_value val;
1379 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001380 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 << "' for <" << tag
1382 << ">; must be an integer");
1383 return {};
1384 }
1385
1386 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001389}
1390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1392 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001393
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1395 if (!maybe_name) {
1396 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 return false;
1398 }
1399
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001400 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001401 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001402 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 return false;
1404 }
1405
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001406 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001407 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001409 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001410 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001411 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001412 return false;
1413 }
1414
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001415 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 return true;
1417}
1418
Adam Lesinski86d67df2017-01-31 13:47:27 -08001419bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001421 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001422
1423 std::unique_ptr<Style> style = util::make_unique<Style>();
1424
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1426 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001427 // 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 -07001428 if (!maybe_parent.value().empty()) {
1429 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001430 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001432 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001433 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001434 }
1435
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001436 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001437 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001438 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001439 }
1440
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441 } else {
1442 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001443 std::string style_name = out_resource->name.entry;
1444 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001446 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001447 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001448 }
1449 }
1450
1451 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 const size_t depth = parser->depth();
1453 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1454 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001455 // Skip text and comments.
1456 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001457 }
1458
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 const std::string& element_namespace = parser->element_namespace();
1460 const std::string& element_name = parser->element_name();
1461 if (element_namespace == "" && element_name == "item") {
1462 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001463
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001464 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1465 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1466 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001467 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001468 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001469 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001470
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001471 if (error) {
1472 return false;
1473 }
1474
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001475 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001477}
1478
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001479bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1480 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1481 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1482 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1483 if (resource_format == 0u) {
1484 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1485 << "'" << format_attr.value() << "' is an invalid format");
1486 return false;
1487 }
1488 }
1489 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001490}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001491
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001492bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1493 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001494}
1495
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001496bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1497 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001498}
1499
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001500bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1501 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001502 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001503 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001504
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001505 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001506
Adam Lesinski75421622017-01-06 15:20:04 -08001507 bool translatable = options_.translatable;
1508 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1509 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1510 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001511 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001512 << "invalid value for 'translatable'. Must be a boolean");
1513 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001514 }
Adam Lesinski75421622017-01-06 15:20:04 -08001515 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001516 }
Adam Lesinski75421622017-01-06 15:20:04 -08001517 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001518
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001519 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 const size_t depth = parser->depth();
1521 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1522 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001523 // Skip text and comments.
1524 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001525 }
1526
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001527 const Source item_source = source_.WithLine(parser->line_number());
1528 const std::string& element_namespace = parser->element_namespace();
1529 const std::string& element_name = parser->element_name();
1530 if (element_namespace.empty() && element_name == "item") {
1531 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001533 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534 error = true;
1535 continue;
1536 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001537 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001538 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001539
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1541 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1542 << "unknown tag <" << element_namespace << ":"
1543 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001544 error = true;
1545 }
1546 }
1547
1548 if (error) {
1549 return false;
1550 }
1551
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001552 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001553 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001554}
1555
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1557 ParsedResource* out_resource) {
1558 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001559
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001560 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001561
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001563 const size_t depth = parser->depth();
1564 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1565 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566 // Skip text and comments.
1567 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001568 }
1569
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001570 const Source item_source = source_.WithLine(parser->line_number());
1571 const std::string& element_namespace = parser->element_namespace();
1572 const std::string& element_name = parser->element_name();
1573 if (element_namespace.empty() && element_name == "item") {
1574 Maybe<StringPiece> maybe_quantity =
1575 xml::FindNonEmptyAttribute(parser, "quantity");
1576 if (!maybe_quantity) {
1577 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001578 << "<item> in <plurals> requires attribute "
1579 << "'quantity'");
1580 error = true;
1581 continue;
1582 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001583
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001584 StringPiece trimmed_quantity =
1585 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001586 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001587 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001590 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001591 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001594 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001595 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001596 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001597 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598 index = Plural::Other;
1599 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001603 error = true;
1604 continue;
1605 }
1606
1607 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001608 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1609 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 error = true;
1611 continue;
1612 }
1613
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001615 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1616 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001617 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001618 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001619
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001622 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1623 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1624 << element_namespace << ":"
1625 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001626 error = true;
1627 }
1628 }
1629
1630 if (error) {
1631 return false;
1632 }
1633
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001634 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001635 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001636}
1637
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1639 ParsedResource* out_resource) {
1640 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001641
Adam Lesinski71be7052017-12-12 16:48:07 -08001642 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1643 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001644
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001646 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1647 diag_->Warn(DiagMessage(out_resource->source)
1648 << "ignoring configuration '" << out_resource->config
1649 << "' for styleable " << out_resource->name.entry);
1650 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651 }
1652
1653 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1654
1655 std::string comment;
1656 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001657 const size_t depth = parser->depth();
1658 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1659 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001660 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001661 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001662 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001663 // Ignore text.
1664 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001665 }
1666
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001667 const Source item_source = source_.WithLine(parser->line_number());
1668 const std::string& element_namespace = parser->element_namespace();
1669 const std::string& element_name = parser->element_name();
1670 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001671 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001672 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001673 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001674 error = true;
1675 continue;
1676 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001677
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001678 // If this is a declaration, the package name may be in the name. Separate
1679 // these out.
1680 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001681 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001682 if (!maybe_ref) {
1683 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1684 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001685 error = true;
1686 continue;
1687 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001688
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001690 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001691
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001693 ParsedResource child_resource;
1694 child_resource.name = child_ref.name.value();
1695 child_resource.source = item_source;
1696 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001697 if (options_.visibility) {
1698 child_resource.visibility_level = options_.visibility.value();
1699 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001700
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001701 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001702 error = true;
1703 continue;
1704 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001705
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001706 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001707 child_ref.SetComment(child_resource.comment);
1708 child_ref.SetSource(item_source);
1709 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001710
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001711 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001712
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001713 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1714 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1715 << element_namespace << ":"
1716 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001717 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001718 }
1719
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001720 comment = {};
1721 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001722
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001723 if (error) {
1724 return false;
1725 }
1726
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001727 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001728 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001729}
1730
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001731} // namespace aapt