11ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski/* 21ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Copyright (C) 2015 The Android Open Source Project 31ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * 41ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 51ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * you may not use this file except in compliance with the License. 61ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * You may obtain a copy of the License at 71ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * 81ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 91ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * 101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * Unless required by applicable law or agreed to in writing, software 111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * See the License for the specific language governing permissions and 141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski * limitations under the License. 151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski */ 161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "Diagnostics.h" 181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "ResourceUtils.h" 191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "SdkConstants.h" 201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "link/Linkers.h" 21467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "link/ReferenceLinker.h" 221ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "process/IResourceTableConsumer.h" 231ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "process/SymbolTable.h" 241ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "util/Util.h" 25467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include "xml/XmlDom.h" 261ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskinamespace aapt { 281ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 291ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskinamespace { 301ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 31467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski/** 32467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Visits all references (including parents of styles, references in styles, arrays, etc) and 33467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * links their symbolic name to their Resource ID, performing mangling and package aliasing 34467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * as needed. 35467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski */ 36467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinskiclass ReferenceVisitor : public ValueVisitor { 37467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinskipublic: 38467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski using ValueVisitor::visit; 39467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski 4064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski ReferenceVisitor(IAaptContext* context, SymbolTable* symbols, xml::IPackageDeclStack* decls, 41467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski CallSite* callSite) : 42467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski mContext(context), mSymbols(symbols), mDecls(decls), mCallSite(callSite), 43467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski mError(false) { 44467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski } 45467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski 46467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski void visit(Reference* ref) override { 47467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski if (!ReferenceLinker::linkReference(ref, mContext, mSymbols, mDecls, mCallSite)) { 48467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski mError = true; 49467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski } 50467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski } 51467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski 52467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski bool hasError() const { 53467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski return mError; 54467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski } 5564587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski 5664587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinskiprivate: 5764587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski IAaptContext* mContext; 5864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski SymbolTable* mSymbols; 5964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski xml::IPackageDeclStack* mDecls; 6064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski CallSite* mCallSite; 6164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski bool mError; 62467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski}; 63467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski 64467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski/** 65467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski * Visits each xml Element and compiles the attributes within. 66467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski */ 67467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinskiclass XmlVisitor : public xml::PackageAwareVisitor { 681ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskipublic: 691ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski using xml::PackageAwareVisitor::visit; 701ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 7164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski XmlVisitor(IAaptContext* context, SymbolTable* symbols, const Source& source, 72467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski std::set<int>* sdkLevelsFound, CallSite* callSite) : 73e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski mContext(context), mSymbols(symbols), mSource(source), mSdkLevelsFound(sdkLevelsFound), 74467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski mCallSite(callSite), mReferenceVisitor(context, symbols, this, callSite) { 751ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 761ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 771ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski void visit(xml::Element* el) override { 78e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski const Source source = mSource.withLine(el->lineNumber); 791ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski for (xml::Attribute& attr : el->attributes) { 80467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski Maybe<xml::ExtractedPackage> maybePackage = 81467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski xml::extractPackageFromNamespace(attr.namespaceUri); 821ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski if (maybePackage) { 831ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // There is a valid package name for this attribute. We will look this up. 84467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski StringPiece16 package = maybePackage.value().package; 851ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski if (package.empty()) { 861ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // Empty package means the 'current' or 'local' package. 871ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski package = mContext->getCompilationPackage(); 881ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 891ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 90467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski Reference attrRef(ResourceNameRef(package, ResourceType::kAttr, attr.name)); 91467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski attrRef.privateReference = maybePackage.value().privateNamespace; 92467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski 93467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski std::string errStr; 94467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski attr.compiledAttribute = ReferenceLinker::compileXmlAttribute( 95467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski attrRef, mContext->getNameMangler(), mSymbols, mCallSite, &errStr); 961ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 971ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // Convert the string value into a compiled Value if this is a valid attribute. 981ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski if (attr.compiledAttribute) { 9964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski if (attr.compiledAttribute.value().id) { 10064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski // Record all SDK levels from which the attributes were defined. 10164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski const size_t sdkLevel = findAttributeSdkLevel( 10264587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski attr.compiledAttribute.value().id.value()); 10364587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski if (sdkLevel > 1) { 10464587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski mSdkLevelsFound->insert(sdkLevel); 10564587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski } 1061ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1071ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 1081ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski const Attribute* attribute = &attr.compiledAttribute.value().attribute; 1091ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski attr.compiledValue = ResourceUtils::parseItemForAttribute(attr.value, 1101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski attribute); 1111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski if (!attr.compiledValue && 1121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski !(attribute->typeMask & android::ResTable_map::TYPE_STRING)) { 1131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // We won't be able to encode this as a string. 1141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski mContext->getDiagnostics()->error( 115e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski DiagMessage(source) << "'" << attr.value << "' " 116e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski << "is incompatible with attribute " 117e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski << package << ":" << attr.name << " " 118e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski << *attribute); 1191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski mError = true; 1201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 12164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski 1221ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } else { 123e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski mContext->getDiagnostics()->error(DiagMessage(source) 124e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski << "attribute '" << package << ":" 125467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski << attr.name << "' " << errStr); 1261ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski mError = true; 1271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 1281ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1291ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } else { 1301ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // We still encode references. 1311ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski attr.compiledValue = ResourceUtils::tryParseReference(attr.value); 1321ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1331ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 1341ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski if (attr.compiledValue) { 1351ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // With a compiledValue, we must resolve the reference and assign it an ID. 136e352b990e1dca7857a4d170f411ddad2065670caAdam Lesinski attr.compiledValue->setSource(source); 137467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski attr.compiledValue->accept(&mReferenceVisitor); 1381ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1391ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1401ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 1411ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski // Call the super implementation. 1421ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski xml::PackageAwareVisitor::visit(el); 1431ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1441ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 145467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski bool hasError() { 146467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski return mError || mReferenceVisitor.hasError(); 1471ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 14864587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski 14964587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinskiprivate: 15064587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski IAaptContext* mContext; 15164587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski SymbolTable* mSymbols; 15264587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski Source mSource; 15364587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski std::set<int>* mSdkLevelsFound; 15464587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski CallSite* mCallSite; 15564587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski ReferenceVisitor mReferenceVisitor; 15664587af8179affd38ee26543b748f2d63b7f67bbAdam Lesinski bool mError = false; 1571ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski}; 1581ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 1591ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} // namespace 1601ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 161467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinskibool XmlReferenceLinker::consume(IAaptContext* context, xml::XmlResource* resource) { 1621ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski mSdkLevelsFound.clear(); 163467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski CallSite callSite = { resource->file.name }; 164467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski XmlVisitor visitor(context, context->getExternalSymbols(), resource->file.source, 165467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski &mSdkLevelsFound, &callSite); 1661ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski if (resource->root) { 1671ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski resource->root->accept(&visitor); 1681ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski return !visitor.hasError(); 1691ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski } 1701ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski return false; 1711ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} 1721ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 1731ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} // namespace aapt 174