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 | |
| 17 | #include "Debug.h" |
| 18 | #include "ResourceUtils.h" |
| 19 | #include "compile/InlineXmlFormatParser.h" |
| 20 | #include "util/Util.h" |
| 21 | #include "xml/XmlDom.h" |
| 22 | #include "xml/XmlUtil.h" |
| 23 | |
| 24 | #include <android-base/macros.h> |
| 25 | #include <sstream> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace aapt { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | /** |
| 33 | * XML Visitor that will find all <aapt:attr> elements for extraction. |
| 34 | */ |
| 35 | class Visitor : public xml::PackageAwareVisitor { |
| 36 | public: |
| 37 | using xml::PackageAwareVisitor::visit; |
| 38 | |
| 39 | struct InlineDeclaration { |
| 40 | xml::Element* el; |
| 41 | std::string attrNamespaceUri; |
| 42 | std::string attrName; |
| 43 | }; |
| 44 | |
| 45 | explicit Visitor(IAaptContext* context, xml::XmlResource* xmlResource) : |
| 46 | mContext(context), mXmlResource(xmlResource) { |
| 47 | } |
| 48 | |
| 49 | void visit(xml::Element* el) override { |
| 50 | if (el->namespaceUri != xml::kSchemaAapt || el->name != "attr") { |
| 51 | xml::PackageAwareVisitor::visit(el); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | const Source& src = mXmlResource->file.source.withLine(el->lineNumber); |
| 56 | |
| 57 | xml::Attribute* attr = el->findAttribute({}, "name"); |
| 58 | if (!attr) { |
| 59 | mContext->getDiagnostics()->error(DiagMessage(src) << "missing 'name' attribute"); |
| 60 | mError = true; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | Maybe<Reference> ref = ResourceUtils::parseXmlAttributeName(attr->value); |
| 65 | if (!ref) { |
| 66 | mContext->getDiagnostics()->error(DiagMessage(src) << "invalid XML attribute '" |
| 67 | << attr->value << "'"); |
| 68 | mError = true; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const ResourceName& name = ref.value().name.value(); |
| 73 | |
| 74 | // Use an empty string for the compilation package because we don't want to default to |
| 75 | // the local package if the user specified name="style" or something. This should just |
| 76 | // be the default namespace. |
| 77 | Maybe<xml::ExtractedPackage> maybePkg = transformPackageAlias(name.package, {}); |
| 78 | if (!maybePkg) { |
| 79 | mContext->getDiagnostics()->error(DiagMessage(src) << "invalid namespace prefix '" |
| 80 | << name.package << "'"); |
| 81 | mError = true; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | const xml::ExtractedPackage& pkg = maybePkg.value(); |
| 86 | const bool privateNamespace = pkg.privateNamespace || ref.value().privateReference; |
| 87 | |
| 88 | InlineDeclaration decl; |
| 89 | decl.el = el; |
| 90 | decl.attrName = name.entry; |
| 91 | if (!pkg.package.empty()) { |
| 92 | decl.attrNamespaceUri = xml::buildPackageNamespace(pkg.package, privateNamespace); |
| 93 | } |
| 94 | |
| 95 | mInlineDeclarations.push_back(std::move(decl)); |
| 96 | } |
| 97 | |
| 98 | const std::vector<InlineDeclaration>& getInlineDeclarations() const { |
| 99 | return mInlineDeclarations; |
| 100 | } |
| 101 | |
| 102 | bool hasError() const { |
| 103 | return mError; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | DISALLOW_COPY_AND_ASSIGN(Visitor); |
| 108 | |
| 109 | IAaptContext* mContext; |
| 110 | xml::XmlResource* mXmlResource; |
| 111 | std::vector<InlineDeclaration> mInlineDeclarations; |
| 112 | bool mError = false; |
| 113 | }; |
| 114 | |
| 115 | } // namespace |
| 116 | |
| 117 | bool InlineXmlFormatParser::consume(IAaptContext* context, xml::XmlResource* doc) { |
| 118 | Visitor visitor(context, doc); |
| 119 | doc->root->accept(&visitor); |
| 120 | if (visitor.hasError()) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | size_t nameSuffixCounter = 0; |
| 125 | for (const Visitor::InlineDeclaration& decl : visitor.getInlineDeclarations()) { |
| 126 | auto newDoc = util::make_unique<xml::XmlResource>(); |
| 127 | newDoc->file.config = doc->file.config; |
| 128 | newDoc->file.source = doc->file.source.withLine(decl.el->lineNumber); |
| 129 | newDoc->file.name = doc->file.name; |
| 130 | |
| 131 | // Modify the new entry name. We need to suffix the entry with a number to avoid |
| 132 | // local collisions, then mangle it with the empty package, such that it won't show up |
| 133 | // in R.java. |
| 134 | |
| 135 | newDoc->file.name.entry = NameMangler::mangleEntry( |
| 136 | {}, newDoc->file.name.entry + "__" + std::to_string(nameSuffixCounter)); |
| 137 | |
| 138 | // Extracted elements must be the only child of <aapt:attr>. |
| 139 | // Make sure there is one root node in the children (ignore empty text). |
| 140 | for (auto& child : decl.el->children) { |
| 141 | const Source childSource = doc->file.source.withLine(child->lineNumber); |
| 142 | if (xml::Text* t = xml::nodeCast<xml::Text>(child.get())) { |
| 143 | if (!util::trimWhitespace(t->text).empty()) { |
| 144 | context->getDiagnostics()->error(DiagMessage(childSource) |
| 145 | << "can't extract text into its own resource"); |
| 146 | return false; |
| 147 | } |
| 148 | } else if (newDoc->root) { |
| 149 | context->getDiagnostics()->error(DiagMessage(childSource) |
| 150 | << "inline XML resources must have a single root"); |
| 151 | return false; |
| 152 | } else { |
| 153 | newDoc->root = std::move(child); |
| 154 | newDoc->root->parent = nullptr; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Walk up and find the parent element. |
| 159 | xml::Node* node = decl.el; |
| 160 | xml::Element* parentEl = nullptr; |
| 161 | while (node->parent && (parentEl = xml::nodeCast<xml::Element>(node->parent)) == nullptr) { |
| 162 | node = node->parent; |
| 163 | } |
| 164 | |
| 165 | if (!parentEl) { |
| 166 | context->getDiagnostics()->error(DiagMessage(newDoc->file.source) |
| 167 | << "no suitable parent for inheriting attribute"); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // Add the inline attribute to the parent. |
| 172 | parentEl->attributes.push_back(xml::Attribute{ |
| 173 | decl.attrNamespaceUri, decl.attrName, "@" + newDoc->file.name.toString() }); |
| 174 | |
| 175 | // Delete the subtree. |
| 176 | for (auto iter = parentEl->children.begin(); iter != parentEl->children.end(); ++iter) { |
| 177 | if (iter->get() == node) { |
| 178 | parentEl->children.erase(iter); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | mQueue.push_back(std::move(newDoc)); |
| 184 | |
| 185 | nameSuffixCounter++; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | } // namespace aapt |