blob: 444f82109de4d7e4d1fc9af568550545aedf7fa0 [file] [log] [blame]
Adam Lesinski5eeaadd2016-08-25 12:26:56 -07001/*
2 * Copyright (C) 2016 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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "compile/InlineXmlFormatParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include <string>
20
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070021#include "ResourceUtils.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070022#include "util/Util.h"
23#include "xml/XmlDom.h"
24#include "xml/XmlUtil.h"
25
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070026namespace aapt {
27
28namespace {
29
Adam Lesinski6b372992017-08-09 10:54:23 -070030struct InlineDeclaration {
31 xml::Element* el;
32 std::string attr_namespace_uri;
33 std::string attr_name;
34};
35
36// XML Visitor that will find all <aapt:attr> elements for extraction.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070037class Visitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 using xml::PackageAwareVisitor::Visit;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 explicit Visitor(IAaptContext* context, xml::XmlResource* xml_resource)
42 : context_(context), xml_resource_(xml_resource) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 void Visit(xml::Element* el) override {
45 if (el->namespace_uri != xml::kSchemaAapt || el->name != "attr") {
46 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 return;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070048 }
49
Jeremy Meyer56f36e82022-05-20 20:35:42 +000050 const android::Source src = xml_resource_->file.source.WithLine(el->line_number);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 xml::Attribute* attr = el->FindAttribute({}, "name");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 if (!attr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000054 context_->GetDiagnostics()->Error(android::DiagMessage(src) << "missing 'name' attribute");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 return;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070057 }
58
Ryan Mitchell4382e442021-07-14 12:53:01 -070059 std::optional<Reference> ref = ResourceUtils::ParseXmlAttributeName(attr->value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 if (!ref) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000061 context_->GetDiagnostics()->Error(android::DiagMessage(src)
62 << "invalid XML attribute '" << attr->value << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070065 }
66
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 const ResourceName& name = ref.value().name.value();
Ryan Mitchell4382e442021-07-14 12:53:01 -070068 std::optional<xml::ExtractedPackage> maybe_pkg = TransformPackageAlias(name.package);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 if (!maybe_pkg) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000070 context_->GetDiagnostics()->Error(android::DiagMessage(src)
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070071 << "invalid namespace prefix '" << name.package << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070074 }
75
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 const xml::ExtractedPackage& pkg = maybe_pkg.value();
Adam Lesinski6b372992017-08-09 10:54:23 -070077 const bool private_namespace = pkg.private_namespace || ref.value().private_reference;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 InlineDeclaration decl;
80 decl.el = el;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 decl.attr_name = name.entry;
Adam Lesinskic9a29262018-03-01 20:04:00 -080082
83 // We need to differentiate between no-namespace defined, or the alias resolves to an empty
84 // package, which means we must use the res-auto schema.
85 if (!name.package.empty()) {
86 if (pkg.package.empty()) {
87 decl.attr_namespace_uri = xml::kSchemaAuto;
88 } else {
89 decl.attr_namespace_uri = xml::BuildPackageNamespace(pkg.package, private_namespace);
90 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 }
92
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 inline_declarations_.push_back(std::move(decl));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 }
95
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 const std::vector<InlineDeclaration>& GetInlineDeclarations() const {
97 return inline_declarations_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 }
99
Adam Lesinski6b372992017-08-09 10:54:23 -0700100 bool HasError() const {
101 return error_;
102 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(Visitor);
106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 IAaptContext* context_;
108 xml::XmlResource* xml_resource_;
109 std::vector<InlineDeclaration> inline_declarations_;
110 bool error_ = false;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700111};
112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700114
Adam Lesinski6b372992017-08-09 10:54:23 -0700115bool InlineXmlFormatParser::Consume(IAaptContext* context, xml::XmlResource* doc) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 Visitor visitor(context, doc);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 doc->root->Accept(&visitor);
118 if (visitor.HasError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 return false;
120 }
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 size_t name_suffix_counter = 0;
Adam Lesinski6b372992017-08-09 10:54:23 -0700123 for (const InlineDeclaration& decl : visitor.GetInlineDeclarations()) {
Adam Lesinski461c8052017-10-26 17:18:19 -0700124 // Create a new XmlResource with the same ResourceFile as the base XmlResource.
125 auto new_doc = util::make_unique<xml::XmlResource>(doc->file);
126
127 // Attach the line number.
128 new_doc->file.source.line = decl.el->line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129
130 // Modify the new entry name. We need to suffix the entry with a number to
Adam Lesinski6b372992017-08-09 10:54:23 -0700131 // avoid local collisions, then mangle it with the empty package, such that it won't show up
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 // in R.java.
Adam Lesinski6b372992017-08-09 10:54:23 -0700133 new_doc->file.name.entry = NameMangler::MangleEntry(
134 {}, new_doc->file.name.entry + "__" + std::to_string(name_suffix_counter));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135
136 // Extracted elements must be the only child of <aapt:attr>.
137 // Make sure there is one root node in the children (ignore empty text).
Adam Lesinski6b372992017-08-09 10:54:23 -0700138 for (std::unique_ptr<xml::Node>& child : decl.el->children) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000139 const android::Source child_source = doc->file.source.WithLine(child->line_number);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 if (xml::Text* t = xml::NodeCast<xml::Text>(child.get())) {
141 if (!util::TrimWhitespace(t->text).empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000142 context->GetDiagnostics()->Error(android::DiagMessage(child_source)
Adam Lesinski6b372992017-08-09 10:54:23 -0700143 << "can't extract text into its own resource");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 return false;
145 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 } else if (new_doc->root) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000147 context->GetDiagnostics()->Error(android::DiagMessage(child_source)
Adam Lesinski6b372992017-08-09 10:54:23 -0700148 << "inline XML resources must have a single root");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700149 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 } else {
Adam Lesinski6b372992017-08-09 10:54:23 -0700151 new_doc->root.reset(static_cast<xml::Element*>(child.release()));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 new_doc->root->parent = nullptr;
Michael Wachenschwanz7b6b02b2017-10-30 19:06:23 -0700153 // Copy down the namespace declarations
154 new_doc->root->namespace_decls = doc->root->namespace_decls;
155 // Recurse for nested inlines
156 Consume(context, new_doc.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700158 }
159
Adam Lesinski6b372992017-08-09 10:54:23 -0700160 // Get the parent element of <aapt:attr>
161 xml::Element* parent_el = decl.el->parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 if (!parent_el) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000163 context->GetDiagnostics()->Error(android::DiagMessage(new_doc->file.source)
Adam Lesinski6b372992017-08-09 10:54:23 -0700164 << "no suitable parent for inheriting attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 return false;
166 }
167
168 // Add the inline attribute to the parent.
Adam Lesinski6b372992017-08-09 10:54:23 -0700169 parent_el->attributes.push_back(xml::Attribute{decl.attr_namespace_uri, decl.attr_name,
Adam Lesinski93190b72017-11-03 15:20:17 -0700170 "@" + new_doc->file.name.to_string()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171
172 // Delete the subtree.
Adam Lesinski6b372992017-08-09 10:54:23 -0700173 for (auto iter = parent_el->children.begin(); iter != parent_el->children.end(); ++iter) {
174 if (iter->get() == decl.el) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 parent_el->children.erase(iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 break;
177 }
178 }
179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 queue_.push_back(std::move(new_doc));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 name_suffix_counter++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 }
184 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700185}
186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187} // namespace aapt