blob: 857cdd5365a0ce360f1eba7ecfb7fdb22f0f405c [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 Lesinski6b372992017-08-09 10:54:23 -070072 Maybe<xml::ExtractedPackage> maybe_pkg = TransformPackageAlias(name.package, {});
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 if (!maybe_pkg) {
Adam Lesinski6b372992017-08-09 10:54:23 -070074 context_->GetDiagnostics()->Error(DiagMessage(src) << "invalid namespace prefix '"
75 << 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 Lesinskice5e56e2016-10-21 17:56:45 -0700121 auto new_doc = util::make_unique<xml::XmlResource>();
122 new_doc->file.config = doc->file.config;
123 new_doc->file.source = doc->file.source.WithLine(decl.el->line_number);
124 new_doc->file.name = doc->file.name;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125
126 // Modify the new entry name. We need to suffix the entry with a number to
Adam Lesinski6b372992017-08-09 10:54:23 -0700127 // avoid local collisions, then mangle it with the empty package, such that it won't show up
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 // in R.java.
Adam Lesinski6b372992017-08-09 10:54:23 -0700129 new_doc->file.name.entry = NameMangler::MangleEntry(
130 {}, new_doc->file.name.entry + "__" + std::to_string(name_suffix_counter));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131
132 // Extracted elements must be the only child of <aapt:attr>.
133 // Make sure there is one root node in the children (ignore empty text).
Adam Lesinski6b372992017-08-09 10:54:23 -0700134 for (std::unique_ptr<xml::Node>& child : decl.el->children) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 const Source child_source = doc->file.source.WithLine(child->line_number);
136 if (xml::Text* t = xml::NodeCast<xml::Text>(child.get())) {
137 if (!util::TrimWhitespace(t->text).empty()) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700138 context->GetDiagnostics()->Error(DiagMessage(child_source)
139 << "can't extract text into its own resource");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 return false;
141 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 } else if (new_doc->root) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700143 context->GetDiagnostics()->Error(DiagMessage(child_source)
144 << "inline XML resources must have a single root");
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700145 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 } else {
Adam Lesinski6b372992017-08-09 10:54:23 -0700147 new_doc->root.reset(static_cast<xml::Element*>(child.release()));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 new_doc->root->parent = nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700150 }
151
Adam Lesinski6b372992017-08-09 10:54:23 -0700152 // Get the parent element of <aapt:attr>
153 xml::Element* parent_el = decl.el->parent;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (!parent_el) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700155 context->GetDiagnostics()->Error(DiagMessage(new_doc->file.source)
156 << "no suitable parent for inheriting attribute");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 return false;
158 }
159
160 // Add the inline attribute to the parent.
Adam Lesinski6b372992017-08-09 10:54:23 -0700161 parent_el->attributes.push_back(xml::Attribute{decl.attr_namespace_uri, decl.attr_name,
162 "@" + new_doc->file.name.ToString()});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163
164 // Delete the subtree.
Adam Lesinski6b372992017-08-09 10:54:23 -0700165 for (auto iter = parent_el->children.begin(); iter != parent_el->children.end(); ++iter) {
166 if (iter->get() == decl.el) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 parent_el->children.erase(iter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 break;
169 }
170 }
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 queue_.push_back(std::move(new_doc));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 name_suffix_counter++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 }
176 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700177}
178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179} // namespace aapt