blob: 7cffeea6fe2c5d27d6ea0a87df1f5c011d2b8610 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080017#include "ResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <functional>
Adam Lesinski73bff1e2017-12-08 16:06:10 -080020#include <limits>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <sstream>
22
23#include "android-base/logging.h"
24
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ResourceTable.h"
26#include "ResourceUtils.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ValueVisitor.h"
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080029#include "util/ImmutableMap.h"
Adam Lesinski75421622017-01-06 15:20:04 -080030#include "util/Maybe.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070031#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080032#include "xml/XmlPullParser.h"
Adam Lesinski9e10ac72015-10-16 14:37:48 -070033
Adam Lesinski71be7052017-12-12 16:48:07 -080034using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080035
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036namespace aapt {
37
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070038constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070040// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
41static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080043}
44
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070045static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
46 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070048 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070050 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070052 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070054 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070056 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070058 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070060 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070062 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080064}
65
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070066static uint32_t ParseFormatType(const StringPiece& piece) {
67 if (piece == "enum") {
68 return android::ResTable_map::TYPE_ENUM;
69 } else if (piece == "flags") {
70 return android::ResTable_map::TYPE_FLAGS;
71 }
72 return ParseFormatTypeNoEnumsOrFlags(piece);
73}
74
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 for (StringPiece part : util::Tokenize(str, '|')) {
78 StringPiece trimmed_part = util::TrimWhitespace(part);
79 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 if (type == 0) {
81 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080082 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 mask |= type;
84 }
85 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080086}
87
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070088// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080089struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 ResourceName name;
91 ConfigDescription config;
92 std::string product;
93 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 ResourceId id;
Adam Lesinski71be7052017-12-12 16:48:07 -080096 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -070097 bool allow_new = false;
Adam Lesinski71be7052017-12-12 16:48:07 -080098 bool overlayable = false;
99
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 std::string comment;
101 std::unique_ptr<Value> value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 std::list<ParsedResource> child_resources;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800103};
104
105// Recursively adds resources to the ResourceTable.
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700106static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
108 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800110 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 }
Adam Lesinski7656554f2016-03-10 21:55:04 -0800112
Adam Lesinski71be7052017-12-12 16:48:07 -0800113 if (res->visibility_level != Visibility::Level::kUndefined) {
114 Visibility visibility;
115 visibility.level = res->visibility_level;
116 visibility.source = res->source;
117 visibility.comment = res->comment;
118 if (!table->SetVisibilityWithId(res->name, visibility, res->id, diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800120 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800122
Adam Lesinski71be7052017-12-12 16:48:07 -0800123 if (res->allow_new) {
124 AllowNew allow_new;
125 allow_new.source = res->source;
126 allow_new.comment = res->comment;
127 if (!table->SetAllowNew(res->name, allow_new, diag)) {
128 return false;
129 }
130 }
131
132 if (res->overlayable) {
133 Overlayable overlayable;
134 overlayable.source = res->source;
135 overlayable.comment = res->comment;
136 if (!table->SetOverlayable(res->name, overlayable, diag)) {
137 return false;
138 }
139 }
140
141 if (res->value != nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 // Attach the comment, source and config to the value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 res->value->SetComment(std::move(res->comment));
144 res->value->SetSource(std::move(res->source));
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800145
Adam Lesinski71be7052017-12-12 16:48:07 -0800146 if (!table->AddResourceWithId(res->name, res->id, res->config, res->product,
147 std::move(res->value), diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 for (ParsedResource& child : res->child_resources) {
154 error |= !AddResourcesToTable(table, diag, &child);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
156 return !error;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800157}
158
159// Convenient aliases for more readable function calls.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160enum { kAllowRawString = true, kNoRawString = false };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
163 const Source& source,
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700164 const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 const ResourceParserOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 : diag_(diag),
167 table_(table),
168 source_(source),
169 config_(config),
170 options_(options) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171
172/**
173 * Build a string from XML that converts nested elements into Span objects.
174 */
Adam Lesinski75421622017-01-06 15:20:04 -0800175bool ResourceParser::FlattenXmlSubtree(
176 xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string,
177 std::vector<UntranslatableSection>* out_untranslatable_sections) {
178 // Keeps track of formatting tags (<b>, <i>) and the range of characters for which they apply.
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700179 // The stack elements refer to the indices in out_style_string->spans.
180 // By first adding to the out_style_string->spans vector, and then using the stack to refer
181 // to this vector, the original order of tags is preserved in cases such as <b><i>hello</b></i>.
182 std::vector<size_t> span_stack;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Adam Lesinski75421622017-01-06 15:20:04 -0800184 // Clear the output variables.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 out_raw_string->clear();
186 out_style_string->spans.clear();
Adam Lesinski75421622017-01-06 15:20:04 -0800187 out_untranslatable_sections->clear();
188
189 // The StringBuilder will concatenate the various segments of text which are initially
190 // separated by tags. It also handles unicode escape codes and quotations.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 util::StringBuilder builder;
Adam Lesinski75421622017-01-06 15:20:04 -0800192
193 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
194 Maybe<size_t> untranslatable_start_depth;
195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 size_t depth = 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
198 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinski75421622017-01-06 15:20:04 -0800199
200 if (event == xml::XmlPullParser::Event::kStartElement) {
201 if (parser->element_namespace().empty()) {
202 // This is an HTML tag which we encode as a span. Add it to the span stack.
203 std::string span_name = parser->element_name();
204 const auto end_attr_iter = parser->end_attributes();
205 for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter; ++attr_iter) {
206 span_name += ";";
207 span_name += attr_iter->name;
208 span_name += "=";
209 span_name += attr_iter->value;
210 }
211
212 // Make sure the string is representable in our binary format.
213 if (builder.Utf16Len() > std::numeric_limits<uint32_t>::max()) {
214 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
215 << "style string '" << builder.ToString() << "' is too long");
216 return false;
217 }
218
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700219 out_style_string->spans.push_back(
220 Span{std::move(span_name), static_cast<uint32_t>(builder.Utf16Len())});
221 span_stack.push_back(out_style_string->spans.size() - 1);
Adam Lesinski75421622017-01-06 15:20:04 -0800222 } else if (parser->element_namespace() == sXliffNamespaceUri) {
223 if (parser->element_name() == "g") {
224 if (untranslatable_start_depth) {
225 // We've already encountered an <xliff:g> tag, and nested <xliff:g> tags are illegal.
226 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
227 << "illegal nested XLIFF 'g' tag");
228 return false;
229 } else {
230 // Mark the start of an untranslatable section. Use UTF8 indices/lengths.
231 untranslatable_start_depth = depth;
232 const size_t current_idx = builder.ToString().size();
233 out_untranslatable_sections->push_back(UntranslatableSection{current_idx, current_idx});
234 }
235 }
236 // Ignore other xliff tags, they get handled by other tools.
237
238 } else {
239 // Besides XLIFF, any other namespaced tag is unsupported and ignored.
240 diag_->Warn(DiagMessage(source_.WithLine(parser->line_number()))
241 << "ignoring element '" << parser->element_name()
242 << "' with unknown namespace '" << parser->element_namespace() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244
Adam Lesinski75421622017-01-06 15:20:04 -0800245 // Enter one level inside the element.
246 depth++;
247 } else if (event == xml::XmlPullParser::Event::kText) {
248 // Record both the raw text and append to the builder to deal with escape sequences
249 // and quotations.
250 out_raw_string->append(parser->text());
251 builder.Append(parser->text());
252 } else if (event == xml::XmlPullParser::Event::kEndElement) {
253 // Return one level from within the element.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 depth--;
255 if (depth == 0) {
256 break;
257 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258
Adam Lesinski75421622017-01-06 15:20:04 -0800259 if (parser->element_namespace().empty()) {
260 // This is an HTML tag which we encode as a span. Update the span
261 // stack and pop the top entry.
Adam Lesinski8049f3d2017-03-31 18:28:14 -0700262 Span& top_span = out_style_string->spans[span_stack.back()];
Adam Lesinski75421622017-01-06 15:20:04 -0800263 top_span.last_char = builder.Utf16Len() - 1;
Adam Lesinski75421622017-01-06 15:20:04 -0800264 span_stack.pop_back();
265 } else if (untranslatable_start_depth == make_value(depth)) {
266 // This is the end of an untranslatable section. Use UTF8 indices/lengths.
267 UntranslatableSection& untranslatable_section = out_untranslatable_sections->back();
268 untranslatable_section.end = builder.ToString().size();
269 untranslatable_start_depth = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 } else if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinski75421622017-01-06 15:20:04 -0800272 // Ignore.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 LOG(FATAL) << "unhandled XML event";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 }
276 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277
Adam Lesinski75421622017-01-06 15:20:04 -0800278 CHECK(span_stack.empty()) << "spans haven't been fully processed";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 out_style_string->str = builder.ToString();
Adam Lesinski75421622017-01-06 15:20:04 -0800280 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283bool ResourceParser::Parse(xml::XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 const size_t depth = parser->depth();
286 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
287 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 // Skip comments and text.
289 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291
Adam Lesinski060b53d2017-07-28 17:10:35 -0700292 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 << "root element must be <resources>");
295 return false;
296 }
297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 break;
300 };
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
303 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
304 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 return false;
306 }
307 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308}
309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
311 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700312
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 bool error = false;
314 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 const size_t depth = parser->depth();
316 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
317 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800321 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700322
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 if (!util::TrimWhitespace(parser->text()).empty()) {
325 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 << "plain text not allowed here");
327 error = true;
328 }
329 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700330 }
331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 // Skip unknown namespace.
336 continue;
337 }
338
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 std::string element_name = parser->element_name();
340 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 comment = "";
342 continue;
343 }
344
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 ParsedResource parsed_resource;
346 parsed_resource.config = config_;
347 parsed_resource.source = source_.WithLine(parser->line_number());
348 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349
350 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700351 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800352 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 }
354
355 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 error = true;
358 continue;
359 }
360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 error = true;
363 }
364 }
365
366 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 for (const ResourceName& stripped_resource : stripped_resources) {
368 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700370 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
371 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 error = true;
373 }
374 }
375
376 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800377}
378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
380 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 struct ItemTypeFormat {
382 ResourceType type;
383 uint32_t format;
384 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800385
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
387 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800388
Adam Lesinski86d67df2017-01-31 13:47:27 -0800389 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
390 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
391 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
392 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
393 {"dimen",
394 {ResourceType::kDimen,
395 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
396 android::ResTable_map::TYPE_DIMENSION}},
397 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
398 {"fraction",
399 {ResourceType::kFraction,
400 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
401 android::ResTable_map::TYPE_DIMENSION}},
402 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
403 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
404 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800405
Adam Lesinski86d67df2017-01-31 13:47:27 -0800406 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
407 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
408 {"array", std::mem_fn(&ResourceParser::ParseArray)},
409 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
410 {"configVarying",
411 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
412 std::placeholders::_2, std::placeholders::_3)},
413 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
414 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
415 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
Adam Lesinski46c4d722017-08-23 13:03:56 -0700416 {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)},
Adam Lesinski86d67df2017-01-31 13:47:27 -0800417 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
418 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
419 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
420 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
421 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
422 std::placeholders::_2, std::placeholders::_3)},
423 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
424 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800425
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800427
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800430
Adam Lesinski86d67df2017-01-31 13:47:27 -0800431 bool can_be_item = true;
432 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800434 can_be_bag = false;
435
Adam Lesinskie597d682017-06-01 17:16:44 -0700436 // The default format for <item> is any. If a format attribute is present, that one will
437 // override the default.
438 resource_format = android::ResTable_map::TYPE_ANY;
439
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700441 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800442 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 << "<item> must have a 'type' attribute");
446 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800447 }
448
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700449 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700451 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700453 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 if (!resource_format) {
455 diag_->Error(DiagMessage(out_resource->source)
456 << "'" << maybe_format.value()
457 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800458 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 }
460 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800461 } else if (resource_type == "bag") {
462 can_be_item = false;
463
464 // Bags have their type encoded in the type attribute.
465 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
466 resource_type = maybe_type.value().to_string();
467 } else {
468 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
469 << "<bag> must have a 'type' attribute");
470 return false;
471 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 }
473
474 // Get the name of the resource. This will be checked later, because not all
475 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if (resource_type == "id") {
479 if (!maybe_name) {
480 diag_->Error(DiagMessage(out_resource->source)
481 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 << "> missing 'name' attribute");
483 return false;
484 }
485
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800487 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 return true;
490 }
491
Adam Lesinski86d67df2017-01-31 13:47:27 -0800492 if (can_be_item) {
493 const auto item_iter = elToItemMap.find(resource_type);
494 if (item_iter != elToItemMap.end()) {
495 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700496
Adam Lesinski86d67df2017-01-31 13:47:27 -0800497 if (!maybe_name) {
498 diag_->Error(DiagMessage(out_resource->source)
499 << "<" << parser->element_name() << "> missing 'name' attribute");
500 return false;
501 }
502
503 out_resource->name.type = item_iter->second.type;
504 out_resource->name.entry = maybe_name.value().to_string();
505
Adam Lesinskie597d682017-06-01 17:16:44 -0700506 // Only use the implied format of the type when there is no explicit format.
507 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800508 resource_format = item_iter->second.format;
509 }
510
511 if (!ParseItem(parser, out_resource, resource_format)) {
512 return false;
513 }
514 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 }
517
518 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800519 if (can_be_bag) {
520 const auto bag_iter = elToBagMap.find(resource_type);
521 if (bag_iter != elToBagMap.end()) {
522 // Ensure we have a name (unless this is a <public-group>).
Adam Lesinski46c4d722017-08-23 13:03:56 -0700523 if (resource_type != "public-group" && resource_type != "overlayable") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800524 if (!maybe_name) {
525 diag_->Error(DiagMessage(out_resource->source)
526 << "<" << parser->element_name() << "> missing 'name' attribute");
527 return false;
528 }
529
530 out_resource->name.entry = maybe_name.value().to_string();
531 }
532
533 // Call the associated parse method. The type will be filled in by the
534 // parse func.
535 if (!bag_iter->second(this, parser, out_resource)) {
536 return false;
537 }
538 return true;
539 }
540 }
541
542 if (can_be_item) {
543 // Try parsing the elementName (or type) as a resource. These shall only be
544 // resources like 'layout' or 'xml' and they can only be references.
545 const ResourceType* parsed_type = ParseResourceType(resource_type);
546 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 if (!maybe_name) {
548 diag_->Error(DiagMessage(out_resource->source)
549 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 << "> missing 'name' attribute");
551 return false;
552 }
553
Adam Lesinski86d67df2017-01-31 13:47:27 -0800554 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800555 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800556 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
557 if (!out_resource->value) {
558 diag_->Error(DiagMessage(out_resource->source)
559 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
560 return false;
561 }
562 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 }
565
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 diag_->Warn(DiagMessage(out_resource->source)
567 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 return false;
569}
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
572 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 const uint32_t format) {
574 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 }
577
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 out_resource->value = ParseXml(parser, format, kNoRawString);
579 if (!out_resource->value) {
580 diag_->Error(DiagMessage(out_resource->source) << "invalid "
581 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 return false;
583 }
584 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800585}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800586
587/**
588 * Reads the entire XML subtree and attempts to parse it as some Item,
589 * with typeMask denoting which items it can be. If allowRawValue is
590 * true, a RawString is returned if the XML couldn't be parsed as
591 * an Item. If allowRawValue is false, nullptr is returned in this
592 * case.
593 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
595 const uint32_t type_mask,
596 const bool allow_raw_value) {
597 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 std::string raw_value;
600 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800601 std::vector<UntranslatableSection> untranslatable_sections;
602 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800603 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 }
605
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700606 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800608 std::unique_ptr<StyledString> styled_string =
609 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700610 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800611 styled_string->untranslatable_sections = std::move(untranslatable_sections);
612 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 }
614
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 // name.package can be empty here, as it will assume the package name of the
617 // table.
618 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 id->SetSource(source_.WithLine(begin_xml_line));
620 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 };
622
623 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 std::unique_ptr<Item> processed_item =
Adam Lesinski71be7052017-12-12 16:48:07 -0800625 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700629 ResolvePackage(parser, ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 }
633
634 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800637 std::unique_ptr<String> string = util::make_unique<String>(
638 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
639 string->untranslatable_sections = std::move(untranslatable_sections);
640 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 }
642
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700643 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
644 if (util::TrimWhitespace(raw_value).empty()) {
645 return ResourceUtils::MakeNull();
646 }
647
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 // We can't parse this so return a RawString if we are allowed.
650 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 }
653 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800654}
655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656bool ResourceParser::ParseString(xml::XmlPullParser* parser,
657 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 if (Maybe<StringPiece> formatted_attr =
660 xml::FindAttribute(parser, "formatted")) {
661 Maybe<bool> maybe_formatted =
662 ResourceUtils::ParseBool(formatted_attr.value());
663 if (!maybe_formatted) {
664 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 << "invalid value for 'formatted'. Must be a boolean");
666 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800667 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700669 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800670
Adam Lesinski75421622017-01-06 15:20:04 -0800671 bool translatable = options_.translatable;
672 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
673 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
674 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 << "invalid value for 'translatable'. Must be a boolean");
677 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800678 }
Adam Lesinski75421622017-01-06 15:20:04 -0800679 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 out_resource->value =
683 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
684 if (!out_resource->value) {
685 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700686 return false;
687 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800688
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800690 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800691
Adam Lesinski75421622017-01-06 15:20:04 -0800692 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 if (!util::VerifyJavaStringFormat(*string_value->value)) {
694 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 msg << "multiple substitutions specified in non-positional format; "
696 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 if (options_.error_on_positional_arguments) {
698 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800700 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800701
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800704 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705
Adam Lesinski75421622017-01-06 15:20:04 -0800706 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
707 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 }
709 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800710}
711
Adam Lesinski71be7052017-12-12 16:48:07 -0800712bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinski46c4d722017-08-23 13:03:56 -0700713 if (out_resource->config != ConfigDescription::DefaultConfig()) {
714 diag_->Warn(DiagMessage(out_resource->source)
715 << "ignoring configuration '" << out_resource->config << "' for <public> tag");
716 }
717
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700718 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
719 if (!maybe_type) {
720 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 << "<public> must have a 'type' attribute");
722 return false;
723 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
726 if (!parsed_type) {
727 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
728 << maybe_type.value()
729 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 return false;
731 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800732
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800734
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800735 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
736 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700737 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800738 diag_->Error(DiagMessage(out_resource->source)
739 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800741 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800744
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700749
Adam Lesinski71be7052017-12-12 16:48:07 -0800750 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800752}
753
Adam Lesinski46c4d722017-08-23 13:03:56 -0700754bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) {
755 if (out_resource->config != ConfigDescription::DefaultConfig()) {
756 diag_->Warn(DiagMessage(out_resource->source)
757 << "ignoring configuration '" << out_resource->config
758 << "' for <public-group> tag");
759 }
760
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700761 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
762 if (!maybe_type) {
763 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800765 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800767
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700768 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
769 if (!parsed_type) {
770 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
771 << maybe_type.value()
772 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800773 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774 }
775
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700776 Maybe<StringPiece> maybe_id_str =
777 xml::FindNonEmptyAttribute(parser, "first-id");
778 if (!maybe_id_str) {
779 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780 << "<public-group> must have a 'first-id' attribute");
781 return false;
782 }
783
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700784 Maybe<ResourceId> maybe_id =
785 ResourceUtils::ParseResourceId(maybe_id_str.value());
786 if (!maybe_id) {
787 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
788 << maybe_id_str.value()
789 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 return false;
791 }
792
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700793 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794
795 std::string comment;
796 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 const size_t depth = parser->depth();
798 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
799 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800800 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700801 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700802 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 // Skip text.
804 continue;
805 }
806
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 const Source item_source = source_.WithLine(parser->line_number());
808 const std::string& element_namespace = parser->element_namespace();
809 const std::string& element_name = parser->element_name();
810 if (element_namespace.empty() && element_name == "public") {
811 Maybe<StringPiece> maybe_name =
812 xml::FindNonEmptyAttribute(parser, "name");
813 if (!maybe_name) {
814 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700815 << "<public> must have a 'name' attribute");
816 error = true;
817 continue;
818 }
819
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700820 if (xml::FindNonEmptyAttribute(parser, "id")) {
821 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 << "'id' is ignored within <public-group>");
823 error = true;
824 continue;
825 }
826
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700827 if (xml::FindNonEmptyAttribute(parser, "type")) {
828 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 << "'type' is ignored within <public-group>");
830 error = true;
831 continue;
832 }
833
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700834 ParsedResource child_resource;
835 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800836 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700837 child_resource.id = next_id;
838 child_resource.comment = std::move(comment);
839 child_resource.source = item_source;
Adam Lesinski71be7052017-12-12 16:48:07 -0800840 child_resource.visibility_level = Visibility::Level::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700843 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700844
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
846 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700847 error = true;
848 }
849 }
850 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800851}
852
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700853bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
854 ParsedResource* out_resource) {
855 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
856 if (!maybe_type) {
857 diag_->Error(DiagMessage(out_resource->source)
858 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 << "> must have a 'type' attribute");
860 return false;
861 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800862
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700863 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
864 if (!parsed_type) {
865 diag_->Error(DiagMessage(out_resource->source)
866 << "invalid resource type '" << maybe_type.value() << "' in <"
867 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700868 return false;
869 }
870
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700871 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800873}
874
Adam Lesinski46c4d722017-08-23 13:03:56 -0700875bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) {
876 if (out_resource->config != ConfigDescription::DefaultConfig()) {
877 diag_->Warn(DiagMessage(out_resource->source)
878 << "ignoring configuration '" << out_resource->config << "' for <"
879 << parser->element_name() << "> tag");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700880 }
Adam Lesinski46c4d722017-08-23 13:03:56 -0700881
882 if (!ParseSymbolImpl(parser, out_resource)) {
883 return false;
884 }
885
Adam Lesinski71be7052017-12-12 16:48:07 -0800886 out_resource->visibility_level = Visibility::Level::kPrivate;
Adam Lesinski46c4d722017-08-23 13:03:56 -0700887 return true;
888}
889
890bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) {
891 if (out_resource->config != ConfigDescription::DefaultConfig()) {
892 diag_->Warn(DiagMessage(out_resource->source)
893 << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag");
894 }
895
896 if (Maybe<StringPiece> maybe_policy = xml::FindNonEmptyAttribute(parser, "policy")) {
897 const StringPiece& policy = maybe_policy.value();
898 if (policy != "system") {
899 diag_->Error(DiagMessage(out_resource->source)
900 << "<overlayable> has invalid policy '" << policy << "'");
901 return false;
902 }
903 }
904
905 bool error = false;
906 const size_t depth = parser->depth();
907 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
908 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
909 // Skip text/comments.
910 continue;
911 }
912
913 const Source item_source = source_.WithLine(parser->line_number());
914 const std::string& element_namespace = parser->element_namespace();
915 const std::string& element_name = parser->element_name();
916 if (element_namespace.empty() && element_name == "item") {
917 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
918 if (!maybe_name) {
919 diag_->Error(DiagMessage(item_source)
920 << "<item> within an <overlayable> tag must have a 'name' attribute");
921 error = true;
922 continue;
923 }
924
925 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
926 if (!maybe_type) {
927 diag_->Error(DiagMessage(item_source)
928 << "<item> within an <overlayable> tag must have a 'type' attribute");
929 error = true;
930 continue;
931 }
932
933 const ResourceType* type = ParseResourceType(maybe_type.value());
934 if (type == nullptr) {
935 diag_->Error(DiagMessage(out_resource->source)
936 << "invalid resource type '" << maybe_type.value()
937 << "' in <item> within an <overlayable>");
938 error = true;
939 continue;
940 }
941
Adam Lesinski71be7052017-12-12 16:48:07 -0800942 ParsedResource child_resource;
943 child_resource.name.type = *type;
944 child_resource.name.entry = maybe_name.value().to_string();
945 child_resource.source = item_source;
946 child_resource.overlayable = true;
947 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski46c4d722017-08-23 13:03:56 -0700948
949 xml::XmlPullParser::SkipCurrentElement(parser);
950 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
951 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
952 error = true;
953 }
954 }
955 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800956}
957
Adam Lesinski71be7052017-12-12 16:48:07 -0800958bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700959 if (ParseSymbolImpl(parser, out_resource)) {
Adam Lesinski71be7052017-12-12 16:48:07 -0800960 out_resource->visibility_level = Visibility::Level::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700961 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700962 return true;
963 }
964 return false;
965}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700966
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700967bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
968 ParsedResource* out_resource) {
969 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700970}
971
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700972bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
973 ParsedResource* out_resource, bool weak) {
974 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700975
976 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700977 if (out_resource->config != ConfigDescription::DefaultConfig()) {
978 diag_->Warn(DiagMessage(out_resource->source)
979 << "ignoring configuration '" << out_resource->config
980 << "' for attribute " << out_resource->name);
981 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700982 }
983
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
987 if (maybe_format) {
988 type_mask = ParseFormatAttribute(maybe_format.value());
989 if (type_mask == 0) {
990 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800991 << "invalid attribute format '" << maybe_format.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700992 return false;
993 }
994 }
995
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700996 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700997
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700998 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
999 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
1000 if (!min_str.empty()) {
1001 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001002 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001003 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001004 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001005 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001006 }
1007
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001008 if (!maybe_min) {
1009 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1010 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 return false;
1012 }
1013 }
1014
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001015 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
1016 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
1017 if (!max_str.empty()) {
1018 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001019 android::Res_value value;
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001020 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001021 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001022 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001023 }
1024
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001025 if (!maybe_max) {
1026 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1027 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001028 return false;
1029 }
1030 }
1031
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001032 if ((maybe_min || maybe_max) &&
1033 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
1034 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001035 << "'min' and 'max' can only be used when format='integer'");
1036 return false;
1037 }
1038
1039 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -07001040 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001041 return a.symbol.name.value() < b.symbol.name.value();
1042 }
1043 };
1044
1045 std::set<Attribute::Symbol, SymbolComparator> items;
1046
1047 std::string comment;
1048 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001049 const size_t depth = parser->depth();
1050 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1051 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001052 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001053 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 // Skip text.
1056 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001057 }
1058
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001059 const Source item_source = source_.WithLine(parser->line_number());
1060 const std::string& element_namespace = parser->element_namespace();
1061 const std::string& element_name = parser->element_name();
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001062 if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001063 if (element_name == "enum") {
1064 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
1065 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001066 << "can not define an <enum>; already defined a <flag>");
1067 error = true;
1068 continue;
1069 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001070 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001071
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001072 } else if (element_name == "flag") {
1073 if (type_mask & android::ResTable_map::TYPE_ENUM) {
1074 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001075 << "can not define a <flag>; already defined an <enum>");
1076 error = true;
1077 continue;
1078 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001079 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001080 }
1081
1082 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001083 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001084 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001085 ParsedResource child_resource;
1086 child_resource.name = symbol.symbol.name.value();
1087 child_resource.source = item_source;
1088 child_resource.value = util::make_unique<Id>();
1089 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001090
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001091 symbol.symbol.SetComment(std::move(comment));
1092 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001093
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001094 auto insert_result = items.insert(std::move(symbol));
1095 if (!insert_result.second) {
1096 const Attribute::Symbol& existing_symbol = *insert_result.first;
1097 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001098 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001099 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001100
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001101 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102 << "first defined here");
1103 error = true;
1104 }
1105 } else {
1106 error = true;
1107 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001108 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1109 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001110 error = true;
1111 }
1112
1113 comment = {};
1114 }
1115
1116 if (error) {
1117 return false;
1118 }
1119
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001120 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(
1121 type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY});
1122 attr->SetWeak(weak);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001123 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001124 attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min());
1125 attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001127 return true;
1128}
1129
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001130Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001131 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001132 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001133
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001134 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1135 if (!maybe_name) {
1136 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001137 << tag << ">");
1138 return {};
1139 }
1140
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001141 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1142 if (!maybe_value) {
1143 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001144 << tag << ">");
1145 return {};
1146 }
1147
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001149 android::Res_value val;
1150 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001151 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001152 << "' for <" << tag
1153 << ">; must be an integer");
1154 return {};
1155 }
1156
1157 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001158 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001159 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001160}
1161
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001162bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1163 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001164
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001165 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1166 if (!maybe_name) {
1167 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001168 return false;
1169 }
1170
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001171 Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001172 if (!maybe_key) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001173 diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001174 return false;
1175 }
1176
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001177 ResolvePackage(parser, &maybe_key.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001178 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001179
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001180 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001181 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001182 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001183 return false;
1184 }
1185
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001186 style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001187 return true;
1188}
1189
Adam Lesinski86d67df2017-01-31 13:47:27 -08001190bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001191 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001192 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001193
1194 std::unique_ptr<Style> style = util::make_unique<Style>();
1195
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001196 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1197 if (maybe_parent) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001198 // If the parent is empty, we don't have a parent, but we also don't infer either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001199 if (!maybe_parent.value().empty()) {
1200 std::string err_str;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001201 style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001202 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001203 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001204 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001205 }
1206
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001207 // Transform the namespace prefix to the actual package name, and mark the reference as
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001208 // private if appropriate.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001209 ResolvePackage(parser, &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001210 }
1211
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001212 } else {
1213 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001214 std::string style_name = out_resource->name.entry;
1215 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001216 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001217 style->parent_inferred = true;
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001218 style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001219 }
1220 }
1221
1222 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001223 const size_t depth = parser->depth();
1224 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1225 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001226 // Skip text and comments.
1227 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001228 }
1229
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001230 const std::string& element_namespace = parser->element_namespace();
1231 const std::string& element_name = parser->element_name();
1232 if (element_namespace == "" && element_name == "item") {
1233 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001234
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001235 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1236 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1237 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001238 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001239 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001240 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001241
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001242 if (error) {
1243 return false;
1244 }
1245
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001246 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001247 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001248}
1249
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001250bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1251 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1252 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1253 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1254 if (resource_format == 0u) {
1255 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1256 << "'" << format_attr.value() << "' is an invalid format");
1257 return false;
1258 }
1259 }
1260 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001261}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001262
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001263bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1264 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001265}
1266
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001267bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1268 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001269}
1270
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001271bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1272 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001273 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001274 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001275
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001277
Adam Lesinski75421622017-01-06 15:20:04 -08001278 bool translatable = options_.translatable;
1279 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1280 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1281 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001282 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001283 << "invalid value for 'translatable'. Must be a boolean");
1284 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001285 }
Adam Lesinski75421622017-01-06 15:20:04 -08001286 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287 }
Adam Lesinski75421622017-01-06 15:20:04 -08001288 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001289
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001290 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001291 const size_t depth = parser->depth();
1292 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1293 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001294 // Skip text and comments.
1295 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001296 }
1297
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 const Source item_source = source_.WithLine(parser->line_number());
1299 const std::string& element_namespace = parser->element_namespace();
1300 const std::string& element_name = parser->element_name();
1301 if (element_namespace.empty() && element_name == "item") {
1302 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001303 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001305 error = true;
1306 continue;
1307 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001308 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001309 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001310
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001311 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1312 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1313 << "unknown tag <" << element_namespace << ":"
1314 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001315 error = true;
1316 }
1317 }
1318
1319 if (error) {
1320 return false;
1321 }
1322
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001323 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001325}
1326
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001327bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1328 ParsedResource* out_resource) {
1329 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001330
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001331 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001332
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001333 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001334 const size_t depth = parser->depth();
1335 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1336 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001337 // Skip text and comments.
1338 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001339 }
1340
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001341 const Source item_source = source_.WithLine(parser->line_number());
1342 const std::string& element_namespace = parser->element_namespace();
1343 const std::string& element_name = parser->element_name();
1344 if (element_namespace.empty() && element_name == "item") {
1345 Maybe<StringPiece> maybe_quantity =
1346 xml::FindNonEmptyAttribute(parser, "quantity");
1347 if (!maybe_quantity) {
1348 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001349 << "<item> in <plurals> requires attribute "
1350 << "'quantity'");
1351 error = true;
1352 continue;
1353 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001354
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001355 StringPiece trimmed_quantity =
1356 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001357 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001358 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001359 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001360 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001361 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001362 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001364 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001365 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001367 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001368 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001369 index = Plural::Other;
1370 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001371 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001372 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001373 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001374 error = true;
1375 continue;
1376 }
1377
1378 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1380 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001381 error = true;
1382 continue;
1383 }
1384
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001385 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001386 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1387 error = true;
1388 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001389 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001390
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001391 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1392 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1393 << element_namespace << ":"
1394 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001395 error = true;
1396 }
1397 }
1398
1399 if (error) {
1400 return false;
1401 }
1402
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001403 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001404 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001405}
1406
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001407bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1408 ParsedResource* out_resource) {
1409 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001410
Adam Lesinski71be7052017-12-12 16:48:07 -08001411 // Declare-styleable is kPrivate by default, because it technically only exists in R.java.
1412 out_resource->visibility_level = Visibility::Level::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001413
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001415 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1416 diag_->Warn(DiagMessage(out_resource->source)
1417 << "ignoring configuration '" << out_resource->config
1418 << "' for styleable " << out_resource->name.entry);
1419 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001420 }
1421
1422 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1423
1424 std::string comment;
1425 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001426 const size_t depth = parser->depth();
1427 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1428 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001429 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001430 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001431 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001432 // Ignore text.
1433 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001434 }
1435
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001436 const Source item_source = source_.WithLine(parser->line_number());
1437 const std::string& element_namespace = parser->element_namespace();
1438 const std::string& element_name = parser->element_name();
1439 if (element_namespace.empty() && element_name == "attr") {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001440 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001441 if (!maybe_name) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001442 diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001443 error = true;
1444 continue;
1445 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001446
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001447 // If this is a declaration, the package name may be in the name. Separate
1448 // these out.
1449 // Eg. <attr name="android:text" />
Adam Lesinski73bff1e2017-12-08 16:06:10 -08001450 Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001451 if (!maybe_ref) {
1452 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1453 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001454 error = true;
1455 continue;
1456 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001457
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001458 Reference& child_ref = maybe_ref.value();
Adam Lesinski1ef0fa92017-08-15 21:32:49 -07001459 xml::ResolvePackage(parser, &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001460
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001461 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001462 ParsedResource child_resource;
1463 child_resource.name = child_ref.name.value();
1464 child_resource.source = item_source;
1465 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001466
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001467 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001468 error = true;
1469 continue;
1470 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001471
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001472 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001473 child_ref.SetComment(child_resource.comment);
1474 child_ref.SetSource(item_source);
1475 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001476
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001477 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001478
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001479 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1480 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1481 << element_namespace << ":"
1482 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001483 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001484 }
1485
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001486 comment = {};
1487 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001488
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001489 if (error) {
1490 return false;
1491 }
1492
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001493 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001494 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001495}
1496
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001497} // namespace aapt