blob: 568bc74895c716da1ed6a07b0be58796cfac4ad4 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include "link/Linkers.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080021#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "process/IResourceTableConsumer.h"
23#include "process/SymbolTable.h"
24#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026
27namespace aapt {
28
29namespace {
30
Adam Lesinski467f1712015-11-16 17:35:44 -080031/**
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 */
36class ReferenceVisitor : public ValueVisitor {
Adam Lesinski467f1712015-11-16 17:35:44 -080037public:
38 using ValueVisitor::visit;
39
Adam Lesinski64587af2016-02-18 18:33:06 -080040 ReferenceVisitor(IAaptContext* context, SymbolTable* symbols, xml::IPackageDeclStack* decls,
Adam Lesinski467f1712015-11-16 17:35:44 -080041 CallSite* callSite) :
42 mContext(context), mSymbols(symbols), mDecls(decls), mCallSite(callSite),
43 mError(false) {
44 }
45
46 void visit(Reference* ref) override {
47 if (!ReferenceLinker::linkReference(ref, mContext, mSymbols, mDecls, mCallSite)) {
48 mError = true;
49 }
50 }
51
52 bool hasError() const {
53 return mError;
54 }
Adam Lesinski64587af2016-02-18 18:33:06 -080055
56private:
57 IAaptContext* mContext;
58 SymbolTable* mSymbols;
59 xml::IPackageDeclStack* mDecls;
60 CallSite* mCallSite;
61 bool mError;
Adam Lesinski467f1712015-11-16 17:35:44 -080062};
63
64/**
65 * Visits each xml Element and compiles the attributes within.
66 */
67class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068public:
69 using xml::PackageAwareVisitor::visit;
70
Adam Lesinski64587af2016-02-18 18:33:06 -080071 XmlVisitor(IAaptContext* context, SymbolTable* symbols, const Source& source,
Adam Lesinski467f1712015-11-16 17:35:44 -080072 std::set<int>* sdkLevelsFound, CallSite* callSite) :
Adam Lesinskie352b992015-11-16 11:59:14 -080073 mContext(context), mSymbols(symbols), mSource(source), mSdkLevelsFound(sdkLevelsFound),
Adam Lesinski467f1712015-11-16 17:35:44 -080074 mCallSite(callSite), mReferenceVisitor(context, symbols, this, callSite) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 }
76
77 void visit(xml::Element* el) override {
Adam Lesinskie352b992015-11-16 11:59:14 -080078 const Source source = mSource.withLine(el->lineNumber);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski467f1712015-11-16 17:35:44 -080080 Maybe<xml::ExtractedPackage> maybePackage =
81 xml::extractPackageFromNamespace(attr.namespaceUri);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 if (maybePackage) {
83 // There is a valid package name for this attribute. We will look this up.
Adam Lesinski467f1712015-11-16 17:35:44 -080084 StringPiece16 package = maybePackage.value().package;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085 if (package.empty()) {
86 // Empty package means the 'current' or 'local' package.
87 package = mContext->getCompilationPackage();
88 }
89
Adam Lesinski467f1712015-11-16 17:35:44 -080090 Reference attrRef(ResourceNameRef(package, ResourceType::kAttr, attr.name));
91 attrRef.privateReference = maybePackage.value().privateNamespace;
92
93 std::string errStr;
94 attr.compiledAttribute = ReferenceLinker::compileXmlAttribute(
95 attrRef, mContext->getNameMangler(), mSymbols, mCallSite, &errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096
97 // Convert the string value into a compiled Value if this is a valid attribute.
98 if (attr.compiledAttribute) {
Adam Lesinski64587af2016-02-18 18:33:06 -080099 if (attr.compiledAttribute.value().id) {
100 // Record all SDK levels from which the attributes were defined.
101 const size_t sdkLevel = findAttributeSdkLevel(
102 attr.compiledAttribute.value().id.value());
103 if (sdkLevel > 1) {
104 mSdkLevelsFound->insert(sdkLevel);
105 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 }
107
108 const Attribute* attribute = &attr.compiledAttribute.value().attribute;
109 attr.compiledValue = ResourceUtils::parseItemForAttribute(attr.value,
110 attribute);
111 if (!attr.compiledValue &&
112 !(attribute->typeMask & android::ResTable_map::TYPE_STRING)) {
113 // We won't be able to encode this as a string.
114 mContext->getDiagnostics()->error(
Adam Lesinskie352b992015-11-16 11:59:14 -0800115 DiagMessage(source) << "'" << attr.value << "' "
116 << "is incompatible with attribute "
117 << package << ":" << attr.name << " "
118 << *attribute);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119 mError = true;
120 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800121
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122 } else {
Adam Lesinskie352b992015-11-16 11:59:14 -0800123 mContext->getDiagnostics()->error(DiagMessage(source)
124 << "attribute '" << package << ":"
Adam Lesinski467f1712015-11-16 17:35:44 -0800125 << attr.name << "' " << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126 mError = true;
127
128 }
129 } else {
130 // We still encode references.
131 attr.compiledValue = ResourceUtils::tryParseReference(attr.value);
132 }
133
134 if (attr.compiledValue) {
135 // With a compiledValue, we must resolve the reference and assign it an ID.
Adam Lesinskie352b992015-11-16 11:59:14 -0800136 attr.compiledValue->setSource(source);
Adam Lesinski467f1712015-11-16 17:35:44 -0800137 attr.compiledValue->accept(&mReferenceVisitor);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138 }
139 }
140
141 // Call the super implementation.
142 xml::PackageAwareVisitor::visit(el);
143 }
144
Adam Lesinski467f1712015-11-16 17:35:44 -0800145 bool hasError() {
146 return mError || mReferenceVisitor.hasError();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800148
149private:
150 IAaptContext* mContext;
151 SymbolTable* mSymbols;
152 Source mSource;
153 std::set<int>* mSdkLevelsFound;
154 CallSite* mCallSite;
155 ReferenceVisitor mReferenceVisitor;
156 bool mError = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157};
158
159} // namespace
160
Adam Lesinski467f1712015-11-16 17:35:44 -0800161bool XmlReferenceLinker::consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 mSdkLevelsFound.clear();
Adam Lesinski467f1712015-11-16 17:35:44 -0800163 CallSite callSite = { resource->file.name };
164 XmlVisitor visitor(context, context->getExternalSymbols(), resource->file.source,
165 &mSdkLevelsFound, &callSite);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166 if (resource->root) {
167 resource->root->accept(&visitor);
168 return !visitor.hasError();
169 }
170 return false;
171}
172
173} // namespace aapt