blob: 90f713b67985b4e7531c144fa16ea69335bbe701 [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 Lesinski8049f3d2017-03-31 18:28:14 -0700158 // The stack elements refer to the indices in out_style_string->spans.
159 // By first adding to the out_style_string->spans vector, and then using the stack to refer
160 // to this vector, the original order of tags is preserved in cases such as <b><i>hello</b></i>.
161 std::vector<size_t> span_stack;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Adam Lesinski75421622017-01-06 15:20:04 -0800163 // Clear the output variables.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 out_raw_string->clear();
165 out_style_string->spans.clear();
Adam Lesinski75421622017-01-06 15:20:04 -0800166 out_untranslatable_sections->clear();
167
168 // The StringBuilder will concatenate the various segments of text which are initially
169 // separated by tags. It also handles unicode escape codes and quotations.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 util::StringBuilder builder;
Adam Lesinski75421622017-01-06 15:20:04 -0800171
172 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
173 Maybe<size_t> untranslatable_start_depth;
174
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 size_t depth = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
177 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800178
179 if (event == xml::XmlPullParser::Event::kStartElement) {
180 if (parser->element_namespace().empty()) {
181 // This is an HTML tag which we encode as a span. Add it to the span stack.
182 std::string span_name = parser->element_name();
183 const auto end_attr_iter = parser->end_attributes();
184 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter; ++attr_iter) {
185 span_name += ";";
186 span_name += attr_iter->name;
187 span_name += "=";
188 span_name += attr_iter->value;
189 }
190
191 // Make sure the string is representable in our binary format.
192 if (builder.Utf16Len() > std::numeric_limits<uint32_t>::max()) {
193 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
194 << "style string '" << builder.ToString() << "' is too long");
195 return false;
196 }
197
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700198 out_style_string->spans.push_back(
199 Span{std::move(span_name), static_cast<uint32_t>(builder.Utf16Len())});
200 span_stack.push_back(out_style_string->spans.size() - 1);
Adam Lesinski75421622017-01-06 15:20:04 -0800201 } else if (parser->element_namespace() == sXliffNamespaceUri) {
202 if (parser->element_name() == "g") {
203 if (untranslatable_start_depth) {
204 // We've already encountered an <xliff:g> tag, and nested <xliff:g> tags are illegal.
205 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
206 << "illegal nested XLIFF 'g' tag");
207 return false;
208 } else {
209 // Mark the start of an untranslatable section. Use UTF8 indices/lengths.
210 untranslatable_start_depth = depth;
211 const size_t current_idx = builder.ToString().size();
212 out_untranslatable_sections->push_back(UntranslatableSection{current_idx, current_idx});
213 }
214 }
215 // Ignore other xliff tags, they get handled by other tools.
216
217 } else {
218 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
219 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
220 << "ignoring element '" << parser->element_name()
221 << "' with unknown namespace '" << parser->element_namespace() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223
Adam Lesinski75421622017-01-06 15:20:04 -0800224 // Enter one level inside the element.
225 depth++;
226 } else if (event == xml::XmlPullParser::Event::kText) {
227 // Record both the raw text and append to the builder to deal with escape sequences
228 // and quotations.
229 out_raw_string->append(parser->text());
230 builder.Append(parser->text());
231 } else if (event == xml::XmlPullParser::Event::kEndElement) {
232 // Return one level from within the element.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 depth--;
234 if (depth == 0) {
235 break;
236 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinski75421622017-01-06 15:20:04 -0800238 if (parser->element_namespace().empty()) {
239 // This is an HTML tag which we encode as a span. Update the span
240 // stack and pop the top entry.
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700241 Span& top_span = out_style_string->spans[span_stack.back()];
Adam Lesinski75421622017-01-06 15:20:04 -0800242 top_span.last_char = builder.Utf16Len() - 1;
Adam Lesinski75421622017-01-06 15:20:04 -0800243 span_stack.pop_back();
244 } else if (untranslatable_start_depth == make_value(depth)) {
245 // This is the end of an untranslatable section. Use UTF8 indices/lengths.
246 UntranslatableSection& untranslatable_section = out_untranslatable_sections->back();
247 untranslatable_section.end = builder.ToString().size();
248 untranslatable_start_depth = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski75421622017-01-06 15:20:04 -0800251 // Ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 LOG(FATAL) << "unhandled XML event";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 }
255 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256
Adam Lesinski75421622017-01-06 15:20:04 -0800257 CHECK(span_stack.empty()) << "spans haven't been fully processed";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 out_style_string->str = builder.ToString();
Adam Lesinski75421622017-01-06 15:20:04 -0800259 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260}
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 const size_t depth = parser->depth();
265 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
266 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 // Skip comments and text.
268 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (!parser->element_namespace().empty() ||
272 parser->element_name() != "resources") {
273 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 << "root element must be <resources>");
275 return false;
276 }
277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 break;
280 };
281
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
283 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
284 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 return false;
286 }
287 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288}
289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
291 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 bool error = false;
294 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 const size_t depth = parser->depth();
296 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
297 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700302
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 if (!util::TrimWhitespace(parser->text()).empty()) {
305 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 << "plain text not allowed here");
307 error = true;
308 }
309 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700310 }
311
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 // Skip unknown namespace.
316 continue;
317 }
318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 std::string element_name = parser->element_name();
320 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 comment = "";
322 continue;
323 }
324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 ParsedResource parsed_resource;
326 parsed_resource.config = config_;
327 parsed_resource.source = source_.WithLine(parser->line_number());
328 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329
330 // Extract the product name if it exists.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 if (Maybe<StringPiece> maybe_product =
332 xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800333 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 }
335
336 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 error = true;
339 continue;
340 }
341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 error = true;
344 }
345 }
346
347 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 for (const ResourceName& stripped_resource : stripped_resources) {
349 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 // Failed to find the resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 diag_->Error(DiagMessage(source_)
352 << "resource '" << stripped_resource
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 << "' "
354 "was filtered out but no product variant remains");
355 error = true;
356 }
357 }
358
359 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800360}
361
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
363 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 struct ItemTypeFormat {
365 ResourceType type;
366 uint32_t format;
367 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800368
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
370 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800371
Adam Lesinski86d67df2017-01-31 13:47:27 -0800372 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
373 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
374 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
375 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
376 {"dimen",
377 {ResourceType::kDimen,
378 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
379 android::ResTable_map::TYPE_DIMENSION}},
380 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
381 {"fraction",
382 {ResourceType::kFraction,
383 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
384 android::ResTable_map::TYPE_DIMENSION}},
385 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
386 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
387 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800388
Adam Lesinski86d67df2017-01-31 13:47:27 -0800389 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
390 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
391 {"array", std::mem_fn(&ResourceParser::ParseArray)},
392 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
393 {"configVarying",
394 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
395 std::placeholders::_2, std::placeholders::_3)},
396 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
397 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
398 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
399 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
400 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
401 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
402 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
403 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
404 std::placeholders::_2, std::placeholders::_3)},
405 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
406 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800409
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800412
Adam Lesinski86d67df2017-01-31 13:47:27 -0800413 bool can_be_item = true;
414 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800416 can_be_bag = false;
417
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 // Items have their type encoded in the type attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 if (Maybe<StringPiece> maybe_type =
420 xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800421 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 << "<item> must have a 'type' attribute");
425 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800426 }
427
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if (Maybe<StringPiece> maybe_format =
429 xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 // An explicit format for this resource was specified. The resource will
431 // retain
432 // its type in its name, but the accepted value for this type is
433 // overridden.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 resource_format = ParseFormatType(maybe_format.value());
435 if (!resource_format) {
436 diag_->Error(DiagMessage(out_resource->source)
437 << "'" << maybe_format.value()
438 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800439 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 }
441 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800442 } else if (resource_type == "bag") {
443 can_be_item = false;
444
445 // Bags have their type encoded in the type attribute.
446 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
447 resource_type = maybe_type.value().to_string();
448 } else {
449 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
450 << "<bag> must have a 'type' attribute");
451 return false;
452 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 }
454
455 // Get the name of the resource. This will be checked later, because not all
456 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 if (resource_type == "id") {
460 if (!maybe_name) {
461 diag_->Error(DiagMessage(out_resource->source)
462 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 << "> missing 'name' attribute");
464 return false;
465 }
466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800468 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 return true;
471 }
472
Adam Lesinski86d67df2017-01-31 13:47:27 -0800473 if (can_be_item) {
474 const auto item_iter = elToItemMap.find(resource_type);
475 if (item_iter != elToItemMap.end()) {
476 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477
Adam Lesinski86d67df2017-01-31 13:47:27 -0800478 if (!maybe_name) {
479 diag_->Error(DiagMessage(out_resource->source)
480 << "<" << parser->element_name() << "> missing 'name' attribute");
481 return false;
482 }
483
484 out_resource->name.type = item_iter->second.type;
485 out_resource->name.entry = maybe_name.value().to_string();
486
487 // Only use the implicit format for this type if it wasn't overridden.
488 if (!resource_format) {
489 resource_format = item_iter->second.format;
490 }
491
492 if (!ParseItem(parser, out_resource, resource_format)) {
493 return false;
494 }
495 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 }
498
499 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800500 if (can_be_bag) {
501 const auto bag_iter = elToBagMap.find(resource_type);
502 if (bag_iter != elToBagMap.end()) {
503 // Ensure we have a name (unless this is a <public-group>).
504 if (resource_type != "public-group") {
505 if (!maybe_name) {
506 diag_->Error(DiagMessage(out_resource->source)
507 << "<" << parser->element_name() << "> missing 'name' attribute");
508 return false;
509 }
510
511 out_resource->name.entry = maybe_name.value().to_string();
512 }
513
514 // Call the associated parse method. The type will be filled in by the
515 // parse func.
516 if (!bag_iter->second(this, parser, out_resource)) {
517 return false;
518 }
519 return true;
520 }
521 }
522
523 if (can_be_item) {
524 // Try parsing the elementName (or type) as a resource. These shall only be
525 // resources like 'layout' or 'xml' and they can only be references.
526 const ResourceType* parsed_type = ParseResourceType(resource_type);
527 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 if (!maybe_name) {
529 diag_->Error(DiagMessage(out_resource->source)
530 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 << "> missing 'name' attribute");
532 return false;
533 }
534
Adam Lesinski86d67df2017-01-31 13:47:27 -0800535 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800536 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800537 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
538 if (!out_resource->value) {
539 diag_->Error(DiagMessage(out_resource->source)
540 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
541 return false;
542 }
543 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 }
546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 diag_->Warn(DiagMessage(out_resource->source)
548 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 return false;
550}
551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
553 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 const uint32_t format) {
555 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 }
558
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 out_resource->value = ParseXml(parser, format, kNoRawString);
560 if (!out_resource->value) {
561 diag_->Error(DiagMessage(out_resource->source) << "invalid "
562 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 return false;
564 }
565 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800566}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567
568/**
569 * Reads the entire XML subtree and attempts to parse it as some Item,
570 * with typeMask denoting which items it can be. If allowRawValue is
571 * true, a RawString is returned if the XML couldn't be parsed as
572 * an Item. If allowRawValue is false, nullptr is returned in this
573 * case.
574 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
576 const uint32_t type_mask,
577 const bool allow_raw_value) {
578 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 std::string raw_value;
581 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800582 std::vector<UntranslatableSection> untranslatable_sections;
583 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800584 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 }
586
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700587 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800589 std::unique_ptr<StyledString> styled_string =
590 util::make_unique<StyledString>(table_->string_pool.MakeRef(
591 style_string, StringPool::Context(StringPool::Context::kStylePriority, config_)));
592 styled_string->untranslatable_sections = std::move(untranslatable_sections);
593 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 }
595
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 // name.package can be empty here, as it will assume the package name of the
598 // table.
599 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 id->SetSource(source_.WithLine(begin_xml_line));
601 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 };
603
604 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 std::unique_ptr<Item> processed_item =
606 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
607 on_create_reference);
608 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
611 TransformReferenceFromNamespace(parser, "", ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 }
615
616 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800619 std::unique_ptr<String> string = util::make_unique<String>(
620 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
621 string->untranslatable_sections = std::move(untranslatable_sections);
622 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 }
624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 // We can't parse this so return a RawString if we are allowed.
627 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 }
630 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800631}
632
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633bool ResourceParser::ParseString(xml::XmlPullParser* parser,
634 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if (Maybe<StringPiece> formatted_attr =
637 xml::FindAttribute(parser, "formatted")) {
638 Maybe<bool> maybe_formatted =
639 ResourceUtils::ParseBool(formatted_attr.value());
640 if (!maybe_formatted) {
641 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 << "invalid value for 'formatted'. Must be a boolean");
643 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800644 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800647
Adam Lesinski75421622017-01-06 15:20:04 -0800648 bool translatable = options_.translatable;
649 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
650 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
651 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 << "invalid value for 'translatable'. Must be a boolean");
654 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800655 }
Adam Lesinski75421622017-01-06 15:20:04 -0800656 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800658
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 out_resource->value =
660 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
661 if (!out_resource->value) {
662 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 return false;
664 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800665
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800667 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800668
Adam Lesinski75421622017-01-06 15:20:04 -0800669 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 if (!util::VerifyJavaStringFormat(*string_value->value)) {
671 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 msg << "multiple substitutions specified in non-positional format; "
673 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 if (options_.error_on_positional_arguments) {
675 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800677 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800681 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682
Adam Lesinski75421622017-01-06 15:20:04 -0800683 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
684 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 }
686 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800687}
688
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
690 ParsedResource* out_resource) {
691 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
692 if (!maybe_type) {
693 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 << "<public> must have a 'type' attribute");
695 return false;
696 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800697
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
699 if (!parsed_type) {
700 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
701 << maybe_type.value()
702 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 return false;
704 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800705
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800707
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800708 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
709 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800711 diag_->Error(DiagMessage(out_resource->source)
712 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800714 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800717
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700718 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700722
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800725}
726
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
728 ParsedResource* out_resource) {
729 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
730 if (!maybe_type) {
731 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800733 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800735
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
737 if (!parsed_type) {
738 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
739 << maybe_type.value()
740 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800741 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 }
743
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 Maybe<StringPiece> maybe_id_str =
745 xml::FindNonEmptyAttribute(parser, "first-id");
746 if (!maybe_id_str) {
747 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 << "<public-group> must have a 'first-id' attribute");
749 return false;
750 }
751
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700752 Maybe<ResourceId> maybe_id =
753 ResourceUtils::ParseResourceId(maybe_id_str.value());
754 if (!maybe_id) {
755 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
756 << maybe_id_str.value()
757 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 return false;
759 }
760
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762
763 std::string comment;
764 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 const size_t depth = parser->depth();
766 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
767 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800768 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 // Skip text.
772 continue;
773 }
774
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700775 const Source item_source = source_.WithLine(parser->line_number());
776 const std::string& element_namespace = parser->element_namespace();
777 const std::string& element_name = parser->element_name();
778 if (element_namespace.empty() && element_name == "public") {
779 Maybe<StringPiece> maybe_name =
780 xml::FindNonEmptyAttribute(parser, "name");
781 if (!maybe_name) {
782 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 << "<public> must have a 'name' attribute");
784 error = true;
785 continue;
786 }
787
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700788 if (xml::FindNonEmptyAttribute(parser, "id")) {
789 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 << "'id' is ignored within <public-group>");
791 error = true;
792 continue;
793 }
794
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700795 if (xml::FindNonEmptyAttribute(parser, "type")) {
796 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 << "'type' is ignored within <public-group>");
798 error = true;
799 continue;
800 }
801
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802 ParsedResource child_resource;
803 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800804 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700805 child_resource.id = next_id;
806 child_resource.comment = std::move(comment);
807 child_resource.source = item_source;
808 child_resource.symbol_state = SymbolState::kPublic;
809 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700811 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700813 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
814 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 error = true;
816 }
817 }
818 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800819}
820
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700821bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
822 ParsedResource* out_resource) {
823 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
824 if (!maybe_type) {
825 diag_->Error(DiagMessage(out_resource->source)
826 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700827 << "> must have a 'type' attribute");
828 return false;
829 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800830
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700831 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
832 if (!parsed_type) {
833 diag_->Error(DiagMessage(out_resource->source)
834 << "invalid resource type '" << maybe_type.value() << "' in <"
835 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700836 return false;
837 }
838
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700839 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800841}
842
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
844 ParsedResource* out_resource) {
845 if (ParseSymbolImpl(parser, out_resource)) {
846 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700847 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700848 }
849 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800850}
851
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700852bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
853 ParsedResource* out_resource) {
854 if (ParseSymbolImpl(parser, out_resource)) {
855 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700856 return true;
857 }
858 return false;
859}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700860
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700861bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
862 ParsedResource* out_resource) {
863 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864}
865
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700866bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
867 ParsedResource* out_resource, bool weak) {
868 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700869
870 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 if (out_resource->config != ConfigDescription::DefaultConfig()) {
872 diag_->Warn(DiagMessage(out_resource->source)
873 << "ignoring configuration '" << out_resource->config
874 << "' for attribute " << out_resource->name);
875 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700876 }
877
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700878 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700880 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
881 if (maybe_format) {
882 type_mask = ParseFormatAttribute(maybe_format.value());
883 if (type_mask == 0) {
884 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
885 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700886 << "'");
887 return false;
888 }
889 }
890
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700891 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700892
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700893 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
894 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
895 if (!min_str.empty()) {
896 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700897 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700898 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700900 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700901 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800902 }
903
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700904 if (!maybe_min) {
905 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
906 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700907 return false;
908 }
909 }
910
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700911 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
912 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
913 if (!max_str.empty()) {
914 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700915 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700916 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700917 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700918 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700919 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800920 }
921
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700922 if (!maybe_max) {
923 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
924 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700925 return false;
926 }
927 }
928
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700929 if ((maybe_min || maybe_max) &&
930 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
931 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700932 << "'min' and 'max' can only be used when format='integer'");
933 return false;
934 }
935
936 struct SymbolComparator {
937 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
938 return a.symbol.name.value() < b.symbol.name.value();
939 }
940 };
941
942 std::set<Attribute::Symbol, SymbolComparator> items;
943
944 std::string comment;
945 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700946 const size_t depth = parser->depth();
947 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
948 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800949 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700950 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700951 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700952 // Skip text.
953 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800954 }
955
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700956 const Source item_source = source_.WithLine(parser->line_number());
957 const std::string& element_namespace = parser->element_namespace();
958 const std::string& element_name = parser->element_name();
959 if (element_namespace.empty() &&
960 (element_name == "flag" || element_name == "enum")) {
961 if (element_name == "enum") {
962 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
963 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700964 << "can not define an <enum>; already defined a <flag>");
965 error = true;
966 continue;
967 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700968 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700969
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700970 } else if (element_name == "flag") {
971 if (type_mask & android::ResTable_map::TYPE_ENUM) {
972 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700973 << "can not define a <flag>; already defined an <enum>");
974 error = true;
975 continue;
976 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700978 }
979
980 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700981 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700983 ParsedResource child_resource;
984 child_resource.name = symbol.symbol.name.value();
985 child_resource.source = item_source;
986 child_resource.value = util::make_unique<Id>();
987 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700988
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700989 symbol.symbol.SetComment(std::move(comment));
990 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 auto insert_result = items.insert(std::move(symbol));
993 if (!insert_result.second) {
994 const Attribute::Symbol& existing_symbol = *insert_result.first;
995 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700996 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700997 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700998
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700999 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001000 << "first defined here");
1001 error = true;
1002 }
1003 } else {
1004 error = true;
1005 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001006 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1007 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001008 error = true;
1009 }
1010
1011 comment = {};
1012 }
1013
1014 if (error) {
1015 return false;
1016 }
1017
1018 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
1019 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001020 attr->type_mask =
1021 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
1022 if (maybe_min) {
1023 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001024 }
1025
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001026 if (maybe_max) {
1027 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001028 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001030 return true;
1031}
1032
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001033Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001034 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001035 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001036
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001037 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1038 if (!maybe_name) {
1039 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001040 << tag << ">");
1041 return {};
1042 }
1043
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001044 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1045 if (!maybe_value) {
1046 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001047 << tag << ">");
1048 return {};
1049 }
1050
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001051 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001052 android::Res_value val;
1053 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 << "' for <" << tag
1056 << ">; must be an integer");
1057 return {};
1058 }
1059
1060 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001061 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001062 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001063}
1064
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001065bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1066 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001067
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001068 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1069 if (!maybe_name) {
1070 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071 return false;
1072 }
1073
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001074 Maybe<Reference> maybe_key =
1075 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1076 if (!maybe_key) {
1077 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1078 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001079 return false;
1080 }
1081
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001082 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1083 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001085 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001086 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001087 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001088 return false;
1089 }
1090
1091 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001092 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093 return true;
1094}
1095
Adam Lesinski86d67df2017-01-31 13:47:27 -08001096bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001097 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001098 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001099
1100 std::unique_ptr<Style> style = util::make_unique<Style>();
1101
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001102 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1103 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001104 // If the parent is empty, we don't have a parent, but we also don't infer
1105 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001106 if (!maybe_parent.value().empty()) {
1107 std::string err_str;
1108 style->parent = ResourceUtils::ParseStyleParentReference(
1109 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001111 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001112 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 }
1114
1115 // Transform the namespace prefix to the actual package name, and mark the
1116 // reference as
1117 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001118 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001119 }
1120
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001121 } else {
1122 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001123 std::string style_name = out_resource->name.entry;
1124 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001125 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001128 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001129 }
1130 }
1131
1132 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001133 const size_t depth = parser->depth();
1134 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1135 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001136 // Skip text and comments.
1137 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001138 }
1139
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001140 const std::string& element_namespace = parser->element_namespace();
1141 const std::string& element_name = parser->element_name();
1142 if (element_namespace == "" && element_name == "item") {
1143 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001144
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001145 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1146 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1147 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001148 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001150 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001151
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 if (error) {
1153 return false;
1154 }
1155
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001156 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001157 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001158}
1159
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001160bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1161 ParsedResource* out_resource) {
1162 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001163}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001164
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1166 ParsedResource* out_resource) {
1167 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001169}
1170
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001171bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1172 ParsedResource* out_resource) {
1173 return ParseArrayImpl(parser, out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001175}
1176
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001177bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1178 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001179 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001180 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001181
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001182 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001183
Adam Lesinski75421622017-01-06 15:20:04 -08001184 bool translatable = options_.translatable;
1185 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1186 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1187 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001188 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189 << "invalid value for 'translatable'. Must be a boolean");
1190 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001191 }
Adam Lesinski75421622017-01-06 15:20:04 -08001192 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193 }
Adam Lesinski75421622017-01-06 15:20:04 -08001194 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001195
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001197 const size_t depth = parser->depth();
1198 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1199 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200 // Skip text and comments.
1201 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001202 }
1203
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 const Source item_source = source_.WithLine(parser->line_number());
1205 const std::string& element_namespace = parser->element_namespace();
1206 const std::string& element_name = parser->element_name();
1207 if (element_namespace.empty() && element_name == "item") {
1208 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001209 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001210 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001211 error = true;
1212 continue;
1213 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001214 item->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001215 array->items.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001216
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001217 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1218 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1219 << "unknown tag <" << element_namespace << ":"
1220 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001221 error = true;
1222 }
1223 }
1224
1225 if (error) {
1226 return false;
1227 }
1228
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001229 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001230 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001231}
1232
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001233bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1234 ParsedResource* out_resource) {
1235 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001236
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001238
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001239 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240 const size_t depth = parser->depth();
1241 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1242 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001243 // Skip text and comments.
1244 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001245 }
1246
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 const Source item_source = source_.WithLine(parser->line_number());
1248 const std::string& element_namespace = parser->element_namespace();
1249 const std::string& element_name = parser->element_name();
1250 if (element_namespace.empty() && element_name == "item") {
1251 Maybe<StringPiece> maybe_quantity =
1252 xml::FindNonEmptyAttribute(parser, "quantity");
1253 if (!maybe_quantity) {
1254 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001255 << "<item> in <plurals> requires attribute "
1256 << "'quantity'");
1257 error = true;
1258 continue;
1259 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001260
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001261 StringPiece trimmed_quantity =
1262 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001263 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001264 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001265 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001266 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001267 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001268 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001269 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001270 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001271 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001272 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001273 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001274 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001275 index = Plural::Other;
1276 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 error = true;
1281 continue;
1282 }
1283
1284 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001285 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1286 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287 error = true;
1288 continue;
1289 }
1290
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001292 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1293 error = true;
1294 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001295 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001296
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001297 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1298 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1299 << element_namespace << ":"
1300 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001301 error = true;
1302 }
1303 }
1304
1305 if (error) {
1306 return false;
1307 }
1308
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001309 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001310 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001311}
1312
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001313bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1314 ParsedResource* out_resource) {
1315 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001316
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 // Declare-styleable is kPrivate by default, because it technically only
1318 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001319 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001320
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001321 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001322 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1323 diag_->Warn(DiagMessage(out_resource->source)
1324 << "ignoring configuration '" << out_resource->config
1325 << "' for styleable " << out_resource->name.entry);
1326 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001327 }
1328
1329 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1330
1331 std::string comment;
1332 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001333 const size_t depth = parser->depth();
1334 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1335 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001336 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001337 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001338 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001339 // Ignore text.
1340 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001341 }
1342
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001343 const Source item_source = source_.WithLine(parser->line_number());
1344 const std::string& element_namespace = parser->element_namespace();
1345 const std::string& element_name = parser->element_name();
1346 if (element_namespace.empty() && element_name == "attr") {
1347 Maybe<StringPiece> maybe_name =
1348 xml::FindNonEmptyAttribute(parser, "name");
1349 if (!maybe_name) {
1350 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001351 << "<attr> tag must have a 'name' attribute");
1352 error = true;
1353 continue;
1354 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001355
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001356 // If this is a declaration, the package name may be in the name. Separate
1357 // these out.
1358 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001359 Maybe<Reference> maybe_ref =
1360 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1361 if (!maybe_ref) {
1362 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1363 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001364 error = true;
1365 continue;
1366 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001367
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 Reference& child_ref = maybe_ref.value();
1369 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001370
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001372 ParsedResource child_resource;
1373 child_resource.name = child_ref.name.value();
1374 child_resource.source = item_source;
1375 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001376
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001377 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 error = true;
1379 continue;
1380 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001381
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001382 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001383 child_ref.SetComment(child_resource.comment);
1384 child_ref.SetSource(item_source);
1385 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001386
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001387 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001388
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1390 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1391 << element_namespace << ":"
1392 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001393 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001394 }
1395
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001396 comment = {};
1397 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001398
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001399 if (error) {
1400 return false;
1401 }
1402
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001405}
1406
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001407} // namespace aapt