blob: f45748f7d10470c9cbbf79c2abb62e92d8a987a3 [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;
Adam Lesinski71be7052017-12-12 16:48:07 -080037using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080038
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039namespace aapt {
40
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070041constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070043// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
44static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080046}
47
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070048static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
49 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070051 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070053 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070055 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070057 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070059 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070061 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070063 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070065 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080067}
68
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070069static uint32_t ParseFormatType(const StringPiece& piece) {
70 if (piece == "enum") {
71 return android::ResTable_map::TYPE_ENUM;
72 } else if (piece == "flags") {
73 return android::ResTable_map::TYPE_FLAGS;
74 }
75 return ParseFormatTypeNoEnumsOrFlags(piece);
76}
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 for (StringPiece part : util::Tokenize(str, '|')) {
81 StringPiece trimmed_part = util::TrimWhitespace(part);
82 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 if (type == 0) {
84 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080085 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 mask |= type;
87 }
88 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080089}
90
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070091// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080092struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 ResourceName name;
94 ConfigDescription config;
95 std::string product;
96 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -080099 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700100 bool allow_new = false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800101 bool overlayable = false;
102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 std::string comment;
104 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800106};
107
108// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700109static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
111 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800113 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800115
Adam Lesinski71be7052017-12-12 16:48:07 -0800116 if (res->visibility_level != Visibility::Level::kUndefined) {
117 Visibility visibility;
118 visibility.level = res->visibility_level;
119 visibility.source = res->source;
120 visibility.comment = res->comment;
121 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800123 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800125
Adam Lesinski71be7052017-12-12 16:48:07 -0800126 if (res->allow_new) {
127 AllowNew allow_new;
128 allow_new.source = res->source;
129 allow_new.comment = res->comment;
130 if (!table->SetAllowNew(res->name, allow_new, diag)) {
131 return false;
132 }
133 }
134
135 if (res->overlayable) {
136 Overlayable overlayable;
137 overlayable.source = res->source;
138 overlayable.comment = res->comment;
139 if (!table->SetOverlayable(res->name, overlayable, diag)) {
140 return false;
141 }
142 }
143
144 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 res->value->SetComment(std::move(res->comment));
147 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800148
Adam Lesinski71be7052017-12-12 16:48:07 -0800149 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
150 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800152 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 for (ParsedResource& child : res->child_resources) {
157 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
159 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800160}
161
162// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
166 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700167 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 : diag_(diag),
170 table_(table),
171 source_(source),
172 config_(config),
173 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800175// Base class Node for representing the various Spans and UntranslatableSections of an XML string.
176// This will be used to traverse and flatten the XML string into a single std::string, with all
177// Span and Untranslatable data maintained in parallel, as indices into the string.
178class Node {
179 public:
180 virtual ~Node() = default;
181
182 // Adds the given child node to this parent node's set of child nodes, moving ownership to the
183 // parent node as well.
184 // Returns a pointer to the child node that was added as a convenience.
185 template <typename T>
186 T* AddChild(std::unique_ptr<T> node) {
187 T* raw_ptr = node.get();
188 children.push_back(std::move(node));
189 return raw_ptr;
190 }
191
192 virtual void Build(StringBuilder* builder) const {
193 for (const auto& child : children) {
194 child->Build(builder);
195 }
196 }
197
198 std::vector<std::unique_ptr<Node>> children;
199};
200
201// A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans.
202class SegmentNode : public Node {
203 public:
204 std::string data;
205
206 void Build(StringBuilder* builder) const override {
207 builder->AppendText(data);
208 }
209};
210
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700211// A chunk of text in the XML string within a CDATA tags.
212class CdataSegmentNode : public SegmentNode {
213 public:
214
215 void Build(StringBuilder* builder) const override {
216 builder->AppendText(data, /* preserve_spaces */ true);
217 }
218};
219
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800220// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
221class SpanNode : public Node {
222 public:
223 std::string name;
224
225 void Build(StringBuilder* builder) const override {
226 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
227 Node::Build(builder);
228 builder->EndSpan(span_handle);
229 }
230};
231
232// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
233class UntranslatableNode : public Node {
234 public:
235 void Build(StringBuilder* builder) const override {
236 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
237 Node::Build(builder);
238 builder->EndUntranslatable(handle);
239 }
240};
241
242// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800243bool ResourceParser::FlattenXmlSubtree(
244 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
245 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800246 std::string raw_string;
247 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800248
249 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
250 Maybe<size_t> untranslatable_start_depth;
251
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800252 Node root;
253 std::vector<Node*> node_stack;
254 node_stack.push_back(&root);
255
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700256 bool cdata_block = false;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800257 bool saw_span_node = false;
258 SegmentNode* first_segment = nullptr;
259 SegmentNode* last_segment = nullptr;
260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800262 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800264
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800265 // First take care of any SegmentNodes that should be created.
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700266 if (event == xml::XmlPullParser::Event::kStartElement
267 || event == xml::XmlPullParser::Event::kEndElement
268 || event == xml::XmlPullParser::Event::kCdataStart
269 || event == xml::XmlPullParser::Event::kCdataEnd) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800270 if (!current_text.empty()) {
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700271 std::unique_ptr<SegmentNode> segment_node = (cdata_block)
272 ? util::make_unique<CdataSegmentNode>() : util::make_unique<SegmentNode>();
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800273 segment_node->data = std::move(current_text);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700274
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800275 last_segment = node_stack.back()->AddChild(std::move(segment_node));
276 if (first_segment == nullptr) {
277 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800278 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800279 current_text = {};
280 }
281 }
Adam Lesinski75421622017-01-06 15:20:04 -0800282
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800283 switch (event) {
284 case xml::XmlPullParser::Event::kText: {
285 current_text += parser->text();
286 raw_string += parser->text();
287 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800288
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800289 case xml::XmlPullParser::Event::kStartElement: {
290 if (parser->element_namespace().empty()) {
291 // This is an HTML tag which we encode as a span. Add it to the span stack.
292 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
293 span_node->name = parser->element_name();
294 const auto end_attr_iter = parser->end_attributes();
295 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
296 ++attr_iter) {
297 span_node->name += ";";
298 span_node->name += attr_iter->name;
299 span_node->name += "=";
300 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800301 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800302
303 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
304 saw_span_node = true;
305 } else if (parser->element_namespace() == sXliffNamespaceUri) {
306 // This is an XLIFF tag, which is not encoded as a span.
307 if (parser->element_name() == "g") {
308 // Check that an 'untranslatable' tag is not already being processed. Nested
309 // <xliff:g> tags are illegal.
310 if (untranslatable_start_depth) {
311 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
312 << "illegal nested XLIFF 'g' tag");
313 return false;
314 } else {
315 // Mark the beginning of an 'untranslatable' section.
316 untranslatable_start_depth = depth;
317 node_stack.push_back(
318 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
319 }
320 } else {
321 // Ignore unknown XLIFF tags, but don't warn.
322 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
323 }
324 } else {
325 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
326 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
327 << "ignoring element '" << parser->element_name()
328 << "' with unknown namespace '" << parser->element_namespace() << "'");
329 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800330 }
Adam Lesinski75421622017-01-06 15:20:04 -0800331
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800332 // Enter one level inside the element.
333 depth++;
334 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800336 case xml::XmlPullParser::Event::kEndElement: {
337 // Return one level from within the element.
338 depth--;
339 if (depth == 0) {
340 break;
341 }
342
343 node_stack.pop_back();
344 if (untranslatable_start_depth == make_value(depth)) {
345 // This is the end of an untranslatable section.
346 untranslatable_start_depth = {};
347 }
348 } break;
349
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700350 case xml::XmlPullParser::Event::kCdataStart: {
351 cdata_block = true;
352 break;
353 }
354
355 case xml::XmlPullParser::Event::kCdataEnd: {
356 cdata_block = false;
357 break;
358 }
359
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800360 default:
361 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 }
364 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800366 // Sanity check to make sure we processed all the nodes.
367 CHECK(node_stack.size() == 1u);
368 CHECK(node_stack.back() == &root);
369
370 if (!saw_span_node) {
371 // If there were no spans, we must treat this string a little differently (according to AAPT).
372 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
373 // from the last segment.
374 if (first_segment != nullptr) {
375 // Trim leading whitespace.
376 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
377 if (trimmed.size() != first_segment->data.size()) {
378 first_segment->data = trimmed.to_string();
379 }
380 }
381
382 if (last_segment != nullptr) {
383 // Trim trailing whitespace.
384 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
385 if (trimmed.size() != last_segment->data.size()) {
386 last_segment->data = trimmed.to_string();
387 }
388 }
389 }
390
391 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
392 // care of recording the correctly adjusted Spans and UntranslatableSections.
393 StringBuilder builder;
394 root.Build(&builder);
395 if (!builder) {
396 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
397 return false;
398 }
399
400 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
401 *out_raw_string = std::move(raw_string);
402 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
403 out_style_string->str = std::move(flattened_string.text);
404 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800405 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800406}
407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 const size_t depth = parser->depth();
411 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
412 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 // Skip comments and text.
414 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800415 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416
Adam Lesinski060b53d2017-07-28 17:10:35 -0700417 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 << "root element must be <resources>");
420 return false;
421 }
422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 break;
425 };
426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
428 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
429 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 return false;
431 }
432 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800433}
434
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
436 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700437
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 bool error = false;
439 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 const size_t depth = parser->depth();
441 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
442 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800446 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700447
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 if (!util::TrimWhitespace(parser->text()).empty()) {
450 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 << "plain text not allowed here");
452 error = true;
453 }
454 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700455 }
456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 // Skip unknown namespace.
461 continue;
462 }
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 std::string element_name = parser->element_name();
465 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 comment = "";
467 continue;
468 }
469
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700470 ParsedResource parsed_resource;
471 parsed_resource.config = config_;
472 parsed_resource.source = source_.WithLine(parser->line_number());
473 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100474 if (options_.visibility) {
475 parsed_resource.visibility_level = options_.visibility.value();
476 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477
478 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700479 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800480 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 }
482
483 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 error = true;
486 continue;
487 }
488
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 error = true;
491 }
492 }
493
494 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 for (const ResourceName& stripped_resource : stripped_resources) {
496 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700498 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
499 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 error = true;
501 }
502 }
503
504 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800505}
506
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
508 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 struct ItemTypeFormat {
510 ResourceType type;
511 uint32_t format;
512 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800513
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
515 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800516
Adam Lesinski86d67df2017-01-31 13:47:27 -0800517 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
518 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
519 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
520 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
521 {"dimen",
522 {ResourceType::kDimen,
523 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
524 android::ResTable_map::TYPE_DIMENSION}},
525 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
526 {"fraction",
527 {ResourceType::kFraction,
528 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
529 android::ResTable_map::TYPE_DIMENSION}},
530 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
531 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
532 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800533
Adam Lesinski86d67df2017-01-31 13:47:27 -0800534 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
535 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
536 {"array", std::mem_fn(&ResourceParser::ParseArray)},
537 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
538 {"configVarying",
539 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
540 std::placeholders::_2, std::placeholders::_3)},
541 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
542 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
543 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700544 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800545 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
546 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
547 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
548 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
549 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
550 std::placeholders::_2, std::placeholders::_3)},
551 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
552 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800553
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800555
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800558
Adam Lesinski86d67df2017-01-31 13:47:27 -0800559 bool can_be_item = true;
560 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800562 can_be_bag = false;
563
Adam Lesinskie597d682017-06-01 17:16:44 -0700564 // The default format for <item> is any. If a format attribute is present, that one will
565 // override the default.
566 resource_format = android::ResTable_map::TYPE_ANY;
567
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700569 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800570 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 << "<item> must have a 'type' attribute");
574 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800575 }
576
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700577 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700579 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700581 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if (!resource_format) {
583 diag_->Error(DiagMessage(out_resource->source)
584 << "'" << maybe_format.value()
585 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800586 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 }
588 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800589 } else if (resource_type == "bag") {
590 can_be_item = false;
591
592 // Bags have their type encoded in the type attribute.
593 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
594 resource_type = maybe_type.value().to_string();
595 } else {
596 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
597 << "<bag> must have a 'type' attribute");
598 return false;
599 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 }
601
602 // Get the name of the resource. This will be checked later, because not all
603 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 if (resource_type == "id") {
607 if (!maybe_name) {
608 diag_->Error(DiagMessage(out_resource->source)
609 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 << "> missing 'name' attribute");
611 return false;
612 }
613
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800615 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700616
617 // Ids either represent a unique resource id or reference another resource id
618 auto item = ParseItem(parser, out_resource, resource_format);
619 if (!item) {
620 return false;
621 }
622
623 String* empty = ValueCast<String>(out_resource->value.get());
624 if (empty && *empty->value == "") {
625 // If no inner element exists, represent a unique identifier
626 out_resource->value = util::make_unique<Id>();
627 } else {
y9efbbef2018-04-18 11:29:09 -0700628 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700629 if (ref && !ref->name && !ref->id) {
630 // A null reference also means there is no inner element when ids are in the form:
631 // <id name="name"/>
632 out_resource->value = util::make_unique<Id>();
633 } else if (!ref || ref->name.value().type != ResourceType::kId) {
634 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700635 diag_->Error(DiagMessage(out_resource->source)
636 << "<" << parser->element_name()
637 << "> inner element must either be a resource reference or empty");
638 return false;
639 }
640 }
641
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 return true;
643 }
644
Adam Lesinski86d67df2017-01-31 13:47:27 -0800645 if (can_be_item) {
646 const auto item_iter = elToItemMap.find(resource_type);
647 if (item_iter != elToItemMap.end()) {
648 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649
Adam Lesinski86d67df2017-01-31 13:47:27 -0800650 if (!maybe_name) {
651 diag_->Error(DiagMessage(out_resource->source)
652 << "<" << parser->element_name() << "> missing 'name' attribute");
653 return false;
654 }
655
656 out_resource->name.type = item_iter->second.type;
657 out_resource->name.entry = maybe_name.value().to_string();
658
Adam Lesinskie597d682017-06-01 17:16:44 -0700659 // Only use the implied format of the type when there is no explicit format.
660 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800661 resource_format = item_iter->second.format;
662 }
663
664 if (!ParseItem(parser, out_resource, resource_format)) {
665 return false;
666 }
667 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
670
671 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800672 if (can_be_bag) {
673 const auto bag_iter = elToBagMap.find(resource_type);
674 if (bag_iter != elToBagMap.end()) {
675 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700676 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800677 if (!maybe_name) {
678 diag_->Error(DiagMessage(out_resource->source)
679 << "<" << parser->element_name() << "> missing 'name' attribute");
680 return false;
681 }
682
683 out_resource->name.entry = maybe_name.value().to_string();
684 }
685
686 // Call the associated parse method. The type will be filled in by the
687 // parse func.
688 if (!bag_iter->second(this, parser, out_resource)) {
689 return false;
690 }
691 return true;
692 }
693 }
694
695 if (can_be_item) {
696 // Try parsing the elementName (or type) as a resource. These shall only be
697 // resources like 'layout' or 'xml' and they can only be references.
698 const ResourceType* parsed_type = ParseResourceType(resource_type);
699 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 if (!maybe_name) {
701 diag_->Error(DiagMessage(out_resource->source)
702 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 << "> missing 'name' attribute");
704 return false;
705 }
706
Adam Lesinski86d67df2017-01-31 13:47:27 -0800707 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800708 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800709 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
710 if (!out_resource->value) {
711 diag_->Error(DiagMessage(out_resource->source)
712 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
713 return false;
714 }
715 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 }
718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 diag_->Warn(DiagMessage(out_resource->source)
720 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 return false;
722}
723
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700724bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
725 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 const uint32_t format) {
727 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 }
730
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 out_resource->value = ParseXml(parser, format, kNoRawString);
732 if (!out_resource->value) {
733 diag_->Error(DiagMessage(out_resource->source) << "invalid "
734 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 return false;
736 }
737 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800738}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800739
740/**
741 * Reads the entire XML subtree and attempts to parse it as some Item,
742 * with typeMask denoting which items it can be. If allowRawValue is
743 * true, a RawString is returned if the XML couldn't be parsed as
744 * an Item. If allowRawValue is false, nullptr is returned in this
745 * case.
746 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
748 const uint32_t type_mask,
749 const bool allow_raw_value) {
750 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800751
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 std::string raw_value;
753 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800754 std::vector<UntranslatableSection> untranslatable_sections;
755 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800756 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757 }
758
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700759 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800761 std::unique_ptr<StyledString> styled_string =
762 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700763 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800764 styled_string->untranslatable_sections = std::move(untranslatable_sections);
765 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 }
767
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 // name.package can be empty here, as it will assume the package name of the
770 // table.
771 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 id->SetSource(source_.WithLine(begin_xml_line));
773 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 };
775
776 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800778 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700779 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700782 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 }
786
787 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700788 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800790 std::unique_ptr<String> string = util::make_unique<String>(
791 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
792 string->untranslatable_sections = std::move(untranslatable_sections);
793 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794 }
795
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700796 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
797 if (util::TrimWhitespace(raw_value).empty()) {
798 return ResourceUtils::MakeNull();
799 }
800
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 // We can't parse this so return a RawString if we are allowed.
803 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700804 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700805 }
806 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800807}
808
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809bool ResourceParser::ParseString(xml::XmlPullParser* parser,
810 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 if (Maybe<StringPiece> formatted_attr =
813 xml::FindAttribute(parser, "formatted")) {
814 Maybe<bool> maybe_formatted =
815 ResourceUtils::ParseBool(formatted_attr.value());
816 if (!maybe_formatted) {
817 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700818 << "invalid value for 'formatted'. Must be a boolean");
819 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800820 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800823
Adam Lesinski75421622017-01-06 15:20:04 -0800824 bool translatable = options_.translatable;
825 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
826 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
827 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700828 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 << "invalid value for 'translatable'. Must be a boolean");
830 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800831 }
Adam Lesinski75421622017-01-06 15:20:04 -0800832 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 out_resource->value =
836 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
837 if (!out_resource->value) {
838 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 return false;
840 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800841
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800843 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800844
Adam Lesinski75421622017-01-06 15:20:04 -0800845 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700846 if (!util::VerifyJavaStringFormat(*string_value->value)) {
847 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 msg << "multiple substitutions specified in non-positional format; "
849 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700850 if (options_.error_on_positional_arguments) {
851 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800853 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800854
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700855 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800857 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858
Adam Lesinski75421622017-01-06 15:20:04 -0800859 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
860 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700861 }
862 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800863}
864
Adam Lesinski71be7052017-12-12 16:48:07 -0800865bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100866 if (options_.visibility) {
867 diag_->Error(DiagMessage(out_resource->source)
868 << "<public> tag not allowed with --visibility flag");
869 return false;
870 }
871
Adam Lesinski46c4d722017-08-23 13:03:56 -0700872 if (out_resource->config != ConfigDescription::DefaultConfig()) {
873 diag_->Warn(DiagMessage(out_resource->source)
874 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
875 }
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
878 if (!maybe_type) {
879 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 << "<public> must have a 'type' attribute");
881 return false;
882 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800883
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700884 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
885 if (!parsed_type) {
886 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
887 << maybe_type.value()
888 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 return false;
890 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800893
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800894 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
895 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800897 diag_->Error(DiagMessage(out_resource->source)
898 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800900 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700901 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800903
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700905 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700906 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700908
Adam Lesinski71be7052017-12-12 16:48:07 -0800909 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800911}
912
Adam Lesinski46c4d722017-08-23 13:03:56 -0700913bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100914 if (options_.visibility) {
915 diag_->Error(DiagMessage(out_resource->source)
916 << "<public-group> tag not allowed with --visibility flag");
917 return false;
918 }
919
Adam Lesinski46c4d722017-08-23 13:03:56 -0700920 if (out_resource->config != ConfigDescription::DefaultConfig()) {
921 diag_->Warn(DiagMessage(out_resource->source)
922 << "ignoring configuration '" << out_resource->config
923 << "' for <public-group> tag");
924 }
925
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700926 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
927 if (!maybe_type) {
928 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700929 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800930 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800932
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700933 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
934 if (!parsed_type) {
935 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
936 << maybe_type.value()
937 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800938 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700939 }
940
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700941 Maybe<StringPiece> maybe_id_str =
942 xml::FindNonEmptyAttribute(parser, "first-id");
943 if (!maybe_id_str) {
944 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 << "<public-group> must have a 'first-id' attribute");
946 return false;
947 }
948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 Maybe<ResourceId> maybe_id =
950 ResourceUtils::ParseResourceId(maybe_id_str.value());
951 if (!maybe_id) {
952 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
953 << maybe_id_str.value()
954 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 return false;
956 }
957
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700958 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700959
960 std::string comment;
961 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 const size_t depth = parser->depth();
963 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
964 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800965 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700966 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968 // Skip text.
969 continue;
970 }
971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972 const Source item_source = source_.WithLine(parser->line_number());
973 const std::string& element_namespace = parser->element_namespace();
974 const std::string& element_name = parser->element_name();
975 if (element_namespace.empty() && element_name == "public") {
976 Maybe<StringPiece> maybe_name =
977 xml::FindNonEmptyAttribute(parser, "name");
978 if (!maybe_name) {
979 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700980 << "<public> must have a 'name' attribute");
981 error = true;
982 continue;
983 }
984
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 if (xml::FindNonEmptyAttribute(parser, "id")) {
986 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987 << "'id' is ignored within <public-group>");
988 error = true;
989 continue;
990 }
991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 if (xml::FindNonEmptyAttribute(parser, "type")) {
993 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994 << "'type' is ignored within <public-group>");
995 error = true;
996 continue;
997 }
998
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 ParsedResource child_resource;
1000 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -08001001 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001002 child_resource.id = next_id;
1003 child_resource.comment = std::move(comment);
1004 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -08001005 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001009
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001010 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1011 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001012 error = true;
1013 }
1014 }
1015 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001016}
1017
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001018bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
1019 ParsedResource* out_resource) {
1020 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1021 if (!maybe_type) {
1022 diag_->Error(DiagMessage(out_resource->source)
1023 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 << "> must have a 'type' attribute");
1025 return false;
1026 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001027
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1029 if (!parsed_type) {
1030 diag_->Error(DiagMessage(out_resource->source)
1031 << "invalid resource type '" << maybe_type.value() << "' in <"
1032 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 return false;
1034 }
1035
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001038}
1039
Adam Lesinski46c4d722017-08-23 13:03:56 -07001040bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001041 if (options_.visibility) {
1042 diag_->Error(DiagMessage(out_resource->source)
1043 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1044 return false;
1045 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001046 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1047 diag_->Warn(DiagMessage(out_resource->source)
1048 << "ignoring configuration '" << out_resource->config << "' for <"
1049 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001050 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001051
1052 if (!ParseSymbolImpl(parser, out_resource)) {
1053 return false;
1054 }
1055
Adam Lesinski71be7052017-12-12 16:48:07 -08001056 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001057 return true;
1058}
1059
1060bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1061 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1062 diag_->Warn(DiagMessage(out_resource->source)
1063 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
1064 }
1065
1066 if (Maybe<StringPiece> maybe_policy = xml::FindNonEmptyAttribute(parser, "policy")) {
1067 const StringPiece& policy = maybe_policy.value();
1068 if (policy != "system") {
1069 diag_->Error(DiagMessage(out_resource->source)
1070 << "<overlayable> has invalid policy '" << policy << "'");
1071 return false;
1072 }
1073 }
1074
1075 bool error = false;
1076 const size_t depth = parser->depth();
1077 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1078 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1079 // Skip text/comments.
1080 continue;
1081 }
1082
1083 const Source item_source = source_.WithLine(parser->line_number());
1084 const std::string& element_namespace = parser->element_namespace();
1085 const std::string& element_name = parser->element_name();
1086 if (element_namespace.empty() && element_name == "item") {
1087 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1088 if (!maybe_name) {
1089 diag_->Error(DiagMessage(item_source)
1090 << "<item> within an <overlayable> tag must have a 'name' attribute");
1091 error = true;
1092 continue;
1093 }
1094
1095 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1096 if (!maybe_type) {
1097 diag_->Error(DiagMessage(item_source)
1098 << "<item> within an <overlayable> tag must have a 'type' attribute");
1099 error = true;
1100 continue;
1101 }
1102
1103 const ResourceType* type = ParseResourceType(maybe_type.value());
1104 if (type == nullptr) {
1105 diag_->Error(DiagMessage(out_resource->source)
1106 << "invalid resource type '" << maybe_type.value()
1107 << "' in <item> within an <overlayable>");
1108 error = true;
1109 continue;
1110 }
1111
Adam Lesinski71be7052017-12-12 16:48:07 -08001112 ParsedResource child_resource;
1113 child_resource.name.type = *type;
1114 child_resource.name.entry = maybe_name.value().to_string();
1115 child_resource.source = item_source;
1116 child_resource.overlayable = true;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001117 if (options_.visibility) {
1118 child_resource.visibility_level = options_.visibility.value();
1119 }
Adam Lesinski71be7052017-12-12 16:48:07 -08001120 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001121
1122 xml::XmlPullParser::SkipCurrentElement(parser);
1123 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1124 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
1125 error = true;
1126 }
1127 }
1128 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001129}
1130
Adam Lesinski71be7052017-12-12 16:48:07 -08001131bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001133 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001134 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135 return true;
1136 }
1137 return false;
1138}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001139
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001140bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1141 ParsedResource* out_resource) {
1142 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001143}
1144
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001145bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1146 ParsedResource* out_resource, bool weak) {
1147 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001148
1149 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001150 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1151 diag_->Warn(DiagMessage(out_resource->source)
1152 << "ignoring configuration '" << out_resource->config
1153 << "' for attribute " << out_resource->name);
1154 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 }
1156
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001157 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1160 if (maybe_format) {
1161 type_mask = ParseFormatAttribute(maybe_format.value());
1162 if (type_mask == 0) {
1163 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001164 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001165 return false;
1166 }
1167 }
1168
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001169 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001170
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001171 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1172 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1173 if (!min_str.empty()) {
1174 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001175 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001176 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001177 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001179 }
1180
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001181 if (!maybe_min) {
1182 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1183 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001184 return false;
1185 }
1186 }
1187
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001188 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1189 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1190 if (!max_str.empty()) {
1191 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001193 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001194 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001195 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001196 }
1197
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001198 if (!maybe_max) {
1199 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1200 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001201 return false;
1202 }
1203 }
1204
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001205 if ((maybe_min || maybe_max) &&
1206 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1207 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208 << "'min' and 'max' can only be used when format='integer'");
1209 return false;
1210 }
1211
1212 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001213 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001214 return a.symbol.name.value() < b.symbol.name.value();
1215 }
1216 };
1217
1218 std::set<Attribute::Symbol, SymbolComparator> items;
1219
1220 std::string comment;
1221 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 const size_t depth = parser->depth();
1223 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1224 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001225 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001227 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 // Skip text.
1229 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001230 }
1231
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232 const Source item_source = source_.WithLine(parser->line_number());
1233 const std::string& element_namespace = parser->element_namespace();
1234 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001235 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001236 if (element_name == "enum") {
1237 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1238 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 << "can not define an <enum>; already defined a <flag>");
1240 error = true;
1241 continue;
1242 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001243 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001244
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001245 } else if (element_name == "flag") {
1246 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1247 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001248 << "can not define a <flag>; already defined an <enum>");
1249 error = true;
1250 continue;
1251 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001253 }
1254
1255 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001256 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001257 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001258 ParsedResource child_resource;
1259 child_resource.name = symbol.symbol.name.value();
1260 child_resource.source = item_source;
1261 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001262 if (options_.visibility) {
1263 child_resource.visibility_level = options_.visibility.value();
1264 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001265 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001266
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001267 symbol.symbol.SetComment(std::move(comment));
1268 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001269
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270 auto insert_result = items.insert(std::move(symbol));
1271 if (!insert_result.second) {
1272 const Attribute::Symbol& existing_symbol = *insert_result.first;
1273 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001274 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001275 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 << "first defined here");
1279 error = true;
1280 }
1281 } else {
1282 error = true;
1283 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001284 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1285 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001286 error = true;
1287 }
1288
1289 comment = {};
1290 }
1291
1292 if (error) {
1293 return false;
1294 }
1295
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001296 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1297 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1298 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001300 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1301 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001302 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001303 return true;
1304}
1305
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001306Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001307 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001310 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1311 if (!maybe_name) {
1312 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001313 << tag << ">");
1314 return {};
1315 }
1316
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001317 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1318 if (!maybe_value) {
1319 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320 << tag << ">");
1321 return {};
1322 }
1323
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001324 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001325 android::Res_value val;
1326 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 << "' for <" << tag
1329 << ">; must be an integer");
1330 return {};
1331 }
1332
1333 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001336}
1337
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1339 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001340
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1342 if (!maybe_name) {
1343 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001344 return false;
1345 }
1346
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001347 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001348 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001349 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 return false;
1351 }
1352
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001353 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001354 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001357 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 return false;
1360 }
1361
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001362 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 return true;
1364}
1365
Adam Lesinski86d67df2017-01-31 13:47:27 -08001366bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001368 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369
1370 std::unique_ptr<Style> style = util::make_unique<Style>();
1371
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1373 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001374 // 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 -07001375 if (!maybe_parent.value().empty()) {
1376 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001377 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001380 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 }
1382
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001383 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001384 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001385 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001386 }
1387
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001388 } else {
1389 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 std::string style_name = out_resource->name.entry;
1391 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001393 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001394 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395 }
1396 }
1397
1398 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 const size_t depth = parser->depth();
1400 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1401 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001402 // Skip text and comments.
1403 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001404 }
1405
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001406 const std::string& element_namespace = parser->element_namespace();
1407 const std::string& element_name = parser->element_name();
1408 if (element_namespace == "" && element_name == "item") {
1409 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001410
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001411 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1412 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1413 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001415 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001416 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001417
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001418 if (error) {
1419 return false;
1420 }
1421
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001422 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001423 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001424}
1425
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001426bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1427 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1428 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1429 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1430 if (resource_format == 0u) {
1431 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1432 << "'" << format_attr.value() << "' is an invalid format");
1433 return false;
1434 }
1435 }
1436 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001437}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001438
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001439bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1440 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001441}
1442
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001443bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1444 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001445}
1446
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001447bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1448 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001449 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001450 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001451
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001452 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001453
Adam Lesinski75421622017-01-06 15:20:04 -08001454 bool translatable = options_.translatable;
1455 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1456 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1457 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001459 << "invalid value for 'translatable'. Must be a boolean");
1460 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001461 }
Adam Lesinski75421622017-01-06 15:20:04 -08001462 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001463 }
Adam Lesinski75421622017-01-06 15:20:04 -08001464 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001465
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001466 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001467 const size_t depth = parser->depth();
1468 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1469 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001470 // Skip text and comments.
1471 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001472 }
1473
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001474 const Source item_source = source_.WithLine(parser->line_number());
1475 const std::string& element_namespace = parser->element_namespace();
1476 const std::string& element_name = parser->element_name();
1477 if (element_namespace.empty() && element_name == "item") {
1478 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001479 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001480 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001481 error = true;
1482 continue;
1483 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001484 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001485 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001486
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1488 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1489 << "unknown tag <" << element_namespace << ":"
1490 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001491 error = true;
1492 }
1493 }
1494
1495 if (error) {
1496 return false;
1497 }
1498
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001499 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001500 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001501}
1502
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001503bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1504 ParsedResource* out_resource) {
1505 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001506
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001507 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001508
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001509 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001510 const size_t depth = parser->depth();
1511 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1512 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001513 // Skip text and comments.
1514 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001515 }
1516
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001517 const Source item_source = source_.WithLine(parser->line_number());
1518 const std::string& element_namespace = parser->element_namespace();
1519 const std::string& element_name = parser->element_name();
1520 if (element_namespace.empty() && element_name == "item") {
1521 Maybe<StringPiece> maybe_quantity =
1522 xml::FindNonEmptyAttribute(parser, "quantity");
1523 if (!maybe_quantity) {
1524 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001525 << "<item> in <plurals> requires attribute "
1526 << "'quantity'");
1527 error = true;
1528 continue;
1529 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001530
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001531 StringPiece trimmed_quantity =
1532 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001533 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001534 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001535 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001536 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001537 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001540 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001541 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001542 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001545 index = Plural::Other;
1546 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001547 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001548 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001549 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001550 error = true;
1551 continue;
1552 }
1553
1554 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001555 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1556 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557 error = true;
1558 continue;
1559 }
1560
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001561 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001562 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1563 error = true;
1564 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001565 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001566
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001567 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1568 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1569 << element_namespace << ":"
1570 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001571 error = true;
1572 }
1573 }
1574
1575 if (error) {
1576 return false;
1577 }
1578
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001580 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001581}
1582
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001583bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1584 ParsedResource* out_resource) {
1585 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001586
Adam Lesinski71be7052017-12-12 16:48:07 -08001587 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1588 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001589
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001590 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001591 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1592 diag_->Warn(DiagMessage(out_resource->source)
1593 << "ignoring configuration '" << out_resource->config
1594 << "' for styleable " << out_resource->name.entry);
1595 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001596 }
1597
1598 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1599
1600 std::string comment;
1601 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001602 const size_t depth = parser->depth();
1603 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1604 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001605 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001606 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001607 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001608 // Ignore text.
1609 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001610 }
1611
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001612 const Source item_source = source_.WithLine(parser->line_number());
1613 const std::string& element_namespace = parser->element_namespace();
1614 const std::string& element_name = parser->element_name();
1615 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001616 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001617 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001618 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001619 error = true;
1620 continue;
1621 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001622
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001623 // If this is a declaration, the package name may be in the name. Separate
1624 // these out.
1625 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001626 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001627 if (!maybe_ref) {
1628 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1629 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001630 error = true;
1631 continue;
1632 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001633
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001634 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001635 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001636
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001637 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001638 ParsedResource child_resource;
1639 child_resource.name = child_ref.name.value();
1640 child_resource.source = item_source;
1641 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001642 if (options_.visibility) {
1643 child_resource.visibility_level = options_.visibility.value();
1644 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001645
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001646 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001647 error = true;
1648 continue;
1649 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001650
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001651 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001652 child_ref.SetComment(child_resource.comment);
1653 child_ref.SetSource(item_source);
1654 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001655
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001656 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001657
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001658 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1659 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1660 << element_namespace << ":"
1661 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001662 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001663 }
1664
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001665 comment = {};
1666 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001667
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001668 if (error) {
1669 return false;
1670 }
1671
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001672 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001673 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001674}
1675
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001676} // namespace aapt