Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "Diagnostics.h" |
| 18 | #include "ResourceUtils.h" |
| 19 | #include "SdkConstants.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "link/Linkers.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 21 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "process/IResourceTableConsumer.h" |
| 23 | #include "process/SymbolTable.h" |
| 24 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 25 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | namespace { |
| 30 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 31 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 32 | * Visits all references (including parents of styles, references in styles, |
| 33 | * arrays, etc) and |
| 34 | * links their symbolic name to their Resource ID, performing mangling and |
| 35 | * package aliasing |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 36 | * as needed. |
| 37 | */ |
| 38 | class ReferenceVisitor : public ValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 39 | public: |
| 40 | using ValueVisitor::visit; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 42 | ReferenceVisitor(IAaptContext* context, SymbolTable* symbols, |
| 43 | xml::IPackageDeclStack* decls, CallSite* callSite) |
| 44 | : mContext(context), |
| 45 | mSymbols(symbols), |
| 46 | mDecls(decls), |
| 47 | mCallSite(callSite), |
| 48 | mError(false) {} |
| 49 | |
| 50 | void visit(Reference* ref) override { |
| 51 | if (!ReferenceLinker::linkReference(ref, mContext, mSymbols, mDecls, |
| 52 | mCallSite)) { |
| 53 | mError = true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 54 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 55 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 57 | bool hasError() const { return mError; } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 59 | private: |
| 60 | IAaptContext* mContext; |
| 61 | SymbolTable* mSymbols; |
| 62 | xml::IPackageDeclStack* mDecls; |
| 63 | CallSite* mCallSite; |
| 64 | bool mError; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Visits each xml Element and compiles the attributes within. |
| 69 | */ |
| 70 | class XmlVisitor : public xml::PackageAwareVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 71 | public: |
| 72 | using xml::PackageAwareVisitor::visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 74 | XmlVisitor(IAaptContext* context, SymbolTable* symbols, const Source& source, |
| 75 | std::set<int>* sdkLevelsFound, CallSite* callSite) |
| 76 | : mContext(context), |
| 77 | mSymbols(symbols), |
| 78 | mSource(source), |
| 79 | mSdkLevelsFound(sdkLevelsFound), |
| 80 | mCallSite(callSite), |
| 81 | mReferenceVisitor(context, symbols, this, callSite) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 83 | void visit(xml::Element* el) override { |
| 84 | const Source source = mSource.withLine(el->lineNumber); |
| 85 | for (xml::Attribute& attr : el->attributes) { |
| 86 | Maybe<xml::ExtractedPackage> maybePackage = |
| 87 | xml::extractPackageFromNamespace(attr.namespaceUri); |
| 88 | if (maybePackage) { |
| 89 | // There is a valid package name for this attribute. We will look this |
| 90 | // up. |
| 91 | StringPiece package = maybePackage.value().package; |
| 92 | if (package.empty()) { |
| 93 | // Empty package means the 'current' or 'local' package. |
| 94 | package = mContext->getCompilationPackage(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 97 | Reference attrRef( |
| 98 | ResourceNameRef(package, ResourceType::kAttr, attr.name)); |
| 99 | attrRef.privateReference = maybePackage.value().privateNamespace; |
| 100 | |
| 101 | std::string errStr; |
| 102 | attr.compiledAttribute = ReferenceLinker::compileXmlAttribute( |
| 103 | attrRef, mContext->getNameMangler(), mSymbols, mCallSite, &errStr); |
| 104 | |
| 105 | // Convert the string value into a compiled Value if this is a valid |
| 106 | // attribute. |
| 107 | if (attr.compiledAttribute) { |
| 108 | if (attr.compiledAttribute.value().id) { |
| 109 | // Record all SDK levels from which the attributes were defined. |
| 110 | const size_t sdkLevel = findAttributeSdkLevel( |
| 111 | attr.compiledAttribute.value().id.value()); |
| 112 | if (sdkLevel > 1) { |
| 113 | mSdkLevelsFound->insert(sdkLevel); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | const Attribute* attribute = |
| 118 | &attr.compiledAttribute.value().attribute; |
| 119 | attr.compiledValue = |
| 120 | ResourceUtils::tryParseItemForAttribute(attr.value, attribute); |
| 121 | if (!attr.compiledValue && |
| 122 | !(attribute->typeMask & android::ResTable_map::TYPE_STRING)) { |
| 123 | // We won't be able to encode this as a string. |
| 124 | mContext->getDiagnostics()->error( |
| 125 | DiagMessage(source) << "'" << attr.value << "' " |
| 126 | << "is incompatible with attribute " |
| 127 | << package << ":" << attr.name << " " |
| 128 | << *attribute); |
| 129 | mError = true; |
| 130 | } |
| 131 | |
| 132 | } else { |
| 133 | mContext->getDiagnostics()->error(DiagMessage(source) |
| 134 | << "attribute '" << package << ":" |
| 135 | << attr.name << "' " << errStr); |
| 136 | mError = true; |
| 137 | } |
| 138 | } else if (!attr.compiledValue) { |
| 139 | // We still encode references, but only if we haven't manually set this |
| 140 | // to |
| 141 | // another compiled value. |
| 142 | attr.compiledValue = ResourceUtils::tryParseReference(attr.value); |
| 143 | } |
| 144 | |
| 145 | if (attr.compiledValue) { |
| 146 | // With a compiledValue, we must resolve the reference and assign it an |
| 147 | // ID. |
| 148 | attr.compiledValue->setSource(source); |
| 149 | attr.compiledValue->accept(&mReferenceVisitor); |
| 150 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 153 | // Call the super implementation. |
| 154 | xml::PackageAwareVisitor::visit(el); |
| 155 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 156 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 157 | bool hasError() { return mError || mReferenceVisitor.hasError(); } |
| 158 | |
| 159 | private: |
| 160 | IAaptContext* mContext; |
| 161 | SymbolTable* mSymbols; |
| 162 | Source mSource; |
| 163 | std::set<int>* mSdkLevelsFound; |
| 164 | CallSite* mCallSite; |
| 165 | ReferenceVisitor mReferenceVisitor; |
| 166 | bool mError = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 169 | } // namespace |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 171 | bool XmlReferenceLinker::consume(IAaptContext* context, |
| 172 | xml::XmlResource* resource) { |
| 173 | mSdkLevelsFound.clear(); |
| 174 | CallSite callSite = {resource->file.name}; |
| 175 | XmlVisitor visitor(context, context->getExternalSymbols(), |
| 176 | resource->file.source, &mSdkLevelsFound, &callSite); |
| 177 | if (resource->root) { |
| 178 | resource->root->accept(&visitor); |
| 179 | return !visitor.hasError(); |
| 180 | } |
| 181 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 184 | } // namespace aapt |