blob: 8461905d80348461b3a010a86b229127fe16d065 [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>
20#include <sstream>
21
22#include "android-base/logging.h"
23
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include "ResourceTable.h"
25#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "ValueVisitor.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080028#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080029#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070030#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070032
Adam Lesinskid5083f62017-01-16 15:07:21 -080033using android::StringPiece;
34
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035namespace aapt {
36
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037constexpr const char* sXliffNamespaceUri =
38 "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Adam Lesinski27afb9e2015-11-06 18:25:04 -080040/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 * Returns true if the element is <skip> or <eat-comment> and can be safely
42 * ignored.
Adam Lesinski27afb9e2015-11-06 18:25:04 -080043 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044static bool ShouldIgnoreElement(const StringPiece& ns,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 const StringPiece& name) {
46 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080047}
48
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049static uint32_t ParseFormatType(const StringPiece& piece) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 if (piece == "reference")
51 return android::ResTable_map::TYPE_REFERENCE;
52 else if (piece == "string")
53 return android::ResTable_map::TYPE_STRING;
54 else if (piece == "integer")
55 return android::ResTable_map::TYPE_INTEGER;
56 else if (piece == "boolean")
57 return android::ResTable_map::TYPE_BOOLEAN;
58 else if (piece == "color")
59 return android::ResTable_map::TYPE_COLOR;
60 else if (piece == "float")
61 return android::ResTable_map::TYPE_FLOAT;
62 else if (piece == "dimension")
63 return android::ResTable_map::TYPE_DIMENSION;
64 else if (piece == "fraction")
65 return android::ResTable_map::TYPE_FRACTION;
66 else if (piece == "enum")
67 return android::ResTable_map::TYPE_ENUM;
68 else if (piece == "flags")
69 return android::ResTable_map::TYPE_FLAGS;
70 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 for (StringPiece part : util::Tokenize(str, '|')) {
76 StringPiece trimmed_part = util::TrimWhitespace(part);
77 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 if (type == 0) {
79 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080080 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 mask |= type;
82 }
83 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080084}
85
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086/**
87 * A parsed resource ready to be added to the ResourceTable.
88 */
89struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 ResourceName name;
91 ConfigDescription config;
92 std::string product;
93 Source source;
94 ResourceId id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 Maybe<SymbolState> symbol_state;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 std::string comment;
97 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080099};
100
101// Recursively adds resources to the ResourceTable.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
105 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800107 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 }
Adam Lesinski76565542016-03-10 21:55:04 -0800109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 if (res->symbol_state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 symbol.state = res->symbol_state.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 symbol.source = res->source;
114 symbol.comment = res->comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 if (!table->SetSymbolState(res->name, res->id, symbol, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800117 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 if (res->value) {
121 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 res->value->SetComment(std::move(res->comment));
123 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 if (!table->AddResource(res->name, res->id, res->config, res->product,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 std::move(res->value), diag)) {
127 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800128 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 for (ParsedResource& child : res->child_resources) {
133 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 }
135 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800136}
137
138// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
142 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700143 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 : diag_(diag),
146 table_(table),
147 source_(source),
148 config_(config),
149 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
151/**
152 * Build a string from XML that converts nested elements into Span objects.
153 */
Adam Lesinski75421622017-01-06 15:20:04 -0800154bool ResourceParser::FlattenXmlSubtree(
155 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
156 std::vector<UntranslatableSection>* out_untranslatable_sections) {
157 // Keeps track of formatting tags (<b>, <i>) and the range of characters for which they apply.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 std::vector<Span> span_stack;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159
Adam Lesinski75421622017-01-06 15:20:04 -0800160 // Clear the output variables.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 out_raw_string->clear();
162 out_style_string->spans.clear();
Adam Lesinski75421622017-01-06 15:20:04 -0800163 out_untranslatable_sections->clear();
164
165 // The StringBuilder will concatenate the various segments of text which are initially
166 // separated by tags. It also handles unicode escape codes and quotations.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 util::StringBuilder builder;
Adam Lesinski75421622017-01-06 15:20:04 -0800168
169 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
170 Maybe<size_t> untranslatable_start_depth;
171
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 size_t depth = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
174 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800175
176 if (event == xml::XmlPullParser::Event::kStartElement) {
177 if (parser->element_namespace().empty()) {
178 // This is an HTML tag which we encode as a span. Add it to the span stack.
179 std::string span_name = parser->element_name();
180 const auto end_attr_iter = parser->end_attributes();
181 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter; ++attr_iter) {
182 span_name += ";";
183 span_name += attr_iter->name;
184 span_name += "=";
185 span_name += attr_iter->value;
186 }
187
188 // Make sure the string is representable in our binary format.
189 if (builder.Utf16Len() > std::numeric_limits<uint32_t>::max()) {
190 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
191 << "style string '" << builder.ToString() << "' is too long");
192 return false;
193 }
194
195 span_stack.push_back(Span{std::move(span_name), static_cast<uint32_t>(builder.Utf16Len())});
196 } else if (parser->element_namespace() == sXliffNamespaceUri) {
197 if (parser->element_name() == "g") {
198 if (untranslatable_start_depth) {
199 // We've already encountered an <xliff:g> tag, and nested <xliff:g> tags are illegal.
200 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
201 << "illegal nested XLIFF 'g' tag");
202 return false;
203 } else {
204 // Mark the start of an untranslatable section. Use UTF8 indices/lengths.
205 untranslatable_start_depth = depth;
206 const size_t current_idx = builder.ToString().size();
207 out_untranslatable_sections->push_back(UntranslatableSection{current_idx, current_idx});
208 }
209 }
210 // Ignore other xliff tags, they get handled by other tools.
211
212 } else {
213 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
214 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
215 << "ignoring element '" << parser->element_name()
216 << "' with unknown namespace '" << parser->element_namespace() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218
Adam Lesinski75421622017-01-06 15:20:04 -0800219 // Enter one level inside the element.
220 depth++;
221 } else if (event == xml::XmlPullParser::Event::kText) {
222 // Record both the raw text and append to the builder to deal with escape sequences
223 // and quotations.
224 out_raw_string->append(parser->text());
225 builder.Append(parser->text());
226 } else if (event == xml::XmlPullParser::Event::kEndElement) {
227 // Return one level from within the element.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 depth--;
229 if (depth == 0) {
230 break;
231 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800232
Adam Lesinski75421622017-01-06 15:20:04 -0800233 if (parser->element_namespace().empty()) {
234 // This is an HTML tag which we encode as a span. Update the span
235 // stack and pop the top entry.
236 Span& top_span = span_stack.back();
237 top_span.last_char = builder.Utf16Len() - 1;
238 out_style_string->spans.push_back(std::move(top_span));
239 span_stack.pop_back();
240 } else if (untranslatable_start_depth == make_value(depth)) {
241 // This is the end of an untranslatable section. Use UTF8 indices/lengths.
242 UntranslatableSection& untranslatable_section = out_untranslatable_sections->back();
243 untranslatable_section.end = builder.ToString().size();
244 untranslatable_start_depth = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski75421622017-01-06 15:20:04 -0800247 // Ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 LOG(FATAL) << "unhandled XML event";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 }
251 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252
Adam Lesinski75421622017-01-06 15:20:04 -0800253 CHECK(span_stack.empty()) << "spans haven't been fully processed";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 out_style_string->str = builder.ToString();
Adam Lesinski75421622017-01-06 15:20:04 -0800255 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256}
257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 const size_t depth = parser->depth();
261 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
262 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 // Skip comments and text.
264 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (!parser->element_namespace().empty() ||
268 parser->element_name() != "resources") {
269 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 << "root element must be <resources>");
271 return false;
272 }
273
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 break;
276 };
277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
279 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
280 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 return false;
282 }
283 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284}
285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
287 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700288
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 bool error = false;
290 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 const size_t depth = parser->depth();
292 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
293 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700298
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 if (!util::TrimWhitespace(parser->text()).empty()) {
301 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 << "plain text not allowed here");
303 error = true;
304 }
305 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700306 }
307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 // Skip unknown namespace.
312 continue;
313 }
314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 std::string element_name = parser->element_name();
316 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 comment = "";
318 continue;
319 }
320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 ParsedResource parsed_resource;
322 parsed_resource.config = config_;
323 parsed_resource.source = source_.WithLine(parser->line_number());
324 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325
326 // Extract the product name if it exists.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 if (Maybe<StringPiece> maybe_product =
328 xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800329 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 }
331
332 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 error = true;
335 continue;
336 }
337
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 error = true;
340 }
341 }
342
343 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 for (const ResourceName& stripped_resource : stripped_resources) {
345 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 // Failed to find the resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700347 diag_->Error(DiagMessage(source_)
348 << "resource '" << stripped_resource
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 << "' "
350 "was filtered out but no product variant remains");
351 error = true;
352 }
353 }
354
355 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356}
357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
359 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 struct ItemTypeFormat {
361 ResourceType type;
362 uint32_t format;
363 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800364
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
366 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800367
Adam Lesinski86d67df2017-01-31 13:47:27 -0800368 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
369 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
370 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
371 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
372 {"dimen",
373 {ResourceType::kDimen,
374 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
375 android::ResTable_map::TYPE_DIMENSION}},
376 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
377 {"fraction",
378 {ResourceType::kFraction,
379 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
380 android::ResTable_map::TYPE_DIMENSION}},
381 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
382 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
383 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800384
Adam Lesinski86d67df2017-01-31 13:47:27 -0800385 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
386 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
387 {"array", std::mem_fn(&ResourceParser::ParseArray)},
388 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
389 {"configVarying",
390 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
391 std::placeholders::_2, std::placeholders::_3)},
392 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
393 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
394 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
395 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
396 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
397 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
398 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
399 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
400 std::placeholders::_2, std::placeholders::_3)},
401 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
402 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800405
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800408
Adam Lesinski86d67df2017-01-31 13:47:27 -0800409 bool can_be_item = true;
410 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800412 can_be_bag = false;
413
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 // Items have their type encoded in the type attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 if (Maybe<StringPiece> maybe_type =
416 xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800417 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 << "<item> must have a 'type' attribute");
421 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800422 }
423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 if (Maybe<StringPiece> maybe_format =
425 xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 // An explicit format for this resource was specified. The resource will
427 // retain
428 // its type in its name, but the accepted value for this type is
429 // overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 resource_format = ParseFormatType(maybe_format.value());
431 if (!resource_format) {
432 diag_->Error(DiagMessage(out_resource->source)
433 << "'" << maybe_format.value()
434 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800435 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 }
437 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800438 } else if (resource_type == "bag") {
439 can_be_item = false;
440
441 // Bags have their type encoded in the type attribute.
442 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
443 resource_type = maybe_type.value().to_string();
444 } else {
445 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
446 << "<bag> must have a 'type' attribute");
447 return false;
448 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 }
450
451 // Get the name of the resource. This will be checked later, because not all
452 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700453 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 if (resource_type == "id") {
456 if (!maybe_name) {
457 diag_->Error(DiagMessage(out_resource->source)
458 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 << "> missing 'name' attribute");
460 return false;
461 }
462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800464 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 return true;
467 }
468
Adam Lesinski86d67df2017-01-31 13:47:27 -0800469 if (can_be_item) {
470 const auto item_iter = elToItemMap.find(resource_type);
471 if (item_iter != elToItemMap.end()) {
472 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473
Adam Lesinski86d67df2017-01-31 13:47:27 -0800474 if (!maybe_name) {
475 diag_->Error(DiagMessage(out_resource->source)
476 << "<" << parser->element_name() << "> missing 'name' attribute");
477 return false;
478 }
479
480 out_resource->name.type = item_iter->second.type;
481 out_resource->name.entry = maybe_name.value().to_string();
482
483 // Only use the implicit format for this type if it wasn't overridden.
484 if (!resource_format) {
485 resource_format = item_iter->second.format;
486 }
487
488 if (!ParseItem(parser, out_resource, resource_format)) {
489 return false;
490 }
491 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 }
494
495 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800496 if (can_be_bag) {
497 const auto bag_iter = elToBagMap.find(resource_type);
498 if (bag_iter != elToBagMap.end()) {
499 // Ensure we have a name (unless this is a <public-group>).
500 if (resource_type != "public-group") {
501 if (!maybe_name) {
502 diag_->Error(DiagMessage(out_resource->source)
503 << "<" << parser->element_name() << "> missing 'name' attribute");
504 return false;
505 }
506
507 out_resource->name.entry = maybe_name.value().to_string();
508 }
509
510 // Call the associated parse method. The type will be filled in by the
511 // parse func.
512 if (!bag_iter->second(this, parser, out_resource)) {
513 return false;
514 }
515 return true;
516 }
517 }
518
519 if (can_be_item) {
520 // Try parsing the elementName (or type) as a resource. These shall only be
521 // resources like 'layout' or 'xml' and they can only be references.
522 const ResourceType* parsed_type = ParseResourceType(resource_type);
523 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 if (!maybe_name) {
525 diag_->Error(DiagMessage(out_resource->source)
526 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 << "> missing 'name' attribute");
528 return false;
529 }
530
Adam Lesinski86d67df2017-01-31 13:47:27 -0800531 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800532 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800533 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
534 if (!out_resource->value) {
535 diag_->Error(DiagMessage(out_resource->source)
536 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
537 return false;
538 }
539 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 diag_->Warn(DiagMessage(out_resource->source)
544 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 return false;
546}
547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
549 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 const uint32_t format) {
551 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 out_resource->value = ParseXml(parser, format, kNoRawString);
556 if (!out_resource->value) {
557 diag_->Error(DiagMessage(out_resource->source) << "invalid "
558 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 return false;
560 }
561 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800562}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800563
564/**
565 * Reads the entire XML subtree and attempts to parse it as some Item,
566 * with typeMask denoting which items it can be. If allowRawValue is
567 * true, a RawString is returned if the XML couldn't be parsed as
568 * an Item. If allowRawValue is false, nullptr is returned in this
569 * case.
570 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
572 const uint32_t type_mask,
573 const bool allow_raw_value) {
574 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800575
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 std::string raw_value;
577 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800578 std::vector<UntranslatableSection> untranslatable_sections;
579 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800580 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 }
582
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800585 std::unique_ptr<StyledString> styled_string =
586 util::make_unique<StyledString>(table_->string_pool.MakeRef(
587 style_string, StringPool::Context(StringPool::Context::kStylePriority, config_)));
588 styled_string->untranslatable_sections = std::move(untranslatable_sections);
589 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 }
591
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 // name.package can be empty here, as it will assume the package name of the
594 // table.
595 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 id->SetSource(source_.WithLine(begin_xml_line));
597 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 };
599
600 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 std::unique_ptr<Item> processed_item =
602 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
603 on_create_reference);
604 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
607 TransformReferenceFromNamespace(parser, "", ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 }
611
612 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800615 std::unique_ptr<String> string = util::make_unique<String>(
616 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
617 string->untranslatable_sections = std::move(untranslatable_sections);
618 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 }
620
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 // We can't parse this so return a RawString if we are allowed.
623 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
626 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800627}
628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629bool ResourceParser::ParseString(xml::XmlPullParser* parser,
630 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 if (Maybe<StringPiece> formatted_attr =
633 xml::FindAttribute(parser, "formatted")) {
634 Maybe<bool> maybe_formatted =
635 ResourceUtils::ParseBool(formatted_attr.value());
636 if (!maybe_formatted) {
637 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 << "invalid value for 'formatted'. Must be a boolean");
639 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800640 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800643
Adam Lesinski75421622017-01-06 15:20:04 -0800644 bool translatable = options_.translatable;
645 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
646 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
647 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 << "invalid value for 'translatable'. Must be a boolean");
650 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800651 }
Adam Lesinski75421622017-01-06 15:20:04 -0800652 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 out_resource->value =
656 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
657 if (!out_resource->value) {
658 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 return false;
660 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800661
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800663 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800664
Adam Lesinski75421622017-01-06 15:20:04 -0800665 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 if (!util::VerifyJavaStringFormat(*string_value->value)) {
667 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 msg << "multiple substitutions specified in non-positional format; "
669 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 if (options_.error_on_positional_arguments) {
671 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800673 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800677 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678
Adam Lesinski75421622017-01-06 15:20:04 -0800679 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
680 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 }
682 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800683}
684
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
686 ParsedResource* out_resource) {
687 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
688 if (!maybe_type) {
689 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 << "<public> must have a 'type' attribute");
691 return false;
692 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
695 if (!parsed_type) {
696 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
697 << maybe_type.value()
698 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 return false;
700 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800701
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800703
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800704 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
705 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800707 diag_->Error(DiagMessage(out_resource->source)
708 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800710 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700718
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800721}
722
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
724 ParsedResource* out_resource) {
725 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
726 if (!maybe_type) {
727 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800729 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
733 if (!parsed_type) {
734 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
735 << maybe_type.value()
736 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800737 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 }
739
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700740 Maybe<StringPiece> maybe_id_str =
741 xml::FindNonEmptyAttribute(parser, "first-id");
742 if (!maybe_id_str) {
743 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 << "<public-group> must have a 'first-id' attribute");
745 return false;
746 }
747
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 Maybe<ResourceId> maybe_id =
749 ResourceUtils::ParseResourceId(maybe_id_str.value());
750 if (!maybe_id) {
751 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
752 << maybe_id_str.value()
753 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 return false;
755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758
759 std::string comment;
760 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 const size_t depth = parser->depth();
762 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
763 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800764 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700766 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 // Skip text.
768 continue;
769 }
770
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700771 const Source item_source = source_.WithLine(parser->line_number());
772 const std::string& element_namespace = parser->element_namespace();
773 const std::string& element_name = parser->element_name();
774 if (element_namespace.empty() && element_name == "public") {
775 Maybe<StringPiece> maybe_name =
776 xml::FindNonEmptyAttribute(parser, "name");
777 if (!maybe_name) {
778 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 << "<public> must have a 'name' attribute");
780 error = true;
781 continue;
782 }
783
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 if (xml::FindNonEmptyAttribute(parser, "id")) {
785 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 << "'id' is ignored within <public-group>");
787 error = true;
788 continue;
789 }
790
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700791 if (xml::FindNonEmptyAttribute(parser, "type")) {
792 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700793 << "'type' is ignored within <public-group>");
794 error = true;
795 continue;
796 }
797
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700798 ParsedResource child_resource;
799 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800800 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 child_resource.id = next_id;
802 child_resource.comment = std::move(comment);
803 child_resource.source = item_source;
804 child_resource.symbol_state = SymbolState::kPublic;
805 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700808
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700809 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
810 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811 error = true;
812 }
813 }
814 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800815}
816
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700817bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
818 ParsedResource* out_resource) {
819 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
820 if (!maybe_type) {
821 diag_->Error(DiagMessage(out_resource->source)
822 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823 << "> must have a 'type' attribute");
824 return false;
825 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800826
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
828 if (!parsed_type) {
829 diag_->Error(DiagMessage(out_resource->source)
830 << "invalid resource type '" << maybe_type.value() << "' in <"
831 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700832 return false;
833 }
834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800837}
838
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700839bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
840 ParsedResource* out_resource) {
841 if (ParseSymbolImpl(parser, out_resource)) {
842 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700843 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844 }
845 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800846}
847
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700848bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
849 ParsedResource* out_resource) {
850 if (ParseSymbolImpl(parser, out_resource)) {
851 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700852 return true;
853 }
854 return false;
855}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700856
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700857bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
858 ParsedResource* out_resource) {
859 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700860}
861
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700862bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
863 ParsedResource* out_resource, bool weak) {
864 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700865
866 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700867 if (out_resource->config != ConfigDescription::DefaultConfig()) {
868 diag_->Warn(DiagMessage(out_resource->source)
869 << "ignoring configuration '" << out_resource->config
870 << "' for attribute " << out_resource->name);
871 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 }
873
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700874 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700876 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
877 if (maybe_format) {
878 type_mask = ParseFormatAttribute(maybe_format.value());
879 if (type_mask == 0) {
880 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
881 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 << "'");
883 return false;
884 }
885 }
886
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700887 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700889 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
890 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
891 if (!min_str.empty()) {
892 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700893 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800898 }
899
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 if (!maybe_min) {
901 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
902 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 return false;
904 }
905 }
906
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700907 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
908 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
909 if (!max_str.empty()) {
910 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700911 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700912 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700913 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800916 }
917
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700918 if (!maybe_max) {
919 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
920 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700921 return false;
922 }
923 }
924
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 if ((maybe_min || maybe_max) &&
926 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
927 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 << "'min' and 'max' can only be used when format='integer'");
929 return false;
930 }
931
932 struct SymbolComparator {
933 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
934 return a.symbol.name.value() < b.symbol.name.value();
935 }
936 };
937
938 std::set<Attribute::Symbol, SymbolComparator> items;
939
940 std::string comment;
941 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700942 const size_t depth = parser->depth();
943 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
944 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800945 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700946 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700947 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700948 // Skip text.
949 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800950 }
951
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700952 const Source item_source = source_.WithLine(parser->line_number());
953 const std::string& element_namespace = parser->element_namespace();
954 const std::string& element_name = parser->element_name();
955 if (element_namespace.empty() &&
956 (element_name == "flag" || element_name == "enum")) {
957 if (element_name == "enum") {
958 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
959 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700960 << "can not define an <enum>; already defined a <flag>");
961 error = true;
962 continue;
963 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700964 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700965
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700966 } else if (element_name == "flag") {
967 if (type_mask & android::ResTable_map::TYPE_ENUM) {
968 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969 << "can not define a <flag>; already defined an <enum>");
970 error = true;
971 continue;
972 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700974 }
975
976 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700979 ParsedResource child_resource;
980 child_resource.name = symbol.symbol.name.value();
981 child_resource.source = item_source;
982 child_resource.value = util::make_unique<Id>();
983 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 symbol.symbol.SetComment(std::move(comment));
986 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700988 auto insert_result = items.insert(std::move(symbol));
989 if (!insert_result.second) {
990 const Attribute::Symbol& existing_symbol = *insert_result.first;
991 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700993 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700996 << "first defined here");
997 error = true;
998 }
999 } else {
1000 error = true;
1001 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001002 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1003 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001004 error = true;
1005 }
1006
1007 comment = {};
1008 }
1009
1010 if (error) {
1011 return false;
1012 }
1013
1014 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
1015 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001016 attr->type_mask =
1017 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
1018 if (maybe_min) {
1019 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001020 }
1021
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001022 if (maybe_max) {
1023 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001026 return true;
1027}
1028
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001031 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1034 if (!maybe_name) {
1035 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036 << tag << ">");
1037 return {};
1038 }
1039
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1041 if (!maybe_value) {
1042 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 << tag << ">");
1044 return {};
1045 }
1046
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001047 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001048 android::Res_value val;
1049 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001050 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 << "' for <" << tag
1052 << ">; must be an integer");
1053 return {};
1054 }
1055
1056 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001059}
1060
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1062 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001063
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1065 if (!maybe_name) {
1066 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001067 return false;
1068 }
1069
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001070 Maybe<Reference> maybe_key =
1071 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1072 if (!maybe_key) {
1073 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1074 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 return false;
1076 }
1077
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001078 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1079 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 return false;
1085 }
1086
1087 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 return true;
1090}
1091
Adam Lesinski86d67df2017-01-31 13:47:27 -08001092bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001093 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001094 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001095
1096 std::unique_ptr<Style> style = util::make_unique<Style>();
1097
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001098 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1099 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100 // If the parent is empty, we don't have a parent, but we also don't infer
1101 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001102 if (!maybe_parent.value().empty()) {
1103 std::string err_str;
1104 style->parent = ResourceUtils::ParseStyleParentReference(
1105 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001106 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001107 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001108 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 }
1110
1111 // Transform the namespace prefix to the actual package name, and mark the
1112 // reference as
1113 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001114 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001115 }
1116
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001117 } else {
1118 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001119 std::string style_name = out_resource->name.entry;
1120 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001124 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 }
1126 }
1127
1128 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001129 const size_t depth = parser->depth();
1130 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1131 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 // Skip text and comments.
1133 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001134 }
1135
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 const std::string& element_namespace = parser->element_namespace();
1137 const std::string& element_name = parser->element_name();
1138 if (element_namespace == "" && element_name == "item") {
1139 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001140
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001141 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1142 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1143 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001145 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001146 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001147
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001148 if (error) {
1149 return false;
1150 }
1151
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001152 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001153 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001154}
1155
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001156bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1157 ParsedResource* out_resource) {
1158 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001159}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001160
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001161bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1162 ParsedResource* out_resource) {
1163 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001164 android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001165}
1166
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001167bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1168 ParsedResource* out_resource) {
1169 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001170 android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001171}
1172
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001173bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1174 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001175 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001176 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001177
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001178 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001179
Adam Lesinski75421622017-01-06 15:20:04 -08001180 bool translatable = options_.translatable;
1181 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1182 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1183 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001184 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001185 << "invalid value for 'translatable'. Must be a boolean");
1186 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001187 }
Adam Lesinski75421622017-01-06 15:20:04 -08001188 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189 }
Adam Lesinski75421622017-01-06 15:20:04 -08001190 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001191
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001193 const size_t depth = parser->depth();
1194 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1195 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196 // Skip text and comments.
1197 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001198 }
1199
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001200 const Source item_source = source_.WithLine(parser->line_number());
1201 const std::string& element_namespace = parser->element_namespace();
1202 const std::string& element_name = parser->element_name();
1203 if (element_namespace.empty() && element_name == "item") {
1204 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001206 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 error = true;
1208 continue;
1209 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001210 item->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211 array->items.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001212
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001213 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1214 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1215 << "unknown tag <" << element_namespace << ":"
1216 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001217 error = true;
1218 }
1219 }
1220
1221 if (error) {
1222 return false;
1223 }
1224
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001225 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001227}
1228
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1230 ParsedResource* out_resource) {
1231 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001232
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001233 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001234
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001235 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001236 const size_t depth = parser->depth();
1237 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1238 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 // Skip text and comments.
1240 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001241 }
1242
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001243 const Source item_source = source_.WithLine(parser->line_number());
1244 const std::string& element_namespace = parser->element_namespace();
1245 const std::string& element_name = parser->element_name();
1246 if (element_namespace.empty() && element_name == "item") {
1247 Maybe<StringPiece> maybe_quantity =
1248 xml::FindNonEmptyAttribute(parser, "quantity");
1249 if (!maybe_quantity) {
1250 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001251 << "<item> in <plurals> requires attribute "
1252 << "'quantity'");
1253 error = true;
1254 continue;
1255 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001256
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001257 StringPiece trimmed_quantity =
1258 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001259 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001260 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001261 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001262 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001265 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001266 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001268 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001269 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271 index = Plural::Other;
1272 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001273 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001274 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001275 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 error = true;
1277 continue;
1278 }
1279
1280 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1282 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 error = true;
1284 continue;
1285 }
1286
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001287 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001288 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1289 error = true;
1290 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001293 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1294 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1295 << element_namespace << ":"
1296 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001297 error = true;
1298 }
1299 }
1300
1301 if (error) {
1302 return false;
1303 }
1304
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001305 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001306 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001307}
1308
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1310 ParsedResource* out_resource) {
1311 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001312
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001313 // Declare-styleable is kPrivate by default, because it technically only
1314 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001315 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001316
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1319 diag_->Warn(DiagMessage(out_resource->source)
1320 << "ignoring configuration '" << out_resource->config
1321 << "' for styleable " << out_resource->name.entry);
1322 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001323 }
1324
1325 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1326
1327 std::string comment;
1328 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 const size_t depth = parser->depth();
1330 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1331 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001332 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001335 // Ignore text.
1336 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001337 }
1338
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001339 const Source item_source = source_.WithLine(parser->line_number());
1340 const std::string& element_namespace = parser->element_namespace();
1341 const std::string& element_name = parser->element_name();
1342 if (element_namespace.empty() && element_name == "attr") {
1343 Maybe<StringPiece> maybe_name =
1344 xml::FindNonEmptyAttribute(parser, "name");
1345 if (!maybe_name) {
1346 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001347 << "<attr> tag must have a 'name' attribute");
1348 error = true;
1349 continue;
1350 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001351
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001352 // If this is a declaration, the package name may be in the name. Separate
1353 // these out.
1354 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 Maybe<Reference> maybe_ref =
1356 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1357 if (!maybe_ref) {
1358 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1359 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001360 error = true;
1361 continue;
1362 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001363
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 Reference& child_ref = maybe_ref.value();
1365 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001366
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001367 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 ParsedResource child_resource;
1369 child_resource.name = child_ref.name.value();
1370 child_resource.source = item_source;
1371 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001372
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 error = true;
1375 continue;
1376 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001377
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 child_ref.SetComment(child_resource.comment);
1380 child_ref.SetSource(item_source);
1381 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001382
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001383 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001384
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1386 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1387 << element_namespace << ":"
1388 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001390 }
1391
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 comment = {};
1393 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001394
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(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001400 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001401}
1402
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403} // namespace aapt