blob: 1c3ac2ad4f1719d9f561fcc265d223b1a367aaaa [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 Lesinskid5fd76a2017-05-16 12:18:08 -070037constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070039// Returns true if the element is <skip> or <eat-comment> and can be safely ignored.
40static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 return ns.empty() && (name == "skip" || name == "eat-comment");
Adam Lesinski27afb9e2015-11-06 18:25:04 -080042}
43
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070044static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) {
45 if (piece == "reference") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 return android::ResTable_map::TYPE_REFERENCE;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070047 } else if (piece == "string") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 return android::ResTable_map::TYPE_STRING;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070049 } else if (piece == "integer") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 return android::ResTable_map::TYPE_INTEGER;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070051 } else if (piece == "boolean") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 return android::ResTable_map::TYPE_BOOLEAN;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070053 } else if (piece == "color") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 return android::ResTable_map::TYPE_COLOR;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070055 } else if (piece == "float") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return android::ResTable_map::TYPE_FLOAT;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070057 } else if (piece == "dimension") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 return android::ResTable_map::TYPE_DIMENSION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070059 } else if (piece == "fraction") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return android::ResTable_map::TYPE_FRACTION;
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070061 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080063}
64
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070065static uint32_t ParseFormatType(const StringPiece& piece) {
66 if (piece == "enum") {
67 return android::ResTable_map::TYPE_ENUM;
68 } else if (piece == "flags") {
69 return android::ResTable_map::TYPE_FLAGS;
70 }
71 return ParseFormatTypeNoEnumsOrFlags(piece);
72}
73
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074static uint32_t ParseFormatAttribute(const StringPiece& str) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 uint32_t mask = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 for (StringPiece part : util::Tokenize(str, '|')) {
77 StringPiece trimmed_part = util::TrimWhitespace(part);
78 uint32_t type = ParseFormatType(trimmed_part);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 if (type == 0) {
80 return 0;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080081 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 mask |= type;
83 }
84 return mask;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080085}
86
Adam Lesinskid5fd76a2017-05-16 12:18:08 -070087// A parsed resource ready to be added to the ResourceTable.
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080088struct ParsedResource {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 ResourceName name;
90 ConfigDescription config;
91 std::string product;
92 Source source;
93 ResourceId id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 Maybe<SymbolState> symbol_state;
Adam Lesinski4488f1c2017-05-26 17:33:38 -070095 bool allow_new = false;
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 Lesinski4488f1c2017-05-26 17:33:38 -0700102static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
104 if (trimmed_comment.size() != res->comment.size()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 // Only if there was a change do we re-assign.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106 res->comment = trimmed_comment.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
Adam Lesinski76565542016-03-10 21:55:04 -0800108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 if (res->symbol_state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 symbol.state = res->symbol_state.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 symbol.source = res->source;
113 symbol.comment = res->comment;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700114 symbol.allow_new = res->allow_new;
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 Lesinski4488f1c2017-05-26 17:33:38 -0700125 if (!table->AddResource(res->name, res->id, res->config, res->product, std::move(res->value),
126 diag)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 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 Lesinski060b53d2017-07-28 17:10:35 -0700271 if (!parser->element_namespace().empty() || parser->element_name() != "resources") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 << "root element must be <resources>");
274 return false;
275 }
276
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 error |= !ParseResources(parser);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 break;
279 };
280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
282 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
283 << "xml parser error: " << parser->error());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return false;
285 }
286 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287}
288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
290 std::set<ResourceName> stripped_resources;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700291
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 bool error = false;
293 std::string comment;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 const size_t depth = parser->depth();
295 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
296 const xml::XmlPullParser::Event event = parser->event();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 if (event == xml::XmlPullParser::Event::kComment) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 comment = parser->comment();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700301
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 if (event == xml::XmlPullParser::Event::kText) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 if (!util::TrimWhitespace(parser->text()).empty()) {
304 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 << "plain text not allowed here");
306 error = true;
307 }
308 continue;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700309 }
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 CHECK(event == xml::XmlPullParser::Event::kStartElement);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 if (!parser->element_namespace().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 // Skip unknown namespace.
315 continue;
316 }
317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 std::string element_name = parser->element_name();
319 if (element_name == "skip" || element_name == "eat-comment") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 comment = "";
321 continue;
322 }
323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 ParsedResource parsed_resource;
325 parsed_resource.config = config_;
326 parsed_resource.source = source_.WithLine(parser->line_number());
327 parsed_resource.comment = std::move(comment);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328
329 // Extract the product name if it exists.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700330 if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800331 parsed_resource.product = maybe_product.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 }
333
334 // Parse the resource regardless of product.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 if (!ParseResource(parser, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 error = true;
337 continue;
338 }
339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 error = true;
342 }
343 }
344
345 // Check that we included at least one variant of each stripped resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700346 for (const ResourceName& stripped_resource : stripped_resources) {
347 if (!table_->FindResource(stripped_resource)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 // Failed to find the resource.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700349 diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource
350 << "' was filtered out but no product variant remains");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 error = true;
352 }
353 }
354
355 return !error;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356}
357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
359 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 struct ItemTypeFormat {
361 ResourceType type;
362 uint32_t format;
363 };
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800364
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
366 ParsedResource*)>;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800367
Adam Lesinski86d67df2017-01-31 13:47:27 -0800368 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
369 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
370 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
371 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
372 {"dimen",
373 {ResourceType::kDimen,
374 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
375 android::ResTable_map::TYPE_DIMENSION}},
376 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
377 {"fraction",
378 {ResourceType::kFraction,
379 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
380 android::ResTable_map::TYPE_DIMENSION}},
381 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
382 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
383 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800384
Adam Lesinski86d67df2017-01-31 13:47:27 -0800385 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
386 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
387 {"array", std::mem_fn(&ResourceParser::ParseArray)},
388 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
389 {"configVarying",
390 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
391 std::placeholders::_2, std::placeholders::_3)},
392 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
393 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
394 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
395 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
396 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
397 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
398 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
399 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
400 std::placeholders::_2, std::placeholders::_3)},
401 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
402 });
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 std::string resource_type = parser->element_name();
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800405
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 // The value format accepted for this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 uint32_t resource_format = 0u;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800408
Adam Lesinski86d67df2017-01-31 13:47:27 -0800409 bool can_be_item = true;
410 bool can_be_bag = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 if (resource_type == "item") {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800412 can_be_bag = false;
413
Adam Lesinskie597d682017-06-01 17:16:44 -0700414 // The default format for <item> is any. If a format attribute is present, that one will
415 // override the default.
416 resource_format = android::ResTable_map::TYPE_ANY;
417
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 // Items have their type encoded in the type attribute.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700419 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800420 resource_type = maybe_type.value().to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 << "<item> must have a 'type' attribute");
424 return false;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800425 }
426
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700427 if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 // An explicit format for this resource was specified. The resource will
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700429 // retain its type in its name, but the accepted value for this type is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 // overridden.
Adam Lesinskid5fd76a2017-05-16 12:18:08 -0700431 resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 if (!resource_format) {
433 diag_->Error(DiagMessage(out_resource->source)
434 << "'" << maybe_format.value()
435 << "' is an invalid format");
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800436 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 }
438 }
Adam Lesinski86d67df2017-01-31 13:47:27 -0800439 } else if (resource_type == "bag") {
440 can_be_item = false;
441
442 // Bags have their type encoded in the type attribute.
443 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
444 resource_type = maybe_type.value().to_string();
445 } else {
446 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
447 << "<bag> must have a 'type' attribute");
448 return false;
449 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 }
451
452 // Get the name of the resource. This will be checked later, because not all
453 // XML elements require a name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 if (resource_type == "id") {
457 if (!maybe_name) {
458 diag_->Error(DiagMessage(out_resource->source)
459 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 << "> missing 'name' attribute");
461 return false;
462 }
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 out_resource->name.type = ResourceType::kId;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800465 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 return true;
468 }
469
Adam Lesinski86d67df2017-01-31 13:47:27 -0800470 if (can_be_item) {
471 const auto item_iter = elToItemMap.find(resource_type);
472 if (item_iter != elToItemMap.end()) {
473 // This is an item, record its type and format and start parsing.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474
Adam Lesinski86d67df2017-01-31 13:47:27 -0800475 if (!maybe_name) {
476 diag_->Error(DiagMessage(out_resource->source)
477 << "<" << parser->element_name() << "> missing 'name' attribute");
478 return false;
479 }
480
481 out_resource->name.type = item_iter->second.type;
482 out_resource->name.entry = maybe_name.value().to_string();
483
Adam Lesinskie597d682017-06-01 17:16:44 -0700484 // Only use the implied format of the type when there is no explicit format.
485 if (resource_format == 0u) {
Adam Lesinski86d67df2017-01-31 13:47:27 -0800486 resource_format = item_iter->second.format;
487 }
488
489 if (!ParseItem(parser, out_resource, resource_format)) {
490 return false;
491 }
492 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 }
495
496 // This might be a bag or something.
Adam Lesinski86d67df2017-01-31 13:47:27 -0800497 if (can_be_bag) {
498 const auto bag_iter = elToBagMap.find(resource_type);
499 if (bag_iter != elToBagMap.end()) {
500 // Ensure we have a name (unless this is a <public-group>).
501 if (resource_type != "public-group") {
502 if (!maybe_name) {
503 diag_->Error(DiagMessage(out_resource->source)
504 << "<" << parser->element_name() << "> missing 'name' attribute");
505 return false;
506 }
507
508 out_resource->name.entry = maybe_name.value().to_string();
509 }
510
511 // Call the associated parse method. The type will be filled in by the
512 // parse func.
513 if (!bag_iter->second(this, parser, out_resource)) {
514 return false;
515 }
516 return true;
517 }
518 }
519
520 if (can_be_item) {
521 // Try parsing the elementName (or type) as a resource. These shall only be
522 // resources like 'layout' or 'xml' and they can only be references.
523 const ResourceType* parsed_type = ParseResourceType(resource_type);
524 if (parsed_type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700525 if (!maybe_name) {
526 diag_->Error(DiagMessage(out_resource->source)
527 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 << "> missing 'name' attribute");
529 return false;
530 }
531
Adam Lesinski86d67df2017-01-31 13:47:27 -0800532 out_resource->name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800533 out_resource->name.entry = maybe_name.value().to_string();
Adam Lesinski86d67df2017-01-31 13:47:27 -0800534 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
535 if (!out_resource->value) {
536 diag_->Error(DiagMessage(out_resource->source)
537 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
538 return false;
539 }
540 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 }
543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 diag_->Warn(DiagMessage(out_resource->source)
545 << "unknown resource type '" << parser->element_name() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 return false;
547}
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
550 ParsedResource* out_resource,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 const uint32_t format) {
552 if (format == android::ResTable_map::TYPE_STRING) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 return ParseString(parser, out_resource);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554 }
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 out_resource->value = ParseXml(parser, format, kNoRawString);
557 if (!out_resource->value) {
558 diag_->Error(DiagMessage(out_resource->source) << "invalid "
559 << out_resource->name.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 return false;
561 }
562 return true;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800563}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800564
565/**
566 * Reads the entire XML subtree and attempts to parse it as some Item,
567 * with typeMask denoting which items it can be. If allowRawValue is
568 * true, a RawString is returned if the XML couldn't be parsed as
569 * an Item. If allowRawValue is false, nullptr is returned in this
570 * case.
571 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
573 const uint32_t type_mask,
574 const bool allow_raw_value) {
575 const size_t begin_xml_line = parser->line_number();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800576
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 std::string raw_value;
578 StyleString style_string;
Adam Lesinski75421622017-01-06 15:20:04 -0800579 std::vector<UntranslatableSection> untranslatable_sections;
580 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800581 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 }
583
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 if (!style_string.spans.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 // This can only be a StyledString.
Adam Lesinski75421622017-01-06 15:20:04 -0800586 std::unique_ptr<StyledString> styled_string =
587 util::make_unique<StyledString>(table_->string_pool.MakeRef(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700588 style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_)));
Adam Lesinski75421622017-01-06 15:20:04 -0800589 styled_string->untranslatable_sections = std::move(untranslatable_sections);
590 return std::move(styled_string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 }
592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 auto on_create_reference = [&](const ResourceName& name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 // name.package can be empty here, as it will assume the package name of the
595 // table.
596 std::unique_ptr<Id> id = util::make_unique<Id>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 id->SetSource(source_.WithLine(begin_xml_line));
598 table_->AddResource(name, {}, {}, std::move(id), diag_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 };
600
601 // Process the raw value.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 std::unique_ptr<Item> processed_item =
603 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
604 on_create_reference);
605 if (processed_item) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 // Fix up the reference.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
608 TransformReferenceFromNamespace(parser, "", ref);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 return processed_item;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 }
612
613 // Try making a regular string.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700614 if (type_mask & android::ResTable_map::TYPE_STRING) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 // Use the trimmed, escaped string.
Adam Lesinski75421622017-01-06 15:20:04 -0800616 std::unique_ptr<String> string = util::make_unique<String>(
617 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
618 string->untranslatable_sections = std::move(untranslatable_sections);
619 return std::move(string);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 }
621
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700622 // If the text is empty, and the value is not allowed to be a string, encode it as a @null.
623 if (util::TrimWhitespace(raw_value).empty()) {
624 return ResourceUtils::MakeNull();
625 }
626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 if (allow_raw_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 // We can't parse this so return a RawString if we are allowed.
629 return util::make_unique<RawString>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 }
632 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800633}
634
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635bool ResourceParser::ParseString(xml::XmlPullParser* parser,
636 ParsedResource* out_resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 bool formatted = true;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 if (Maybe<StringPiece> formatted_attr =
639 xml::FindAttribute(parser, "formatted")) {
640 Maybe<bool> maybe_formatted =
641 ResourceUtils::ParseBool(formatted_attr.value());
642 if (!maybe_formatted) {
643 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 << "invalid value for 'formatted'. Must be a boolean");
645 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800646 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 formatted = maybe_formatted.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800649
Adam Lesinski75421622017-01-06 15:20:04 -0800650 bool translatable = options_.translatable;
651 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
652 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
653 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700654 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700655 << "invalid value for 'translatable'. Must be a boolean");
656 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800657 }
Adam Lesinski75421622017-01-06 15:20:04 -0800658 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800660
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 out_resource->value =
662 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
663 if (!out_resource->value) {
664 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665 return false;
666 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800667
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
Adam Lesinski75421622017-01-06 15:20:04 -0800669 string_value->SetTranslatable(translatable);
Adam Lesinski393b5f02015-12-17 13:03:11 -0800670
Adam Lesinski75421622017-01-06 15:20:04 -0800671 if (formatted && translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 if (!util::VerifyJavaStringFormat(*string_value->value)) {
673 DiagMessage msg(out_resource->source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 msg << "multiple substitutions specified in non-positional format; "
675 "did you mean to add the formatted=\"false\" attribute?";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 if (options_.error_on_positional_arguments) {
677 diag_->Error(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 return false;
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800679 }
Adam Lesinski393b5f02015-12-17 13:03:11 -0800680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 diag_->Warn(msg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 }
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800683 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684
Adam Lesinski75421622017-01-06 15:20:04 -0800685 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
686 string_value->SetTranslatable(translatable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 }
688 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800689}
690
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
692 ParsedResource* out_resource) {
693 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
694 if (!maybe_type) {
695 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 << "<public> must have a 'type' attribute");
697 return false;
698 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800699
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700700 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
701 if (!parsed_type) {
702 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
703 << maybe_type.value()
704 << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 return false;
706 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800707
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 out_resource->name.type = *parsed_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800709
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800710 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
711 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 if (!maybe_id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800713 diag_->Error(DiagMessage(out_resource->source)
714 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 return false;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800716 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 out_resource->id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 }
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800719
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 if (*parsed_type == ResourceType::kId) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 // An ID marked as public is also the definition of an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 out_resource->value = util::make_unique<Id>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 return true;
Adam Lesinski27afb9e2015-11-06 18:25:04 -0800727}
728
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
730 ParsedResource* out_resource) {
731 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
732 if (!maybe_type) {
733 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 << "<public-group> must have a 'type' attribute");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800735 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800737
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700738 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
739 if (!parsed_type) {
740 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
741 << maybe_type.value()
742 << "' in <public-group>");
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800743 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 }
745
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700746 Maybe<StringPiece> maybe_id_str =
747 xml::FindNonEmptyAttribute(parser, "first-id");
748 if (!maybe_id_str) {
749 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 << "<public-group> must have a 'first-id' attribute");
751 return false;
752 }
753
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 Maybe<ResourceId> maybe_id =
755 ResourceUtils::ParseResourceId(maybe_id_str.value());
756 if (!maybe_id) {
757 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
758 << maybe_id_str.value()
759 << "' in <public-group>");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 return false;
761 }
762
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700763 ResourceId next_id = maybe_id.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764
765 std::string comment;
766 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700767 const size_t depth = parser->depth();
768 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
769 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800770 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700772 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700773 // Skip text.
774 continue;
775 }
776
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700777 const Source item_source = source_.WithLine(parser->line_number());
778 const std::string& element_namespace = parser->element_namespace();
779 const std::string& element_name = parser->element_name();
780 if (element_namespace.empty() && element_name == "public") {
781 Maybe<StringPiece> maybe_name =
782 xml::FindNonEmptyAttribute(parser, "name");
783 if (!maybe_name) {
784 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700785 << "<public> must have a 'name' attribute");
786 error = true;
787 continue;
788 }
789
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700790 if (xml::FindNonEmptyAttribute(parser, "id")) {
791 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 << "'id' is ignored within <public-group>");
793 error = true;
794 continue;
795 }
796
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700797 if (xml::FindNonEmptyAttribute(parser, "type")) {
798 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700799 << "'type' is ignored within <public-group>");
800 error = true;
801 continue;
802 }
803
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700804 ParsedResource child_resource;
805 child_resource.name.type = *parsed_type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800806 child_resource.name.entry = maybe_name.value().to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700807 child_resource.id = next_id;
808 child_resource.comment = std::move(comment);
809 child_resource.source = item_source;
810 child_resource.symbol_state = SymbolState::kPublic;
811 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700812
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700813 next_id.id += 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700815 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
816 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 error = true;
818 }
819 }
820 return !error;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800821}
822
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700823bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
824 ParsedResource* out_resource) {
825 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
826 if (!maybe_type) {
827 diag_->Error(DiagMessage(out_resource->source)
828 << "<" << parser->element_name()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829 << "> must have a 'type' attribute");
830 return false;
831 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800832
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700833 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
834 if (!parsed_type) {
835 diag_->Error(DiagMessage(out_resource->source)
836 << "invalid resource type '" << maybe_type.value() << "' in <"
837 << parser->element_name() << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 return false;
839 }
840
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700841 out_resource->name.type = *parsed_type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700842 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800843}
844
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700845bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
846 ParsedResource* out_resource) {
847 if (ParseSymbolImpl(parser, out_resource)) {
848 out_resource->symbol_state = SymbolState::kPrivate;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700849 return true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700850 }
851 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800852}
853
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700854bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
855 ParsedResource* out_resource) {
856 if (ParseSymbolImpl(parser, out_resource)) {
857 out_resource->symbol_state = SymbolState::kUndefined;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700858 out_resource->allow_new = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700859 return true;
860 }
861 return false;
862}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700863
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700864bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
865 ParsedResource* out_resource) {
866 return ParseAttrImpl(parser, out_resource, false);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700867}
868
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700869bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
870 ParsedResource* out_resource, bool weak) {
871 out_resource->name.type = ResourceType::kAttr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700872
873 // Attributes only end up in default configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700874 if (out_resource->config != ConfigDescription::DefaultConfig()) {
875 diag_->Warn(DiagMessage(out_resource->source)
876 << "ignoring configuration '" << out_resource->config
877 << "' for attribute " << out_resource->name);
878 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 }
880
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700881 uint32_t type_mask = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700883 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
884 if (maybe_format) {
885 type_mask = ParseFormatAttribute(maybe_format.value());
886 if (type_mask == 0) {
887 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
888 << "invalid attribute format '" << maybe_format.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700889 << "'");
890 return false;
891 }
892 }
893
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700894 Maybe<int32_t> maybe_min, maybe_max;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700895
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700896 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
897 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
898 if (!min_str.empty()) {
899 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700900 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700901 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700902 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700903 maybe_min = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700904 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800905 }
906
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700907 if (!maybe_min) {
908 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
909 << "invalid 'min' value '" << min_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700910 return false;
911 }
912 }
913
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700914 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
915 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
916 if (!max_str.empty()) {
917 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700918 android::Res_value value;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700919 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700920 &value)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700921 maybe_max = static_cast<int32_t>(value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700922 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800923 }
924
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700925 if (!maybe_max) {
926 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
927 << "invalid 'max' value '" << max_str << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700928 return false;
929 }
930 }
931
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700932 if ((maybe_min || maybe_max) &&
933 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
934 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700935 << "'min' and 'max' can only be used when format='integer'");
936 return false;
937 }
938
939 struct SymbolComparator {
Yi Kong087e2d42017-05-02 12:49:25 -0700940 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700941 return a.symbol.name.value() < b.symbol.name.value();
942 }
943 };
944
945 std::set<Attribute::Symbol, SymbolComparator> items;
946
947 std::string comment;
948 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700949 const size_t depth = parser->depth();
950 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
951 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800952 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700953 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700954 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700955 // Skip text.
956 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800957 }
958
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700959 const Source item_source = source_.WithLine(parser->line_number());
960 const std::string& element_namespace = parser->element_namespace();
961 const std::string& element_name = parser->element_name();
962 if (element_namespace.empty() &&
963 (element_name == "flag" || element_name == "enum")) {
964 if (element_name == "enum") {
965 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
966 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700967 << "can not define an <enum>; already defined a <flag>");
968 error = true;
969 continue;
970 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700971 type_mask |= android::ResTable_map::TYPE_ENUM;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700972
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700973 } else if (element_name == "flag") {
974 if (type_mask & android::ResTable_map::TYPE_ENUM) {
975 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700976 << "can not define a <flag>; already defined an <enum>");
977 error = true;
978 continue;
979 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700980 type_mask |= android::ResTable_map::TYPE_FLAGS;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700981 }
982
983 if (Maybe<Attribute::Symbol> s =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700984 ParseEnumOrFlagItem(parser, element_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700985 Attribute::Symbol& symbol = s.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700986 ParsedResource child_resource;
987 child_resource.name = symbol.symbol.name.value();
988 child_resource.source = item_source;
989 child_resource.value = util::make_unique<Id>();
990 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700991
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700992 symbol.symbol.SetComment(std::move(comment));
993 symbol.symbol.SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700994
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700995 auto insert_result = items.insert(std::move(symbol));
996 if (!insert_result.second) {
997 const Attribute::Symbol& existing_symbol = *insert_result.first;
998 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700999 << "duplicate symbol '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001000 << existing_symbol.symbol.name.value().entry << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001001
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001002 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001003 << "first defined here");
1004 error = true;
1005 }
1006 } else {
1007 error = true;
1008 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001009 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1010 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001011 error = true;
1012 }
1013
1014 comment = {};
1015 }
1016
1017 if (error) {
1018 return false;
1019 }
1020
1021 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
1022 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001023 attr->type_mask =
1024 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
1025 if (maybe_min) {
1026 attr->min_int = maybe_min.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001027 }
1028
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001029 if (maybe_max) {
1030 attr->max_int = maybe_max.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001031 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001032 out_resource->value = std::move(attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001033 return true;
1034}
1035
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001036Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001037 xml::XmlPullParser* parser, const StringPiece& tag) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001038 const Source source = source_.WithLine(parser->line_number());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001039
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001040 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1041 if (!maybe_name) {
1042 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001043 << tag << ">");
1044 return {};
1045 }
1046
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001047 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1048 if (!maybe_value) {
1049 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001050 << tag << ">");
1051 return {};
1052 }
1053
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001054 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001055 android::Res_value val;
1056 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001057 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001058 << "' for <" << tag
1059 << ">; must be an integer");
1060 return {};
1061 }
1062
1063 return Attribute::Symbol{
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001064 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001065 val.data};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001066}
1067
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001068bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1069 const Source source = source_.WithLine(parser->line_number());
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001070
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001071 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1072 if (!maybe_name) {
1073 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001074 return false;
1075 }
1076
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001077 Maybe<Reference> maybe_key =
1078 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1079 if (!maybe_key) {
1080 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1081 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001082 return false;
1083 }
1084
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001085 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1086 maybe_key.value().SetSource(source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001087
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001088 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001089 if (!value) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001090 diag_->Error(DiagMessage(source) << "could not parse style item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001091 return false;
1092 }
1093
1094 style->entries.push_back(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001095 Style::Entry{std::move(maybe_key.value()), std::move(value)});
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001096 return true;
1097}
1098
Adam Lesinski86d67df2017-01-31 13:47:27 -08001099bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001100 ParsedResource* out_resource) {
Adam Lesinski86d67df2017-01-31 13:47:27 -08001101 out_resource->name.type = type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001102
1103 std::unique_ptr<Style> style = util::make_unique<Style>();
1104
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001105 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1106 if (maybe_parent) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001107 // If the parent is empty, we don't have a parent, but we also don't infer
1108 // either.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001109 if (!maybe_parent.value().empty()) {
1110 std::string err_str;
1111 style->parent = ResourceUtils::ParseStyleParentReference(
1112 maybe_parent.value(), &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001113 if (!style->parent) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001114 diag_->Error(DiagMessage(out_resource->source) << err_str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001115 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001116 }
1117
1118 // Transform the namespace prefix to the actual package name, and mark the
1119 // reference as
1120 // private if appropriate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001121 TransformReferenceFromNamespace(parser, "", &style->parent.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001122 }
1123
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001124 } else {
1125 // No parent was specified, so try inferring it from the style name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001126 std::string style_name = out_resource->name.entry;
1127 size_t pos = style_name.find_last_of(u'.');
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001128 if (pos != std::string::npos) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001129 style->parent_inferred = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001130 style->parent = Reference(
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001131 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001132 }
1133 }
1134
1135 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001136 const size_t depth = parser->depth();
1137 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1138 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001139 // Skip text and comments.
1140 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001141 }
1142
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001143 const std::string& element_namespace = parser->element_namespace();
1144 const std::string& element_name = parser->element_name();
1145 if (element_namespace == "" && element_name == "item") {
1146 error |= !ParseStyleItem(parser, style.get());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001147
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001148 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1149 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1150 << ":" << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001151 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001152 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001153 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001154
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001155 if (error) {
1156 return false;
1157 }
1158
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001159 out_resource->value = std::move(style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001160 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001161}
1162
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001163bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1164 uint32_t resource_format = android::ResTable_map::TYPE_ANY;
1165 if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) {
1166 resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value());
1167 if (resource_format == 0u) {
1168 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1169 << "'" << format_attr.value() << "' is an invalid format");
1170 return false;
1171 }
1172 }
1173 return ParseArrayImpl(parser, out_resource, resource_format);
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001174}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001175
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001176bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1177 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001178}
1179
Adam Lesinskid5fd76a2017-05-16 12:18:08 -07001180bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) {
1181 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001182}
1183
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001184bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1185 ParsedResource* out_resource,
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001186 const uint32_t typeMask) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001187 out_resource->name.type = ResourceType::kArray;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001188
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001189 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001190
Adam Lesinski75421622017-01-06 15:20:04 -08001191 bool translatable = options_.translatable;
1192 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1193 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1194 if (!maybe_translatable) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001195 diag_->Error(DiagMessage(out_resource->source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001196 << "invalid value for 'translatable'. Must be a boolean");
1197 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -07001198 }
Adam Lesinski75421622017-01-06 15:20:04 -08001199 translatable = maybe_translatable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001200 }
Adam Lesinski75421622017-01-06 15:20:04 -08001201 array->SetTranslatable(translatable);
Adam Lesinski458b8772016-04-25 14:20:21 -07001202
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001203 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001204 const size_t depth = parser->depth();
1205 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1206 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001207 // Skip text and comments.
1208 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001209 }
1210
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001211 const Source item_source = source_.WithLine(parser->line_number());
1212 const std::string& element_namespace = parser->element_namespace();
1213 const std::string& element_name = parser->element_name();
1214 if (element_namespace.empty() && element_name == "item") {
1215 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001216 if (!item) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001217 diag_->Error(DiagMessage(item_source) << "could not parse array item");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001218 error = true;
1219 continue;
1220 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001221 item->SetSource(item_source);
Adam Lesinski4ffea042017-08-10 15:37:28 -07001222 array->elements.emplace_back(std::move(item));
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001223
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001224 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1225 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1226 << "unknown tag <" << element_namespace << ":"
1227 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001228 error = true;
1229 }
1230 }
1231
1232 if (error) {
1233 return false;
1234 }
1235
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001236 out_resource->value = std::move(array);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001237 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001238}
1239
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001240bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1241 ParsedResource* out_resource) {
1242 out_resource->name.type = ResourceType::kPlurals;
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001243
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001244 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001245
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001246 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001247 const size_t depth = parser->depth();
1248 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1249 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001250 // Skip text and comments.
1251 continue;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001252 }
1253
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001254 const Source item_source = source_.WithLine(parser->line_number());
1255 const std::string& element_namespace = parser->element_namespace();
1256 const std::string& element_name = parser->element_name();
1257 if (element_namespace.empty() && element_name == "item") {
1258 Maybe<StringPiece> maybe_quantity =
1259 xml::FindNonEmptyAttribute(parser, "quantity");
1260 if (!maybe_quantity) {
1261 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001262 << "<item> in <plurals> requires attribute "
1263 << "'quantity'");
1264 error = true;
1265 continue;
1266 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001267
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001268 StringPiece trimmed_quantity =
1269 util::TrimWhitespace(maybe_quantity.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001270 size_t index = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001271 if (trimmed_quantity == "zero") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001272 index = Plural::Zero;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001273 } else if (trimmed_quantity == "one") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001274 index = Plural::One;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001275 } else if (trimmed_quantity == "two") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001276 index = Plural::Two;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001277 } else if (trimmed_quantity == "few") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001278 index = Plural::Few;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001279 } else if (trimmed_quantity == "many") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001280 index = Plural::Many;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001281 } else if (trimmed_quantity == "other") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001282 index = Plural::Other;
1283 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001284 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001285 << "<item> in <plural> has invalid value '"
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001286 << trimmed_quantity << "' for attribute 'quantity'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001287 error = true;
1288 continue;
1289 }
1290
1291 if (plural->values[index]) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001292 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1293 << trimmed_quantity << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001294 error = true;
1295 continue;
1296 }
1297
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001298 if (!(plural->values[index] = ParseXml(
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001299 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1300 error = true;
1301 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001302 plural->values[index]->SetSource(item_source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001303
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001304 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1305 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1306 << element_namespace << ":"
1307 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001308 error = true;
1309 }
1310 }
1311
1312 if (error) {
1313 return false;
1314 }
1315
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001316 out_resource->value = std::move(plural);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001317 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001318}
1319
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001320bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1321 ParsedResource* out_resource) {
1322 out_resource->name.type = ResourceType::kStyleable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001323
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001324 // Declare-styleable is kPrivate by default, because it technically only
1325 // exists in R.java.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001326 out_resource->symbol_state = SymbolState::kPublic;
Adam Lesinski9f222042015-11-04 13:51:45 -08001327
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001328 // Declare-styleable only ends up in default config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001329 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1330 diag_->Warn(DiagMessage(out_resource->source)
1331 << "ignoring configuration '" << out_resource->config
1332 << "' for styleable " << out_resource->name.entry);
1333 out_resource->config = ConfigDescription::DefaultConfig();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001334 }
1335
1336 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1337
1338 std::string comment;
1339 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001340 const size_t depth = parser->depth();
1341 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1342 if (parser->event() == xml::XmlPullParser::Event::kComment) {
Adam Lesinskid5083f62017-01-16 15:07:21 -08001343 comment = util::TrimWhitespace(parser->comment()).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001344 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001345 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001346 // Ignore text.
1347 continue;
Adam Lesinski52364f72016-01-11 13:10:24 -08001348 }
1349
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001350 const Source item_source = source_.WithLine(parser->line_number());
1351 const std::string& element_namespace = parser->element_namespace();
1352 const std::string& element_name = parser->element_name();
1353 if (element_namespace.empty() && element_name == "attr") {
1354 Maybe<StringPiece> maybe_name =
1355 xml::FindNonEmptyAttribute(parser, "name");
1356 if (!maybe_name) {
1357 diag_->Error(DiagMessage(item_source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001358 << "<attr> tag must have a 'name' attribute");
1359 error = true;
1360 continue;
1361 }
Adam Lesinski7ff3ee12015-12-14 16:08:50 -08001362
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001363 // If this is a declaration, the package name may be in the name. Separate
1364 // these out.
1365 // Eg. <attr name="android:text" />
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001366 Maybe<Reference> maybe_ref =
1367 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1368 if (!maybe_ref) {
1369 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1370 << maybe_name.value() << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001371 error = true;
1372 continue;
1373 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001374
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001375 Reference& child_ref = maybe_ref.value();
1376 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001377
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001378 // Create the ParsedResource that will add the attribute to the table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001379 ParsedResource child_resource;
1380 child_resource.name = child_ref.name.value();
1381 child_resource.source = item_source;
1382 child_resource.comment = std::move(comment);
Adam Lesinski467f1712015-11-16 17:35:44 -08001383
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001384 if (!ParseAttrImpl(parser, &child_resource, true)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001385 error = true;
1386 continue;
1387 }
Adam Lesinski467f1712015-11-16 17:35:44 -08001388
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001389 // Create the reference to this attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001390 child_ref.SetComment(child_resource.comment);
1391 child_ref.SetSource(item_source);
1392 styleable->entries.push_back(std::move(child_ref));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001393
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001394 out_resource->child_resources.push_back(std::move(child_resource));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001395
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001396 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1397 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1398 << element_namespace << ":"
1399 << element_name << ">");
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001400 error = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001401 }
1402
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001403 comment = {};
1404 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -07001405
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001406 if (error) {
1407 return false;
1408 }
1409
Adam Lesinskice5e56e2016-10-21 17:56:45 -07001410 out_resource->value = std::move(styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001411 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001412}
1413
Adam Lesinskicacb28f2016-10-19 12:18:14 -07001414} // namespace aapt