blob: 968376b8229b6dc641f2cecf5e8b322df6635d9a [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Adam Lesinski2eed52e2018-02-21 15:55:58 -080035using ::aapt::ResourceUtils::StringBuilder;
36using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad5c541f62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070042constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070044// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
45static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080047}
48
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
50 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070054 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068}
69
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070static uint32_t ParseFormatType(const StringPiece& piece) {
71 if (piece == "enum") {
72 return android::ResTable_map::TYPE_ENUM;
73 } else if (piece == "flags") {
74 return android::ResTable_map::TYPE_FLAGS;
75 }
76 return ParseFormatTypeNoEnumsOrFlags(piece);
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 uint32_t mask = 0;
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -080081 for (const StringPiece& part : util::Tokenize(str, '|')) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 StringPiece trimmed_part = util::TrimWhitespace(part);
83 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 if (type == 0) {
85 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 mask |= type;
88 }
89 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090}
91
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070092// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080093struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 ResourceName name;
95 ConfigDescription config;
96 std::string product;
97 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800100 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700101 bool allow_new = false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800102 bool overlayable = false;
103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::string comment;
105 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800107};
108
109// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700110static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
112 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800114 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 }
Adam Lesinski76565542016-03-10 21:55:04 -0800116
Adam Lesinski71be7052017-12-12 16:48:07 -0800117 if (res->visibility_level != Visibility::Level::kUndefined) {
118 Visibility visibility;
119 visibility.level = res->visibility_level;
120 visibility.source = res->source;
121 visibility.comment = res->comment;
122 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800126
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->allow_new) {
128 AllowNew allow_new;
129 allow_new.source = res->source;
130 allow_new.comment = res->comment;
131 if (!table->SetAllowNew(res->name, allow_new, diag)) {
132 return false;
133 }
134 }
135
136 if (res->overlayable) {
137 Overlayable overlayable;
138 overlayable.source = res->source;
139 overlayable.comment = res->comment;
140 if (!table->SetOverlayable(res->name, overlayable, diag)) {
141 return false;
142 }
143 }
144
145 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 res->value->SetComment(std::move(res->comment));
148 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800149
Adam Lesinski71be7052017-12-12 16:48:07 -0800150 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
151 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800153 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 for (ParsedResource& child : res->child_resources) {
158 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
160 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800161}
162
163// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
167 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700168 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 : diag_(diag),
171 table_(table),
172 source_(source),
173 config_(config),
174 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800176// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
177// This will be used to traverse and flatten the XML string into a single std::string, with all
178// Span and Untranslatable data maintained in parallel, as indices into the string.
179class Node {
180 public:
181 virtual ~Node() = default;
182
183 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
184 // parent node as well.
185 // Returns a pointer to the child node that was added as a convenience.
186 template <typename T>
187 T* AddChild(std::unique_ptr<T> node) {
188 T* raw_ptr = node.get();
189 children.push_back(std::move(node));
190 return raw_ptr;
191 }
192
193 virtual void Build(StringBuilder* builder) const {
194 for (const auto& child : children) {
195 child->Build(builder);
196 }
197 }
198
199 std::vector<std::unique_ptr<Node>> children;
200};
201
202// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
203class SegmentNode : public Node {
204 public:
205 std::string data;
206
207 void Build(StringBuilder* builder) const override {
208 builder->AppendText(data);
209 }
210};
211
212// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
213class SpanNode : public Node {
214 public:
215 std::string name;
216
217 void Build(StringBuilder* builder) const override {
218 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
219 Node::Build(builder);
220 builder->EndSpan(span_handle);
221 }
222};
223
224// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
225class UntranslatableNode : public Node {
226 public:
227 void Build(StringBuilder* builder) const override {
228 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
229 Node::Build(builder);
230 builder->EndUntranslatable(handle);
231 }
232};
233
234// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800235bool ResourceParser::FlattenXmlSubtree(
236 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
237 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800238 std::string raw_string;
239 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800240
241 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
242 Maybe<size_t> untranslatable_start_depth;
243
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800244 Node root;
245 std::vector<Node*> node_stack;
246 node_stack.push_back(&root);
247
248 bool saw_span_node = false;
249 SegmentNode* first_segment = nullptr;
250 SegmentNode* last_segment = nullptr;
251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800253 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800255
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800256 // First take care of any SegmentNodes that should be created.
257 if (event == xml::XmlPullParser::Event::kStartElement ||
258 event == xml::XmlPullParser::Event::kEndElement) {
259 if (!current_text.empty()) {
260 std::unique_ptr<SegmentNode> segment_node = util::make_unique<SegmentNode>();
261 segment_node->data = std::move(current_text);
262 last_segment = node_stack.back()->AddChild(std::move(segment_node));
263 if (first_segment == nullptr) {
264 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800265 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800266 current_text = {};
267 }
268 }
Adam Lesinski75421622017-01-06 15:20:04 -0800269
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800270 switch (event) {
271 case xml::XmlPullParser::Event::kText: {
272 current_text += parser->text();
273 raw_string += parser->text();
274 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800275
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800276 case xml::XmlPullParser::Event::kStartElement: {
277 if (parser->element_namespace().empty()) {
278 // This is an HTML tag which we encode as a span. Add it to the span stack.
279 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
280 span_node->name = parser->element_name();
281 const auto end_attr_iter = parser->end_attributes();
282 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
283 ++attr_iter) {
284 span_node->name += ";";
285 span_node->name += attr_iter->name;
286 span_node->name += "=";
287 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800288 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289
290 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
291 saw_span_node = true;
292 } else if (parser->element_namespace() == sXliffNamespaceUri) {
293 // This is an XLIFF tag, which is not encoded as a span.
294 if (parser->element_name() == "g") {
295 // Check that an 'untranslatable' tag is not already being processed. Nested
296 // <xliff:g> tags are illegal.
297 if (untranslatable_start_depth) {
298 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
299 << "illegal nested XLIFF 'g' tag");
300 return false;
301 } else {
302 // Mark the beginning of an 'untranslatable' section.
303 untranslatable_start_depth = depth;
304 node_stack.push_back(
305 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
306 }
307 } else {
308 // Ignore unknown XLIFF tags, but don't warn.
309 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
310 }
311 } else {
312 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
313 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
314 << "ignoring element '" << parser->element_name()
315 << "' with unknown namespace '" << parser->element_namespace() << "'");
316 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800317 }
Adam Lesinski75421622017-01-06 15:20:04 -0800318
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800319 // Enter one level inside the element.
320 depth++;
321 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700322
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800323 case xml::XmlPullParser::Event::kEndElement: {
324 // Return one level from within the element.
325 depth--;
326 if (depth == 0) {
327 break;
328 }
329
330 node_stack.pop_back();
331 if (untranslatable_start_depth == make_value(depth)) {
332 // This is the end of an untranslatable section.
333 untranslatable_start_depth = {};
334 }
335 } break;
336
337 default:
338 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 }
341 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800343 // Sanity check to make sure we processed all the nodes.
344 CHECK(node_stack.size() == 1u);
345 CHECK(node_stack.back() == &root);
346
347 if (!saw_span_node) {
348 // If there were no spans, we must treat this string a little differently (according to AAPT).
349 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
350 // from the last segment.
351 if (first_segment != nullptr) {
352 // Trim leading whitespace.
353 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
354 if (trimmed.size() != first_segment->data.size()) {
355 first_segment->data = trimmed.to_string();
356 }
357 }
358
359 if (last_segment != nullptr) {
360 // Trim trailing whitespace.
361 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
362 if (trimmed.size() != last_segment->data.size()) {
363 last_segment->data = trimmed.to_string();
364 }
365 }
366 }
367
368 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
369 // care of recording the correctly adjusted Spans and UntranslatableSections.
370 StringBuilder builder;
371 root.Build(&builder);
372 if (!builder) {
373 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
374 return false;
375 }
376
377 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
378 *out_raw_string = std::move(raw_string);
379 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
380 out_style_string->str = std::move(flattened_string.text);
381 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800382 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800383}
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 const size_t depth = parser->depth();
388 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
389 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 // Skip comments and text.
391 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800392 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393
Adam Lesinski060b53d2017-07-28 17:10:35 -0700394 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 << "root element must be <resources>");
397 return false;
398 }
399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 break;
402 };
403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
405 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
406 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 return false;
408 }
409 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410}
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
413 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700414
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 bool error = false;
416 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 const size_t depth = parser->depth();
418 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
419 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800423 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700424
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 if (!util::TrimWhitespace(parser->text()).empty()) {
427 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 << "plain text not allowed here");
429 error = true;
430 }
431 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700432 }
433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 // Skip unknown namespace.
438 continue;
439 }
440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 std::string element_name = parser->element_name();
442 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 comment = "";
444 continue;
445 }
446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 ParsedResource parsed_resource;
448 parsed_resource.config = config_;
449 parsed_resource.source = source_.WithLine(parser->line_number());
450 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451
452 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700453 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800454 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 }
456
457 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 error = true;
460 continue;
461 }
462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 error = true;
465 }
466 }
467
468 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 for (const ResourceName& stripped_resource : stripped_resources) {
470 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700472 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
473 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 error = true;
475 }
476 }
477
478 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800479}
480
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
482 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 struct ItemTypeFormat {
484 ResourceType type;
485 uint32_t format;
486 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800487
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
489 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800490
Adam Lesinski86d67df2017-01-31 13:47:27 -0800491 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
492 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
493 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
494 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
495 {"dimen",
496 {ResourceType::kDimen,
497 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
498 android::ResTable_map::TYPE_DIMENSION}},
499 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
500 {"fraction",
501 {ResourceType::kFraction,
502 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
503 android::ResTable_map::TYPE_DIMENSION}},
504 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
505 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
506 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800507
Adam Lesinski86d67df2017-01-31 13:47:27 -0800508 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
509 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
510 {"array", std::mem_fn(&ResourceParser::ParseArray)},
511 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
512 {"configVarying",
513 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
514 std::placeholders::_2, std::placeholders::_3)},
515 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
516 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
517 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700518 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800519 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
520 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
521 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
522 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
523 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
524 std::placeholders::_2, std::placeholders::_3)},
525 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
526 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800529
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800532
Adam Lesinski86d67df2017-01-31 13:47:27 -0800533 bool can_be_item = true;
534 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800536 can_be_bag = false;
537
Adam Lesinskie597d682017-06-01 17:16:44 -0700538 // The default format for <item> is any. If a format attribute is present, that one will
539 // override the default.
540 resource_format = android::ResTable_map::TYPE_ANY;
541
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700543 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800544 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 << "<item> must have a 'type' attribute");
548 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800549 }
550
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700551 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700553 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700555 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 if (!resource_format) {
557 diag_->Error(DiagMessage(out_resource->source)
558 << "'" << maybe_format.value()
559 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800560 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 }
562 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800563 } else if (resource_type == "bag") {
564 can_be_item = false;
565
566 // Bags have their type encoded in the type attribute.
567 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
568 resource_type = maybe_type.value().to_string();
569 } else {
570 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
571 << "<bag> must have a 'type' attribute");
572 return false;
573 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 }
575
576 // Get the name of the resource. This will be checked later, because not all
577 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 if (resource_type == "id") {
581 if (!maybe_name) {
582 diag_->Error(DiagMessage(out_resource->source)
583 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 << "> missing 'name' attribute");
585 return false;
586 }
587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800589 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700590
591 // Ids either represent a unique resource id or reference another resource id
592 auto item = ParseItem(parser, out_resource, resource_format);
593 if (!item) {
594 return false;
595 }
596
597 String* empty = ValueCast<String>(out_resource->value.get());
598 if (empty && *empty->value == "") {
599 // If no inner element exists, represent a unique identifier
600 out_resource->value = util::make_unique<Id>();
601 } else {
y9efbbef2018-04-18 11:29:09 -0700602 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700603 if (ref && !ref->name && !ref->id) {
604 // A null reference also means there is no inner element when ids are in the form:
605 // <id name="name"/>
606 out_resource->value = util::make_unique<Id>();
607 } else if (!ref || ref->name.value().type != ResourceType::kId) {
608 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700609 diag_->Error(DiagMessage(out_resource->source)
610 << "<" << parser->element_name()
611 << "> inner element must either be a resource reference or empty");
612 return false;
613 }
614 }
615
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 return true;
617 }
618
Adam Lesinski86d67df2017-01-31 13:47:27 -0800619 if (can_be_item) {
620 const auto item_iter = elToItemMap.find(resource_type);
621 if (item_iter != elToItemMap.end()) {
622 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623
Adam Lesinski86d67df2017-01-31 13:47:27 -0800624 if (!maybe_name) {
625 diag_->Error(DiagMessage(out_resource->source)
626 << "<" << parser->element_name() << "> missing 'name' attribute");
627 return false;
628 }
629
630 out_resource->name.type = item_iter->second.type;
631 out_resource->name.entry = maybe_name.value().to_string();
632
Adam Lesinskie597d682017-06-01 17:16:44 -0700633 // Only use the implied format of the type when there is no explicit format.
634 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800635 resource_format = item_iter->second.format;
636 }
637
638 if (!ParseItem(parser, out_resource, resource_format)) {
639 return false;
640 }
641 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 }
644
645 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800646 if (can_be_bag) {
647 const auto bag_iter = elToBagMap.find(resource_type);
648 if (bag_iter != elToBagMap.end()) {
649 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700650 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800651 if (!maybe_name) {
652 diag_->Error(DiagMessage(out_resource->source)
653 << "<" << parser->element_name() << "> missing 'name' attribute");
654 return false;
655 }
656
657 out_resource->name.entry = maybe_name.value().to_string();
658 }
659
660 // Call the associated parse method. The type will be filled in by the
661 // parse func.
662 if (!bag_iter->second(this, parser, out_resource)) {
663 return false;
664 }
665 return true;
666 }
667 }
668
669 if (can_be_item) {
670 // Try parsing the elementName (or type) as a resource. These shall only be
671 // resources like 'layout' or 'xml' and they can only be references.
672 const ResourceType* parsed_type = ParseResourceType(resource_type);
673 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 if (!maybe_name) {
675 diag_->Error(DiagMessage(out_resource->source)
676 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 << "> missing 'name' attribute");
678 return false;
679 }
680
Adam Lesinski86d67df2017-01-31 13:47:27 -0800681 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800682 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800683 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
684 if (!out_resource->value) {
685 diag_->Error(DiagMessage(out_resource->source)
686 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
687 return false;
688 }
689 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 }
692
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 diag_->Warn(DiagMessage(out_resource->source)
694 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 return false;
696}
697
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
699 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 const uint32_t format) {
701 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 out_resource->value = ParseXml(parser, format, kNoRawString);
706 if (!out_resource->value) {
707 diag_->Error(DiagMessage(out_resource->source) << "invalid "
708 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 return false;
710 }
711 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800712}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800713
714/**
715 * Reads the entire XML subtree and attempts to parse it as some Item,
716 * with typeMask denoting which items it can be. If allowRawValue is
717 * true, a RawString is returned if the XML couldn't be parsed as
718 * an Item. If allowRawValue is false, nullptr is returned in this
719 * case.
720 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
722 const uint32_t type_mask,
723 const bool allow_raw_value) {
724 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 std::string raw_value;
727 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800728 std::vector<UntranslatableSection> untranslatable_sections;
729 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800730 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 }
732
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800735 std::unique_ptr<StyledString> styled_string =
736 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700737 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800738 styled_string->untranslatable_sections = std::move(untranslatable_sections);
739 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 }
741
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 // name.package can be empty here, as it will assume the package name of the
744 // table.
745 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 id->SetSource(source_.WithLine(begin_xml_line));
747 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 };
749
750 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800752 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700756 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 }
760
761 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700762 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700763 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800764 std::unique_ptr<String> string = util::make_unique<String>(
765 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
766 string->untranslatable_sections = std::move(untranslatable_sections);
767 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 }
769
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700770 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
771 if (util::TrimWhitespace(raw_value).empty()) {
772 return ResourceUtils::MakeNull();
773 }
774
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700776 // We can't parse this so return a RawString if we are allowed.
777 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 }
780 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800781}
782
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700783bool ResourceParser::ParseString(xml::XmlPullParser* parser,
784 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786 if (Maybe<StringPiece> formatted_attr =
787 xml::FindAttribute(parser, "formatted")) {
788 Maybe<bool> maybe_formatted =
789 ResourceUtils::ParseBool(formatted_attr.value());
790 if (!maybe_formatted) {
791 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 << "invalid value for 'formatted'. Must be a boolean");
793 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800794 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700795 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800797
Adam Lesinski75421622017-01-06 15:20:04 -0800798 bool translatable = options_.translatable;
799 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
800 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
801 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 << "invalid value for 'translatable'. Must be a boolean");
804 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800805 }
Adam Lesinski75421622017-01-06 15:20:04 -0800806 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800808
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 out_resource->value =
810 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
811 if (!out_resource->value) {
812 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 return false;
814 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800815
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700816 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800817 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800818
Adam Lesinski75421622017-01-06 15:20:04 -0800819 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 if (!util::VerifyJavaStringFormat(*string_value->value)) {
821 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 msg << "multiple substitutions specified in non-positional format; "
823 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700824 if (options_.error_on_positional_arguments) {
825 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800827 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800828
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700829 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700830 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800831 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832
Adam Lesinski75421622017-01-06 15:20:04 -0800833 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
834 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 }
836 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800837}
838
Adam Lesinski71be7052017-12-12 16:48:07 -0800839bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700840 if (out_resource->config != ConfigDescription::DefaultConfig()) {
841 diag_->Warn(DiagMessage(out_resource->source)
842 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
843 }
844
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
846 if (!maybe_type) {
847 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 << "<public> must have a 'type' attribute");
849 return false;
850 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800851
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700852 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
853 if (!parsed_type) {
854 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
855 << maybe_type.value()
856 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 return false;
858 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800859
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700860 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800861
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800862 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
863 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800865 diag_->Error(DiagMessage(out_resource->source)
866 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800868 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700870 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800871
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700872 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700873 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700874 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700876
Adam Lesinski71be7052017-12-12 16:48:07 -0800877 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800879}
880
Adam Lesinski46c4d722017-08-23 13:03:56 -0700881bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
882 if (out_resource->config != ConfigDescription::DefaultConfig()) {
883 diag_->Warn(DiagMessage(out_resource->source)
884 << "ignoring configuration '" << out_resource->config
885 << "' for <public-group> tag");
886 }
887
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700888 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
889 if (!maybe_type) {
890 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800892 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800894
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700895 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
896 if (!parsed_type) {
897 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
898 << maybe_type.value()
899 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800900 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 }
902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 Maybe<StringPiece> maybe_id_str =
904 xml::FindNonEmptyAttribute(parser, "first-id");
905 if (!maybe_id_str) {
906 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 << "<public-group> must have a 'first-id' attribute");
908 return false;
909 }
910
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911 Maybe<ResourceId> maybe_id =
912 ResourceUtils::ParseResourceId(maybe_id_str.value());
913 if (!maybe_id) {
914 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
915 << maybe_id_str.value()
916 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 return false;
918 }
919
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700920 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921
922 std::string comment;
923 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700924 const size_t depth = parser->depth();
925 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
926 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800927 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700929 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930 // Skip text.
931 continue;
932 }
933
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700934 const Source item_source = source_.WithLine(parser->line_number());
935 const std::string& element_namespace = parser->element_namespace();
936 const std::string& element_name = parser->element_name();
937 if (element_namespace.empty() && element_name == "public") {
938 Maybe<StringPiece> maybe_name =
939 xml::FindNonEmptyAttribute(parser, "name");
940 if (!maybe_name) {
941 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700942 << "<public> must have a 'name' attribute");
943 error = true;
944 continue;
945 }
946
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 if (xml::FindNonEmptyAttribute(parser, "id")) {
948 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 << "'id' is ignored within <public-group>");
950 error = true;
951 continue;
952 }
953
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700954 if (xml::FindNonEmptyAttribute(parser, "type")) {
955 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700956 << "'type' is ignored within <public-group>");
957 error = true;
958 continue;
959 }
960
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700961 ParsedResource child_resource;
962 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800963 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 child_resource.id = next_id;
965 child_resource.comment = std::move(comment);
966 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800967 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700970 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
973 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 error = true;
975 }
976 }
977 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800978}
979
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
981 ParsedResource* out_resource) {
982 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
983 if (!maybe_type) {
984 diag_->Error(DiagMessage(out_resource->source)
985 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986 << "> must have a 'type' attribute");
987 return false;
988 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800989
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700990 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
991 if (!parsed_type) {
992 diag_->Error(DiagMessage(out_resource->source)
993 << "invalid resource type '" << maybe_type.value() << "' in <"
994 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 return false;
996 }
997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001000}
1001
Adam Lesinski46c4d722017-08-23 13:03:56 -07001002bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1003 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1004 diag_->Warn(DiagMessage(out_resource->source)
1005 << "ignoring configuration '" << out_resource->config << "' for <"
1006 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001008
1009 if (!ParseSymbolImpl(parser, out_resource)) {
1010 return false;
1011 }
1012
Adam Lesinski71be7052017-12-12 16:48:07 -08001013 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001014 return true;
1015}
1016
1017bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1018 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1019 diag_->Warn(DiagMessage(out_resource->source)
1020 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
1021 }
1022
1023 if (Maybe<StringPiece> maybe_policy = xml::FindNonEmptyAttribute(parser, "policy")) {
1024 const StringPiece& policy = maybe_policy.value();
1025 if (policy != "system") {
1026 diag_->Error(DiagMessage(out_resource->source)
1027 << "<overlayable> has invalid policy '" << policy << "'");
1028 return false;
1029 }
1030 }
1031
1032 bool error = false;
1033 const size_t depth = parser->depth();
1034 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1035 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1036 // Skip text/comments.
1037 continue;
1038 }
1039
1040 const Source item_source = source_.WithLine(parser->line_number());
1041 const std::string& element_namespace = parser->element_namespace();
1042 const std::string& element_name = parser->element_name();
1043 if (element_namespace.empty() && element_name == "item") {
1044 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1045 if (!maybe_name) {
1046 diag_->Error(DiagMessage(item_source)
1047 << "<item> within an <overlayable> tag must have a 'name' attribute");
1048 error = true;
1049 continue;
1050 }
1051
1052 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1053 if (!maybe_type) {
1054 diag_->Error(DiagMessage(item_source)
1055 << "<item> within an <overlayable> tag must have a 'type' attribute");
1056 error = true;
1057 continue;
1058 }
1059
1060 const ResourceType* type = ParseResourceType(maybe_type.value());
1061 if (type == nullptr) {
1062 diag_->Error(DiagMessage(out_resource->source)
1063 << "invalid resource type '" << maybe_type.value()
1064 << "' in <item> within an <overlayable>");
1065 error = true;
1066 continue;
1067 }
1068
Adam Lesinski71be7052017-12-12 16:48:07 -08001069 ParsedResource child_resource;
1070 child_resource.name.type = *type;
1071 child_resource.name.entry = maybe_name.value().to_string();
1072 child_resource.source = item_source;
1073 child_resource.overlayable = true;
1074 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001075
1076 xml::XmlPullParser::SkipCurrentElement(parser);
1077 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1078 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
1079 error = true;
1080 }
1081 }
1082 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001083}
1084
Adam Lesinski71be7052017-12-12 16:48:07 -08001085bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001087 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001088 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 return true;
1090 }
1091 return false;
1092}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001093
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001094bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1095 ParsedResource* out_resource) {
1096 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001097}
1098
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1100 ParsedResource* out_resource, bool weak) {
1101 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102
1103 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001104 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1105 diag_->Warn(DiagMessage(out_resource->source)
1106 << "ignoring configuration '" << out_resource->config
1107 << "' for attribute " << out_resource->name);
1108 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 }
1110
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001111 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001113 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1114 if (maybe_format) {
1115 type_mask = ParseFormatAttribute(maybe_format.value());
1116 if (type_mask == 0) {
1117 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001118 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001119 return false;
1120 }
1121 }
1122
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001123 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001124
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001125 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1126 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1127 if (!min_str.empty()) {
1128 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001129 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001130 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001131 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001133 }
1134
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001135 if (!maybe_min) {
1136 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1137 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001138 return false;
1139 }
1140 }
1141
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1143 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1144 if (!max_str.empty()) {
1145 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001147 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001149 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001150 }
1151
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 if (!maybe_max) {
1153 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1154 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 return false;
1156 }
1157 }
1158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 if ((maybe_min || maybe_max) &&
1160 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1161 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001162 << "'min' and 'max' can only be used when format='integer'");
1163 return false;
1164 }
1165
1166 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001167 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 return a.symbol.name.value() < b.symbol.name.value();
1169 }
1170 };
1171
1172 std::set<Attribute::Symbol, SymbolComparator> items;
1173
1174 std::string comment;
1175 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001176 const size_t depth = parser->depth();
1177 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1178 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001179 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001180 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001181 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001182 // Skip text.
1183 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001184 }
1185
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001186 const Source item_source = source_.WithLine(parser->line_number());
1187 const std::string& element_namespace = parser->element_namespace();
1188 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001189 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190 if (element_name == "enum") {
1191 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1192 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 << "can not define an <enum>; already defined a <flag>");
1194 error = true;
1195 continue;
1196 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001197 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001198
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001199 } else if (element_name == "flag") {
1200 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1201 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202 << "can not define a <flag>; already defined an <enum>");
1203 error = true;
1204 continue;
1205 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001206 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 }
1208
1209 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001210 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001212 ParsedResource child_resource;
1213 child_resource.name = symbol.symbol.name.value();
1214 child_resource.source = item_source;
1215 child_resource.value = util::make_unique<Id>();
1216 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001217
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001218 symbol.symbol.SetComment(std::move(comment));
1219 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 auto insert_result = items.insert(std::move(symbol));
1222 if (!insert_result.second) {
1223 const Attribute::Symbol& existing_symbol = *insert_result.first;
1224 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001225 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001226 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001227
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001228 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001229 << "first defined here");
1230 error = true;
1231 }
1232 } else {
1233 error = true;
1234 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1236 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 error = true;
1238 }
1239
1240 comment = {};
1241 }
1242
1243 if (error) {
1244 return false;
1245 }
1246
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001247 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1248 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1249 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001251 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1252 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001253 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001254 return true;
1255}
1256
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001258 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001259 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001260
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1262 if (!maybe_name) {
1263 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 << tag << ">");
1265 return {};
1266 }
1267
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001268 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1269 if (!maybe_value) {
1270 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271 << tag << ">");
1272 return {};
1273 }
1274
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001275 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 android::Res_value val;
1277 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001278 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001279 << "' for <" << tag
1280 << ">; must be an integer");
1281 return {};
1282 }
1283
1284 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001286 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001287}
1288
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001289bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1290 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001291
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001292 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1293 if (!maybe_name) {
1294 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001295 return false;
1296 }
1297
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001298 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001299 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001300 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 return false;
1302 }
1303
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001304 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001307 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 return false;
1311 }
1312
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001313 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001314 return true;
1315}
1316
Adam Lesinski86d67df2017-01-31 13:47:27 -08001317bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001319 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320
1321 std::unique_ptr<Style> style = util::make_unique<Style>();
1322
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1324 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001325 // 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 -07001326 if (!maybe_parent.value().empty()) {
1327 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001328 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001329 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001330 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001331 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332 }
1333
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001334 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001336 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001337 }
1338
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 } else {
1340 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 std::string style_name = out_resource->name.entry;
1342 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001343 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001345 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 }
1347 }
1348
1349 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 const size_t depth = parser->depth();
1351 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1352 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001353 // Skip text and comments.
1354 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001355 }
1356
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001357 const std::string& element_namespace = parser->element_namespace();
1358 const std::string& element_name = parser->element_name();
1359 if (element_namespace == "" && element_name == "item") {
1360 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001361
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1363 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1364 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001366 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001367 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001368
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 if (error) {
1370 return false;
1371 }
1372
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001375}
1376
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001377bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1378 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1379 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1380 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1381 if (resource_format == 0u) {
1382 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1383 << "'" << format_attr.value() << "' is an invalid format");
1384 return false;
1385 }
1386 }
1387 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001388}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001389
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001390bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1391 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001392}
1393
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001394bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1395 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001396}
1397
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001398bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1399 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001400 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001401 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001402
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001404
Adam Lesinski75421622017-01-06 15:20:04 -08001405 bool translatable = options_.translatable;
1406 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1407 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1408 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001409 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001410 << "invalid value for 'translatable'. Must be a boolean");
1411 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001412 }
Adam Lesinski75421622017-01-06 15:20:04 -08001413 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414 }
Adam Lesinski75421622017-01-06 15:20:04 -08001415 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001416
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001417 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001418 const size_t depth = parser->depth();
1419 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1420 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001421 // Skip text and comments.
1422 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001423 }
1424
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001425 const Source item_source = source_.WithLine(parser->line_number());
1426 const std::string& element_namespace = parser->element_namespace();
1427 const std::string& element_name = parser->element_name();
1428 if (element_namespace.empty() && element_name == "item") {
1429 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001430 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001432 error = true;
1433 continue;
1434 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001435 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001436 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001437
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001438 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1439 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1440 << "unknown tag <" << element_namespace << ":"
1441 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001442 error = true;
1443 }
1444 }
1445
1446 if (error) {
1447 return false;
1448 }
1449
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001450 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001451 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001452}
1453
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001454bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1455 ParsedResource* out_resource) {
1456 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001457
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001458 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001459
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001460 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001461 const size_t depth = parser->depth();
1462 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1463 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001464 // Skip text and comments.
1465 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001466 }
1467
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001468 const Source item_source = source_.WithLine(parser->line_number());
1469 const std::string& element_namespace = parser->element_namespace();
1470 const std::string& element_name = parser->element_name();
1471 if (element_namespace.empty() && element_name == "item") {
1472 Maybe<StringPiece> maybe_quantity =
1473 xml::FindNonEmptyAttribute(parser, "quantity");
1474 if (!maybe_quantity) {
1475 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001476 << "<item> in <plurals> requires attribute "
1477 << "'quantity'");
1478 error = true;
1479 continue;
1480 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001481
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 StringPiece trimmed_quantity =
1483 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001484 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001485 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001488 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001489 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001491 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001492 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001495 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001496 index = Plural::Other;
1497 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001498 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001499 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001500 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001501 error = true;
1502 continue;
1503 }
1504
1505 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001506 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1507 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001508 error = true;
1509 continue;
1510 }
1511
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001512 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1514 error = true;
1515 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001516 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001517
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001518 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1519 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1520 << element_namespace << ":"
1521 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001522 error = true;
1523 }
1524 }
1525
1526 if (error) {
1527 return false;
1528 }
1529
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001530 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001531 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001532}
1533
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1535 ParsedResource* out_resource) {
1536 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001537
Adam Lesinski71be7052017-12-12 16:48:07 -08001538 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1539 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001540
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001542 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1543 diag_->Warn(DiagMessage(out_resource->source)
1544 << "ignoring configuration '" << out_resource->config
1545 << "' for styleable " << out_resource->name.entry);
1546 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547 }
1548
1549 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1550
1551 std::string comment;
1552 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001553 const size_t depth = parser->depth();
1554 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1555 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001556 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001558 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 // Ignore text.
1560 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001561 }
1562
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001563 const Source item_source = source_.WithLine(parser->line_number());
1564 const std::string& element_namespace = parser->element_namespace();
1565 const std::string& element_name = parser->element_name();
1566 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001567 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001568 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001569 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001570 error = true;
1571 continue;
1572 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001573
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001574 // If this is a declaration, the package name may be in the name. Separate
1575 // these out.
1576 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001577 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001578 if (!maybe_ref) {
1579 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1580 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001581 error = true;
1582 continue;
1583 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001584
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001586 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001587
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001588 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 ParsedResource child_resource;
1590 child_resource.name = child_ref.name.value();
1591 child_resource.source = item_source;
1592 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001593
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001595 error = true;
1596 continue;
1597 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001598
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001599 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001600 child_ref.SetComment(child_resource.comment);
1601 child_ref.SetSource(item_source);
1602 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001603
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001605
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001606 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1607 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1608 << element_namespace << ":"
1609 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001611 }
1612
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001613 comment = {};
1614 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001615
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001616 if (error) {
1617 return false;
1618 }
1619
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001620 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001622}
1623
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624} // namespace aapt