blob: 4f25e0968c0ecb90c02f9303a1e054d995da17c2 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Adam Lesinski2eed52e2018-02-21 15:55:58 -080035using ::aapt::ResourceUtils::StringBuilder;
36using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070042constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070044// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
45static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080047}
48
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
50 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070054 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068}
69
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070static uint32_t ParseFormatType(const StringPiece& piece) {
71 if (piece == "enum") {
72 return android::ResTable_map::TYPE_ENUM;
73 } else if (piece == "flags") {
74 return android::ResTable_map::TYPE_FLAGS;
75 }
76 return ParseFormatTypeNoEnumsOrFlags(piece);
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 for (StringPiece part : util::Tokenize(str, '|')) {
82 StringPiece trimmed_part = util::TrimWhitespace(part);
83 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 if (type == 0) {
85 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 mask |= type;
88 }
89 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090}
91
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070092// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080093struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 ResourceName name;
95 ConfigDescription config;
96 std::string product;
97 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800100 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700101 bool allow_new = false;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700102 std::vector<Overlayable> overlayable_declarations;
Adam Lesinski71be7052017-12-12 16:48:07 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::string comment;
105 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800107};
108
109// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700110static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
112 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800114 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800116
Adam Lesinski71be7052017-12-12 16:48:07 -0800117 if (res->visibility_level != Visibility::Level::kUndefined) {
118 Visibility visibility;
119 visibility.level = res->visibility_level;
120 visibility.source = res->source;
121 visibility.comment = res->comment;
122 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800126
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->allow_new) {
128 AllowNew allow_new;
129 allow_new.source = res->source;
130 allow_new.comment = res->comment;
131 if (!table->SetAllowNew(res->name, allow_new, diag)) {
132 return false;
133 }
134 }
135
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700136 for (auto& overlayable : res->overlayable_declarations) {
137 if (!table->AddOverlayable(res->name, overlayable, diag)) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800138 return false;
139 }
140 }
141
142 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 res->value->SetComment(std::move(res->comment));
145 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800146
Adam Lesinski71be7052017-12-12 16:48:07 -0800147 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
148 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800150 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 for (ParsedResource& child : res->child_resources) {
155 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
157 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800158}
159
160// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
164 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700165 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 : diag_(diag),
168 table_(table),
169 source_(source),
170 config_(config),
171 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800173// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
174// This will be used to traverse and flatten the XML string into a single std::string, with all
175// Span and Untranslatable data maintained in parallel, as indices into the string.
176class Node {
177 public:
178 virtual ~Node() = default;
179
180 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
181 // parent node as well.
182 // Returns a pointer to the child node that was added as a convenience.
183 template <typename T>
184 T* AddChild(std::unique_ptr<T> node) {
185 T* raw_ptr = node.get();
186 children.push_back(std::move(node));
187 return raw_ptr;
188 }
189
190 virtual void Build(StringBuilder* builder) const {
191 for (const auto& child : children) {
192 child->Build(builder);
193 }
194 }
195
196 std::vector<std::unique_ptr<Node>> children;
197};
198
199// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
200class SegmentNode : public Node {
201 public:
202 std::string data;
203
204 void Build(StringBuilder* builder) const override {
205 builder->AppendText(data);
206 }
207};
208
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700209// A chunk of text in the XML string within a CDATA tags.
210class CdataSegmentNode : public SegmentNode {
211 public:
212
213 void Build(StringBuilder* builder) const override {
214 builder->AppendText(data, /* preserve_spaces */ true);
215 }
216};
217
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800218// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
219class SpanNode : public Node {
220 public:
221 std::string name;
222
223 void Build(StringBuilder* builder) const override {
224 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
225 Node::Build(builder);
226 builder->EndSpan(span_handle);
227 }
228};
229
230// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
231class UntranslatableNode : public Node {
232 public:
233 void Build(StringBuilder* builder) const override {
234 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
235 Node::Build(builder);
236 builder->EndUntranslatable(handle);
237 }
238};
239
240// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800241bool ResourceParser::FlattenXmlSubtree(
242 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
243 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800244 std::string raw_string;
245 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800246
247 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
248 Maybe<size_t> untranslatable_start_depth;
249
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800250 Node root;
251 std::vector<Node*> node_stack;
252 node_stack.push_back(&root);
253
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700254 bool cdata_block = false;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800255 bool saw_span_node = false;
256 SegmentNode* first_segment = nullptr;
257 SegmentNode* last_segment = nullptr;
258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800260 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800262
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800263 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700264 if (event == xml::XmlPullParser::Event::kStartElement
265 || event == xml::XmlPullParser::Event::kEndElement
266 || event == xml::XmlPullParser::Event::kCdataStart
267 || event == xml::XmlPullParser::Event::kCdataEnd) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 if (!current_text.empty()) {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269 std::unique_ptr<SegmentNode> segment_node = (cdata_block)
270 ? util::make_unique<CdataSegmentNode>() : util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800271 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700272
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800273 last_segment = node_stack.back()->AddChild(std::move(segment_node));
274 if (first_segment == nullptr) {
275 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800276 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800277 current_text = {};
278 }
279 }
Adam Lesinski75421622017-01-06 15:20:04 -0800280
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800281 switch (event) {
282 case xml::XmlPullParser::Event::kText: {
283 current_text += parser->text();
284 raw_string += parser->text();
285 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800286
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800287 case xml::XmlPullParser::Event::kStartElement: {
288 if (parser->element_namespace().empty()) {
289 // This is an HTML tag which we encode as a span. Add it to the span stack.
290 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
291 span_node->name = parser->element_name();
292 const auto end_attr_iter = parser->end_attributes();
293 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
294 ++attr_iter) {
295 span_node->name += ";";
296 span_node->name += attr_iter->name;
297 span_node->name += "=";
298 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800299 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800300
301 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
302 saw_span_node = true;
303 } else if (parser->element_namespace() == sXliffNamespaceUri) {
304 // This is an XLIFF tag, which is not encoded as a span.
305 if (parser->element_name() == "g") {
306 // Check that an 'untranslatable' tag is not already being processed. Nested
307 // <xliff:g> tags are illegal.
308 if (untranslatable_start_depth) {
309 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
310 << "illegal nested XLIFF 'g' tag");
311 return false;
312 } else {
313 // Mark the beginning of an 'untranslatable' section.
314 untranslatable_start_depth = depth;
315 node_stack.push_back(
316 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
317 }
318 } else {
319 // Ignore unknown XLIFF tags, but don't warn.
320 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
321 }
322 } else {
323 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
324 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
325 << "ignoring element '" << parser->element_name()
326 << "' with unknown namespace '" << parser->element_namespace() << "'");
327 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800328 }
Adam Lesinski75421622017-01-06 15:20:04 -0800329
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800330 // Enter one level inside the element.
331 depth++;
332 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700333
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800334 case xml::XmlPullParser::Event::kEndElement: {
335 // Return one level from within the element.
336 depth--;
337 if (depth == 0) {
338 break;
339 }
340
341 node_stack.pop_back();
342 if (untranslatable_start_depth == make_value(depth)) {
343 // This is the end of an untranslatable section.
344 untranslatable_start_depth = {};
345 }
346 } break;
347
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700348 case xml::XmlPullParser::Event::kCdataStart: {
349 cdata_block = true;
350 break;
351 }
352
353 case xml::XmlPullParser::Event::kCdataEnd: {
354 cdata_block = false;
355 break;
356 }
357
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800358 default:
359 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 }
362 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800364 // Sanity check to make sure we processed all the nodes.
365 CHECK(node_stack.size() == 1u);
366 CHECK(node_stack.back() == &root);
367
368 if (!saw_span_node) {
369 // If there were no spans, we must treat this string a little differently (according to AAPT).
370 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
371 // from the last segment.
372 if (first_segment != nullptr) {
373 // Trim leading whitespace.
374 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
375 if (trimmed.size() != first_segment->data.size()) {
376 first_segment->data = trimmed.to_string();
377 }
378 }
379
380 if (last_segment != nullptr) {
381 // Trim trailing whitespace.
382 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
383 if (trimmed.size() != last_segment->data.size()) {
384 last_segment->data = trimmed.to_string();
385 }
386 }
387 }
388
389 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
390 // care of recording the correctly adjusted Spans and UntranslatableSections.
391 StringBuilder builder;
392 root.Build(&builder);
393 if (!builder) {
394 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
395 return false;
396 }
397
398 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
399 *out_raw_string = std::move(raw_string);
400 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
401 out_style_string->str = std::move(flattened_string.text);
402 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800403 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800404}
405
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 const size_t depth = parser->depth();
409 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
410 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 // Skip comments and text.
412 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800413 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414
Adam Lesinski060b53d2017-07-28 17:10:35 -0700415 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 << "root element must be <resources>");
418 return false;
419 }
420
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 break;
423 };
424
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
426 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
427 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 return false;
429 }
430 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431}
432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
434 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700435
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 bool error = false;
437 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700438 const size_t depth = parser->depth();
439 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
440 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800444 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700445
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 if (!util::TrimWhitespace(parser->text()).empty()) {
448 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 << "plain text not allowed here");
450 error = true;
451 }
452 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 // Skip unknown namespace.
459 continue;
460 }
461
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 std::string element_name = parser->element_name();
463 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 comment = "";
465 continue;
466 }
467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468 ParsedResource parsed_resource;
469 parsed_resource.config = config_;
470 parsed_resource.source = source_.WithLine(parser->line_number());
471 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100472 if (options_.visibility) {
473 parsed_resource.visibility_level = options_.visibility.value();
474 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475
476 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700477 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800478 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 }
480
481 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 error = true;
484 continue;
485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 error = true;
489 }
490 }
491
492 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 for (const ResourceName& stripped_resource : stripped_resources) {
494 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700496 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
497 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 error = true;
499 }
500 }
501
502 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503}
504
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
506 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 struct ItemTypeFormat {
508 ResourceType type;
509 uint32_t format;
510 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800511
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
513 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800514
Adam Lesinski86d67df2017-01-31 13:47:27 -0800515 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
516 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
517 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
518 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
519 {"dimen",
520 {ResourceType::kDimen,
521 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
522 android::ResTable_map::TYPE_DIMENSION}},
523 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
524 {"fraction",
525 {ResourceType::kFraction,
526 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
527 android::ResTable_map::TYPE_DIMENSION}},
528 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
529 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
530 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800531
Adam Lesinski86d67df2017-01-31 13:47:27 -0800532 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
533 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
534 {"array", std::mem_fn(&ResourceParser::ParseArray)},
535 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
536 {"configVarying",
537 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
538 std::placeholders::_2, std::placeholders::_3)},
539 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
540 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
541 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700542 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800543 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
544 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
545 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
546 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
547 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
548 std::placeholders::_2, std::placeholders::_3)},
549 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
550 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800553
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800556
Adam Lesinski86d67df2017-01-31 13:47:27 -0800557 bool can_be_item = true;
558 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800560 can_be_bag = false;
561
Adam Lesinskie597d682017-06-01 17:16:44 -0700562 // The default format for <item> is any. If a format attribute is present, that one will
563 // override the default.
564 resource_format = android::ResTable_map::TYPE_ANY;
565
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700567 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800568 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 << "<item> must have a 'type' attribute");
572 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800573 }
574
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700575 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700577 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700579 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 if (!resource_format) {
581 diag_->Error(DiagMessage(out_resource->source)
582 << "'" << maybe_format.value()
583 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800584 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 }
586 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800587 } else if (resource_type == "bag") {
588 can_be_item = false;
589
590 // Bags have their type encoded in the type attribute.
591 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
592 resource_type = maybe_type.value().to_string();
593 } else {
594 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
595 << "<bag> must have a 'type' attribute");
596 return false;
597 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 }
599
600 // Get the name of the resource. This will be checked later, because not all
601 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 if (resource_type == "id") {
605 if (!maybe_name) {
606 diag_->Error(DiagMessage(out_resource->source)
607 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 << "> missing 'name' attribute");
609 return false;
610 }
611
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800613 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700614
615 // Ids either represent a unique resource id or reference another resource id
616 auto item = ParseItem(parser, out_resource, resource_format);
617 if (!item) {
618 return false;
619 }
620
621 String* empty = ValueCast<String>(out_resource->value.get());
622 if (empty && *empty->value == "") {
623 // If no inner element exists, represent a unique identifier
624 out_resource->value = util::make_unique<Id>();
625 } else {
y9efbbef2018-04-18 11:29:09 -0700626 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700627 if (ref && !ref->name && !ref->id) {
628 // A null reference also means there is no inner element when ids are in the form:
629 // <id name="name"/>
630 out_resource->value = util::make_unique<Id>();
631 } else if (!ref || ref->name.value().type != ResourceType::kId) {
632 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700633 diag_->Error(DiagMessage(out_resource->source)
634 << "<" << parser->element_name()
635 << "> inner element must either be a resource reference or empty");
636 return false;
637 }
638 }
639
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 return true;
641 }
642
Adam Lesinski86d67df2017-01-31 13:47:27 -0800643 if (can_be_item) {
644 const auto item_iter = elToItemMap.find(resource_type);
645 if (item_iter != elToItemMap.end()) {
646 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647
Adam Lesinski86d67df2017-01-31 13:47:27 -0800648 if (!maybe_name) {
649 diag_->Error(DiagMessage(out_resource->source)
650 << "<" << parser->element_name() << "> missing 'name' attribute");
651 return false;
652 }
653
654 out_resource->name.type = item_iter->second.type;
655 out_resource->name.entry = maybe_name.value().to_string();
656
Adam Lesinskie597d682017-06-01 17:16:44 -0700657 // Only use the implied format of the type when there is no explicit format.
658 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800659 resource_format = item_iter->second.format;
660 }
661
662 if (!ParseItem(parser, out_resource, resource_format)) {
663 return false;
664 }
665 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 }
668
669 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800670 if (can_be_bag) {
671 const auto bag_iter = elToBagMap.find(resource_type);
672 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700673 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700674 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800675 if (!maybe_name) {
676 diag_->Error(DiagMessage(out_resource->source)
677 << "<" << parser->element_name() << "> missing 'name' attribute");
678 return false;
679 }
680
681 out_resource->name.entry = maybe_name.value().to_string();
682 }
683
684 // Call the associated parse method. The type will be filled in by the
685 // parse func.
686 if (!bag_iter->second(this, parser, out_resource)) {
687 return false;
688 }
689 return true;
690 }
691 }
692
693 if (can_be_item) {
694 // Try parsing the elementName (or type) as a resource. These shall only be
695 // resources like 'layout' or 'xml' and they can only be references.
696 const ResourceType* parsed_type = ParseResourceType(resource_type);
697 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 if (!maybe_name) {
699 diag_->Error(DiagMessage(out_resource->source)
700 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 << "> missing 'name' attribute");
702 return false;
703 }
704
Adam Lesinski86d67df2017-01-31 13:47:27 -0800705 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800706 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800707 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
708 if (!out_resource->value) {
709 diag_->Error(DiagMessage(out_resource->source)
710 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
711 return false;
712 }
713 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 diag_->Warn(DiagMessage(out_resource->source)
718 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 return false;
720}
721
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
723 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 const uint32_t format) {
725 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 }
728
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 out_resource->value = ParseXml(parser, format, kNoRawString);
730 if (!out_resource->value) {
731 diag_->Error(DiagMessage(out_resource->source) << "invalid "
732 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 return false;
734 }
735 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800736}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800737
738/**
739 * Reads the entire XML subtree and attempts to parse it as some Item,
740 * with typeMask denoting which items it can be. If allowRawValue is
741 * true, a RawString is returned if the XML couldn't be parsed as
742 * an Item. If allowRawValue is false, nullptr is returned in this
743 * case.
744 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
746 const uint32_t type_mask,
747 const bool allow_raw_value) {
748 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800749
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 std::string raw_value;
751 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800752 std::vector<UntranslatableSection> untranslatable_sections;
753 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800754 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800759 std::unique_ptr<StyledString> styled_string =
760 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700761 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800762 styled_string->untranslatable_sections = std::move(untranslatable_sections);
763 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 }
765
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 // name.package can be empty here, as it will assume the package name of the
768 // table.
769 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 id->SetSource(source_.WithLine(begin_xml_line));
771 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 };
773
774 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800776 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700780 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 }
784
785 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800788 std::unique_ptr<String> string = util::make_unique<String>(
789 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
790 string->untranslatable_sections = std::move(untranslatable_sections);
791 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 }
793
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700794 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
795 if (util::TrimWhitespace(raw_value).empty()) {
796 return ResourceUtils::MakeNull();
797 }
798
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700799 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 // We can't parse this so return a RawString if we are allowed.
801 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700802 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
803 StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700804 }
805 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800806}
807
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700808bool ResourceParser::ParseString(xml::XmlPullParser* parser,
809 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811 if (Maybe<StringPiece> formatted_attr =
812 xml::FindAttribute(parser, "formatted")) {
813 Maybe<bool> maybe_formatted =
814 ResourceUtils::ParseBool(formatted_attr.value());
815 if (!maybe_formatted) {
816 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 << "invalid value for 'formatted'. Must be a boolean");
818 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800819 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700821 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800822
Adam Lesinski75421622017-01-06 15:20:04 -0800823 bool translatable = options_.translatable;
824 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
825 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
826 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700828 << "invalid value for 'translatable'. Must be a boolean");
829 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800830 }
Adam Lesinski75421622017-01-06 15:20:04 -0800831 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 out_resource->value =
835 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
836 if (!out_resource->value) {
837 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 return false;
839 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800840
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800842 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800843
Adam Lesinski75421622017-01-06 15:20:04 -0800844 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 if (!util::VerifyJavaStringFormat(*string_value->value)) {
846 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847 msg << "multiple substitutions specified in non-positional format; "
848 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700849 if (options_.error_on_positional_arguments) {
850 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700851 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800852 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800856 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857
Adam Lesinski75421622017-01-06 15:20:04 -0800858 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
859 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860 }
861 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862}
863
Adam Lesinski71be7052017-12-12 16:48:07 -0800864bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100865 if (options_.visibility) {
866 diag_->Error(DiagMessage(out_resource->source)
867 << "<public> tag not allowed with --visibility flag");
868 return false;
869 }
870
Adam Lesinski46c4d722017-08-23 13:03:56 -0700871 if (out_resource->config != ConfigDescription::DefaultConfig()) {
872 diag_->Warn(DiagMessage(out_resource->source)
873 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
874 }
875
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
877 if (!maybe_type) {
878 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 << "<public> must have a 'type' attribute");
880 return false;
881 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800882
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
884 if (!parsed_type) {
885 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
886 << maybe_type.value()
887 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 return false;
889 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800892
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800893 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
894 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800896 diag_->Error(DiagMessage(out_resource->source)
897 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800899 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700905 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700907
Adam Lesinski71be7052017-12-12 16:48:07 -0800908 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800910}
911
Adam Lesinski46c4d722017-08-23 13:03:56 -0700912bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100913 if (options_.visibility) {
914 diag_->Error(DiagMessage(out_resource->source)
915 << "<public-group> tag not allowed with --visibility flag");
916 return false;
917 }
918
Adam Lesinski46c4d722017-08-23 13:03:56 -0700919 if (out_resource->config != ConfigDescription::DefaultConfig()) {
920 diag_->Warn(DiagMessage(out_resource->source)
921 << "ignoring configuration '" << out_resource->config
922 << "' for <public-group> tag");
923 }
924
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
926 if (!maybe_type) {
927 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800929 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
933 if (!parsed_type) {
934 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
935 << maybe_type.value()
936 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800937 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700938 }
939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 Maybe<StringPiece> maybe_id_str =
941 xml::FindNonEmptyAttribute(parser, "first-id");
942 if (!maybe_id_str) {
943 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700944 << "<public-group> must have a 'first-id' attribute");
945 return false;
946 }
947
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700948 Maybe<ResourceId> maybe_id =
949 ResourceUtils::ParseResourceId(maybe_id_str.value());
950 if (!maybe_id) {
951 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
952 << maybe_id_str.value()
953 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700954 return false;
955 }
956
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700957 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958
959 std::string comment;
960 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 const size_t depth = parser->depth();
962 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
963 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800964 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 // Skip text.
968 continue;
969 }
970
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 const Source item_source = source_.WithLine(parser->line_number());
972 const std::string& element_namespace = parser->element_namespace();
973 const std::string& element_name = parser->element_name();
974 if (element_namespace.empty() && element_name == "public") {
975 Maybe<StringPiece> maybe_name =
976 xml::FindNonEmptyAttribute(parser, "name");
977 if (!maybe_name) {
978 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700979 << "<public> must have a 'name' attribute");
980 error = true;
981 continue;
982 }
983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 if (xml::FindNonEmptyAttribute(parser, "id")) {
985 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 << "'id' is ignored within <public-group>");
987 error = true;
988 continue;
989 }
990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 if (xml::FindNonEmptyAttribute(parser, "type")) {
992 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700993 << "'type' is ignored within <public-group>");
994 error = true;
995 continue;
996 }
997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 ParsedResource child_resource;
999 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -08001000 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 child_resource.id = next_id;
1002 child_resource.comment = std::move(comment);
1003 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -08001004 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001006
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001007 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1010 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 error = true;
1012 }
1013 }
1014 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001015}
1016
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001017bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1018 ParsedResource* out_resource) {
1019 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1020 if (!maybe_type) {
1021 diag_->Error(DiagMessage(out_resource->source)
1022 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 << "> must have a 'type' attribute");
1024 return false;
1025 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001026
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001027 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1028 if (!parsed_type) {
1029 diag_->Error(DiagMessage(out_resource->source)
1030 << "invalid resource type '" << maybe_type.value() << "' in <"
1031 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032 return false;
1033 }
1034
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001037}
1038
Adam Lesinski46c4d722017-08-23 13:03:56 -07001039bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001040 if (options_.visibility) {
1041 diag_->Error(DiagMessage(out_resource->source)
1042 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1043 return false;
1044 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001045 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1046 diag_->Warn(DiagMessage(out_resource->source)
1047 << "ignoring configuration '" << out_resource->config << "' for <"
1048 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001049 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001050
1051 if (!ParseSymbolImpl(parser, out_resource)) {
1052 return false;
1053 }
1054
Adam Lesinski71be7052017-12-12 16:48:07 -08001055 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001056 return true;
1057}
1058
1059bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1060 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1061 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001062 << "ignoring configuration '" << out_resource->config
1063 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001064 }
1065
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001066 std::string comment;
1067 std::vector<Overlayable::Policy> policies;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001068
1069 bool error = false;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001070 const size_t start_depth = parser->depth();
1071 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1072 xml::XmlPullParser::Event event = parser->event();
1073 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
1074 // Break the loop when exiting the overyabale element
1075 break;
1076 } else if (event == xml::XmlPullParser::Event::kEndElement
1077 && parser->depth() == start_depth + 1) {
1078 // Clear the current policies when exiting the policy element
1079 policies.clear();
1080 continue;
1081 } else if (event == xml::XmlPullParser::Event::kComment) {
1082 // Get the comment of individual item elements
1083 comment = parser->comment();
1084 continue;
1085 } else if (event != xml::XmlPullParser::Event::kStartElement) {
1086 // Skip to the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001087 continue;
1088 }
1089
1090 const Source item_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001091 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001092 const std::string& element_namespace = parser->element_namespace();
1093
Adam Lesinski46c4d722017-08-23 13:03:56 -07001094 if (element_namespace.empty() && element_name == "item") {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001095 if (!ParseOverlayableItem(parser, policies, comment, out_resource)) {
Adam Lesinski46c4d722017-08-23 13:03:56 -07001096 error = true;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001097 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001098 } else if (element_namespace.empty() && element_name == "policy") {
1099 if (!policies.empty()) {
1100 // If the policy list is not empty, then we are currently inside a policy element
1101 diag_->Error(DiagMessage(item_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001102 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001103 break;
1104 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1105 // Parse the polices separated by vertical bar characters to allow for specifying multiple
1106 // policies at once
1107 for (StringPiece part : util::Tokenize(maybe_type.value(), '|')) {
1108 StringPiece trimmed_part = util::TrimWhitespace(part);
1109 if (trimmed_part == "public") {
1110 policies.push_back(Overlayable::Policy::kPublic);
1111 } else if (trimmed_part == "product") {
1112 policies.push_back(Overlayable::Policy::kProduct);
1113 } else if (trimmed_part == "product_services") {
1114 policies.push_back(Overlayable::Policy::kProductServices);
1115 } else if (trimmed_part == "system") {
1116 policies.push_back(Overlayable::Policy::kSystem);
1117 } else if (trimmed_part == "vendor") {
1118 policies.push_back(Overlayable::Policy::kVendor);
1119 } else {
1120 diag_->Error(DiagMessage(out_resource->source)
1121 << "<policy> has unsupported type '" << trimmed_part << "'");
1122 error = true;
1123 continue;
1124 }
1125 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001126 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001127 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001128 diag_->Error(DiagMessage(item_source) << "invalid element <" << element_name << "> in "
1129 << " <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001130 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001131 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001132 }
1133 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001134
Adam Lesinski46c4d722017-08-23 13:03:56 -07001135 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001136}
1137
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001138bool ResourceParser::ParseOverlayableItem(xml::XmlPullParser* parser,
1139 const std::vector<Overlayable::Policy>& policies,
1140 const std::string& comment,
1141 ParsedResource* out_resource) {
1142 const Source item_source = source_.WithLine(parser->line_number());
1143
1144 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1145 if (!maybe_name) {
1146 diag_->Error(DiagMessage(item_source)
1147 << "<item> within an <overlayable> tag must have a 'name' attribute");
1148 return false;
1149 }
1150
1151 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1152 if (!maybe_type) {
1153 diag_->Error(DiagMessage(item_source)
1154 << "<item> within an <overlayable> tag must have a 'type' attribute");
1155 return false;
1156 }
1157
1158 const ResourceType* type = ParseResourceType(maybe_type.value());
1159 if (type == nullptr) {
1160 diag_->Error(DiagMessage(out_resource->source)
1161 << "invalid resource type '" << maybe_type.value()
1162 << "' in <item> within an <overlayable>");
1163 return false;
1164 }
1165
1166 ParsedResource child_resource;
1167 child_resource.name.type = *type;
1168 child_resource.name.entry = maybe_name.value().to_string();
1169 child_resource.source = item_source;
1170
1171 if (policies.empty()) {
1172 Overlayable overlayable;
1173 overlayable.source = item_source;
1174 overlayable.comment = comment;
1175 child_resource.overlayable_declarations.push_back(overlayable);
1176 } else {
1177 for (Overlayable::Policy policy : policies) {
1178 Overlayable overlayable;
1179 overlayable.policy = policy;
1180 overlayable.source = item_source;
1181 overlayable.comment = comment;
1182 child_resource.overlayable_declarations.push_back(overlayable);
1183 }
1184 }
1185
1186 if (options_.visibility) {
1187 child_resource.visibility_level = options_.visibility.value();
1188 }
1189 out_resource->child_resources.push_back(std::move(child_resource));
1190 return true;
1191}
1192
Adam Lesinski71be7052017-12-12 16:48:07 -08001193bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001194 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001195 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001196 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197 return true;
1198 }
1199 return false;
1200}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001201
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001202bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1203 ParsedResource* out_resource) {
1204 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205}
1206
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001207bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1208 ParsedResource* out_resource, bool weak) {
1209 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210
1211 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1213 diag_->Warn(DiagMessage(out_resource->source)
1214 << "ignoring configuration '" << out_resource->config
1215 << "' for attribute " << out_resource->name);
1216 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001217 }
1218
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001219 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1222 if (maybe_format) {
1223 type_mask = ParseFormatAttribute(maybe_format.value());
1224 if (type_mask == 0) {
1225 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001226 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227 return false;
1228 }
1229 }
1230
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001231 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001232
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1234 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1235 if (!min_str.empty()) {
1236 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001238 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001239 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001240 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001241 }
1242
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001243 if (!maybe_min) {
1244 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1245 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246 return false;
1247 }
1248 }
1249
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001250 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1251 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1252 if (!max_str.empty()) {
1253 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001254 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001255 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001256 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001257 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001258 }
1259
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001260 if (!maybe_max) {
1261 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1262 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 return false;
1264 }
1265 }
1266
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001267 if ((maybe_min || maybe_max) &&
1268 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1269 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001270 << "'min' and 'max' can only be used when format='integer'");
1271 return false;
1272 }
1273
1274 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001275 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 return a.symbol.name.value() < b.symbol.name.value();
1277 }
1278 };
1279
1280 std::set<Attribute::Symbol, SymbolComparator> items;
1281
1282 std::string comment;
1283 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001284 const size_t depth = parser->depth();
1285 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1286 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001287 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001288 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001289 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290 // Skip text.
1291 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001292 }
1293
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 const Source item_source = source_.WithLine(parser->line_number());
1295 const std::string& element_namespace = parser->element_namespace();
1296 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001297 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 if (element_name == "enum") {
1299 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1300 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 << "can not define an <enum>; already defined a <flag>");
1302 error = true;
1303 continue;
1304 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001307 } else if (element_name == "flag") {
1308 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1309 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 << "can not define a <flag>; already defined an <enum>");
1311 error = true;
1312 continue;
1313 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001314 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 }
1316
1317 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001319 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 ParsedResource child_resource;
1321 child_resource.name = symbol.symbol.name.value();
1322 child_resource.source = item_source;
1323 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001324 if (options_.visibility) {
1325 child_resource.visibility_level = options_.visibility.value();
1326 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 symbol.symbol.SetComment(std::move(comment));
1330 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 auto insert_result = items.insert(std::move(symbol));
1333 if (!insert_result.second) {
1334 const Attribute::Symbol& existing_symbol = *insert_result.first;
1335 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001336 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001339 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 << "first defined here");
1341 error = true;
1342 }
1343 } else {
1344 error = true;
1345 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001346 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1347 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001348 error = true;
1349 }
1350
1351 comment = {};
1352 }
1353
1354 if (error) {
1355 return false;
1356 }
1357
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001358 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1359 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1360 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001362 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1363 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 return true;
1366}
1367
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1373 if (!maybe_name) {
1374 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001375 << tag << ">");
1376 return {};
1377 }
1378
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1380 if (!maybe_value) {
1381 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 << tag << ">");
1383 return {};
1384 }
1385
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001387 android::Res_value val;
1388 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390 << "' for <" << tag
1391 << ">; must be an integer");
1392 return {};
1393 }
1394
1395 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001396 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001398}
1399
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001400bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1401 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001402
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1404 if (!maybe_name) {
1405 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406 return false;
1407 }
1408
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001409 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001411 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001412 return false;
1413 }
1414
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001415 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001417
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001418 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001419 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001420 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 return false;
1422 }
1423
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001424 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001425 return true;
1426}
1427
Adam Lesinski86d67df2017-01-31 13:47:27 -08001428bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001429 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001430 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431
1432 std::unique_ptr<Style> style = util::make_unique<Style>();
1433
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1435 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001436 // 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 -07001437 if (!maybe_parent.value().empty()) {
1438 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001439 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001441 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001442 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001443 }
1444
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001445 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001446 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001447 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001448 }
1449
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001450 } else {
1451 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 std::string style_name = out_resource->name.entry;
1453 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001455 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001456 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457 }
1458 }
1459
1460 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001461 const size_t depth = parser->depth();
1462 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1463 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 // Skip text and comments.
1465 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001466 }
1467
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001468 const std::string& element_namespace = parser->element_namespace();
1469 const std::string& element_name = parser->element_name();
1470 if (element_namespace == "" && element_name == "item") {
1471 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001472
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001473 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1474 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1475 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001477 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001478 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001479
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001480 if (error) {
1481 return false;
1482 }
1483
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001484 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001485 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001486}
1487
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001488bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1489 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1490 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1491 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1492 if (resource_format == 0u) {
1493 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1494 << "'" << format_attr.value() << "' is an invalid format");
1495 return false;
1496 }
1497 }
1498 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001499}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001500
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001501bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1502 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001503}
1504
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001505bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1506 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001507}
1508
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001509bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1510 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001511 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001512 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001513
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001515
Adam Lesinski75421622017-01-06 15:20:04 -08001516 bool translatable = options_.translatable;
1517 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1518 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1519 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001520 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001521 << "invalid value for 'translatable'. Must be a boolean");
1522 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001523 }
Adam Lesinski75421622017-01-06 15:20:04 -08001524 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001525 }
Adam Lesinski75421622017-01-06 15:20:04 -08001526 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001527
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001528 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001529 const size_t depth = parser->depth();
1530 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1531 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001532 // Skip text and comments.
1533 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001534 }
1535
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 const Source item_source = source_.WithLine(parser->line_number());
1537 const std::string& element_namespace = parser->element_namespace();
1538 const std::string& element_name = parser->element_name();
1539 if (element_namespace.empty() && element_name == "item") {
1540 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001542 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543 error = true;
1544 continue;
1545 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001546 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001547 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001548
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1550 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1551 << "unknown tag <" << element_namespace << ":"
1552 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001553 error = true;
1554 }
1555 }
1556
1557 if (error) {
1558 return false;
1559 }
1560
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001561 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001563}
1564
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001565bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1566 ParsedResource* out_resource) {
1567 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001568
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001569 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001570
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001571 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001572 const size_t depth = parser->depth();
1573 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1574 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001575 // Skip text and comments.
1576 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001577 }
1578
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 const Source item_source = source_.WithLine(parser->line_number());
1580 const std::string& element_namespace = parser->element_namespace();
1581 const std::string& element_name = parser->element_name();
1582 if (element_namespace.empty() && element_name == "item") {
1583 Maybe<StringPiece> maybe_quantity =
1584 xml::FindNonEmptyAttribute(parser, "quantity");
1585 if (!maybe_quantity) {
1586 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001587 << "<item> in <plurals> requires attribute "
1588 << "'quantity'");
1589 error = true;
1590 continue;
1591 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001592
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 StringPiece trimmed_quantity =
1594 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001596 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001597 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001598 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001601 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001603 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001605 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001607 index = Plural::Other;
1608 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001611 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001612 error = true;
1613 continue;
1614 }
1615
1616 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001617 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1618 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 error = true;
1620 continue;
1621 }
1622
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1625 error = true;
1626 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001628
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1630 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1631 << element_namespace << ":"
1632 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001633 error = true;
1634 }
1635 }
1636
1637 if (error) {
1638 return false;
1639 }
1640
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001641 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001642 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001643}
1644
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001645bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1646 ParsedResource* out_resource) {
1647 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001648
Adam Lesinski71be7052017-12-12 16:48:07 -08001649 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1650 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001651
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001652 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001653 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1654 diag_->Warn(DiagMessage(out_resource->source)
1655 << "ignoring configuration '" << out_resource->config
1656 << "' for styleable " << out_resource->name.entry);
1657 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001658 }
1659
1660 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1661
1662 std::string comment;
1663 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001664 const size_t depth = parser->depth();
1665 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1666 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001667 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001669 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 // Ignore text.
1671 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001672 }
1673
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 const Source item_source = source_.WithLine(parser->line_number());
1675 const std::string& element_namespace = parser->element_namespace();
1676 const std::string& element_name = parser->element_name();
1677 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001678 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001680 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001681 error = true;
1682 continue;
1683 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001684
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001685 // If this is a declaration, the package name may be in the name. Separate
1686 // these out.
1687 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001688 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001689 if (!maybe_ref) {
1690 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1691 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001692 error = true;
1693 continue;
1694 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001695
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001696 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001697 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001698
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001699 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001700 ParsedResource child_resource;
1701 child_resource.name = child_ref.name.value();
1702 child_resource.source = item_source;
1703 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001704 if (options_.visibility) {
1705 child_resource.visibility_level = options_.visibility.value();
1706 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001707
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001708 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001709 error = true;
1710 continue;
1711 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001712
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001713 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001714 child_ref.SetComment(child_resource.comment);
1715 child_ref.SetSource(item_source);
1716 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001717
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001718 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001719
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001720 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1721 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1722 << element_namespace << ":"
1723 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001724 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001725 }
1726
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001727 comment = {};
1728 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001729
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001730 if (error) {
1731 return false;
1732 }
1733
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001734 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001735 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001736}
1737
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001738} // namespace aapt