blob: 945f98a95577215f466c768f597fc53110d168dd [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/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 * 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 Lesinski467f1712015-11-16 17:35:44 -080036 * as needed.
37 */
38class ReferenceVisitor : public ValueVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 public:
40 using ValueVisitor::visit;
Adam Lesinski467f1712015-11-16 17:35:44 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 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 Lesinski467f1712015-11-16 17:35:44 -080054 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 }
Adam Lesinski467f1712015-11-16 17:35:44 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 bool hasError() const { return mError; }
Adam Lesinski467f1712015-11-16 17:35:44 -080058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 private:
60 IAaptContext* mContext;
61 SymbolTable* mSymbols;
62 xml::IPackageDeclStack* mDecls;
63 CallSite* mCallSite;
64 bool mError;
Adam Lesinski467f1712015-11-16 17:35:44 -080065};
66
67/**
68 * Visits each xml Element and compiles the attributes within.
69 */
70class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 public:
72 using xml::PackageAwareVisitor::visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 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 Lesinski1ab598f2015-08-14 14:26:04 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 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 Lesinski1ab598f2015-08-14 14:26:04 -070095 }
96
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 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 Lesinski1ab598f2015-08-14 14:26:04 -0700151 }
152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // Call the super implementation.
154 xml::PackageAwareVisitor::visit(el);
155 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 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 Lesinski1ab598f2015-08-14 14:26:04 -0700167};
168
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171bool 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 Lesinski1ab598f2015-08-14 14:26:04 -0700182}
183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184} // namespace aapt