blob: 47ca2660f15aeaa6b11b853dc1f42f19927b12f9 [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 Lesinski7656554f2016-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 Lesinskice5e56e2016-10-21 17:56:45 -0700704 if (Maybe<StringPiece> maybe_id_str =
705 xml::FindNonEmptyAttribute(parser, "id")) {
706 Maybe<ResourceId> maybe_id =
707 ResourceUtils::ParseResourceId(maybe_id_str.value());
708 if (!maybe_id) {
709 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
710 << maybe_id.value()
711 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800713 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700721
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800724}
725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
727 ParsedResource* out_resource) {
728 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
729 if (!maybe_type) {
730 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800732 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800734
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700735 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
736 if (!parsed_type) {
737 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
738 << maybe_type.value()
739 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800740 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 }
742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 Maybe<StringPiece> maybe_id_str =
744 xml::FindNonEmptyAttribute(parser, "first-id");
745 if (!maybe_id_str) {
746 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 << "<public-group> must have a 'first-id' attribute");
748 return false;
749 }
750
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700751 Maybe<ResourceId> maybe_id =
752 ResourceUtils::ParseResourceId(maybe_id_str.value());
753 if (!maybe_id) {
754 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
755 << maybe_id_str.value()
756 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757 return false;
758 }
759
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761
762 std::string comment;
763 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700764 const size_t depth = parser->depth();
765 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
766 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800767 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700769 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 // Skip text.
771 continue;
772 }
773
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 const Source item_source = source_.WithLine(parser->line_number());
775 const std::string& element_namespace = parser->element_namespace();
776 const std::string& element_name = parser->element_name();
777 if (element_namespace.empty() && element_name == "public") {
778 Maybe<StringPiece> maybe_name =
779 xml::FindNonEmptyAttribute(parser, "name");
780 if (!maybe_name) {
781 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700782 << "<public> must have a 'name' attribute");
783 error = true;
784 continue;
785 }
786
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 if (xml::FindNonEmptyAttribute(parser, "id")) {
788 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789 << "'id' is ignored within <public-group>");
790 error = true;
791 continue;
792 }
793
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700794 if (xml::FindNonEmptyAttribute(parser, "type")) {
795 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700796 << "'type' is ignored within <public-group>");
797 error = true;
798 continue;
799 }
800
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700801 ParsedResource child_resource;
802 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800803 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700804 child_resource.id = next_id;
805 child_resource.comment = std::move(comment);
806 child_resource.source = item_source;
807 child_resource.symbol_state = SymbolState::kPublic;
808 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700810 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700811
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700812 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
813 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 error = true;
815 }
816 }
817 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800818}
819
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
821 ParsedResource* out_resource) {
822 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
823 if (!maybe_type) {
824 diag_->Error(DiagMessage(out_resource->source)
825 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700826 << "> must have a 'type' attribute");
827 return false;
828 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800829
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700830 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
831 if (!parsed_type) {
832 diag_->Error(DiagMessage(out_resource->source)
833 << "invalid resource type '" << maybe_type.value() << "' in <"
834 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700835 return false;
836 }
837
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700838 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800840}
841
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700842bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
843 ParsedResource* out_resource) {
844 if (ParseSymbolImpl(parser, out_resource)) {
845 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700846 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847 }
848 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800849}
850
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700851bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
852 ParsedResource* out_resource) {
853 if (ParseSymbolImpl(parser, out_resource)) {
854 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 return true;
856 }
857 return false;
858}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700859
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700860bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
861 ParsedResource* out_resource) {
862 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863}
864
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700865bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
866 ParsedResource* out_resource, bool weak) {
867 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868
869 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700870 if (out_resource->config != ConfigDescription::DefaultConfig()) {
871 diag_->Warn(DiagMessage(out_resource->source)
872 << "ignoring configuration '" << out_resource->config
873 << "' for attribute " << out_resource->name);
874 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 }
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700878
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700879 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
880 if (maybe_format) {
881 type_mask = ParseFormatAttribute(maybe_format.value());
882 if (type_mask == 0) {
883 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
884 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700885 << "'");
886 return false;
887 }
888 }
889
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700890 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700891
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700892 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
893 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
894 if (!min_str.empty()) {
895 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700896 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700897 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700898 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700899 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700900 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800901 }
902
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 if (!maybe_min) {
904 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
905 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906 return false;
907 }
908 }
909
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700910 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
911 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
912 if (!max_str.empty()) {
913 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700914 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700915 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700916 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700917 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800919 }
920
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 if (!maybe_max) {
922 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
923 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700924 return false;
925 }
926 }
927
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700928 if ((maybe_min || maybe_max) &&
929 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
930 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700931 << "'min' and 'max' can only be used when format='integer'");
932 return false;
933 }
934
935 struct SymbolComparator {
936 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
937 return a.symbol.name.value() < b.symbol.name.value();
938 }
939 };
940
941 std::set<Attribute::Symbol, SymbolComparator> items;
942
943 std::string comment;
944 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700945 const size_t depth = parser->depth();
946 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
947 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800948 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700949 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700950 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700951 // Skip text.
952 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800953 }
954
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700955 const Source item_source = source_.WithLine(parser->line_number());
956 const std::string& element_namespace = parser->element_namespace();
957 const std::string& element_name = parser->element_name();
958 if (element_namespace.empty() &&
959 (element_name == "flag" || element_name == "enum")) {
960 if (element_name == "enum") {
961 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
962 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963 << "can not define an <enum>; already defined a <flag>");
964 error = true;
965 continue;
966 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700968
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700969 } else if (element_name == "flag") {
970 if (type_mask & android::ResTable_map::TYPE_ENUM) {
971 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972 << "can not define a <flag>; already defined an <enum>");
973 error = true;
974 continue;
975 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700976 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700977 }
978
979 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700982 ParsedResource child_resource;
983 child_resource.name = symbol.symbol.name.value();
984 child_resource.source = item_source;
985 child_resource.value = util::make_unique<Id>();
986 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700987
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700988 symbol.symbol.SetComment(std::move(comment));
989 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700990
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700991 auto insert_result = items.insert(std::move(symbol));
992 if (!insert_result.second) {
993 const Attribute::Symbol& existing_symbol = *insert_result.first;
994 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700995 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 << "first defined here");
1000 error = true;
1001 }
1002 } else {
1003 error = true;
1004 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001005 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1006 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001007 error = true;
1008 }
1009
1010 comment = {};
1011 }
1012
1013 if (error) {
1014 return false;
1015 }
1016
1017 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
1018 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001019 attr->type_mask =
1020 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
1021 if (maybe_min) {
1022 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001023 }
1024
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 if (maybe_max) {
1026 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001028 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001029 return true;
1030}
1031
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001032Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001034 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1037 if (!maybe_name) {
1038 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039 << tag << ">");
1040 return {};
1041 }
1042
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001043 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1044 if (!maybe_value) {
1045 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001046 << tag << ">");
1047 return {};
1048 }
1049
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001050 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001051 android::Res_value val;
1052 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001053 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001054 << "' for <" << tag
1055 << ">; must be an integer");
1056 return {};
1057 }
1058
1059 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001060 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001061 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001062}
1063
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1065 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001066
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001067 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1068 if (!maybe_name) {
1069 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001070 return false;
1071 }
1072
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001073 Maybe<Reference> maybe_key =
1074 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1075 if (!maybe_key) {
1076 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1077 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001078 return false;
1079 }
1080
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001081 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1082 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001083
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001084 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001085 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001086 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087 return false;
1088 }
1089
1090 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001091 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001092 return true;
1093}
1094
Adam Lesinski86d67df2017-01-31 13:47:27 -08001095bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001096 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001097 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001098
1099 std::unique_ptr<Style> style = util::make_unique<Style>();
1100
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1102 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001103 // If the parent is empty, we don't have a parent, but we also don't infer
1104 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001105 if (!maybe_parent.value().empty()) {
1106 std::string err_str;
1107 style->parent = ResourceUtils::ParseStyleParentReference(
1108 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001109 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001110 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001111 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001112 }
1113
1114 // Transform the namespace prefix to the actual package name, and mark the
1115 // reference as
1116 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001117 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001118 }
1119
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001120 } else {
1121 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001122 std::string style_name = out_resource->name.entry;
1123 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001124 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001125 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001126 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001127 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 }
1129 }
1130
1131 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 const size_t depth = parser->depth();
1133 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1134 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001135 // Skip text and comments.
1136 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001137 }
1138
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001139 const std::string& element_namespace = parser->element_namespace();
1140 const std::string& element_name = parser->element_name();
1141 if (element_namespace == "" && element_name == "item") {
1142 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001143
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001144 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1145 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1146 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001147 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001149 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001150
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001151 if (error) {
1152 return false;
1153 }
1154
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001155 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001156 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001157}
1158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1160 ParsedResource* out_resource) {
1161 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001162}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001163
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001164bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1165 ParsedResource* out_resource) {
1166 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001167 android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001168}
1169
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001170bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1171 ParsedResource* out_resource) {
1172 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001173 android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001174}
1175
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001176bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1177 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001178 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001179 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001180
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001182
Adam Lesinski75421622017-01-06 15:20:04 -08001183 bool translatable = options_.translatable;
1184 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1185 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1186 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001188 << "invalid value for 'translatable'. Must be a boolean");
1189 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001190 }
Adam Lesinski75421622017-01-06 15:20:04 -08001191 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001192 }
Adam Lesinski75421622017-01-06 15:20:04 -08001193 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001194
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001195 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 const size_t depth = parser->depth();
1197 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1198 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001199 // Skip text and comments.
1200 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001201 }
1202
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001203 const Source item_source = source_.WithLine(parser->line_number());
1204 const std::string& element_namespace = parser->element_namespace();
1205 const std::string& element_name = parser->element_name();
1206 if (element_namespace.empty() && element_name == "item") {
1207 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001209 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001210 error = true;
1211 continue;
1212 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001213 item->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001214 array->items.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001215
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001216 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1217 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1218 << "unknown tag <" << element_namespace << ":"
1219 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001220 error = true;
1221 }
1222 }
1223
1224 if (error) {
1225 return false;
1226 }
1227
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001228 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001229 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001230}
1231
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001232bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1233 ParsedResource* out_resource) {
1234 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001235
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001236 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001237
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001239 const size_t depth = parser->depth();
1240 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1241 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 // Skip text and comments.
1243 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001244 }
1245
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 const Source item_source = source_.WithLine(parser->line_number());
1247 const std::string& element_namespace = parser->element_namespace();
1248 const std::string& element_name = parser->element_name();
1249 if (element_namespace.empty() && element_name == "item") {
1250 Maybe<StringPiece> maybe_quantity =
1251 xml::FindNonEmptyAttribute(parser, "quantity");
1252 if (!maybe_quantity) {
1253 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001254 << "<item> in <plurals> requires attribute "
1255 << "'quantity'");
1256 error = true;
1257 continue;
1258 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001259
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001260 StringPiece trimmed_quantity =
1261 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001262 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001263 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001264 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001265 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001266 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001267 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001268 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001269 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001270 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001271 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001272 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001273 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001274 index = Plural::Other;
1275 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001276 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001277 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001278 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001279 error = true;
1280 continue;
1281 }
1282
1283 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001284 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1285 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001286 error = true;
1287 continue;
1288 }
1289
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001290 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001291 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1292 error = true;
1293 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001294 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001295
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001296 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1297 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1298 << element_namespace << ":"
1299 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001300 error = true;
1301 }
1302 }
1303
1304 if (error) {
1305 return false;
1306 }
1307
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001309 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001310}
1311
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001312bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1313 ParsedResource* out_resource) {
1314 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001315
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001316 // Declare-styleable is kPrivate by default, because it technically only
1317 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001318 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001319
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001320 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001321 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1322 diag_->Warn(DiagMessage(out_resource->source)
1323 << "ignoring configuration '" << out_resource->config
1324 << "' for styleable " << out_resource->name.entry);
1325 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001326 }
1327
1328 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1329
1330 std::string comment;
1331 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001332 const size_t depth = parser->depth();
1333 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1334 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001335 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001336 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001337 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001338 // Ignore text.
1339 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001340 }
1341
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001342 const Source item_source = source_.WithLine(parser->line_number());
1343 const std::string& element_namespace = parser->element_namespace();
1344 const std::string& element_name = parser->element_name();
1345 if (element_namespace.empty() && element_name == "attr") {
1346 Maybe<StringPiece> maybe_name =
1347 xml::FindNonEmptyAttribute(parser, "name");
1348 if (!maybe_name) {
1349 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001350 << "<attr> tag must have a 'name' attribute");
1351 error = true;
1352 continue;
1353 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001354
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001355 // If this is a declaration, the package name may be in the name. Separate
1356 // these out.
1357 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 Maybe<Reference> maybe_ref =
1359 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1360 if (!maybe_ref) {
1361 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1362 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 error = true;
1364 continue;
1365 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001366
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001367 Reference& child_ref = maybe_ref.value();
1368 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001369
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001370 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 ParsedResource child_resource;
1372 child_resource.name = child_ref.name.value();
1373 child_resource.source = item_source;
1374 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001375
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001376 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001377 error = true;
1378 continue;
1379 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001380
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001382 child_ref.SetComment(child_resource.comment);
1383 child_ref.SetSource(item_source);
1384 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001385
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001386 out_resource->child_resources.push_back(std::move(child_resource));
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(item_source) << "unknown tag <"
1390 << element_namespace << ":"
1391 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001392 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001393 }
1394
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395 comment = {};
1396 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001397
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001398 if (error) {
1399 return false;
1400 }
1401
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001402 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001404}
1405
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406} // namespace aapt