Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "compile/InlineXmlFormatParser.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 21 | #include "ResourceUtils.h" |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 22 | #include "util/Util.h" |
| 23 | #include "xml/XmlDom.h" |
| 24 | #include "xml/XmlUtil.h" |
| 25 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | namespace { |
| 29 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 30 | struct 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 Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 37 | class Visitor : public xml::PackageAwareVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | using xml::PackageAwareVisitor::Visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | explicit Visitor(IAaptContext* context, xml::XmlResource* xml_resource) |
| 42 | : context_(context), xml_resource_(xml_resource) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | void Visit(xml::Element* el) override { |
| 45 | if (el->namespace_uri != xml::kSchemaAapt || el->name != "attr") { |
| 46 | xml::PackageAwareVisitor::Visit(el); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | return; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 50 | const Source src = xml_resource_->file.source.WithLine(el->line_number); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | xml::Attribute* attr = el->FindAttribute({}, "name"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | if (!attr) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 54 | context_->GetDiagnostics()->Error(DiagMessage(src) << "missing 'name' attribute"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | return; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | Maybe<Reference> ref = ResourceUtils::ParseXmlAttributeName(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | if (!ref) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 61 | context_->GetDiagnostics()->Error(DiagMessage(src) << "invalid XML attribute '" << attr->value |
| 62 | << "'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | return; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | const ResourceName& name = ref.value().name.value(); |
| 68 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 69 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | // be the default namespace. |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 72 | Maybe<xml::ExtractedPackage> maybe_pkg = TransformPackageAlias(name.package); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | if (!maybe_pkg) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 74 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 75 | << "invalid namespace prefix '" << name.package << "'"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | error_ = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | return; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | const xml::ExtractedPackage& pkg = maybe_pkg.value(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 81 | const bool private_namespace = pkg.private_namespace || ref.value().private_reference; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | InlineDeclaration decl; |
| 84 | decl.el = el; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | decl.attr_name = name.entry; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | if (!pkg.package.empty()) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 87 | decl.attr_namespace_uri = xml::BuildPackageNamespace(pkg.package, private_namespace); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | inline_declarations_.push_back(std::move(decl)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | const std::vector<InlineDeclaration>& GetInlineDeclarations() const { |
| 94 | return inline_declarations_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 97 | bool HasError() const { |
| 98 | return error_; |
| 99 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(Visitor); |
| 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | IAaptContext* context_; |
| 105 | xml::XmlResource* xml_resource_; |
| 106 | std::vector<InlineDeclaration> inline_declarations_; |
| 107 | bool error_ = false; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | } // namespace |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 112 | bool InlineXmlFormatParser::Consume(IAaptContext* context, xml::XmlResource* doc) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | Visitor visitor(context, doc); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | doc->root->Accept(&visitor); |
| 115 | if (visitor.HasError()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | size_t name_suffix_counter = 0; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 120 | for (const InlineDeclaration& decl : visitor.GetInlineDeclarations()) { |
Adam Lesinski | 461c805 | 2017-10-26 17:18:19 -0700 | [diff] [blame] | 121 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | |
| 127 | // Modify the new entry name. We need to suffix the entry with a number to |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 128 | // avoid local collisions, then mangle it with the empty package, such that it won't show up |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | // in R.java. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 130 | new_doc->file.name.entry = NameMangler::MangleEntry( |
| 131 | {}, new_doc->file.name.entry + "__" + std::to_string(name_suffix_counter)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | |
| 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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 135 | for (std::unique_ptr<xml::Node>& child : decl.el->children) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | 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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 139 | context->GetDiagnostics()->Error(DiagMessage(child_source) |
| 140 | << "can't extract text into its own resource"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | } else if (new_doc->root) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 144 | context->GetDiagnostics()->Error(DiagMessage(child_source) |
| 145 | << "inline XML resources must have a single root"); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 146 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | } else { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 148 | new_doc->root.reset(static_cast<xml::Element*>(child.release())); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | new_doc->root->parent = nullptr; |
Michael Wachenschwanz | 7b6b02b | 2017-10-30 19:06:23 -0700 | [diff] [blame] | 150 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 157 | // Get the parent element of <aapt:attr> |
| 158 | xml::Element* parent_el = decl.el->parent; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | if (!parent_el) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 160 | context->GetDiagnostics()->Error(DiagMessage(new_doc->file.source) |
| 161 | << "no suitable parent for inheriting attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // Add the inline attribute to the parent. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 166 | parent_el->attributes.push_back(xml::Attribute{decl.attr_namespace_uri, decl.attr_name, |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 167 | "@" + new_doc->file.name.to_string()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | |
| 169 | // Delete the subtree. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 170 | for (auto iter = parent_el->children.begin(); iter != parent_el->children.end(); ++iter) { |
| 171 | if (iter->get() == decl.el) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | parent_el->children.erase(iter); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | queue_.push_back(std::move(new_doc)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | name_suffix_counter++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | } |
| 181 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | } // namespace aapt |