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 | 6b37299 | 2017-08-09 10:54:23 -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 | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 74 | context_->GetDiagnostics()->Error(DiagMessage(src) << "invalid namespace prefix '" |
| 75 | << 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 | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | |
| 126 | // 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] | 127 | // 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] | 128 | // in R.java. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 129 | new_doc->file.name.entry = NameMangler::MangleEntry( |
| 130 | {}, new_doc->file.name.entry + "__" + std::to_string(name_suffix_counter)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | |
| 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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 134 | for (std::unique_ptr<xml::Node>& child : decl.el->children) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | 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 Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 138 | context->GetDiagnostics()->Error(DiagMessage(child_source) |
| 139 | << "can't extract text into its own resource"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | } else if (new_doc->root) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 143 | context->GetDiagnostics()->Error(DiagMessage(child_source) |
| 144 | << "inline XML resources must have a single root"); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 145 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | } else { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 147 | new_doc->root.reset(static_cast<xml::Element*>(child.release())); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | new_doc->root->parent = nullptr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 152 | // Get the parent element of <aapt:attr> |
| 153 | xml::Element* parent_el = decl.el->parent; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | if (!parent_el) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 155 | context->GetDiagnostics()->Error(DiagMessage(new_doc->file.source) |
| 156 | << "no suitable parent for inheriting attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | // Add the inline attribute to the parent. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 161 | parent_el->attributes.push_back(xml::Attribute{decl.attr_namespace_uri, decl.attr_name, |
| 162 | "@" + new_doc->file.name.ToString()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | |
| 164 | // Delete the subtree. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 165 | for (auto iter = parent_el->children.begin(); iter != parent_el->children.end(); ++iter) { |
| 166 | if (iter->get() == decl.el) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | parent_el->children.erase(iter); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | queue_.push_back(std::move(new_doc)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | name_suffix_counter++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | } |
| 176 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | } // namespace aapt |