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