2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ResourceParser.h"
22 #include "android-base/logging.h"
24 #include "ResourceTable.h"
25 #include "ResourceUtils.h"
26 #include "ResourceValues.h"
27 #include "ValueVisitor.h"
28 #include "util/ImmutableMap.h"
29 #include "util/Maybe.h"
30 #include "util/Util.h"
31 #include "xml/XmlPullParser.h"
33 using android::StringPiece;
37 constexpr const char* sXliffNamespaceUri =
38 "urn:oasis:names:tc:xliff:document:1.2";
41 * Returns true if the element is <skip> or <eat-comment> and can be safely
44 static bool ShouldIgnoreElement(const StringPiece& ns,
45 const StringPiece& name) {
46 return ns.empty() && (name == "skip" || name == "eat-comment");
49 static uint32_t ParseFormatType(const StringPiece& piece) {
50 if (piece == "reference")
51 return android::ResTable_map::TYPE_REFERENCE;
52 else if (piece == "string")
53 return android::ResTable_map::TYPE_STRING;
54 else if (piece == "integer")
55 return android::ResTable_map::TYPE_INTEGER;
56 else if (piece == "boolean")
57 return android::ResTable_map::TYPE_BOOLEAN;
58 else if (piece == "color")
59 return android::ResTable_map::TYPE_COLOR;
60 else if (piece == "float")
61 return android::ResTable_map::TYPE_FLOAT;
62 else if (piece == "dimension")
63 return android::ResTable_map::TYPE_DIMENSION;
64 else if (piece == "fraction")
65 return android::ResTable_map::TYPE_FRACTION;
66 else if (piece == "enum")
67 return android::ResTable_map::TYPE_ENUM;
68 else if (piece == "flags")
69 return android::ResTable_map::TYPE_FLAGS;
73 static uint32_t ParseFormatAttribute(const StringPiece& str) {
75 for (StringPiece part : util::Tokenize(str, '|')) {
76 StringPiece trimmed_part = util::TrimWhitespace(part);
77 uint32_t type = ParseFormatType(trimmed_part);
87 * A parsed resource ready to be added to the ResourceTable.
89 struct ParsedResource {
91 ConfigDescription config;
95 Maybe<SymbolState> symbol_state;
97 std::unique_ptr<Value> value;
98 std::list<ParsedResource> child_resources;
101 // Recursively adds resources to the ResourceTable.
102 static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag,
103 ParsedResource* res) {
104 StringPiece trimmed_comment = util::TrimWhitespace(res->comment);
105 if (trimmed_comment.size() != res->comment.size()) {
106 // Only if there was a change do we re-assign.
107 res->comment = trimmed_comment.to_string();
110 if (res->symbol_state) {
112 symbol.state = res->symbol_state.value();
113 symbol.source = res->source;
114 symbol.comment = res->comment;
115 if (!table->SetSymbolState(res->name, res->id, symbol, diag)) {
121 // Attach the comment, source and config to the value.
122 res->value->SetComment(std::move(res->comment));
123 res->value->SetSource(std::move(res->source));
125 if (!table->AddResource(res->name, res->id, res->config, res->product,
126 std::move(res->value), diag)) {
132 for (ParsedResource& child : res->child_resources) {
133 error |= !AddResourcesToTable(table, diag, &child);
138 // Convenient aliases for more readable function calls.
139 enum { kAllowRawString = true, kNoRawString = false };
141 ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table,
142 const Source& source,
143 const ConfigDescription& config,
144 const ResourceParserOptions& options)
152 * Build a string from XML that converts nested elements into Span objects.
154 bool 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.
158 // 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;
163 // Clear the output variables.
164 out_raw_string->clear();
165 out_style_string->spans.clear();
166 out_untranslatable_sections->clear();
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.
170 util::StringBuilder builder;
172 // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal.
173 Maybe<size_t> untranslatable_start_depth;
176 while (xml::XmlPullParser::IsGoodEvent(parser->Next())) {
177 const xml::XmlPullParser::Event event = parser->event();
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) {
186 span_name += attr_iter->name;
188 span_name += attr_iter->value;
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");
198 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);
201 } 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");
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});
215 // Ignore other xliff tags, they get handled by other tools.
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() << "'");
224 // Enter one level inside the element.
226 } else if (event == xml::XmlPullParser::Event::kText) {
227 // Record both the raw text and append to the builder to deal with escape sequences
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.
238 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.
241 Span& top_span = out_style_string->spans[span_stack.back()];
242 top_span.last_char = builder.Utf16Len() - 1;
243 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 = {};
250 } else if (event == xml::XmlPullParser::Event::kComment) {
253 LOG(FATAL) << "unhandled XML event";
257 CHECK(span_stack.empty()) << "spans haven't been fully processed";
258 out_style_string->str = builder.ToString();
262 bool ResourceParser::Parse(xml::XmlPullParser* parser) {
264 const size_t depth = parser->depth();
265 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
266 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
267 // Skip comments and text.
271 if (!parser->element_namespace().empty() ||
272 parser->element_name() != "resources") {
273 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
274 << "root element must be <resources>");
278 error |= !ParseResources(parser);
282 if (parser->event() == xml::XmlPullParser::Event::kBadDocument) {
283 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
284 << "xml parser error: " << parser->error());
290 bool ResourceParser::ParseResources(xml::XmlPullParser* parser) {
291 std::set<ResourceName> stripped_resources;
295 const size_t depth = parser->depth();
296 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
297 const xml::XmlPullParser::Event event = parser->event();
298 if (event == xml::XmlPullParser::Event::kComment) {
299 comment = parser->comment();
303 if (event == xml::XmlPullParser::Event::kText) {
304 if (!util::TrimWhitespace(parser->text()).empty()) {
305 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
306 << "plain text not allowed here");
312 CHECK(event == xml::XmlPullParser::Event::kStartElement);
314 if (!parser->element_namespace().empty()) {
315 // Skip unknown namespace.
319 std::string element_name = parser->element_name();
320 if (element_name == "skip" || element_name == "eat-comment") {
325 ParsedResource parsed_resource;
326 parsed_resource.config = config_;
327 parsed_resource.source = source_.WithLine(parser->line_number());
328 parsed_resource.comment = std::move(comment);
330 // Extract the product name if it exists.
331 if (Maybe<StringPiece> maybe_product =
332 xml::FindNonEmptyAttribute(parser, "product")) {
333 parsed_resource.product = maybe_product.value().to_string();
336 // Parse the resource regardless of product.
337 if (!ParseResource(parser, &parsed_resource)) {
342 if (!AddResourcesToTable(table_, diag_, &parsed_resource)) {
347 // Check that we included at least one variant of each stripped resource.
348 for (const ResourceName& stripped_resource : stripped_resources) {
349 if (!table_->FindResource(stripped_resource)) {
350 // Failed to find the resource.
351 diag_->Error(DiagMessage(source_)
352 << "resource '" << stripped_resource
354 "was filtered out but no product variant remains");
362 bool ResourceParser::ParseResource(xml::XmlPullParser* parser,
363 ParsedResource* out_resource) {
364 struct ItemTypeFormat {
369 using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*,
372 static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({
373 {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}},
374 {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}},
375 {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}},
377 {ResourceType::kDimen,
378 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
379 android::ResTable_map::TYPE_DIMENSION}},
380 {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}},
382 {ResourceType::kFraction,
383 android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION |
384 android::ResTable_map::TYPE_DIMENSION}},
385 {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}},
386 {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}},
389 static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({
390 {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)},
391 {"array", std::mem_fn(&ResourceParser::ParseArray)},
392 {"attr", std::mem_fn(&ResourceParser::ParseAttr)},
394 std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying,
395 std::placeholders::_2, std::placeholders::_3)},
396 {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)},
397 {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)},
398 {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
399 {"plurals", std::mem_fn(&ResourceParser::ParsePlural)},
400 {"public", std::mem_fn(&ResourceParser::ParsePublic)},
401 {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)},
402 {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)},
403 {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle,
404 std::placeholders::_2, std::placeholders::_3)},
405 {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)},
408 std::string resource_type = parser->element_name();
410 // The value format accepted for this resource.
411 uint32_t resource_format = 0u;
413 bool can_be_item = true;
414 bool can_be_bag = true;
415 if (resource_type == "item") {
418 // Items have their type encoded in the type attribute.
419 if (Maybe<StringPiece> maybe_type =
420 xml::FindNonEmptyAttribute(parser, "type")) {
421 resource_type = maybe_type.value().to_string();
423 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
424 << "<item> must have a 'type' attribute");
428 if (Maybe<StringPiece> maybe_format =
429 xml::FindNonEmptyAttribute(parser, "format")) {
430 // An explicit format for this resource was specified. The resource will
432 // its type in its name, but the accepted value for this type is
434 resource_format = ParseFormatType(maybe_format.value());
435 if (!resource_format) {
436 diag_->Error(DiagMessage(out_resource->source)
437 << "'" << maybe_format.value()
438 << "' is an invalid format");
442 } else if (resource_type == "bag") {
445 // Bags have their type encoded in the type attribute.
446 if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) {
447 resource_type = maybe_type.value().to_string();
449 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
450 << "<bag> must have a 'type' attribute");
455 // Get the name of the resource. This will be checked later, because not all
456 // XML elements require a name.
457 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
459 if (resource_type == "id") {
461 diag_->Error(DiagMessage(out_resource->source)
462 << "<" << parser->element_name()
463 << "> missing 'name' attribute");
467 out_resource->name.type = ResourceType::kId;
468 out_resource->name.entry = maybe_name.value().to_string();
469 out_resource->value = util::make_unique<Id>();
474 const auto item_iter = elToItemMap.find(resource_type);
475 if (item_iter != elToItemMap.end()) {
476 // This is an item, record its type and format and start parsing.
479 diag_->Error(DiagMessage(out_resource->source)
480 << "<" << parser->element_name() << "> missing 'name' attribute");
484 out_resource->name.type = item_iter->second.type;
485 out_resource->name.entry = maybe_name.value().to_string();
487 // Only use the implicit format for this type if it wasn't overridden.
488 if (!resource_format) {
489 resource_format = item_iter->second.format;
492 if (!ParseItem(parser, out_resource, resource_format)) {
499 // This might be a bag or something.
501 const auto bag_iter = elToBagMap.find(resource_type);
502 if (bag_iter != elToBagMap.end()) {
503 // Ensure we have a name (unless this is a <public-group>).
504 if (resource_type != "public-group") {
506 diag_->Error(DiagMessage(out_resource->source)
507 << "<" << parser->element_name() << "> missing 'name' attribute");
511 out_resource->name.entry = maybe_name.value().to_string();
514 // Call the associated parse method. The type will be filled in by the
516 if (!bag_iter->second(this, parser, out_resource)) {
524 // Try parsing the elementName (or type) as a resource. These shall only be
525 // resources like 'layout' or 'xml' and they can only be references.
526 const ResourceType* parsed_type = ParseResourceType(resource_type);
529 diag_->Error(DiagMessage(out_resource->source)
530 << "<" << parser->element_name()
531 << "> missing 'name' attribute");
535 out_resource->name.type = *parsed_type;
536 out_resource->name.entry = maybe_name.value().to_string();
537 out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString);
538 if (!out_resource->value) {
539 diag_->Error(DiagMessage(out_resource->source)
540 << "invalid value for type '" << *parsed_type << "'. Expected a reference");
547 diag_->Warn(DiagMessage(out_resource->source)
548 << "unknown resource type '" << parser->element_name() << "'");
552 bool ResourceParser::ParseItem(xml::XmlPullParser* parser,
553 ParsedResource* out_resource,
554 const uint32_t format) {
555 if (format == android::ResTable_map::TYPE_STRING) {
556 return ParseString(parser, out_resource);
559 out_resource->value = ParseXml(parser, format, kNoRawString);
560 if (!out_resource->value) {
561 diag_->Error(DiagMessage(out_resource->source) << "invalid "
562 << out_resource->name.type);
569 * Reads the entire XML subtree and attempts to parse it as some Item,
570 * with typeMask denoting which items it can be. If allowRawValue is
571 * true, a RawString is returned if the XML couldn't be parsed as
572 * an Item. If allowRawValue is false, nullptr is returned in this
575 std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser,
576 const uint32_t type_mask,
577 const bool allow_raw_value) {
578 const size_t begin_xml_line = parser->line_number();
580 std::string raw_value;
581 StyleString style_string;
582 std::vector<UntranslatableSection> untranslatable_sections;
583 if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) {
587 if (!style_string.spans.empty()) {
588 // This can only be a StyledString.
589 std::unique_ptr<StyledString> styled_string =
590 util::make_unique<StyledString>(table_->string_pool.MakeRef(
591 style_string, StringPool::Context(StringPool::Context::kStylePriority, config_)));
592 styled_string->untranslatable_sections = std::move(untranslatable_sections);
593 return std::move(styled_string);
596 auto on_create_reference = [&](const ResourceName& name) {
597 // name.package can be empty here, as it will assume the package name of the
599 std::unique_ptr<Id> id = util::make_unique<Id>();
600 id->SetSource(source_.WithLine(begin_xml_line));
601 table_->AddResource(name, {}, {}, std::move(id), diag_);
604 // Process the raw value.
605 std::unique_ptr<Item> processed_item =
606 ResourceUtils::TryParseItemForAttribute(raw_value, type_mask,
607 on_create_reference);
608 if (processed_item) {
609 // Fix up the reference.
610 if (Reference* ref = ValueCast<Reference>(processed_item.get())) {
611 TransformReferenceFromNamespace(parser, "", ref);
613 return processed_item;
616 // Try making a regular string.
617 if (type_mask & android::ResTable_map::TYPE_STRING) {
618 // Use the trimmed, escaped string.
619 std::unique_ptr<String> string = util::make_unique<String>(
620 table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_)));
621 string->untranslatable_sections = std::move(untranslatable_sections);
622 return std::move(string);
625 if (allow_raw_value) {
626 // We can't parse this so return a RawString if we are allowed.
627 return util::make_unique<RawString>(
628 table_->string_pool.MakeRef(raw_value, StringPool::Context(config_)));
633 bool ResourceParser::ParseString(xml::XmlPullParser* parser,
634 ParsedResource* out_resource) {
635 bool formatted = true;
636 if (Maybe<StringPiece> formatted_attr =
637 xml::FindAttribute(parser, "formatted")) {
638 Maybe<bool> maybe_formatted =
639 ResourceUtils::ParseBool(formatted_attr.value());
640 if (!maybe_formatted) {
641 diag_->Error(DiagMessage(out_resource->source)
642 << "invalid value for 'formatted'. Must be a boolean");
645 formatted = maybe_formatted.value();
648 bool translatable = options_.translatable;
649 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
650 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
651 if (!maybe_translatable) {
652 diag_->Error(DiagMessage(out_resource->source)
653 << "invalid value for 'translatable'. Must be a boolean");
656 translatable = maybe_translatable.value();
659 out_resource->value =
660 ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString);
661 if (!out_resource->value) {
662 diag_->Error(DiagMessage(out_resource->source) << "not a valid string");
666 if (String* string_value = ValueCast<String>(out_resource->value.get())) {
667 string_value->SetTranslatable(translatable);
669 if (formatted && translatable) {
670 if (!util::VerifyJavaStringFormat(*string_value->value)) {
671 DiagMessage msg(out_resource->source);
672 msg << "multiple substitutions specified in non-positional format; "
673 "did you mean to add the formatted=\"false\" attribute?";
674 if (options_.error_on_positional_arguments) {
683 } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) {
684 string_value->SetTranslatable(translatable);
689 bool ResourceParser::ParsePublic(xml::XmlPullParser* parser,
690 ParsedResource* out_resource) {
691 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
693 diag_->Error(DiagMessage(out_resource->source)
694 << "<public> must have a 'type' attribute");
698 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
700 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
701 << maybe_type.value()
706 out_resource->name.type = *parsed_type;
708 if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) {
709 Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value());
711 diag_->Error(DiagMessage(out_resource->source)
712 << "invalid resource ID '" << maybe_id_str.value() << "' in <public>");
715 out_resource->id = maybe_id.value();
718 if (*parsed_type == ResourceType::kId) {
719 // An ID marked as public is also the definition of an ID.
720 out_resource->value = util::make_unique<Id>();
723 out_resource->symbol_state = SymbolState::kPublic;
727 bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser,
728 ParsedResource* out_resource) {
729 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
731 diag_->Error(DiagMessage(out_resource->source)
732 << "<public-group> must have a 'type' attribute");
736 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
738 diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '"
739 << maybe_type.value()
740 << "' in <public-group>");
744 Maybe<StringPiece> maybe_id_str =
745 xml::FindNonEmptyAttribute(parser, "first-id");
747 diag_->Error(DiagMessage(out_resource->source)
748 << "<public-group> must have a 'first-id' attribute");
752 Maybe<ResourceId> maybe_id =
753 ResourceUtils::ParseResourceId(maybe_id_str.value());
755 diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '"
756 << maybe_id_str.value()
757 << "' in <public-group>");
761 ResourceId next_id = maybe_id.value();
765 const size_t depth = parser->depth();
766 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
767 if (parser->event() == xml::XmlPullParser::Event::kComment) {
768 comment = util::TrimWhitespace(parser->comment()).to_string();
770 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
775 const Source item_source = source_.WithLine(parser->line_number());
776 const std::string& element_namespace = parser->element_namespace();
777 const std::string& element_name = parser->element_name();
778 if (element_namespace.empty() && element_name == "public") {
779 Maybe<StringPiece> maybe_name =
780 xml::FindNonEmptyAttribute(parser, "name");
782 diag_->Error(DiagMessage(item_source)
783 << "<public> must have a 'name' attribute");
788 if (xml::FindNonEmptyAttribute(parser, "id")) {
789 diag_->Error(DiagMessage(item_source)
790 << "'id' is ignored within <public-group>");
795 if (xml::FindNonEmptyAttribute(parser, "type")) {
796 diag_->Error(DiagMessage(item_source)
797 << "'type' is ignored within <public-group>");
802 ParsedResource child_resource;
803 child_resource.name.type = *parsed_type;
804 child_resource.name.entry = maybe_name.value().to_string();
805 child_resource.id = next_id;
806 child_resource.comment = std::move(comment);
807 child_resource.source = item_source;
808 child_resource.symbol_state = SymbolState::kPublic;
809 out_resource->child_resources.push_back(std::move(child_resource));
813 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
814 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
821 bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser,
822 ParsedResource* out_resource) {
823 Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type");
825 diag_->Error(DiagMessage(out_resource->source)
826 << "<" << parser->element_name()
827 << "> must have a 'type' attribute");
831 const ResourceType* parsed_type = ParseResourceType(maybe_type.value());
833 diag_->Error(DiagMessage(out_resource->source)
834 << "invalid resource type '" << maybe_type.value() << "' in <"
835 << parser->element_name() << ">");
839 out_resource->name.type = *parsed_type;
843 bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser,
844 ParsedResource* out_resource) {
845 if (ParseSymbolImpl(parser, out_resource)) {
846 out_resource->symbol_state = SymbolState::kPrivate;
852 bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser,
853 ParsedResource* out_resource) {
854 if (ParseSymbolImpl(parser, out_resource)) {
855 out_resource->symbol_state = SymbolState::kUndefined;
861 bool ResourceParser::ParseAttr(xml::XmlPullParser* parser,
862 ParsedResource* out_resource) {
863 return ParseAttrImpl(parser, out_resource, false);
866 bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser,
867 ParsedResource* out_resource, bool weak) {
868 out_resource->name.type = ResourceType::kAttr;
870 // Attributes only end up in default configuration.
871 if (out_resource->config != ConfigDescription::DefaultConfig()) {
872 diag_->Warn(DiagMessage(out_resource->source)
873 << "ignoring configuration '" << out_resource->config
874 << "' for attribute " << out_resource->name);
875 out_resource->config = ConfigDescription::DefaultConfig();
878 uint32_t type_mask = 0;
880 Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format");
882 type_mask = ParseFormatAttribute(maybe_format.value());
883 if (type_mask == 0) {
884 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
885 << "invalid attribute format '" << maybe_format.value()
891 Maybe<int32_t> maybe_min, maybe_max;
893 if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) {
894 StringPiece min_str = util::TrimWhitespace(maybe_min_str.value());
895 if (!min_str.empty()) {
896 std::u16string min_str16 = util::Utf8ToUtf16(min_str);
897 android::Res_value value;
898 if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(),
900 maybe_min = static_cast<int32_t>(value.data);
905 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
906 << "invalid 'min' value '" << min_str << "'");
911 if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) {
912 StringPiece max_str = util::TrimWhitespace(maybe_max_str.value());
913 if (!max_str.empty()) {
914 std::u16string max_str16 = util::Utf8ToUtf16(max_str);
915 android::Res_value value;
916 if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(),
918 maybe_max = static_cast<int32_t>(value.data);
923 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
924 << "invalid 'max' value '" << max_str << "'");
929 if ((maybe_min || maybe_max) &&
930 (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) {
931 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
932 << "'min' and 'max' can only be used when format='integer'");
936 struct SymbolComparator {
937 bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) {
938 return a.symbol.name.value() < b.symbol.name.value();
942 std::set<Attribute::Symbol, SymbolComparator> items;
946 const size_t depth = parser->depth();
947 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
948 if (parser->event() == xml::XmlPullParser::Event::kComment) {
949 comment = util::TrimWhitespace(parser->comment()).to_string();
951 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
956 const Source item_source = source_.WithLine(parser->line_number());
957 const std::string& element_namespace = parser->element_namespace();
958 const std::string& element_name = parser->element_name();
959 if (element_namespace.empty() &&
960 (element_name == "flag" || element_name == "enum")) {
961 if (element_name == "enum") {
962 if (type_mask & android::ResTable_map::TYPE_FLAGS) {
963 diag_->Error(DiagMessage(item_source)
964 << "can not define an <enum>; already defined a <flag>");
968 type_mask |= android::ResTable_map::TYPE_ENUM;
970 } else if (element_name == "flag") {
971 if (type_mask & android::ResTable_map::TYPE_ENUM) {
972 diag_->Error(DiagMessage(item_source)
973 << "can not define a <flag>; already defined an <enum>");
977 type_mask |= android::ResTable_map::TYPE_FLAGS;
980 if (Maybe<Attribute::Symbol> s =
981 ParseEnumOrFlagItem(parser, element_name)) {
982 Attribute::Symbol& symbol = s.value();
983 ParsedResource child_resource;
984 child_resource.name = symbol.symbol.name.value();
985 child_resource.source = item_source;
986 child_resource.value = util::make_unique<Id>();
987 out_resource->child_resources.push_back(std::move(child_resource));
989 symbol.symbol.SetComment(std::move(comment));
990 symbol.symbol.SetSource(item_source);
992 auto insert_result = items.insert(std::move(symbol));
993 if (!insert_result.second) {
994 const Attribute::Symbol& existing_symbol = *insert_result.first;
995 diag_->Error(DiagMessage(item_source)
996 << "duplicate symbol '"
997 << existing_symbol.symbol.name.value().entry << "'");
999 diag_->Note(DiagMessage(existing_symbol.symbol.GetSource())
1000 << "first defined here");
1006 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1007 diag_->Error(DiagMessage(item_source) << ":" << element_name << ">");
1018 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak);
1019 attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end());
1021 type_mask ? type_mask : uint32_t(android::ResTable_map::TYPE_ANY);
1023 attr->min_int = maybe_min.value();
1027 attr->max_int = maybe_max.value();
1029 out_resource->value = std::move(attr);
1033 Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(
1034 xml::XmlPullParser* parser, const StringPiece& tag) {
1035 const Source source = source_.WithLine(parser->line_number());
1037 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1039 diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <"
1044 Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value");
1046 diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <"
1051 std::u16string value16 = util::Utf8ToUtf16(maybe_value.value());
1052 android::Res_value val;
1053 if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) {
1054 diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value()
1056 << ">; must be an integer");
1060 return Attribute::Symbol{
1061 Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())),
1065 bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) {
1066 const Source source = source_.WithLine(parser->line_number());
1068 Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name");
1070 diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute");
1074 Maybe<Reference> maybe_key =
1075 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1077 diag_->Error(DiagMessage(source) << "invalid attribute name '"
1078 << maybe_name.value() << "'");
1082 TransformReferenceFromNamespace(parser, "", &maybe_key.value());
1083 maybe_key.value().SetSource(source);
1085 std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString);
1087 diag_->Error(DiagMessage(source) << "could not parse style item");
1091 style->entries.push_back(
1092 Style::Entry{std::move(maybe_key.value()), std::move(value)});
1096 bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
1097 ParsedResource* out_resource) {
1098 out_resource->name.type = type;
1100 std::unique_ptr<Style> style = util::make_unique<Style>();
1102 Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent");
1104 // If the parent is empty, we don't have a parent, but we also don't infer
1106 if (!maybe_parent.value().empty()) {
1107 std::string err_str;
1108 style->parent = ResourceUtils::ParseStyleParentReference(
1109 maybe_parent.value(), &err_str);
1110 if (!style->parent) {
1111 diag_->Error(DiagMessage(out_resource->source) << err_str);
1115 // Transform the namespace prefix to the actual package name, and mark the
1117 // private if appropriate.
1118 TransformReferenceFromNamespace(parser, "", &style->parent.value());
1122 // No parent was specified, so try inferring it from the style name.
1123 std::string style_name = out_resource->name.entry;
1124 size_t pos = style_name.find_last_of(u'.');
1125 if (pos != std::string::npos) {
1126 style->parent_inferred = true;
1127 style->parent = Reference(
1128 ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos)));
1133 const size_t depth = parser->depth();
1134 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1135 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1136 // Skip text and comments.
1140 const std::string& element_namespace = parser->element_namespace();
1141 const std::string& element_name = parser->element_name();
1142 if (element_namespace == "" && element_name == "item") {
1143 error |= !ParseStyleItem(parser, style.get());
1145 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1146 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1147 << ":" << element_name << ">");
1156 out_resource->value = std::move(style);
1160 bool ResourceParser::ParseArray(xml::XmlPullParser* parser,
1161 ParsedResource* out_resource) {
1162 return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_ANY);
1165 bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser,
1166 ParsedResource* out_resource) {
1167 return ParseArrayImpl(parser, out_resource,
1168 android::ResTable_map::TYPE_INTEGER);
1171 bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser,
1172 ParsedResource* out_resource) {
1173 return ParseArrayImpl(parser, out_resource,
1174 android::ResTable_map::TYPE_STRING);
1177 bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser,
1178 ParsedResource* out_resource,
1179 const uint32_t typeMask) {
1180 out_resource->name.type = ResourceType::kArray;
1182 std::unique_ptr<Array> array = util::make_unique<Array>();
1184 bool translatable = options_.translatable;
1185 if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) {
1186 Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value());
1187 if (!maybe_translatable) {
1188 diag_->Error(DiagMessage(out_resource->source)
1189 << "invalid value for 'translatable'. Must be a boolean");
1192 translatable = maybe_translatable.value();
1194 array->SetTranslatable(translatable);
1197 const size_t depth = parser->depth();
1198 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1199 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1200 // Skip text and comments.
1204 const Source item_source = source_.WithLine(parser->line_number());
1205 const std::string& element_namespace = parser->element_namespace();
1206 const std::string& element_name = parser->element_name();
1207 if (element_namespace.empty() && element_name == "item") {
1208 std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString);
1210 diag_->Error(DiagMessage(item_source) << "could not parse array item");
1214 item->SetSource(item_source);
1215 array->items.emplace_back(std::move(item));
1217 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1218 diag_->Error(DiagMessage(source_.WithLine(parser->line_number()))
1219 << "unknown tag <" << element_namespace << ":"
1220 << element_name << ">");
1229 out_resource->value = std::move(array);
1233 bool ResourceParser::ParsePlural(xml::XmlPullParser* parser,
1234 ParsedResource* out_resource) {
1235 out_resource->name.type = ResourceType::kPlurals;
1237 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
1240 const size_t depth = parser->depth();
1241 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1242 if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1243 // Skip text and comments.
1247 const Source item_source = source_.WithLine(parser->line_number());
1248 const std::string& element_namespace = parser->element_namespace();
1249 const std::string& element_name = parser->element_name();
1250 if (element_namespace.empty() && element_name == "item") {
1251 Maybe<StringPiece> maybe_quantity =
1252 xml::FindNonEmptyAttribute(parser, "quantity");
1253 if (!maybe_quantity) {
1254 diag_->Error(DiagMessage(item_source)
1255 << "<item> in <plurals> requires attribute "
1261 StringPiece trimmed_quantity =
1262 util::TrimWhitespace(maybe_quantity.value());
1264 if (trimmed_quantity == "zero") {
1265 index = Plural::Zero;
1266 } else if (trimmed_quantity == "one") {
1267 index = Plural::One;
1268 } else if (trimmed_quantity == "two") {
1269 index = Plural::Two;
1270 } else if (trimmed_quantity == "few") {
1271 index = Plural::Few;
1272 } else if (trimmed_quantity == "many") {
1273 index = Plural::Many;
1274 } else if (trimmed_quantity == "other") {
1275 index = Plural::Other;
1277 diag_->Error(DiagMessage(item_source)
1278 << "<item> in <plural> has invalid value '"
1279 << trimmed_quantity << "' for attribute 'quantity'");
1284 if (plural->values[index]) {
1285 diag_->Error(DiagMessage(item_source) << "duplicate quantity '"
1286 << trimmed_quantity << "'");
1291 if (!(plural->values[index] = ParseXml(
1292 parser, android::ResTable_map::TYPE_STRING, kNoRawString))) {
1295 plural->values[index]->SetSource(item_source);
1297 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1298 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1299 << element_namespace << ":"
1300 << element_name << ">");
1309 out_resource->value = std::move(plural);
1313 bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser,
1314 ParsedResource* out_resource) {
1315 out_resource->name.type = ResourceType::kStyleable;
1317 // Declare-styleable is kPrivate by default, because it technically only
1318 // exists in R.java.
1319 out_resource->symbol_state = SymbolState::kPublic;
1321 // Declare-styleable only ends up in default config;
1322 if (out_resource->config != ConfigDescription::DefaultConfig()) {
1323 diag_->Warn(DiagMessage(out_resource->source)
1324 << "ignoring configuration '" << out_resource->config
1325 << "' for styleable " << out_resource->name.entry);
1326 out_resource->config = ConfigDescription::DefaultConfig();
1329 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>();
1331 std::string comment;
1333 const size_t depth = parser->depth();
1334 while (xml::XmlPullParser::NextChildNode(parser, depth)) {
1335 if (parser->event() == xml::XmlPullParser::Event::kComment) {
1336 comment = util::TrimWhitespace(parser->comment()).to_string();
1338 } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) {
1343 const Source item_source = source_.WithLine(parser->line_number());
1344 const std::string& element_namespace = parser->element_namespace();
1345 const std::string& element_name = parser->element_name();
1346 if (element_namespace.empty() && element_name == "attr") {
1347 Maybe<StringPiece> maybe_name =
1348 xml::FindNonEmptyAttribute(parser, "name");
1350 diag_->Error(DiagMessage(item_source)
1351 << "<attr> tag must have a 'name' attribute");
1356 // If this is a declaration, the package name may be in the name. Separate
1358 // Eg. <attr name="android:text" />
1359 Maybe<Reference> maybe_ref =
1360 ResourceUtils::ParseXmlAttributeName(maybe_name.value());
1362 diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '"
1363 << maybe_name.value() << "'");
1368 Reference& child_ref = maybe_ref.value();
1369 xml::TransformReferenceFromNamespace(parser, "", &child_ref);
1371 // Create the ParsedResource that will add the attribute to the table.
1372 ParsedResource child_resource;
1373 child_resource.name = child_ref.name.value();
1374 child_resource.source = item_source;
1375 child_resource.comment = std::move(comment);
1377 if (!ParseAttrImpl(parser, &child_resource, true)) {
1382 // Create the reference to this attribute.
1383 child_ref.SetComment(child_resource.comment);
1384 child_ref.SetSource(item_source);
1385 styleable->entries.push_back(std::move(child_ref));
1387 out_resource->child_resources.push_back(std::move(child_resource));
1389 } else if (!ShouldIgnoreElement(element_namespace, element_name)) {
1390 diag_->Error(DiagMessage(item_source) << "unknown tag <"
1391 << element_namespace << ":"
1392 << element_name << ">");
1403 out_resource->value = std::move(styleable);