blob: 9a3f14c8e08e536beb73c318f05d4cca90024ae3 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski2eed52e2018-02-21 15:55:58 -080029#include "text/Utf8Iterator.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080030#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080031#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070034
Adam Lesinski2eed52e2018-02-21 15:55:58 -080035using ::aapt::ResourceUtils::StringBuilder;
36using ::aapt::text::Utf8Iterator;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070042constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070044// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
45static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080047}
48
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
50 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070054 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070064 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080068}
69
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070070static uint32_t ParseFormatType(const StringPiece& piece) {
71 if (piece == "enum") {
72 return android::ResTable_map::TYPE_ENUM;
73 } else if (piece == "flags") {
74 return android::ResTable_map::TYPE_FLAGS;
75 }
76 return ParseFormatTypeNoEnumsOrFlags(piece);
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 for (StringPiece part : util::Tokenize(str, '|')) {
82 StringPiece trimmed_part = util::TrimWhitespace(part);
83 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 if (type == 0) {
85 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 mask |= type;
88 }
89 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080090}
91
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070092// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080093struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 ResourceName name;
95 ConfigDescription config;
96 std::string product;
97 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -0800100 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700101 bool allow_new = false;
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 Lesinski7656554f2016-03-10 21:55:04 -0800116
Adam Lesinski71be7052017-12-12 16:48:07 -0800117 if (res->visibility_level != Visibility::Level::kUndefined) {
118 Visibility visibility;
119 visibility.level = res->visibility_level;
120 visibility.source = res->source;
121 visibility.comment = res->comment;
122 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800126
Adam Lesinski71be7052017-12-12 16:48:07 -0800127 if (res->allow_new) {
128 AllowNew allow_new;
129 allow_new.source = res->source;
130 allow_new.comment = res->comment;
131 if (!table->SetAllowNew(res->name, allow_new, diag)) {
132 return false;
133 }
134 }
135
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
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700212// A chunk of text in the XML string within a CDATA tags.
213class CdataSegmentNode : public SegmentNode {
214 public:
215
216 void Build(StringBuilder* builder) const override {
217 builder->AppendText(data, /* preserve_spaces */ true);
218 }
219};
220
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800221// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
222class SpanNode : public Node {
223 public:
224 std::string name;
225
226 void Build(StringBuilder* builder) const override {
227 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
228 Node::Build(builder);
229 builder->EndSpan(span_handle);
230 }
231};
232
233// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
234class UntranslatableNode : public Node {
235 public:
236 void Build(StringBuilder* builder) const override {
237 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
238 Node::Build(builder);
239 builder->EndUntranslatable(handle);
240 }
241};
242
243// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800244bool ResourceParser::FlattenXmlSubtree(
245 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
246 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800247 std::string raw_string;
248 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800249
250 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
251 Maybe<size_t> untranslatable_start_depth;
252
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800253 Node root;
254 std::vector<Node*> node_stack;
255 node_stack.push_back(&root);
256
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700257 bool cdata_block = false;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800258 bool saw_span_node = false;
259 SegmentNode* first_segment = nullptr;
260 SegmentNode* last_segment = nullptr;
261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800263 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800265
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800266 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700267 if (event == xml::XmlPullParser::Event::kStartElement
268 || event == xml::XmlPullParser::Event::kEndElement
269 || event == xml::XmlPullParser::Event::kCdataStart
270 || event == xml::XmlPullParser::Event::kCdataEnd) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800271 if (!current_text.empty()) {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700272 std::unique_ptr<SegmentNode> segment_node = (cdata_block)
273 ? util::make_unique<CdataSegmentNode>() : util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800274 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700275
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800276 last_segment = node_stack.back()->AddChild(std::move(segment_node));
277 if (first_segment == nullptr) {
278 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800279 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800280 current_text = {};
281 }
282 }
Adam Lesinski75421622017-01-06 15:20:04 -0800283
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800284 switch (event) {
285 case xml::XmlPullParser::Event::kText: {
286 current_text += parser->text();
287 raw_string += parser->text();
288 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800289
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800290 case xml::XmlPullParser::Event::kStartElement: {
291 if (parser->element_namespace().empty()) {
292 // This is an HTML tag which we encode as a span. Add it to the span stack.
293 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
294 span_node->name = parser->element_name();
295 const auto end_attr_iter = parser->end_attributes();
296 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
297 ++attr_iter) {
298 span_node->name += ";";
299 span_node->name += attr_iter->name;
300 span_node->name += "=";
301 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800302 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800303
304 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
305 saw_span_node = true;
306 } else if (parser->element_namespace() == sXliffNamespaceUri) {
307 // This is an XLIFF tag, which is not encoded as a span.
308 if (parser->element_name() == "g") {
309 // Check that an 'untranslatable' tag is not already being processed. Nested
310 // <xliff:g> tags are illegal.
311 if (untranslatable_start_depth) {
312 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
313 << "illegal nested XLIFF 'g' tag");
314 return false;
315 } else {
316 // Mark the beginning of an 'untranslatable' section.
317 untranslatable_start_depth = depth;
318 node_stack.push_back(
319 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
320 }
321 } else {
322 // Ignore unknown XLIFF tags, but don't warn.
323 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
324 }
325 } else {
326 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
327 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
328 << "ignoring element '" << parser->element_name()
329 << "' with unknown namespace '" << parser->element_namespace() << "'");
330 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800331 }
Adam Lesinski75421622017-01-06 15:20:04 -0800332
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800333 // Enter one level inside the element.
334 depth++;
335 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700336
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800337 case xml::XmlPullParser::Event::kEndElement: {
338 // Return one level from within the element.
339 depth--;
340 if (depth == 0) {
341 break;
342 }
343
344 node_stack.pop_back();
345 if (untranslatable_start_depth == make_value(depth)) {
346 // This is the end of an untranslatable section.
347 untranslatable_start_depth = {};
348 }
349 } break;
350
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700351 case xml::XmlPullParser::Event::kCdataStart: {
352 cdata_block = true;
353 break;
354 }
355
356 case xml::XmlPullParser::Event::kCdataEnd: {
357 cdata_block = false;
358 break;
359 }
360
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800361 default:
362 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 }
365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800367 // Sanity check to make sure we processed all the nodes.
368 CHECK(node_stack.size() == 1u);
369 CHECK(node_stack.back() == &root);
370
371 if (!saw_span_node) {
372 // If there were no spans, we must treat this string a little differently (according to AAPT).
373 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
374 // from the last segment.
375 if (first_segment != nullptr) {
376 // Trim leading whitespace.
377 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
378 if (trimmed.size() != first_segment->data.size()) {
379 first_segment->data = trimmed.to_string();
380 }
381 }
382
383 if (last_segment != nullptr) {
384 // Trim trailing whitespace.
385 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
386 if (trimmed.size() != last_segment->data.size()) {
387 last_segment->data = trimmed.to_string();
388 }
389 }
390 }
391
392 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
393 // care of recording the correctly adjusted Spans and UntranslatableSections.
394 StringBuilder builder;
395 root.Build(&builder);
396 if (!builder) {
397 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
398 return false;
399 }
400
401 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
402 *out_raw_string = std::move(raw_string);
403 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
404 out_style_string->str = std::move(flattened_string.text);
405 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800406 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800407}
408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 const size_t depth = parser->depth();
412 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
413 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 // Skip comments and text.
415 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800416 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417
Adam Lesinski060b53d2017-07-28 17:10:35 -0700418 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 << "root element must be <resources>");
421 return false;
422 }
423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 break;
426 };
427
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
429 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
430 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 return false;
432 }
433 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800434}
435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
437 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700438
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 bool error = false;
440 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 const size_t depth = parser->depth();
442 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
443 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800447 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700448
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 if (!util::TrimWhitespace(parser->text()).empty()) {
451 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 << "plain text not allowed here");
453 error = true;
454 }
455 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700456 }
457
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 // Skip unknown namespace.
462 continue;
463 }
464
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 std::string element_name = parser->element_name();
466 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 comment = "";
468 continue;
469 }
470
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 ParsedResource parsed_resource;
472 parsed_resource.config = config_;
473 parsed_resource.source = source_.WithLine(parser->line_number());
474 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100475 if (options_.visibility) {
476 parsed_resource.visibility_level = options_.visibility.value();
477 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478
479 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700480 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800481 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 }
483
484 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 error = true;
487 continue;
488 }
489
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 error = true;
492 }
493 }
494
495 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 for (const ResourceName& stripped_resource : stripped_resources) {
497 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700499 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
500 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 error = true;
502 }
503 }
504
505 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800506}
507
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
509 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 struct ItemTypeFormat {
511 ResourceType type;
512 uint32_t format;
513 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800514
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
516 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800517
Adam Lesinski86d67df2017-01-31 13:47:27 -0800518 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
519 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
520 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
521 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
522 {"dimen",
523 {ResourceType::kDimen,
524 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
525 android::ResTable_map::TYPE_DIMENSION}},
526 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
527 {"fraction",
528 {ResourceType::kFraction,
529 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
530 android::ResTable_map::TYPE_DIMENSION}},
531 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
532 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
533 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800534
Adam Lesinski86d67df2017-01-31 13:47:27 -0800535 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
536 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
537 {"array", std::mem_fn(&ResourceParser::ParseArray)},
538 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
539 {"configVarying",
540 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
541 std::placeholders::_2, std::placeholders::_3)},
542 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
543 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
544 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700545 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800546 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
547 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
548 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
549 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
550 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
551 std::placeholders::_2, std::placeholders::_3)},
552 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
553 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800556
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800559
Adam Lesinski86d67df2017-01-31 13:47:27 -0800560 bool can_be_item = true;
561 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800563 can_be_bag = false;
564
Adam Lesinskie597d682017-06-01 17:16:44 -0700565 // The default format for <item> is any. If a format attribute is present, that one will
566 // override the default.
567 resource_format = android::ResTable_map::TYPE_ANY;
568
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700570 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800571 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 << "<item> must have a 'type' attribute");
575 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800576 }
577
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700578 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700580 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700582 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if (!resource_format) {
584 diag_->Error(DiagMessage(out_resource->source)
585 << "'" << maybe_format.value()
586 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800587 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 }
589 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800590 } else if (resource_type == "bag") {
591 can_be_item = false;
592
593 // Bags have their type encoded in the type attribute.
594 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
595 resource_type = maybe_type.value().to_string();
596 } else {
597 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
598 << "<bag> must have a 'type' attribute");
599 return false;
600 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 }
602
603 // Get the name of the resource. This will be checked later, because not all
604 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 if (resource_type == "id") {
608 if (!maybe_name) {
609 diag_->Error(DiagMessage(out_resource->source)
610 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 << "> missing 'name' attribute");
612 return false;
613 }
614
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800616 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700617
618 // Ids either represent a unique resource id or reference another resource id
619 auto item = ParseItem(parser, out_resource, resource_format);
620 if (!item) {
621 return false;
622 }
623
624 String* empty = ValueCast<String>(out_resource->value.get());
625 if (empty && *empty->value == "") {
626 // If no inner element exists, represent a unique identifier
627 out_resource->value = util::make_unique<Id>();
628 } else {
y9efbbef2018-04-18 11:29:09 -0700629 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700630 if (ref && !ref->name && !ref->id) {
631 // A null reference also means there is no inner element when ids are in the form:
632 // <id name="name"/>
633 out_resource->value = util::make_unique<Id>();
634 } else if (!ref || ref->name.value().type != ResourceType::kId) {
635 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700636 diag_->Error(DiagMessage(out_resource->source)
637 << "<" << parser->element_name()
638 << "> inner element must either be a resource reference or empty");
639 return false;
640 }
641 }
642
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 return true;
644 }
645
Adam Lesinski86d67df2017-01-31 13:47:27 -0800646 if (can_be_item) {
647 const auto item_iter = elToItemMap.find(resource_type);
648 if (item_iter != elToItemMap.end()) {
649 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650
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.type = item_iter->second.type;
658 out_resource->name.entry = maybe_name.value().to_string();
659
Adam Lesinskie597d682017-06-01 17:16:44 -0700660 // Only use the implied format of the type when there is no explicit format.
661 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800662 resource_format = item_iter->second.format;
663 }
664
665 if (!ParseItem(parser, out_resource, resource_format)) {
666 return false;
667 }
668 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 }
671
672 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800673 if (can_be_bag) {
674 const auto bag_iter = elToBagMap.find(resource_type);
675 if (bag_iter != elToBagMap.end()) {
676 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700677 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800678 if (!maybe_name) {
679 diag_->Error(DiagMessage(out_resource->source)
680 << "<" << parser->element_name() << "> missing 'name' attribute");
681 return false;
682 }
683
684 out_resource->name.entry = maybe_name.value().to_string();
685 }
686
687 // Call the associated parse method. The type will be filled in by the
688 // parse func.
689 if (!bag_iter->second(this, parser, out_resource)) {
690 return false;
691 }
692 return true;
693 }
694 }
695
696 if (can_be_item) {
697 // Try parsing the elementName (or type) as a resource. These shall only be
698 // resources like 'layout' or 'xml' and they can only be references.
699 const ResourceType* parsed_type = ParseResourceType(resource_type);
700 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 if (!maybe_name) {
702 diag_->Error(DiagMessage(out_resource->source)
703 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 << "> missing 'name' attribute");
705 return false;
706 }
707
Adam Lesinski86d67df2017-01-31 13:47:27 -0800708 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800709 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800710 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
711 if (!out_resource->value) {
712 diag_->Error(DiagMessage(out_resource->source)
713 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
714 return false;
715 }
716 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 }
719
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 diag_->Warn(DiagMessage(out_resource->source)
721 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 return false;
723}
724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
726 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 const uint32_t format) {
728 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 out_resource->value = ParseXml(parser, format, kNoRawString);
733 if (!out_resource->value) {
734 diag_->Error(DiagMessage(out_resource->source) << "invalid "
735 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 return false;
737 }
738 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800739}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800740
741/**
742 * Reads the entire XML subtree and attempts to parse it as some Item,
743 * with typeMask denoting which items it can be. If allowRawValue is
744 * true, a RawString is returned if the XML couldn't be parsed as
745 * an Item. If allowRawValue is false, nullptr is returned in this
746 * case.
747 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
749 const uint32_t type_mask,
750 const bool allow_raw_value) {
751 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800752
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 std::string raw_value;
754 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800755 std::vector<UntranslatableSection> untranslatable_sections;
756 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800757 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 }
759
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800762 std::unique_ptr<StyledString> styled_string =
763 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700764 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800765 styled_string->untranslatable_sections = std::move(untranslatable_sections);
766 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 }
768
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700769 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 // name.package can be empty here, as it will assume the package name of the
771 // table.
772 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700773 id->SetSource(source_.WithLine(begin_xml_line));
774 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 };
776
777 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800779 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700780 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700782 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700783 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700785 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 }
787
788 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800791 std::unique_ptr<String> string = util::make_unique<String>(
792 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
793 string->untranslatable_sections = std::move(untranslatable_sections);
794 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 }
796
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700797 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
798 if (util::TrimWhitespace(raw_value).empty()) {
799 return ResourceUtils::MakeNull();
800 }
801
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 // We can't parse this so return a RawString if we are allowed.
804 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700805 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
806 StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807 }
808 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800809}
810
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811bool ResourceParser::ParseString(xml::XmlPullParser* parser,
812 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700814 if (Maybe<StringPiece> formatted_attr =
815 xml::FindAttribute(parser, "formatted")) {
816 Maybe<bool> maybe_formatted =
817 ResourceUtils::ParseBool(formatted_attr.value());
818 if (!maybe_formatted) {
819 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 << "invalid value for 'formatted'. Must be a boolean");
821 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800822 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800825
Adam Lesinski75421622017-01-06 15:20:04 -0800826 bool translatable = options_.translatable;
827 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
828 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
829 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 << "invalid value for 'translatable'. Must be a boolean");
832 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800833 }
Adam Lesinski75421622017-01-06 15:20:04 -0800834 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800836
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 out_resource->value =
838 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
839 if (!out_resource->value) {
840 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700841 return false;
842 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800843
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700844 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800845 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800846
Adam Lesinski75421622017-01-06 15:20:04 -0800847 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848 if (!util::VerifyJavaStringFormat(*string_value->value)) {
849 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 msg << "multiple substitutions specified in non-positional format; "
851 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700852 if (options_.error_on_positional_arguments) {
853 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700854 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800855 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800859 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860
Adam Lesinski75421622017-01-06 15:20:04 -0800861 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
862 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 }
864 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800865}
866
Adam Lesinski71be7052017-12-12 16:48:07 -0800867bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100868 if (options_.visibility) {
869 diag_->Error(DiagMessage(out_resource->source)
870 << "<public> tag not allowed with --visibility flag");
871 return false;
872 }
873
Adam Lesinski46c4d722017-08-23 13:03:56 -0700874 if (out_resource->config != ConfigDescription::DefaultConfig()) {
875 diag_->Warn(DiagMessage(out_resource->source)
876 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
877 }
878
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
880 if (!maybe_type) {
881 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 << "<public> must have a 'type' attribute");
883 return false;
884 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800885
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700886 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
887 if (!parsed_type) {
888 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
889 << maybe_type.value()
890 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891 return false;
892 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800895
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800896 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
897 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800899 diag_->Error(DiagMessage(out_resource->source)
900 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800902 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800905
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700908 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700909 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700910
Adam Lesinski71be7052017-12-12 16:48:07 -0800911 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700912 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800913}
914
Adam Lesinski46c4d722017-08-23 13:03:56 -0700915bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100916 if (options_.visibility) {
917 diag_->Error(DiagMessage(out_resource->source)
918 << "<public-group> tag not allowed with --visibility flag");
919 return false;
920 }
921
Adam Lesinski46c4d722017-08-23 13:03:56 -0700922 if (out_resource->config != ConfigDescription::DefaultConfig()) {
923 diag_->Warn(DiagMessage(out_resource->source)
924 << "ignoring configuration '" << out_resource->config
925 << "' for <public-group> tag");
926 }
927
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
929 if (!maybe_type) {
930 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800932 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700933 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800934
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700935 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
936 if (!parsed_type) {
937 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
938 << maybe_type.value()
939 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800940 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941 }
942
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700943 Maybe<StringPiece> maybe_id_str =
944 xml::FindNonEmptyAttribute(parser, "first-id");
945 if (!maybe_id_str) {
946 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700947 << "<public-group> must have a 'first-id' attribute");
948 return false;
949 }
950
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 Maybe<ResourceId> maybe_id =
952 ResourceUtils::ParseResourceId(maybe_id_str.value());
953 if (!maybe_id) {
954 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
955 << maybe_id_str.value()
956 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 return false;
958 }
959
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700960 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700961
962 std::string comment;
963 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 const size_t depth = parser->depth();
965 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
966 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800967 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700969 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970 // Skip text.
971 continue;
972 }
973
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700974 const Source item_source = source_.WithLine(parser->line_number());
975 const std::string& element_namespace = parser->element_namespace();
976 const std::string& element_name = parser->element_name();
977 if (element_namespace.empty() && element_name == "public") {
978 Maybe<StringPiece> maybe_name =
979 xml::FindNonEmptyAttribute(parser, "name");
980 if (!maybe_name) {
981 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 << "<public> must have a 'name' attribute");
983 error = true;
984 continue;
985 }
986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 if (xml::FindNonEmptyAttribute(parser, "id")) {
988 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 << "'id' is ignored within <public-group>");
990 error = true;
991 continue;
992 }
993
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700994 if (xml::FindNonEmptyAttribute(parser, "type")) {
995 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700996 << "'type' is ignored within <public-group>");
997 error = true;
998 continue;
999 }
1000
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001001 ParsedResource child_resource;
1002 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -08001003 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001004 child_resource.id = next_id;
1005 child_resource.comment = std::move(comment);
1006 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -08001007 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001012 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1013 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 error = true;
1015 }
1016 }
1017 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001018}
1019
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001020bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1021 ParsedResource* out_resource) {
1022 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1023 if (!maybe_type) {
1024 diag_->Error(DiagMessage(out_resource->source)
1025 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 << "> must have a 'type' attribute");
1027 return false;
1028 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001029
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001030 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1031 if (!parsed_type) {
1032 diag_->Error(DiagMessage(out_resource->source)
1033 << "invalid resource type '" << maybe_type.value() << "' in <"
1034 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 return false;
1036 }
1037
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001040}
1041
Adam Lesinski46c4d722017-08-23 13:03:56 -07001042bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001043 if (options_.visibility) {
1044 diag_->Error(DiagMessage(out_resource->source)
1045 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1046 return false;
1047 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001048 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1049 diag_->Warn(DiagMessage(out_resource->source)
1050 << "ignoring configuration '" << out_resource->config << "' for <"
1051 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001052 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001053
1054 if (!ParseSymbolImpl(parser, out_resource)) {
1055 return false;
1056 }
1057
Adam Lesinski71be7052017-12-12 16:48:07 -08001058 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001059 return true;
1060}
1061
1062bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1063 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1064 diag_->Warn(DiagMessage(out_resource->source)
1065 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
1066 }
1067
1068 if (Maybe<StringPiece> maybe_policy = xml::FindNonEmptyAttribute(parser, "policy")) {
1069 const StringPiece& policy = maybe_policy.value();
1070 if (policy != "system") {
1071 diag_->Error(DiagMessage(out_resource->source)
1072 << "<overlayable> has invalid policy '" << policy << "'");
1073 return false;
1074 }
1075 }
1076
1077 bool error = false;
1078 const size_t depth = parser->depth();
1079 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1080 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1081 // Skip text/comments.
1082 continue;
1083 }
1084
1085 const Source item_source = source_.WithLine(parser->line_number());
1086 const std::string& element_namespace = parser->element_namespace();
1087 const std::string& element_name = parser->element_name();
1088 if (element_namespace.empty() && element_name == "item") {
1089 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1090 if (!maybe_name) {
1091 diag_->Error(DiagMessage(item_source)
1092 << "<item> within an <overlayable> tag must have a 'name' attribute");
1093 error = true;
1094 continue;
1095 }
1096
1097 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1098 if (!maybe_type) {
1099 diag_->Error(DiagMessage(item_source)
1100 << "<item> within an <overlayable> tag must have a 'type' attribute");
1101 error = true;
1102 continue;
1103 }
1104
1105 const ResourceType* type = ParseResourceType(maybe_type.value());
1106 if (type == nullptr) {
1107 diag_->Error(DiagMessage(out_resource->source)
1108 << "invalid resource type '" << maybe_type.value()
1109 << "' in <item> within an <overlayable>");
1110 error = true;
1111 continue;
1112 }
1113
Adam Lesinski71be7052017-12-12 16:48:07 -08001114 ParsedResource child_resource;
1115 child_resource.name.type = *type;
1116 child_resource.name.entry = maybe_name.value().to_string();
1117 child_resource.source = item_source;
1118 child_resource.overlayable = true;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001119 if (options_.visibility) {
1120 child_resource.visibility_level = options_.visibility.value();
1121 }
Adam Lesinski71be7052017-12-12 16:48:07 -08001122 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001123
1124 xml::XmlPullParser::SkipCurrentElement(parser);
1125 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1126 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
1127 error = true;
1128 }
1129 }
1130 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001131}
1132
Adam Lesinski71be7052017-12-12 16:48:07 -08001133bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001134 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001135 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001136 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001137 return true;
1138 }
1139 return false;
1140}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001141
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001142bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1143 ParsedResource* out_resource) {
1144 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001145}
1146
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001147bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1148 ParsedResource* out_resource, bool weak) {
1149 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150
1151 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1153 diag_->Warn(DiagMessage(out_resource->source)
1154 << "ignoring configuration '" << out_resource->config
1155 << "' for attribute " << out_resource->name);
1156 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 }
1158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001160
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001161 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1162 if (maybe_format) {
1163 type_mask = ParseFormatAttribute(maybe_format.value());
1164 if (type_mask == 0) {
1165 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001166 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001167 return false;
1168 }
1169 }
1170
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001171 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001172
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001173 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1174 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1175 if (!min_str.empty()) {
1176 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001177 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001178 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001179 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001180 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001181 }
1182
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001183 if (!maybe_min) {
1184 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1185 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001186 return false;
1187 }
1188 }
1189
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001190 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1191 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1192 if (!max_str.empty()) {
1193 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001194 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001195 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001197 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001198 }
1199
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001200 if (!maybe_max) {
1201 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1202 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 return false;
1204 }
1205 }
1206
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001207 if ((maybe_min || maybe_max) &&
1208 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1209 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210 << "'min' and 'max' can only be used when format='integer'");
1211 return false;
1212 }
1213
1214 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001215 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001216 return a.symbol.name.value() < b.symbol.name.value();
1217 }
1218 };
1219
1220 std::set<Attribute::Symbol, SymbolComparator> items;
1221
1222 std::string comment;
1223 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 const size_t depth = parser->depth();
1225 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1226 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001227 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 // Skip text.
1231 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001232 }
1233
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001234 const Source item_source = source_.WithLine(parser->line_number());
1235 const std::string& element_namespace = parser->element_namespace();
1236 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001237 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001238 if (element_name == "enum") {
1239 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1240 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001241 << "can not define an <enum>; already defined a <flag>");
1242 error = true;
1243 continue;
1244 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001245 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 } else if (element_name == "flag") {
1248 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1249 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 << "can not define a <flag>; already defined an <enum>");
1251 error = true;
1252 continue;
1253 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255 }
1256
1257 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001258 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001259 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001260 ParsedResource child_resource;
1261 child_resource.name = symbol.symbol.name.value();
1262 child_resource.source = item_source;
1263 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001264 if (options_.visibility) {
1265 child_resource.visibility_level = options_.visibility.value();
1266 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001267 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001268
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001269 symbol.symbol.SetComment(std::move(comment));
1270 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272 auto insert_result = items.insert(std::move(symbol));
1273 if (!insert_result.second) {
1274 const Attribute::Symbol& existing_symbol = *insert_result.first;
1275 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 << "first defined here");
1281 error = true;
1282 }
1283 } else {
1284 error = true;
1285 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1287 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001288 error = true;
1289 }
1290
1291 comment = {};
1292 }
1293
1294 if (error) {
1295 return false;
1296 }
1297
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001298 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1299 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1300 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001302 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1303 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305 return true;
1306}
1307
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001311
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1313 if (!maybe_name) {
1314 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 << tag << ">");
1316 return {};
1317 }
1318
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1320 if (!maybe_value) {
1321 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001322 << tag << ">");
1323 return {};
1324 }
1325
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001326 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 android::Res_value val;
1328 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001330 << "' for <" << tag
1331 << ">; must be an integer");
1332 return {};
1333 }
1334
1335 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001336 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001337 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001338}
1339
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1341 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001342
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1344 if (!maybe_name) {
1345 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 return false;
1347 }
1348
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001349 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001351 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352 return false;
1353 }
1354
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001355 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001357
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 return false;
1362 }
1363
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001364 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 return true;
1366}
1367
Adam Lesinski86d67df2017-01-31 13:47:27 -08001368bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001369 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001370 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371
1372 std::unique_ptr<Style> style = util::make_unique<Style>();
1373
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001374 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1375 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001376 // 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 -07001377 if (!maybe_parent.value().empty()) {
1378 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001379 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001380 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001381 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001382 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001383 }
1384
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001385 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001387 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001388 }
1389
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390 } else {
1391 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001392 std::string style_name = out_resource->name.entry;
1393 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001394 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001395 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001396 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001397 }
1398 }
1399
1400 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001401 const size_t depth = parser->depth();
1402 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1403 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 // Skip text and comments.
1405 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001406 }
1407
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001408 const std::string& element_namespace = parser->element_namespace();
1409 const std::string& element_name = parser->element_name();
1410 if (element_namespace == "" && element_name == "item") {
1411 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001412
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001413 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1414 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1415 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001417 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001419
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 if (error) {
1421 return false;
1422 }
1423
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001424 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001425 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001426}
1427
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001428bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1429 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1430 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1431 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1432 if (resource_format == 0u) {
1433 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1434 << "'" << format_attr.value() << "' is an invalid format");
1435 return false;
1436 }
1437 }
1438 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001439}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001440
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001441bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1442 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001443}
1444
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001445bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1446 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001447}
1448
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001449bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1450 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001451 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001452 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001453
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001455
Adam Lesinski75421622017-01-06 15:20:04 -08001456 bool translatable = options_.translatable;
1457 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1458 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1459 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001460 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 << "invalid value for 'translatable'. Must be a boolean");
1462 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001463 }
Adam Lesinski75421622017-01-06 15:20:04 -08001464 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001465 }
Adam Lesinski75421622017-01-06 15:20:04 -08001466 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001467
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001469 const size_t depth = parser->depth();
1470 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1471 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001472 // Skip text and comments.
1473 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001474 }
1475
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001476 const Source item_source = source_.WithLine(parser->line_number());
1477 const std::string& element_namespace = parser->element_namespace();
1478 const std::string& element_name = parser->element_name();
1479 if (element_namespace.empty() && element_name == "item") {
1480 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001481 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001482 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001483 error = true;
1484 continue;
1485 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001486 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001487 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001488
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001489 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1490 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1491 << "unknown tag <" << element_namespace << ":"
1492 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001493 error = true;
1494 }
1495 }
1496
1497 if (error) {
1498 return false;
1499 }
1500
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001501 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001503}
1504
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001505bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1506 ParsedResource* out_resource) {
1507 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001508
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001510
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001511 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001512 const size_t depth = parser->depth();
1513 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1514 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001515 // Skip text and comments.
1516 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001517 }
1518
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519 const Source item_source = source_.WithLine(parser->line_number());
1520 const std::string& element_namespace = parser->element_namespace();
1521 const std::string& element_name = parser->element_name();
1522 if (element_namespace.empty() && element_name == "item") {
1523 Maybe<StringPiece> maybe_quantity =
1524 xml::FindNonEmptyAttribute(parser, "quantity");
1525 if (!maybe_quantity) {
1526 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 << "<item> in <plurals> requires attribute "
1528 << "'quantity'");
1529 error = true;
1530 continue;
1531 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001532
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001533 StringPiece trimmed_quantity =
1534 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001537 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001542 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001546 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001547 index = Plural::Other;
1548 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001551 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001552 error = true;
1553 continue;
1554 }
1555
1556 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001557 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1558 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001559 error = true;
1560 continue;
1561 }
1562
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001563 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001564 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1565 error = true;
1566 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001567 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001568
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001569 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1570 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1571 << element_namespace << ":"
1572 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001573 error = true;
1574 }
1575 }
1576
1577 if (error) {
1578 return false;
1579 }
1580
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001581 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001582 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001583}
1584
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001585bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1586 ParsedResource* out_resource) {
1587 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001588
Adam Lesinski71be7052017-12-12 16:48:07 -08001589 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1590 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001591
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001592 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001593 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1594 diag_->Warn(DiagMessage(out_resource->source)
1595 << "ignoring configuration '" << out_resource->config
1596 << "' for styleable " << out_resource->name.entry);
1597 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001598 }
1599
1600 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1601
1602 std::string comment;
1603 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 const size_t depth = parser->depth();
1605 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1606 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001607 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001609 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001610 // Ignore text.
1611 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001612 }
1613
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001614 const Source item_source = source_.WithLine(parser->line_number());
1615 const std::string& element_namespace = parser->element_namespace();
1616 const std::string& element_name = parser->element_name();
1617 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001618 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001619 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001620 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001621 error = true;
1622 continue;
1623 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001624
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001625 // If this is a declaration, the package name may be in the name. Separate
1626 // these out.
1627 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001628 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 if (!maybe_ref) {
1630 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1631 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001632 error = true;
1633 continue;
1634 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001635
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001636 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001637 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001638
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001639 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001640 ParsedResource child_resource;
1641 child_resource.name = child_ref.name.value();
1642 child_resource.source = item_source;
1643 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001644 if (options_.visibility) {
1645 child_resource.visibility_level = options_.visibility.value();
1646 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001647
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001648 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001649 error = true;
1650 continue;
1651 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001652
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001654 child_ref.SetComment(child_resource.comment);
1655 child_ref.SetSource(item_source);
1656 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001657
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001659
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001660 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1661 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1662 << element_namespace << ":"
1663 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001664 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001665 }
1666
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001667 comment = {};
1668 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001669
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001670 if (error) {
1671 return false;
1672 }
1673
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001674 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001675 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001676}
1677
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001678} // namespace aapt