blob: 8b6c524e5c8d3f810f0bb789a5c742c91528e655 [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
Adam Lesinski6b372992017-08-09 10:54:23 -070050 const 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) {
Adam Lesinski6b372992017-08-09 10:54:23 -070054 context_->GetDiagnostics()->Error(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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 Maybe<Reference> ref = ResourceUtils::ParseXmlAttributeName(attr->value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 if (!ref) {
Adam Lesinski6b372992017-08-09 10:54:23 -070061 context_->GetDiagnostics()->Error(DiagMessage(src) << "invalid XML attribute '" << attr->value
62 << "'");
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();
68
Adam Lesinski6b372992017-08-09 10:54:23 -070069 // Use an empty string for the compilation package because we don't want to default to
70 // the local package if the user specified name="style" or something. This should just
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 // be the default namespace.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070072 Maybe<xml::ExtractedPackage> maybe_pkg = TransformPackageAlias(name.package);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 if (!maybe_pkg) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070074 context_->GetDiagnostics()->Error(DiagMessage(src)
75 << "invalid namespace prefix '" << name.package << "'");
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 return;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070078 }
79
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 const xml::ExtractedPackage& pkg = maybe_pkg.value();
Adam Lesinski6b372992017-08-09 10:54:23 -070081 const bool private_namespace = pkg.private_namespace || ref.value().private_reference;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 InlineDeclaration decl;
84 decl.el = el;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 decl.attr_name = name.entry;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 if (!pkg.package.empty()) {
Adam Lesinski6b372992017-08-09 10:54:23 -070087 decl.attr_namespace_uri = xml::BuildPackageNamespace(pkg.package, private_namespace);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 }
89
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 inline_declarations_.push_back(std::move(decl));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 }
92
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 const std::vector<InlineDeclaration>& GetInlineDeclarations() const {
94 return inline_declarations_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
96
Adam Lesinski6b372992017-08-09 10:54:23 -070097 bool HasError() const {
98 return error_;
99 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(Visitor);
103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 IAaptContext* context_;
105 xml::XmlResource* xml_resource_;
106 std::vector<InlineDeclaration> inline_declarations_;
107 bool error_ = false;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700108};
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110} // namespace
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700111
Adam Lesinski6b372992017-08-09 10:54:23 -0700112bool InlineXmlFormatParser::Consume(IAaptContext* context, xml::XmlResource* doc) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 Visitor visitor(context, doc);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 doc->root->Accept(&visitor);
115 if (visitor.HasError()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 return false;
117 }
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 size_t name_suffix_counter = 0;
Adam Lesinski6b372992017-08-09 10:54:23 -0700120 for (const InlineDeclaration& decl : visitor.GetInlineDeclarations()) {
Adam Lesinski461c8052017-10-26 17:18:19 -0700121 // Create a new XmlResource with the same ResourceFile as the base XmlResource.
122 auto new_doc = util::make_unique<xml::XmlResource>(doc->file);
123
124 // Attach the line number.
125 new_doc->file.source.line = decl.el->line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126
127 // Modify the new entry name. We need to suffix the entry with a number to
Adam Lesinski6b372992017-08-09 10:54:23 -0700128 // avoid local collisions, then mangle it with the empty package, such that it won't show up
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 // in R.java.
Adam Lesinski6b372992017-08-09 10:54:23 -0700130 new_doc->file.name.entry = NameMangler::MangleEntry(
131 {}, new_doc->file.name.entry + "__" + std::to_string(name_suffix_counter));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132
133 // Extracted elements must be the only child of <aapt:attr>.
134 // Make sure there is one root node in the children (ignore empty text).
Adam Lesinski6b372992017-08-09 10:54:23 -0700135 for (std::unique_ptr<xml::Node>& child : decl.el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 const Source child_source = doc->file.source.WithLine(child->line_number);
137 if (xml::Text* t = xml::NodeCast<xml::Text>(child.get())) {
138 if (!util::TrimWhitespace(t->text).empty()) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700139 context->GetDiagnostics()->Error(DiagMessage(child_source)
140 << "can't extract text into its own resource");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 return false;
142 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 } else if (new_doc->root) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700144 context->GetDiagnostics()->Error(DiagMessage(child_source)
145 << "inline XML resources must have a single root");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700146 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 } else {
Adam Lesinski6b372992017-08-09 10:54:23 -0700148 new_doc->root.reset(static_cast<xml::Element*>(child.release()));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 new_doc->root->parent = nullptr;
Michael Wachenschwanz7b6b02b2017-10-30 19:06:23 -0700150 // Copy down the namespace declarations
151 new_doc->root->namespace_decls = doc->root->namespace_decls;
152 // Recurse for nested inlines
153 Consume(context, new_doc.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700155 }
156
Adam Lesinski6b372992017-08-09 10:54:23 -0700157 // Get the parent element of <aapt:attr>
158 xml::Element* parent_el = decl.el->parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 if (!parent_el) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700160 context->GetDiagnostics()->Error(DiagMessage(new_doc->file.source)
161 << "no suitable parent for inheriting attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 return false;
163 }
164
165 // Add the inline attribute to the parent.
Adam Lesinski6b372992017-08-09 10:54:23 -0700166 parent_el->attributes.push_back(xml::Attribute{decl.attr_namespace_uri, decl.attr_name,
167 "@" + new_doc->file.name.ToString()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168
169 // Delete the subtree.
Adam Lesinski6b372992017-08-09 10:54:23 -0700170 for (auto iter = parent_el->children.begin(); iter != parent_el->children.end(); ++iter) {
171 if (iter->get() == decl.el) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 parent_el->children.erase(iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 break;
174 }
175 }
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 queue_.push_back(std::move(new_doc));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 name_suffix_counter++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
181 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700182}
183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184} // namespace aapt