blob: 1efabbb46fd592fd5d7ed4832f355195ffd145ec [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Winson62ac8b52019-12-04 08:36:48 -080035#include "idmap2/Policies.h"
36
Adam Lesinski2eed52e2018-02-21 15:55:58 -080037using ::aapt::ResourceUtils::StringBuilder;
38using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020039using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080040using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080041
Winson62ac8b52019-12-04 08:36:48 -080042using android::idmap2::policy::kPolicyStringToFlag;
43
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044namespace aapt {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070045namespace {
46constexpr const char* kPublicGroupTag = "public-group";
47constexpr const char* kStagingPublicGroupTag = "staging-public-group";
48} // namespace
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070050constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
53static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080055}
56
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070057static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
58 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070068 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070072 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070074 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080076}
77
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070078static uint32_t ParseFormatType(const StringPiece& piece) {
79 if (piece == "enum") {
80 return android::ResTable_map::TYPE_ENUM;
81 } else if (piece == "flags") {
82 return android::ResTable_map::TYPE_FLAGS;
83 }
84 return ParseFormatTypeNoEnumsOrFlags(piece);
85}
86
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080089 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 StringPiece trimmed_part = util::TrimWhitespace(part);
91 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 if (type == 0) {
93 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080094 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 mask |= type;
96 }
97 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080098}
99
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700100// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800101struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 ResourceName name;
103 ConfigDescription config;
104 std::string product;
105 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800108 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700109 bool staged_api = false;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700110 bool allow_new = false;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800111 Maybe<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 std::string comment;
114 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800116};
117
118// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700119static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
121 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800123 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 }
Adam Lesinski76565542016-03-10 21:55:04 -0800125
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700126 NewResourceBuilder res_builder(res->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->visibility_level != Visibility::Level::kUndefined) {
128 Visibility visibility;
129 visibility.level = res->visibility_level;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700130 visibility.staged_api = res->staged_api;
Adam Lesinski71be7052017-12-12 16:48:07 -0800131 visibility.source = res->source;
132 visibility.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700133 res_builder.SetVisibility(visibility);
134 }
135
136 if (res->id.is_valid()) {
137 res_builder.SetId(res->id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800139
Adam Lesinski71be7052017-12-12 16:48:07 -0800140 if (res->allow_new) {
141 AllowNew allow_new;
142 allow_new.source = res->source;
143 allow_new.comment = res->comment;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700144 res_builder.SetAllowNew(allow_new);
Adam Lesinski71be7052017-12-12 16:48:07 -0800145 }
146
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800147 if (res->overlayable_item) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700148 res_builder.SetOverlayable(res->overlayable_item.value());
Adam Lesinski71be7052017-12-12 16:48:07 -0800149 }
150
151 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 res->value->SetComment(std::move(res->comment));
154 res->value->SetSource(std::move(res->source));
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700155 res_builder.SetValue(std::move(res->value), res->config, res->product);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 bool error = false;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700159 if (!res->name.entry.empty()) {
160 if (!table->AddResource(res_builder.Build(), diag)) {
161 return false;
162 }
163 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 for (ParsedResource& child : res->child_resources) {
165 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 }
167 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800168}
169
170// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
174 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700175 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 : diag_(diag),
178 table_(table),
179 source_(source),
180 config_(config),
181 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800183// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
184// This will be used to traverse and flatten the XML string into a single std::string, with all
185// Span and Untranslatable data maintained in parallel, as indices into the string.
186class Node {
187 public:
188 virtual ~Node() = default;
189
190 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
191 // parent node as well.
192 // Returns a pointer to the child node that was added as a convenience.
193 template <typename T>
194 T* AddChild(std::unique_ptr<T> node) {
195 T* raw_ptr = node.get();
196 children.push_back(std::move(node));
197 return raw_ptr;
198 }
199
200 virtual void Build(StringBuilder* builder) const {
201 for (const auto& child : children) {
202 child->Build(builder);
203 }
204 }
205
206 std::vector<std::unique_ptr<Node>> children;
207};
208
209// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
210class SegmentNode : public Node {
211 public:
212 std::string data;
213
214 void Build(StringBuilder* builder) const override {
215 builder->AppendText(data);
216 }
217};
218
219// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
220class SpanNode : public Node {
221 public:
222 std::string name;
223
224 void Build(StringBuilder* builder) const override {
225 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
226 Node::Build(builder);
227 builder->EndSpan(span_handle);
228 }
229};
230
231// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
232class UntranslatableNode : public Node {
233 public:
234 void Build(StringBuilder* builder) const override {
235 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
236 Node::Build(builder);
237 builder->EndUntranslatable(handle);
238 }
239};
240
241// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800242bool ResourceParser::FlattenXmlSubtree(
243 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
244 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800245 std::string raw_string;
246 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800247
248 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
249 Maybe<size_t> untranslatable_start_depth;
250
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800251 Node root;
252 std::vector<Node*> node_stack;
253 node_stack.push_back(&root);
254
255 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
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800265 || event == xml::XmlPullParser::Event::kEndElement) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800266 if (!current_text.empty()) {
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800267 auto segment_node = util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800268 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700269
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800270 last_segment = node_stack.back()->AddChild(std::move(segment_node));
271 if (first_segment == nullptr) {
272 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800273 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 current_text = {};
275 }
276 }
Adam Lesinski75421622017-01-06 15:20:04 -0800277
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800278 switch (event) {
279 case xml::XmlPullParser::Event::kText: {
280 current_text += parser->text();
281 raw_string += parser->text();
282 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800283
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800284 case xml::XmlPullParser::Event::kStartElement: {
285 if (parser->element_namespace().empty()) {
286 // This is an HTML tag which we encode as a span. Add it to the span stack.
287 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
288 span_node->name = parser->element_name();
289 const auto end_attr_iter = parser->end_attributes();
290 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
291 ++attr_iter) {
292 span_node->name += ";";
293 span_node->name += attr_iter->name;
294 span_node->name += "=";
295 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800296 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800297
298 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
299 saw_span_node = true;
300 } else if (parser->element_namespace() == sXliffNamespaceUri) {
301 // This is an XLIFF tag, which is not encoded as a span.
302 if (parser->element_name() == "g") {
303 // Check that an 'untranslatable' tag is not already being processed. Nested
304 // <xliff:g> tags are illegal.
305 if (untranslatable_start_depth) {
306 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
307 << "illegal nested XLIFF 'g' tag");
308 return false;
309 } else {
310 // Mark the beginning of an 'untranslatable' section.
311 untranslatable_start_depth = depth;
312 node_stack.push_back(
313 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
314 }
315 } else {
316 // Ignore unknown XLIFF tags, but don't warn.
317 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
318 }
319 } else {
320 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
321 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
322 << "ignoring element '" << parser->element_name()
323 << "' with unknown namespace '" << parser->element_namespace() << "'");
324 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800325 }
Adam Lesinski75421622017-01-06 15:20:04 -0800326
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800327 // Enter one level inside the element.
328 depth++;
329 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800331 case xml::XmlPullParser::Event::kEndElement: {
332 // Return one level from within the element.
333 depth--;
334 if (depth == 0) {
335 break;
336 }
337
338 node_stack.pop_back();
339 if (untranslatable_start_depth == make_value(depth)) {
340 // This is the end of an untranslatable section.
341 untranslatable_start_depth = {};
342 }
343 } break;
344
345 default:
346 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 }
349 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700351 // Validity check to make sure we processed all the nodes.
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800352 CHECK(node_stack.size() == 1u);
353 CHECK(node_stack.back() == &root);
354
355 if (!saw_span_node) {
356 // If there were no spans, we must treat this string a little differently (according to AAPT).
357 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
358 // from the last segment.
359 if (first_segment != nullptr) {
360 // Trim leading whitespace.
361 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
362 if (trimmed.size() != first_segment->data.size()) {
363 first_segment->data = trimmed.to_string();
364 }
365 }
366
367 if (last_segment != nullptr) {
368 // Trim trailing whitespace.
369 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
370 if (trimmed.size() != last_segment->data.size()) {
371 last_segment->data = trimmed.to_string();
372 }
373 }
374 }
375
376 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
377 // care of recording the correctly adjusted Spans and UntranslatableSections.
378 StringBuilder builder;
379 root.Build(&builder);
380 if (!builder) {
381 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
382 return false;
383 }
384
385 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
386 *out_raw_string = std::move(raw_string);
387 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
388 out_style_string->str = std::move(flattened_string.text);
389 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800390 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391}
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 const size_t depth = parser->depth();
396 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
397 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 // Skip comments and text.
399 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800400 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401
Adam Lesinski060b53d2017-07-28 17:10:35 -0700402 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 << "root element must be <resources>");
405 return false;
406 }
407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 break;
410 };
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
413 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
414 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 return false;
416 }
417 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418}
419
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
421 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700422
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 bool error = false;
424 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 const size_t depth = parser->depth();
426 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
427 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800431 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700432
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (!util::TrimWhitespace(parser->text()).empty()) {
435 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 << "plain text not allowed here");
437 error = true;
438 }
439 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700440 }
441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 // Skip unknown namespace.
446 continue;
447 }
448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 std::string element_name = parser->element_name();
450 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 comment = "";
452 continue;
453 }
454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 ParsedResource parsed_resource;
456 parsed_resource.config = config_;
457 parsed_resource.source = source_.WithLine(parser->line_number());
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -0800458 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100460 if (options_.visibility) {
461 parsed_resource.visibility_level = options_.visibility.value();
462 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463
464 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700465 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800466 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 }
468
469 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 error = true;
472 continue;
473 }
474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 error = true;
477 }
478 }
479
480 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 for (const ResourceName& stripped_resource : stripped_resources) {
482 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700484 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
485 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 error = true;
487 }
488 }
489
490 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491}
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
494 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 struct ItemTypeFormat {
496 ResourceType type;
497 uint32_t format;
498 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800499
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
501 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800502
Adam Lesinski86d67df2017-01-31 13:47:27 -0800503 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
504 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
505 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
506 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
507 {"dimen",
508 {ResourceType::kDimen,
509 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
510 android::ResTable_map::TYPE_DIMENSION}},
511 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
512 {"fraction",
513 {ResourceType::kFraction,
514 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
515 android::ResTable_map::TYPE_DIMENSION}},
516 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
517 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
518 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800519
Adam Lesinski86d67df2017-01-31 13:47:27 -0800520 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
521 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
522 {"array", std::mem_fn(&ResourceParser::ParseArray)},
523 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
524 {"configVarying",
525 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
526 std::placeholders::_2, std::placeholders::_3)},
527 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
528 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
529 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700530 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800531 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
532 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
533 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700534 {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800535 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
536 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
537 std::placeholders::_2, std::placeholders::_3)},
538 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
539 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800542
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800545
Adam Lesinski86d67df2017-01-31 13:47:27 -0800546 bool can_be_item = true;
547 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800549 can_be_bag = false;
550
Adam Lesinskie597d682017-06-01 17:16:44 -0700551 // The default format for <item> is any. If a format attribute is present, that one will
552 // override the default.
553 resource_format = android::ResTable_map::TYPE_ANY;
554
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700556 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800557 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 << "<item> must have a 'type' attribute");
561 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800562 }
563
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700564 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700566 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700568 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 if (!resource_format) {
570 diag_->Error(DiagMessage(out_resource->source)
571 << "'" << maybe_format.value()
572 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800573 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 }
575 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800576 } else if (resource_type == "bag") {
577 can_be_item = false;
578
579 // Bags have their type encoded in the type attribute.
580 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
581 resource_type = maybe_type.value().to_string();
582 } else {
583 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
584 << "<bag> must have a 'type' attribute");
585 return false;
586 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 }
588
589 // Get the name of the resource. This will be checked later, because not all
590 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 if (resource_type == "id") {
594 if (!maybe_name) {
595 diag_->Error(DiagMessage(out_resource->source)
596 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 << "> missing 'name' attribute");
598 return false;
599 }
600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800602 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700603
604 // Ids either represent a unique resource id or reference another resource id
605 auto item = ParseItem(parser, out_resource, resource_format);
606 if (!item) {
607 return false;
608 }
609
610 String* empty = ValueCast<String>(out_resource->value.get());
611 if (empty && *empty->value == "") {
612 // If no inner element exists, represent a unique identifier
613 out_resource->value = util::make_unique<Id>();
614 } else {
y9efbbef2018-04-18 11:29:09 -0700615 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700616 if (ref && !ref->name && !ref->id) {
617 // A null reference also means there is no inner element when ids are in the form:
618 // <id name="name"/>
619 out_resource->value = util::make_unique<Id>();
620 } else if (!ref || ref->name.value().type != ResourceType::kId) {
621 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700622 diag_->Error(DiagMessage(out_resource->source)
623 << "<" << parser->element_name()
624 << "> inner element must either be a resource reference or empty");
625 return false;
626 }
627 }
628
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 return true;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700630 } else if (resource_type == "macro") {
631 if (!maybe_name) {
632 diag_->Error(DiagMessage(out_resource->source)
633 << "<" << parser->element_name() << "> missing 'name' attribute");
634 return false;
635 }
636
637 out_resource->name.type = ResourceType::kMacro;
638 out_resource->name.entry = maybe_name.value().to_string();
639 return ParseMacro(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 }
641
Adam Lesinski86d67df2017-01-31 13:47:27 -0800642 if (can_be_item) {
643 const auto item_iter = elToItemMap.find(resource_type);
644 if (item_iter != elToItemMap.end()) {
645 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646
Adam Lesinski86d67df2017-01-31 13:47:27 -0800647 if (!maybe_name) {
648 diag_->Error(DiagMessage(out_resource->source)
649 << "<" << parser->element_name() << "> missing 'name' attribute");
650 return false;
651 }
652
653 out_resource->name.type = item_iter->second.type;
654 out_resource->name.entry = maybe_name.value().to_string();
655
Adam Lesinskie597d682017-06-01 17:16:44 -0700656 // Only use the implied format of the type when there is no explicit format.
657 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800658 resource_format = item_iter->second.format;
659 }
660
661 if (!ParseItem(parser, out_resource, resource_format)) {
662 return false;
663 }
664 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 }
667
668 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800669 if (can_be_bag) {
670 const auto bag_iter = elToBagMap.find(resource_type);
671 if (bag_iter != elToBagMap.end()) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700672 // Ensure we have a name (unless this is a <public-group> or <overlayable>).
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700673 if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag &&
674 resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800675 if (!maybe_name) {
676 diag_->Error(DiagMessage(out_resource->source)
677 << "<" << parser->element_name() << "> missing 'name' attribute");
678 return false;
679 }
680
681 out_resource->name.entry = maybe_name.value().to_string();
682 }
683
684 // Call the associated parse method. The type will be filled in by the
685 // parse func.
686 if (!bag_iter->second(this, parser, out_resource)) {
687 return false;
688 }
689 return true;
690 }
691 }
692
693 if (can_be_item) {
694 // Try parsing the elementName (or type) as a resource. These shall only be
695 // resources like 'layout' or 'xml' and they can only be references.
696 const ResourceType* parsed_type = ParseResourceType(resource_type);
697 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 if (!maybe_name) {
699 diag_->Error(DiagMessage(out_resource->source)
700 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 << "> missing 'name' attribute");
702 return false;
703 }
704
Adam Lesinski86d67df2017-01-31 13:47:27 -0800705 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800706 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800707 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
708 if (!out_resource->value) {
709 diag_->Error(DiagMessage(out_resource->source)
710 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
711 return false;
712 }
713 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
716
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000717 // If the resource type was not recognized, write the error and return false.
718 diag_->Error(DiagMessage(out_resource->source)
Ryan Mitchell2e2c3b62019-04-26 01:16:52 -0700719 << "unknown resource type '" << resource_type << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 return false;
721}
722
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
724 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 const uint32_t format) {
726 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 }
729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 out_resource->value = ParseXml(parser, format, kNoRawString);
731 if (!out_resource->value) {
732 diag_->Error(DiagMessage(out_resource->source) << "invalid "
733 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 return false;
735 }
736 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800737}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800738
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700739std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree(
740 xml::XmlPullParser* parser) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700741 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 std::string raw_value;
744 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800745 std::vector<UntranslatableSection> untranslatable_sections;
746 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800747 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 }
749
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700750 return FlattenedXmlSubTree{.raw_value = raw_value,
751 .style_string = style_string,
752 .untranslatable_sections = untranslatable_sections,
753 .namespace_resolver = parser,
754 .source = source_.WithLine(begin_xml_line)};
755}
756
757/**
758 * Reads the entire XML subtree and attempts to parse it as some Item,
759 * with typeMask denoting which items it can be. If allowRawValue is
760 * true, a RawString is returned if the XML couldn't be parsed as
761 * an Item. If allowRawValue is false, nullptr is returned in this
762 * case.
763 */
764std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
765 const bool allow_raw_value) {
766 auto sub_tree = CreateFlattenSubTree(parser);
767 if (!sub_tree.has_value()) {
768 return {};
769 }
770 return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_);
771}
772
773std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree,
774 const uint32_t type_mask, const bool allow_raw_value,
775 ResourceTable& table,
776 const android::ConfigDescription& config,
777 IDiagnostics& diag) {
778 if (!xmlsub_tree.style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800780 std::unique_ptr<StyledString> styled_string =
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700781 util::make_unique<StyledString>(table.string_pool.MakeRef(
782 xmlsub_tree.style_string,
783 StringPool::Context(StringPool::Context::kNormalPriority, config)));
784 styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800785 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 }
787
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700788 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789 // name.package can be empty here, as it will assume the package name of the
790 // table.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700791 auto id = util::make_unique<Id>();
792 id->SetSource(xmlsub_tree.source);
793 return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794 };
795
796 // Process the raw value.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700797 std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
798 xmlsub_tree.raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700799 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 // Fix up the reference.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700801 if (auto ref = ValueCast<Reference>(processed_item.get())) {
802 ref->allow_raw = allow_raw_value;
803 ResolvePackage(xmlsub_tree.namespace_resolver, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700804 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 }
807
808 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800811 std::unique_ptr<String> string = util::make_unique<String>(
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700812 table.string_pool.MakeRef(xmlsub_tree.style_string.str, StringPool::Context(config)));
813 string->untranslatable_sections = xmlsub_tree.untranslatable_sections;
Adam Lesinski75421622017-01-06 15:20:04 -0800814 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 }
816
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700817 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 // We can't parse this so return a RawString if we are allowed.
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700819 return util::make_unique<RawString>(table.string_pool.MakeRef(
820 util::TrimWhitespace(xmlsub_tree.raw_value), StringPool::Context(config)));
821 } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) {
Ryan Mitchellfc3874a2019-05-28 16:30:17 -0700822 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
823 return ResourceUtils::MakeNull();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
825 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800826}
827
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828bool ResourceParser::ParseString(xml::XmlPullParser* parser,
829 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700830 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 if (Maybe<StringPiece> formatted_attr =
832 xml::FindAttribute(parser, "formatted")) {
833 Maybe<bool> maybe_formatted =
834 ResourceUtils::ParseBool(formatted_attr.value());
835 if (!maybe_formatted) {
836 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 << "invalid value for 'formatted'. Must be a boolean");
838 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800839 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700840 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800842
Adam Lesinski75421622017-01-06 15:20:04 -0800843 bool translatable = options_.translatable;
844 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
845 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
846 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700847 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 << "invalid value for 'translatable'. Must be a boolean");
849 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800850 }
Adam Lesinski75421622017-01-06 15:20:04 -0800851 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 out_resource->value =
855 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
856 if (!out_resource->value) {
857 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 return false;
859 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800862 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800863
Adam Lesinski75421622017-01-06 15:20:04 -0800864 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865 if (!util::VerifyJavaStringFormat(*string_value->value)) {
866 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867 msg << "multiple substitutions specified in non-positional format; "
868 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869 if (options_.error_on_positional_arguments) {
870 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700871 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800872 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800873
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700874 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800876 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700877
Adam Lesinski75421622017-01-06 15:20:04 -0800878 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
879 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 }
881 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800882}
883
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700884bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) {
885 auto sub_tree = CreateFlattenSubTree(parser);
886 if (!sub_tree) {
887 return false;
888 }
889
890 if (out_resource->config != ConfigDescription::DefaultConfig()) {
891 diag_->Error(DiagMessage(out_resource->source)
892 << "<macro> tags cannot be declared in configurations other than the default "
893 "configuration'");
894 return false;
895 }
896
897 auto macro = std::make_unique<Macro>();
898 macro->raw_value = std::move(sub_tree->raw_value);
899 macro->style_string = std::move(sub_tree->style_string);
900 macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections);
901
902 for (const auto& decl : parser->package_decls()) {
903 macro->alias_namespaces.emplace_back(
904 Macro::Namespace{.alias = decl.prefix,
905 .package_name = decl.package.package,
906 .is_private = decl.package.private_namespace});
907 }
908
909 out_resource->value = std::move(macro);
910 return true;
911}
912
Adam Lesinski71be7052017-12-12 16:48:07 -0800913bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100914 if (options_.visibility) {
915 diag_->Error(DiagMessage(out_resource->source)
916 << "<public> tag not allowed with --visibility flag");
917 return false;
918 }
919
Adam Lesinski46c4d722017-08-23 13:03:56 -0700920 if (out_resource->config != ConfigDescription::DefaultConfig()) {
921 diag_->Warn(DiagMessage(out_resource->source)
922 << "ignoring configuration '" << out_resource->config << "' for <public> 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> must have a 'type' attribute");
929 return false;
930 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -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>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700937 return false;
938 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800939
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700940 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800941
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800942 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
943 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700944 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800945 diag_->Error(DiagMessage(out_resource->source)
946 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800948 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700950 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800951
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700954 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700956
Adam Lesinski71be7052017-12-12 16:48:07 -0800957 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700958 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800959}
960
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700961template <typename Func>
962bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource,
963 const char* tag_name, IDiagnostics* diag, Func&& func) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700964 if (out_resource->config != ConfigDescription::DefaultConfig()) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700965 diag->Warn(DiagMessage(out_resource->source)
966 << "ignoring configuration '" << out_resource->config << "' for <" << tag_name
967 << "> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -0700968 }
969
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700970 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
971 if (!maybe_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700972 diag->Error(DiagMessage(out_resource->source)
973 << "<" << tag_name << "> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800974 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800976
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
978 if (!parsed_type) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700979 diag->Error(DiagMessage(out_resource->source)
980 << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800981 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 }
983
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700984 Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 if (!maybe_id_str) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700986 diag->Error(DiagMessage(out_resource->source)
987 << "<" << tag_name << "> must have a 'first-id' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988 return false;
989 }
990
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700991 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 if (!maybe_id) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700993 diag->Error(DiagMessage(out_resource->source)
994 << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 return false;
996 }
997
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700999 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 const size_t depth = parser->depth();
1002 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1003 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001004 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 // Skip text.
1008 continue;
1009 }
1010
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001011 const Source item_source = out_resource->source.WithLine(parser->line_number());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001012 const std::string& element_namespace = parser->element_namespace();
1013 const std::string& element_name = parser->element_name();
1014 if (element_namespace.empty() && element_name == "public") {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001015 auto maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 if (!maybe_name) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001017 diag->Error(DiagMessage(item_source) << "<public> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001018 error = true;
1019 continue;
1020 }
1021
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001022 if (xml::FindNonEmptyAttribute(parser, "id")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001023 diag->Error(DiagMessage(item_source) << "'id' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 error = true;
1025 continue;
1026 }
1027
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 if (xml::FindNonEmptyAttribute(parser, "type")) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001029 diag->Error(DiagMessage(item_source) << "'type' is ignored within <" << tag_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 error = true;
1031 continue;
1032 }
1033
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001034 ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{
1035 .name = ResourceName{{}, *parsed_type, maybe_name.value().to_string()},
1036 .source = item_source,
1037 .id = next_id,
1038 .comment = std::move(comment),
1039 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001041 // Execute group specific code.
1042 func(entry_res, next_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001044 next_id.id++;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001045 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001046 diag->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 error = true;
1048 }
1049 }
1050 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001051}
1052
Ryan Mitchell2e9bec12021-03-22 09:31:00 -07001053bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser,
1054 ParsedResource* out_resource) {
1055 return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_,
1056 [](ParsedResource& parsed_entry, ResourceId id) {
1057 parsed_entry.id = id;
1058 parsed_entry.staged_api = true;
1059 parsed_entry.visibility_level = Visibility::Level::kPublic;
1060 });
1061}
1062
1063bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1064 if (options_.visibility) {
1065 diag_->Error(DiagMessage(out_resource->source)
1066 << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag");
1067 return false;
1068 }
1069
1070 return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_,
1071 [](ParsedResource& parsed_entry, ResourceId id) {
1072 parsed_entry.id = id;
1073 parsed_entry.visibility_level = Visibility::Level::kPublic;
1074 });
1075}
1076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1078 ParsedResource* out_resource) {
1079 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1080 if (!maybe_type) {
1081 diag_->Error(DiagMessage(out_resource->source)
1082 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083 << "> must have a 'type' attribute");
1084 return false;
1085 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001086
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001087 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1088 if (!parsed_type) {
1089 diag_->Error(DiagMessage(out_resource->source)
1090 << "invalid resource type '" << maybe_type.value() << "' in <"
1091 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001092 return false;
1093 }
1094
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001095 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001096 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001097}
1098
Adam Lesinski46c4d722017-08-23 13:03:56 -07001099bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001100 if (options_.visibility) {
1101 diag_->Error(DiagMessage(out_resource->source)
1102 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1103 return false;
1104 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001105 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1106 diag_->Warn(DiagMessage(out_resource->source)
1107 << "ignoring configuration '" << out_resource->config << "' for <"
1108 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001110
1111 if (!ParseSymbolImpl(parser, out_resource)) {
1112 return false;
1113 }
1114
Adam Lesinski71be7052017-12-12 16:48:07 -08001115 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001116 return true;
1117}
1118
1119bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1120 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1121 diag_->Warn(DiagMessage(out_resource->source)
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001122 << "ignoring configuration '" << out_resource->config
1123 << "' for <overlayable> tag");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001124 }
1125
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001126 Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name");
1127 if (!overlayable_name) {
1128 diag_->Error(DiagMessage(out_resource->source)
1129 << "<overlayable> tag must have a 'name' attribute");
1130 return false;
1131 }
1132
1133 const std::string kActorUriScheme =
1134 android::base::StringPrintf("%s://", Overlayable::kActorScheme);
1135 Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor");
1136 if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) {
1137 diag_->Error(DiagMessage(out_resource->source)
1138 << "specified <overlayable> tag 'actor' attribute must use the scheme '"
1139 << Overlayable::kActorScheme << "'");
1140 return false;
1141 }
1142
1143 // Create a overlayable entry grouping that represents this <overlayable>
1144 auto overlayable = std::make_shared<Overlayable>(
1145 overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "",
Ryan Mitchellb5b162b2019-02-07 20:07:21 -08001146 source_);
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001147
Adam Lesinski46c4d722017-08-23 13:03:56 -07001148 bool error = false;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001149 std::string comment;
Winson62ac8b52019-12-04 08:36:48 -08001150 PolicyFlags current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001151 const size_t start_depth = parser->depth();
1152 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
1153 xml::XmlPullParser::Event event = parser->event();
1154 if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001155 // Break the loop when exiting the <overlayable>
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001156 break;
1157 } else if (event == xml::XmlPullParser::Event::kEndElement
1158 && parser->depth() == start_depth + 1) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001159 // Clear the current policies when exiting the <policy> tags
Winson62ac8b52019-12-04 08:36:48 -08001160 current_policies = PolicyFlags::NONE;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001161 continue;
1162 } else if (event == xml::XmlPullParser::Event::kComment) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001163 // Retrieve the comment of individual <item> tags
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001164 comment = parser->comment();
1165 continue;
1166 } else if (event != xml::XmlPullParser::Event::kStartElement) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001167 // Skip to the start of the next element
Adam Lesinski46c4d722017-08-23 13:03:56 -07001168 continue;
1169 }
1170
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001171 const Source element_source = source_.WithLine(parser->line_number());
Adam Lesinski46c4d722017-08-23 13:03:56 -07001172 const std::string& element_name = parser->element_name();
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001173 const std::string& element_namespace = parser->element_namespace();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001174 if (element_namespace.empty() && element_name == "item") {
Winson62ac8b52019-12-04 08:36:48 -08001175 if (current_policies == PolicyFlags::NONE) {
Winsonb2d7f532019-02-04 16:32:43 -08001176 diag_->Error(DiagMessage(element_source)
1177 << "<item> within an <overlayable> must be inside a <policy> block");
1178 error = true;
1179 continue;
1180 }
1181
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001182 // Items specify the name and type of resource that should be overlayable
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001183 Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name");
1184 if (!item_name) {
1185 diag_->Error(DiagMessage(element_source)
1186 << "<item> within an <overlayable> must have a 'name' attribute");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001187 error = true;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001188 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001189 }
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001190
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001191 Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type");
1192 if (!item_type) {
1193 diag_->Error(DiagMessage(element_source)
1194 << "<item> within an <overlayable> must have a 'type' attribute");
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001195 error = true;
1196 continue;
1197 }
1198
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001199 const ResourceType* type = ParseResourceType(item_type.value());
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001200 if (type == nullptr) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001201 diag_->Error(DiagMessage(element_source)
1202 << "invalid resource type '" << item_type.value()
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001203 << "' in <item> within an <overlayable>");
1204 error = true;
1205 continue;
1206 }
1207
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001208 OverlayableItem overlayable_item(overlayable);
1209 overlayable_item.policies = current_policies;
1210 overlayable_item.comment = comment;
1211 overlayable_item.source = element_source;
1212
1213 ParsedResource child_resource{};
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001214 child_resource.name.type = *type;
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001215 child_resource.name.entry = item_name.value().to_string();
1216 child_resource.overlayable_item = overlayable_item;
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001217 out_resource->child_resources.push_back(std::move(child_resource));
1218
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001219 } else if (element_namespace.empty() && element_name == "policy") {
Winson62ac8b52019-12-04 08:36:48 -08001220 if (current_policies != PolicyFlags::NONE) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001221 // If the policy list is not empty, then we are currently inside a policy element
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001222 diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001223 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001224 break;
1225 } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
1226 // Parse the polices separated by vertical bar characters to allow for specifying multiple
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001227 // policies. Items within the policy tag will have the specified policy.
Ryan Mitchell939df092019-04-09 17:13:50 -07001228 for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) {
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001229 StringPiece trimmed_part = util::TrimWhitespace(part);
Winson62ac8b52019-12-04 08:36:48 -08001230 const auto policy = std::find_if(kPolicyStringToFlag.begin(),
1231 kPolicyStringToFlag.end(),
1232 [trimmed_part](const auto& it) {
1233 return trimmed_part == it.first;
1234 });
1235 if (policy == kPolicyStringToFlag.end()) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001236 diag_->Error(DiagMessage(element_source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -08001237 << "<policy> has unsupported type '" << trimmed_part << "'");
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001238 error = true;
1239 continue;
1240 }
Ryan Mitchell939df092019-04-09 17:13:50 -07001241
1242 current_policies |= policy->second;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001243 }
Winsonb2d7f532019-02-04 16:32:43 -08001244 } else {
1245 diag_->Error(DiagMessage(element_source)
Ryan Mitchell939df092019-04-09 17:13:50 -07001246 << "<policy> must have a 'type' attribute");
Winsonb2d7f532019-02-04 16:32:43 -08001247 error = true;
1248 continue;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001249 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001250 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001251 diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> "
Ryan Mitchell939df092019-04-09 17:13:50 -07001252 << " in <overlayable>");
Adam Lesinski46c4d722017-08-23 13:03:56 -07001253 error = true;
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001254 break;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001255 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -08001256
1257 comment.clear();
Adam Lesinski46c4d722017-08-23 13:03:56 -07001258 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -07001259
Adam Lesinski46c4d722017-08-23 13:03:56 -07001260 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001261}
1262
Adam Lesinski71be7052017-12-12 16:48:07 -08001263bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001265 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001266 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 return true;
1268 }
1269 return false;
1270}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001271
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1273 ParsedResource* out_resource) {
1274 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001275}
1276
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1278 ParsedResource* out_resource, bool weak) {
1279 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280
1281 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001282 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1283 diag_->Warn(DiagMessage(out_resource->source)
1284 << "ignoring configuration '" << out_resource->config
1285 << "' for attribute " << out_resource->name);
1286 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287 }
1288
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001289 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1292 if (maybe_format) {
1293 type_mask = ParseFormatAttribute(maybe_format.value());
1294 if (type_mask == 0) {
1295 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001296 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297 return false;
1298 }
1299 }
1300
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001303 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1304 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1305 if (!min_str.empty()) {
1306 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001308 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001311 }
1312
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001313 if (!maybe_min) {
1314 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1315 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316 return false;
1317 }
1318 }
1319
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1321 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1322 if (!max_str.empty()) {
1323 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001325 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001326 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001328 }
1329
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 if (!maybe_max) {
1331 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1332 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 return false;
1334 }
1335 }
1336
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 if ((maybe_min || maybe_max) &&
1338 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1339 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 << "'min' and 'max' can only be used when format='integer'");
1341 return false;
1342 }
1343
1344 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001345 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 return a.symbol.name.value() < b.symbol.name.value();
1347 }
1348 };
1349
1350 std::set<Attribute::Symbol, SymbolComparator> items;
1351
1352 std::string comment;
1353 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 const size_t depth = parser->depth();
1355 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1356 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001357 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001359 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001360 // Skip text.
1361 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001362 }
1363
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 const Source item_source = source_.WithLine(parser->line_number());
1365 const std::string& element_namespace = parser->element_namespace();
1366 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001367 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 if (element_name == "enum") {
1369 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1370 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 << "can not define an <enum>; already defined a <flag>");
1372 error = true;
1373 continue;
1374 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001376
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 } else if (element_name == "flag") {
1378 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1379 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001380 << "can not define a <flag>; already defined an <enum>");
1381 error = true;
1382 continue;
1383 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 }
1386
1387 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 ParsedResource child_resource;
1391 child_resource.name = symbol.symbol.name.value();
1392 child_resource.source = item_source;
1393 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001394 if (options_.visibility) {
1395 child_resource.visibility_level = options_.visibility.value();
1396 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001397 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 symbol.symbol.SetComment(std::move(comment));
1400 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001401
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 auto insert_result = items.insert(std::move(symbol));
1403 if (!insert_result.second) {
1404 const Attribute::Symbol& existing_symbol = *insert_result.first;
1405 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001407 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001408
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001409 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001410 << "first defined here");
1411 error = true;
1412 }
1413 } else {
1414 error = true;
1415 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001416 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1417 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 error = true;
1419 }
1420
1421 comment = {};
1422 }
1423
1424 if (error) {
1425 return false;
1426 }
1427
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001428 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1429 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1430 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001431 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001432 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1433 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001434 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001435 return true;
1436}
1437
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001438Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001439 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001440 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001441
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001442 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1443 if (!maybe_name) {
1444 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001445 << tag << ">");
1446 return {};
1447 }
1448
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1450 if (!maybe_value) {
1451 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001452 << tag << ">");
1453 return {};
1454 }
1455
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001456 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001457 android::Res_value val;
1458 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001459 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001460 << "' for <" << tag
1461 << ">; must be an integer");
1462 return {};
1463 }
1464
1465 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001466 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Ryan Mitchellc1676802019-05-20 16:47:08 -07001467 val.data, val.dataType};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001468}
1469
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001470bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1471 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001472
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001473 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1474 if (!maybe_name) {
1475 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 return false;
1477 }
1478
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001479 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001480 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001481 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001482 return false;
1483 }
1484
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001485 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001486 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001487
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001488 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001489 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001490 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 return false;
1492 }
1493
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001494 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001495 return true;
1496}
1497
Adam Lesinski86d67df2017-01-31 13:47:27 -08001498bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001500 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001501
1502 std::unique_ptr<Style> style = util::make_unique<Style>();
1503
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001504 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1505 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001506 // 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 -07001507 if (!maybe_parent.value().empty()) {
1508 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001509 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001511 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001512 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 }
1514
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001515 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001516 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001517 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001518 }
1519
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520 } else {
1521 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001522 std::string style_name = out_resource->name.entry;
1523 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001524 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001525 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001526 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 }
1528 }
1529
1530 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001531 const size_t depth = parser->depth();
1532 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1533 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534 // Skip text and comments.
1535 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001536 }
1537
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 const std::string& element_namespace = parser->element_namespace();
1539 const std::string& element_name = parser->element_name();
1540 if (element_namespace == "" && element_name == "item") {
1541 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001542
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001543 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1544 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1545 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001546 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001547 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001548 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001549
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 if (error) {
1551 return false;
1552 }
1553
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001554 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001555 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001556}
1557
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001558bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1559 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1560 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1561 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1562 if (resource_format == 0u) {
1563 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1564 << "'" << format_attr.value() << "' is an invalid format");
1565 return false;
1566 }
1567 }
1568 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001569}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001570
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001571bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1572 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001573}
1574
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001575bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1576 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001577}
1578
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1580 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001581 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001582 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001583
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001584 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001585
Adam Lesinski75421622017-01-06 15:20:04 -08001586 bool translatable = options_.translatable;
1587 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1588 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1589 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001590 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001591 << "invalid value for 'translatable'. Must be a boolean");
1592 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001593 }
Adam Lesinski75421622017-01-06 15:20:04 -08001594 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 }
Adam Lesinski75421622017-01-06 15:20:04 -08001596 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001597
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001599 const size_t depth = parser->depth();
1600 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1601 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001602 // Skip text and comments.
1603 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001604 }
1605
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 const Source item_source = source_.WithLine(parser->line_number());
1607 const std::string& element_namespace = parser->element_namespace();
1608 const std::string& element_name = parser->element_name();
1609 if (element_namespace.empty() && element_name == "item") {
1610 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001611 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 error = true;
1614 continue;
1615 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001616 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001617 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001618
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001619 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1620 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1621 << "unknown tag <" << element_namespace << ":"
1622 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 error = true;
1624 }
1625 }
1626
1627 if (error) {
1628 return false;
1629 }
1630
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001631 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001632 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001633}
1634
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1636 ParsedResource* out_resource) {
1637 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001638
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001639 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001640
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001641 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001642 const size_t depth = parser->depth();
1643 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1644 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 // Skip text and comments.
1646 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001647 }
1648
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649 const Source item_source = source_.WithLine(parser->line_number());
1650 const std::string& element_namespace = parser->element_namespace();
1651 const std::string& element_name = parser->element_name();
1652 if (element_namespace.empty() && element_name == "item") {
1653 Maybe<StringPiece> maybe_quantity =
1654 xml::FindNonEmptyAttribute(parser, "quantity");
1655 if (!maybe_quantity) {
1656 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001657 << "<item> in <plurals> requires attribute "
1658 << "'quantity'");
1659 error = true;
1660 continue;
1661 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001662
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001663 StringPiece trimmed_quantity =
1664 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001666 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001667 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001668 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001669 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001670 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001671 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001672 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001673 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001675 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001676 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001677 index = Plural::Other;
1678 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001679 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001680 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001681 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001682 error = true;
1683 continue;
1684 }
1685
1686 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001687 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1688 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001689 error = true;
1690 continue;
1691 }
1692
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001693 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001694 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1695 error = true;
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001696 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001697 }
Ryan Mitchell2f9077d2019-02-15 16:02:09 -08001698
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001699 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001700
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001701 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1702 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1703 << element_namespace << ":"
1704 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001705 error = true;
1706 }
1707 }
1708
1709 if (error) {
1710 return false;
1711 }
1712
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001713 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001714 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001715}
1716
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001717bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1718 ParsedResource* out_resource) {
1719 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001720
Donald Chai94e4a012020-01-06 13:52:41 -08001721 if (!options_.preserve_visibility_of_styleables) {
1722 // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one
1723 // knows exactly what for.
1724 //
1725 // FWIW, styleables only appear in generated R classes. For custom views these should always be
1726 // package-private (to be used only by the view class); themes are a different story.
1727 out_resource->visibility_level = Visibility::Level::kPublic;
1728 }
Adam Lesinski9f222042015-11-04 13:51:45 -08001729
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001730 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001731 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1732 diag_->Warn(DiagMessage(out_resource->source)
1733 << "ignoring configuration '" << out_resource->config
1734 << "' for styleable " << out_resource->name.entry);
1735 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001736 }
1737
1738 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1739
1740 std::string comment;
1741 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001742 const size_t depth = parser->depth();
1743 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1744 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001745 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001746 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001747 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001748 // Ignore text.
1749 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001750 }
1751
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001752 const Source item_source = source_.WithLine(parser->line_number());
1753 const std::string& element_namespace = parser->element_namespace();
1754 const std::string& element_name = parser->element_name();
1755 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001756 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001757 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001758 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001759 error = true;
1760 continue;
1761 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001762
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001763 // If this is a declaration, the package name may be in the name. Separate
1764 // these out.
1765 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001766 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001767 if (!maybe_ref) {
1768 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1769 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001770 error = true;
1771 continue;
1772 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001773
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001774 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001775 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001776
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001777 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001778 ParsedResource child_resource;
1779 child_resource.name = child_ref.name.value();
1780 child_resource.source = item_source;
Chih-Hung Hsieh7a616f62020-03-05 15:59:25 -08001781 // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001782 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001783 if (options_.visibility) {
1784 child_resource.visibility_level = options_.visibility.value();
1785 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001786
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001787 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001788 error = true;
1789 continue;
1790 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001791
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001792 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001793 child_ref.SetComment(child_resource.comment);
1794 child_ref.SetSource(item_source);
1795 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001796
Ryan Mitchellacde95c32019-04-23 05:44:21 -07001797 // Do not add referenced attributes that do not define a format to the table.
1798 CHECK(child_resource.value != nullptr);
1799 Attribute* attr = ValueCast<Attribute>(child_resource.value.get());
1800
1801 CHECK(attr != nullptr);
1802 if (attr->type_mask != android::ResTable_map::TYPE_ANY) {
1803 out_resource->child_resources.push_back(std::move(child_resource));
1804 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001805
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001806 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1807 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1808 << element_namespace << ":"
1809 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001810 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001811 }
1812
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001813 comment = {};
1814 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001815
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001816 if (error) {
1817 return false;
1818 }
1819
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001820 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001821 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001822}
1823
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001824} // namespace aapt