blob: ef5912bfad928b35f905a2130794ad8bd24148a4 [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
211// A tag that will be encoded into the final flattened string. Tags like <b> or <i>.
212class SpanNode : public Node {
213 public:
214 std::string name;
215
216 void Build(StringBuilder* builder) const override {
217 StringBuilder::SpanHandle span_handle = builder->StartSpan(name);
218 Node::Build(builder);
219 builder->EndSpan(span_handle);
220 }
221};
222
223// An XLIFF 'g' tag, which marks a section of the string as untranslatable.
224class UntranslatableNode : public Node {
225 public:
226 void Build(StringBuilder* builder) const override {
227 StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable();
228 Node::Build(builder);
229 builder->EndUntranslatable(handle);
230 }
231};
232
233// Build a string from XML that converts nested elements into Span objects.
Adam Lesinski75421622017-01-06 15:20:04 -0800234bool ResourceParser::FlattenXmlSubtree(
235 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
236 std::vector<UntranslatableSection>* out_untranslatable_sections) {
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800237 std::string raw_string;
238 std::string current_text;
Adam Lesinski75421622017-01-06 15:20:04 -0800239
240 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
241 Maybe<size_t> untranslatable_start_depth;
242
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800243 Node root;
244 std::vector<Node*> node_stack;
245 node_stack.push_back(&root);
246
247 bool saw_span_node = false;
248 SegmentNode* first_segment = nullptr;
249 SegmentNode* last_segment = nullptr;
250
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 size_t depth = 1;
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800252 while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800254
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800255 // First take care of any SegmentNodes that should be created.
256 if (event == xml::XmlPullParser::Event::kStartElement ||
257 event == xml::XmlPullParser::Event::kEndElement) {
258 if (!current_text.empty()) {
259 std::unique_ptr<SegmentNode> segment_node = util::make_unique<SegmentNode>();
260 segment_node->data = std::move(current_text);
261 last_segment = node_stack.back()->AddChild(std::move(segment_node));
262 if (first_segment == nullptr) {
263 first_segment = last_segment;
Adam Lesinski75421622017-01-06 15:20:04 -0800264 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800265 current_text = {};
266 }
267 }
Adam Lesinski75421622017-01-06 15:20:04 -0800268
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800269 switch (event) {
270 case xml::XmlPullParser::Event::kText: {
271 current_text += parser->text();
272 raw_string += parser->text();
273 } break;
Adam Lesinski75421622017-01-06 15:20:04 -0800274
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800275 case xml::XmlPullParser::Event::kStartElement: {
276 if (parser->element_namespace().empty()) {
277 // This is an HTML tag which we encode as a span. Add it to the span stack.
278 std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>();
279 span_node->name = parser->element_name();
280 const auto end_attr_iter = parser->end_attributes();
281 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter;
282 ++attr_iter) {
283 span_node->name += ";";
284 span_node->name += attr_iter->name;
285 span_node->name += "=";
286 span_node->name += attr_iter->value;
Adam Lesinski75421622017-01-06 15:20:04 -0800287 }
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800288
289 node_stack.push_back(node_stack.back()->AddChild(std::move(span_node)));
290 saw_span_node = true;
291 } else if (parser->element_namespace() == sXliffNamespaceUri) {
292 // This is an XLIFF tag, which is not encoded as a span.
293 if (parser->element_name() == "g") {
294 // Check that an 'untranslatable' tag is not already being processed. Nested
295 // <xliff:g> tags are illegal.
296 if (untranslatable_start_depth) {
297 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
298 << "illegal nested XLIFF 'g' tag");
299 return false;
300 } else {
301 // Mark the beginning of an 'untranslatable' section.
302 untranslatable_start_depth = depth;
303 node_stack.push_back(
304 node_stack.back()->AddChild(util::make_unique<UntranslatableNode>()));
305 }
306 } else {
307 // Ignore unknown XLIFF tags, but don't warn.
308 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
309 }
310 } else {
311 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
312 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
313 << "ignoring element '" << parser->element_name()
314 << "' with unknown namespace '" << parser->element_namespace() << "'");
315 node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>()));
Adam Lesinski75421622017-01-06 15:20:04 -0800316 }
Adam Lesinski75421622017-01-06 15:20:04 -0800317
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800318 // Enter one level inside the element.
319 depth++;
320 } break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800322 case xml::XmlPullParser::Event::kEndElement: {
323 // Return one level from within the element.
324 depth--;
325 if (depth == 0) {
326 break;
327 }
328
329 node_stack.pop_back();
330 if (untranslatable_start_depth == make_value(depth)) {
331 // This is the end of an untranslatable section.
332 untranslatable_start_depth = {};
333 }
334 } break;
335
336 default:
337 // ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 break;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 }
340 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800342 // Sanity check to make sure we processed all the nodes.
343 CHECK(node_stack.size() == 1u);
344 CHECK(node_stack.back() == &root);
345
346 if (!saw_span_node) {
347 // If there were no spans, we must treat this string a little differently (according to AAPT).
348 // Find and strip the leading whitespace from the first segment, and the trailing whitespace
349 // from the last segment.
350 if (first_segment != nullptr) {
351 // Trim leading whitespace.
352 StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data);
353 if (trimmed.size() != first_segment->data.size()) {
354 first_segment->data = trimmed.to_string();
355 }
356 }
357
358 if (last_segment != nullptr) {
359 // Trim trailing whitespace.
360 StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data);
361 if (trimmed.size() != last_segment->data.size()) {
362 last_segment->data = trimmed.to_string();
363 }
364 }
365 }
366
367 // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take
368 // care of recording the correctly adjusted Spans and UntranslatableSections.
369 StringBuilder builder;
370 root.Build(&builder);
371 if (!builder) {
372 diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError());
373 return false;
374 }
375
376 ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString();
377 *out_raw_string = std::move(raw_string);
378 *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections);
379 out_style_string->str = std::move(flattened_string.text);
380 out_style_string->spans = std::move(flattened_string.spans);
Adam Lesinski75421622017-01-06 15:20:04 -0800381 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800382}
383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 const size_t depth = parser->depth();
387 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
388 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 // Skip comments and text.
390 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800391 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392
Adam Lesinski060b53d2017-07-28 17:10:35 -0700393 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 << "root element must be <resources>");
396 return false;
397 }
398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 break;
401 };
402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
404 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
405 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 return false;
407 }
408 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800409}
410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
412 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700413
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 bool error = false;
415 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 const size_t depth = parser->depth();
417 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
418 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800422 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700423
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 if (!util::TrimWhitespace(parser->text()).empty()) {
426 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 << "plain text not allowed here");
428 error = true;
429 }
430 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700431 }
432
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700435 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 // Skip unknown namespace.
437 continue;
438 }
439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 std::string element_name = parser->element_name();
441 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 comment = "";
443 continue;
444 }
445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 ParsedResource parsed_resource;
447 parsed_resource.config = config_;
448 parsed_resource.source = source_.WithLine(parser->line_number());
449 parsed_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100450 if (options_.visibility) {
451 parsed_resource.visibility_level = options_.visibility.value();
452 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453
454 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700455 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800456 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 }
458
459 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 error = true;
462 continue;
463 }
464
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 error = true;
467 }
468 }
469
470 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 for (const ResourceName& stripped_resource : stripped_resources) {
472 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700474 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
475 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 error = true;
477 }
478 }
479
480 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481}
482
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
484 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 struct ItemTypeFormat {
486 ResourceType type;
487 uint32_t format;
488 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800489
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
491 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800492
Adam Lesinski86d67df2017-01-31 13:47:27 -0800493 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
494 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
495 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
496 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
497 {"dimen",
498 {ResourceType::kDimen,
499 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
500 android::ResTable_map::TYPE_DIMENSION}},
501 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
502 {"fraction",
503 {ResourceType::kFraction,
504 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
505 android::ResTable_map::TYPE_DIMENSION}},
506 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
507 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
508 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800509
Adam Lesinski86d67df2017-01-31 13:47:27 -0800510 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
511 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
512 {"array", std::mem_fn(&ResourceParser::ParseArray)},
513 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
514 {"configVarying",
515 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
516 std::placeholders::_2, std::placeholders::_3)},
517 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
518 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
519 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700520 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800521 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
522 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
523 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
524 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
525 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
526 std::placeholders::_2, std::placeholders::_3)},
527 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
528 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800531
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800534
Adam Lesinski86d67df2017-01-31 13:47:27 -0800535 bool can_be_item = true;
536 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800538 can_be_bag = false;
539
Adam Lesinskie597d682017-06-01 17:16:44 -0700540 // The default format for <item> is any. If a format attribute is present, that one will
541 // override the default.
542 resource_format = android::ResTable_map::TYPE_ANY;
543
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700545 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800546 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 << "<item> must have a 'type' attribute");
550 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800551 }
552
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700553 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700555 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700557 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 if (!resource_format) {
559 diag_->Error(DiagMessage(out_resource->source)
560 << "'" << maybe_format.value()
561 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800562 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
564 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800565 } else if (resource_type == "bag") {
566 can_be_item = false;
567
568 // Bags have their type encoded in the type attribute.
569 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
570 resource_type = maybe_type.value().to_string();
571 } else {
572 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
573 << "<bag> must have a 'type' attribute");
574 return false;
575 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 }
577
578 // Get the name of the resource. This will be checked later, because not all
579 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 if (resource_type == "id") {
583 if (!maybe_name) {
584 diag_->Error(DiagMessage(out_resource->source)
585 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 << "> missing 'name' attribute");
587 return false;
588 }
589
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800591 out_resource->name.entry = maybe_name.value().to_string();
y9efbbef2018-04-18 11:29:09 -0700592
593 // Ids either represent a unique resource id or reference another resource id
594 auto item = ParseItem(parser, out_resource, resource_format);
595 if (!item) {
596 return false;
597 }
598
599 String* empty = ValueCast<String>(out_resource->value.get());
600 if (empty && *empty->value == "") {
601 // If no inner element exists, represent a unique identifier
602 out_resource->value = util::make_unique<Id>();
603 } else {
y9efbbef2018-04-18 11:29:09 -0700604 Reference* ref = ValueCast<Reference>(out_resource->value.get());
Ryan Mitchelleaf77e12018-04-25 15:00:50 -0700605 if (ref && !ref->name && !ref->id) {
606 // A null reference also means there is no inner element when ids are in the form:
607 // <id name="name"/>
608 out_resource->value = util::make_unique<Id>();
609 } else if (!ref || ref->name.value().type != ResourceType::kId) {
610 // If an inner element exists, the inner element must be a reference to another resource id
y9efbbef2018-04-18 11:29:09 -0700611 diag_->Error(DiagMessage(out_resource->source)
612 << "<" << parser->element_name()
613 << "> inner element must either be a resource reference or empty");
614 return false;
615 }
616 }
617
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 return true;
619 }
620
Adam Lesinski86d67df2017-01-31 13:47:27 -0800621 if (can_be_item) {
622 const auto item_iter = elToItemMap.find(resource_type);
623 if (item_iter != elToItemMap.end()) {
624 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625
Adam Lesinski86d67df2017-01-31 13:47:27 -0800626 if (!maybe_name) {
627 diag_->Error(DiagMessage(out_resource->source)
628 << "<" << parser->element_name() << "> missing 'name' attribute");
629 return false;
630 }
631
632 out_resource->name.type = item_iter->second.type;
633 out_resource->name.entry = maybe_name.value().to_string();
634
Adam Lesinskie597d682017-06-01 17:16:44 -0700635 // Only use the implied format of the type when there is no explicit format.
636 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800637 resource_format = item_iter->second.format;
638 }
639
640 if (!ParseItem(parser, out_resource, resource_format)) {
641 return false;
642 }
643 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 }
646
647 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800648 if (can_be_bag) {
649 const auto bag_iter = elToBagMap.find(resource_type);
650 if (bag_iter != elToBagMap.end()) {
651 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700652 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800653 if (!maybe_name) {
654 diag_->Error(DiagMessage(out_resource->source)
655 << "<" << parser->element_name() << "> missing 'name' attribute");
656 return false;
657 }
658
659 out_resource->name.entry = maybe_name.value().to_string();
660 }
661
662 // Call the associated parse method. The type will be filled in by the
663 // parse func.
664 if (!bag_iter->second(this, parser, out_resource)) {
665 return false;
666 }
667 return true;
668 }
669 }
670
671 if (can_be_item) {
672 // Try parsing the elementName (or type) as a resource. These shall only be
673 // resources like 'layout' or 'xml' and they can only be references.
674 const ResourceType* parsed_type = ParseResourceType(resource_type);
675 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 if (!maybe_name) {
677 diag_->Error(DiagMessage(out_resource->source)
678 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 << "> missing 'name' attribute");
680 return false;
681 }
682
Adam Lesinski86d67df2017-01-31 13:47:27 -0800683 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800684 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800685 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
686 if (!out_resource->value) {
687 diag_->Error(DiagMessage(out_resource->source)
688 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
689 return false;
690 }
691 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 }
694
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 diag_->Warn(DiagMessage(out_resource->source)
696 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 return false;
698}
699
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
701 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 const uint32_t format) {
703 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 }
706
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 out_resource->value = ParseXml(parser, format, kNoRawString);
708 if (!out_resource->value) {
709 diag_->Error(DiagMessage(out_resource->source) << "invalid "
710 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 return false;
712 }
713 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800714}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800715
716/**
717 * Reads the entire XML subtree and attempts to parse it as some Item,
718 * with typeMask denoting which items it can be. If allowRawValue is
719 * true, a RawString is returned if the XML couldn't be parsed as
720 * an Item. If allowRawValue is false, nullptr is returned in this
721 * case.
722 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
724 const uint32_t type_mask,
725 const bool allow_raw_value) {
726 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 std::string raw_value;
729 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800730 std::vector<UntranslatableSection> untranslatable_sections;
731 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 }
734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800737 std::unique_ptr<StyledString> styled_string =
738 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700739 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800740 styled_string->untranslatable_sections = std::move(untranslatable_sections);
741 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
743
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 // name.package can be empty here, as it will assume the package name of the
746 // table.
747 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 id->SetSource(source_.WithLine(begin_xml_line));
749 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 };
751
752 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700753 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800754 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700758 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 }
762
763 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800766 std::unique_ptr<String> string = util::make_unique<String>(
767 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
768 string->untranslatable_sections = std::move(untranslatable_sections);
769 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 }
771
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700772 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
773 if (util::TrimWhitespace(raw_value).empty()) {
774 return ResourceUtils::MakeNull();
775 }
776
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 // We can't parse this so return a RawString if we are allowed.
779 return util::make_unique<RawString>(
Ryan Mitchell633d7962018-06-11 15:29:21 -0700780 table_->string_pool.MakeRef(util::TrimWhitespace(raw_value),
781 StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 }
783 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800784}
785
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700786bool ResourceParser::ParseString(xml::XmlPullParser* parser,
787 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700789 if (Maybe<StringPiece> formatted_attr =
790 xml::FindAttribute(parser, "formatted")) {
791 Maybe<bool> maybe_formatted =
792 ResourceUtils::ParseBool(formatted_attr.value());
793 if (!maybe_formatted) {
794 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 << "invalid value for 'formatted'. Must be a boolean");
796 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800797 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700798 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800800
Adam Lesinski75421622017-01-06 15:20:04 -0800801 bool translatable = options_.translatable;
802 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
803 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
804 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806 << "invalid value for 'translatable'. Must be a boolean");
807 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800808 }
Adam Lesinski75421622017-01-06 15:20:04 -0800809 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800811
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 out_resource->value =
813 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
814 if (!out_resource->value) {
815 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 return false;
817 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800818
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700819 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800820 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800821
Adam Lesinski75421622017-01-06 15:20:04 -0800822 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823 if (!util::VerifyJavaStringFormat(*string_value->value)) {
824 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700825 msg << "multiple substitutions specified in non-positional format; "
826 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 if (options_.error_on_positional_arguments) {
828 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800830 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800831
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800834 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835
Adam Lesinski75421622017-01-06 15:20:04 -0800836 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
837 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 }
839 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800840}
841
Adam Lesinski71be7052017-12-12 16:48:07 -0800842bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100843 if (options_.visibility) {
844 diag_->Error(DiagMessage(out_resource->source)
845 << "<public> tag not allowed with --visibility flag");
846 return false;
847 }
848
Adam Lesinski46c4d722017-08-23 13:03:56 -0700849 if (out_resource->config != ConfigDescription::DefaultConfig()) {
850 diag_->Warn(DiagMessage(out_resource->source)
851 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
852 }
853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
855 if (!maybe_type) {
856 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700857 << "<public> must have a 'type' attribute");
858 return false;
859 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
862 if (!parsed_type) {
863 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
864 << maybe_type.value()
865 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700866 return false;
867 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800868
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800870
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800871 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
872 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700873 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800874 diag_->Error(DiagMessage(out_resource->source)
875 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800877 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800880
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700884 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700885
Adam Lesinski71be7052017-12-12 16:48:07 -0800886 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700887 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800888}
889
Adam Lesinski46c4d722017-08-23 13:03:56 -0700890bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100891 if (options_.visibility) {
892 diag_->Error(DiagMessage(out_resource->source)
893 << "<public-group> tag not allowed with --visibility flag");
894 return false;
895 }
896
Adam Lesinski46c4d722017-08-23 13:03:56 -0700897 if (out_resource->config != ConfigDescription::DefaultConfig()) {
898 diag_->Warn(DiagMessage(out_resource->source)
899 << "ignoring configuration '" << out_resource->config
900 << "' for <public-group> tag");
901 }
902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
904 if (!maybe_type) {
905 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800907 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700908 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800909
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
911 if (!parsed_type) {
912 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
913 << maybe_type.value()
914 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800915 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 }
917
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700918 Maybe<StringPiece> maybe_id_str =
919 xml::FindNonEmptyAttribute(parser, "first-id");
920 if (!maybe_id_str) {
921 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 << "<public-group> must have a 'first-id' attribute");
923 return false;
924 }
925
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700926 Maybe<ResourceId> maybe_id =
927 ResourceUtils::ParseResourceId(maybe_id_str.value());
928 if (!maybe_id) {
929 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
930 << maybe_id_str.value()
931 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700932 return false;
933 }
934
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700935 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700936
937 std::string comment;
938 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700939 const size_t depth = parser->depth();
940 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
941 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800942 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700943 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700944 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700945 // Skip text.
946 continue;
947 }
948
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 const Source item_source = source_.WithLine(parser->line_number());
950 const std::string& element_namespace = parser->element_namespace();
951 const std::string& element_name = parser->element_name();
952 if (element_namespace.empty() && element_name == "public") {
953 Maybe<StringPiece> maybe_name =
954 xml::FindNonEmptyAttribute(parser, "name");
955 if (!maybe_name) {
956 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700957 << "<public> must have a 'name' attribute");
958 error = true;
959 continue;
960 }
961
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700962 if (xml::FindNonEmptyAttribute(parser, "id")) {
963 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700964 << "'id' is ignored within <public-group>");
965 error = true;
966 continue;
967 }
968
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700969 if (xml::FindNonEmptyAttribute(parser, "type")) {
970 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700971 << "'type' is ignored within <public-group>");
972 error = true;
973 continue;
974 }
975
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 ParsedResource child_resource;
977 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800978 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 child_resource.id = next_id;
980 child_resource.comment = std::move(comment);
981 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800982 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700984
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700985 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700986
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700987 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
988 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700989 error = true;
990 }
991 }
992 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800993}
994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
996 ParsedResource* out_resource) {
997 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
998 if (!maybe_type) {
999 diag_->Error(DiagMessage(out_resource->source)
1000 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001 << "> must have a 'type' attribute");
1002 return false;
1003 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001004
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
1006 if (!parsed_type) {
1007 diag_->Error(DiagMessage(out_resource->source)
1008 << "invalid resource type '" << maybe_type.value() << "' in <"
1009 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001010 return false;
1011 }
1012
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001013 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001014 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001015}
1016
Adam Lesinski46c4d722017-08-23 13:03:56 -07001017bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001018 if (options_.visibility) {
1019 diag_->Error(DiagMessage(out_resource->source)
1020 << "<java-symbol> and <symbol> tags not allowed with --visibility flag");
1021 return false;
1022 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001023 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1024 diag_->Warn(DiagMessage(out_resource->source)
1025 << "ignoring configuration '" << out_resource->config << "' for <"
1026 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 }
Adam Lesinski46c4d722017-08-23 13:03:56 -07001028
1029 if (!ParseSymbolImpl(parser, out_resource)) {
1030 return false;
1031 }
1032
Adam Lesinski71be7052017-12-12 16:48:07 -08001033 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -07001034 return true;
1035}
1036
1037bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1038 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1039 diag_->Warn(DiagMessage(out_resource->source)
1040 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
1041 }
1042
1043 if (Maybe<StringPiece> maybe_policy = xml::FindNonEmptyAttribute(parser, "policy")) {
1044 const StringPiece& policy = maybe_policy.value();
1045 if (policy != "system") {
1046 diag_->Error(DiagMessage(out_resource->source)
1047 << "<overlayable> has invalid policy '" << policy << "'");
1048 return false;
1049 }
1050 }
1051
1052 bool error = false;
1053 const size_t depth = parser->depth();
1054 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1055 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1056 // Skip text/comments.
1057 continue;
1058 }
1059
1060 const Source item_source = source_.WithLine(parser->line_number());
1061 const std::string& element_namespace = parser->element_namespace();
1062 const std::string& element_name = parser->element_name();
1063 if (element_namespace.empty() && element_name == "item") {
1064 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1065 if (!maybe_name) {
1066 diag_->Error(DiagMessage(item_source)
1067 << "<item> within an <overlayable> tag must have a 'name' attribute");
1068 error = true;
1069 continue;
1070 }
1071
1072 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
1073 if (!maybe_type) {
1074 diag_->Error(DiagMessage(item_source)
1075 << "<item> within an <overlayable> tag must have a 'type' attribute");
1076 error = true;
1077 continue;
1078 }
1079
1080 const ResourceType* type = ParseResourceType(maybe_type.value());
1081 if (type == nullptr) {
1082 diag_->Error(DiagMessage(out_resource->source)
1083 << "invalid resource type '" << maybe_type.value()
1084 << "' in <item> within an <overlayable>");
1085 error = true;
1086 continue;
1087 }
1088
Adam Lesinski71be7052017-12-12 16:48:07 -08001089 ParsedResource child_resource;
1090 child_resource.name.type = *type;
1091 child_resource.name.entry = maybe_name.value().to_string();
1092 child_resource.source = item_source;
1093 child_resource.overlayable = true;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001094 if (options_.visibility) {
1095 child_resource.visibility_level = options_.visibility.value();
1096 }
Adam Lesinski71be7052017-12-12 16:48:07 -08001097 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski46c4d722017-08-23 13:03:56 -07001098
1099 xml::XmlPullParser::SkipCurrentElement(parser);
1100 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1101 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
1102 error = true;
1103 }
1104 }
1105 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001106}
1107
Adam Lesinski71be7052017-12-12 16:48:07 -08001108bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001109 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -08001110 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -07001111 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112 return true;
1113 }
1114 return false;
1115}
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001116
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001117bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
1118 ParsedResource* out_resource) {
1119 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120}
1121
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
1123 ParsedResource* out_resource, bool weak) {
1124 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125
1126 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001127 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1128 diag_->Warn(DiagMessage(out_resource->source)
1129 << "ignoring configuration '" << out_resource->config
1130 << "' for attribute " << out_resource->name);
1131 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 }
1133
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001134 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
1137 if (maybe_format) {
1138 type_mask = ParseFormatAttribute(maybe_format.value());
1139 if (type_mask == 0) {
1140 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001141 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001142 return false;
1143 }
1144 }
1145
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001146 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001147
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
1149 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1150 if (!min_str.empty()) {
1151 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001153 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001154 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001156 }
1157
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 if (!maybe_min) {
1159 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1160 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001161 return false;
1162 }
1163 }
1164
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1166 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1167 if (!max_str.empty()) {
1168 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001169 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001170 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001171 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001172 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001173 }
1174
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001175 if (!maybe_max) {
1176 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1177 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 return false;
1179 }
1180 }
1181
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 if ((maybe_min || maybe_max) &&
1183 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1184 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 << "'min' and 'max' can only be used when format='integer'");
1186 return false;
1187 }
1188
1189 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001190 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001191 return a.symbol.name.value() < b.symbol.name.value();
1192 }
1193 };
1194
1195 std::set<Attribute::Symbol, SymbolComparator> items;
1196
1197 std::string comment;
1198 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001199 const size_t depth = parser->depth();
1200 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1201 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001202 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205 // Skip text.
1206 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001207 }
1208
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001209 const Source item_source = source_.WithLine(parser->line_number());
1210 const std::string& element_namespace = parser->element_namespace();
1211 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001212 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001213 if (element_name == "enum") {
1214 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1215 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001216 << "can not define an <enum>; already defined a <flag>");
1217 error = true;
1218 continue;
1219 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001220 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001221
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001222 } else if (element_name == "flag") {
1223 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1224 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001225 << "can not define a <flag>; already defined an <enum>");
1226 error = true;
1227 continue;
1228 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 }
1231
1232 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001234 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 ParsedResource child_resource;
1236 child_resource.name = symbol.symbol.name.value();
1237 child_resource.source = item_source;
1238 child_resource.value = util::make_unique<Id>();
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001239 if (options_.visibility) {
1240 child_resource.visibility_level = options_.visibility.value();
1241 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001242 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001243
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001244 symbol.symbol.SetComment(std::move(comment));
1245 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 auto insert_result = items.insert(std::move(symbol));
1248 if (!insert_result.second) {
1249 const Attribute::Symbol& existing_symbol = *insert_result.first;
1250 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001251 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001252 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001253
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255 << "first defined here");
1256 error = true;
1257 }
1258 } else {
1259 error = true;
1260 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1262 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 error = true;
1264 }
1265
1266 comment = {};
1267 }
1268
1269 if (error) {
1270 return false;
1271 }
1272
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001273 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1274 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1275 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001277 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1278 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 return true;
1281}
1282
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001283Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001284 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001286
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001287 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1288 if (!maybe_name) {
1289 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290 << tag << ">");
1291 return {};
1292 }
1293
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1295 if (!maybe_value) {
1296 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297 << tag << ">");
1298 return {};
1299 }
1300
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001301 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001302 android::Res_value val;
1303 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305 << "' for <" << tag
1306 << ">; must be an integer");
1307 return {};
1308 }
1309
1310 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001312 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001313}
1314
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1316 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001317
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1319 if (!maybe_name) {
1320 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321 return false;
1322 }
1323
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001324 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001325 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001326 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 return false;
1328 }
1329
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001330 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001331 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001332
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001333 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001334 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001335 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001336 return false;
1337 }
1338
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001339 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001340 return true;
1341}
1342
Adam Lesinski86d67df2017-01-31 13:47:27 -08001343bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001344 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001345 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346
1347 std::unique_ptr<Style> style = util::make_unique<Style>();
1348
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001349 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1350 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001351 // 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 -07001352 if (!maybe_parent.value().empty()) {
1353 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001354 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001356 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001357 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 }
1359
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001360 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001362 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001363 }
1364
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 } else {
1366 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 std::string style_name = out_resource->name.entry;
1368 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001370 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001371 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 }
1373 }
1374
1375 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 const size_t depth = parser->depth();
1377 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1378 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001379 // Skip text and comments.
1380 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001381 }
1382
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001383 const std::string& element_namespace = parser->element_namespace();
1384 const std::string& element_name = parser->element_name();
1385 if (element_namespace == "" && element_name == "item") {
1386 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001387
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001388 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1389 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1390 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001391 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001392 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001394
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395 if (error) {
1396 return false;
1397 }
1398
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001399 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001400 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001401}
1402
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001403bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1404 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1405 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1406 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1407 if (resource_format == 0u) {
1408 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1409 << "'" << format_attr.value() << "' is an invalid format");
1410 return false;
1411 }
1412 }
1413 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001414}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001415
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001416bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1417 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001418}
1419
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001420bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1421 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001422}
1423
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001424bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1425 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001426 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001427 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001428
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001429 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001430
Adam Lesinski75421622017-01-06 15:20:04 -08001431 bool translatable = options_.translatable;
1432 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1433 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1434 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001435 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001436 << "invalid value for 'translatable'. Must be a boolean");
1437 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001438 }
Adam Lesinski75421622017-01-06 15:20:04 -08001439 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001440 }
Adam Lesinski75421622017-01-06 15:20:04 -08001441 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001442
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001443 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001444 const size_t depth = parser->depth();
1445 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1446 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001447 // Skip text and comments.
1448 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001449 }
1450
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001451 const Source item_source = source_.WithLine(parser->line_number());
1452 const std::string& element_namespace = parser->element_namespace();
1453 const std::string& element_name = parser->element_name();
1454 if (element_namespace.empty() && element_name == "item") {
1455 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001456 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001457 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001458 error = true;
1459 continue;
1460 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001461 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001462 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001463
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001464 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1465 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1466 << "unknown tag <" << element_namespace << ":"
1467 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 error = true;
1469 }
1470 }
1471
1472 if (error) {
1473 return false;
1474 }
1475
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001476 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001477 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001478}
1479
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001480bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1481 ParsedResource* out_resource) {
1482 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001483
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001484 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001485
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001487 const size_t depth = parser->depth();
1488 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1489 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001490 // Skip text and comments.
1491 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001492 }
1493
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001494 const Source item_source = source_.WithLine(parser->line_number());
1495 const std::string& element_namespace = parser->element_namespace();
1496 const std::string& element_name = parser->element_name();
1497 if (element_namespace.empty() && element_name == "item") {
1498 Maybe<StringPiece> maybe_quantity =
1499 xml::FindNonEmptyAttribute(parser, "quantity");
1500 if (!maybe_quantity) {
1501 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001502 << "<item> in <plurals> requires attribute "
1503 << "'quantity'");
1504 error = true;
1505 continue;
1506 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001507
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001508 StringPiece trimmed_quantity =
1509 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001510 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001511 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001512 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001513 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001514 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001515 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001516 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001517 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001518 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001519 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001520 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001521 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001522 index = Plural::Other;
1523 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001524 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001525 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001526 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001527 error = true;
1528 continue;
1529 }
1530
1531 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001532 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1533 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001534 error = true;
1535 continue;
1536 }
1537
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001538 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001539 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1540 error = true;
1541 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001542 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001543
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001544 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1545 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1546 << element_namespace << ":"
1547 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001548 error = true;
1549 }
1550 }
1551
1552 if (error) {
1553 return false;
1554 }
1555
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001556 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001557 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001558}
1559
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001560bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1561 ParsedResource* out_resource) {
1562 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001563
Adam Lesinski71be7052017-12-12 16:48:07 -08001564 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1565 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001566
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001567 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001568 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1569 diag_->Warn(DiagMessage(out_resource->source)
1570 << "ignoring configuration '" << out_resource->config
1571 << "' for styleable " << out_resource->name.entry);
1572 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001573 }
1574
1575 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1576
1577 std::string comment;
1578 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001579 const size_t depth = parser->depth();
1580 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1581 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001582 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001583 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001584 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001585 // Ignore text.
1586 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001587 }
1588
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001589 const Source item_source = source_.WithLine(parser->line_number());
1590 const std::string& element_namespace = parser->element_namespace();
1591 const std::string& element_name = parser->element_name();
1592 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001593 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001594 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001595 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001596 error = true;
1597 continue;
1598 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001599
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001600 // If this is a declaration, the package name may be in the name. Separate
1601 // these out.
1602 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001603 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001604 if (!maybe_ref) {
1605 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1606 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001607 error = true;
1608 continue;
1609 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001610
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001611 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001612 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001613
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001614 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001615 ParsedResource child_resource;
1616 child_resource.name = child_ref.name.value();
1617 child_resource.source = item_source;
1618 child_resource.comment = std::move(comment);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +01001619 if (options_.visibility) {
1620 child_resource.visibility_level = options_.visibility.value();
1621 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001622
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001623 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001624 error = true;
1625 continue;
1626 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001627
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001628 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001629 child_ref.SetComment(child_resource.comment);
1630 child_ref.SetSource(item_source);
1631 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001632
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001633 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001634
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001635 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1636 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1637 << element_namespace << ":"
1638 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001639 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001640 }
1641
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001642 comment = {};
1643 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001644
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001645 if (error) {
1646 return false;
1647 }
1648
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001649 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001650 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001651}
1652
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001653} // namespace aapt