blob: a26d7637ab3a27097c87160398ae31620488bf94 [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 {
37private:
38 IAaptContext* mContext;
39 ISymbolTable* mSymbols;
40 xml::IPackageDeclStack* mDecls;
41 CallSite* mCallSite;
42 bool mError;
43
44public:
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 */
67class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068private:
69 IAaptContext* mContext;
70 ISymbolTable* mSymbols;
Adam Lesinskie352b992015-11-16 11:59:14 -080071 Source mSource;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 std::set<int>* mSdkLevelsFound;
Adam Lesinski467f1712015-11-16 17:35:44 -080073 CallSite* mCallSite;
74 ReferenceVisitor mReferenceVisitor;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 bool mError = false;
76
77public:
78 using xml::PackageAwareVisitor::visit;
79
Adam Lesinski467f1712015-11-16 17:35:44 -080080 XmlVisitor(IAaptContext* context, ISymbolTable* symbols, const Source& source,
81 std::set<int>* sdkLevelsFound, CallSite* callSite) :
Adam Lesinskie352b992015-11-16 11:59:14 -080082 mContext(context), mSymbols(symbols), mSource(source), mSdkLevelsFound(sdkLevelsFound),
Adam Lesinski467f1712015-11-16 17:35:44 -080083 mCallSite(callSite), mReferenceVisitor(context, symbols, this, callSite) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 }
85
86 void visit(xml::Element* el) override {
Adam Lesinskie352b992015-11-16 11:59:14 -080087 const Source source = mSource.withLine(el->lineNumber);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski467f1712015-11-16 17:35:44 -080089 Maybe<xml::ExtractedPackage> maybePackage =
90 xml::extractPackageFromNamespace(attr.namespaceUri);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 if (maybePackage) {
92 // There is a valid package name for this attribute. We will look this up.
Adam Lesinski467f1712015-11-16 17:35:44 -080093 StringPiece16 package = maybePackage.value().package;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094 if (package.empty()) {
95 // Empty package means the 'current' or 'local' package.
96 package = mContext->getCompilationPackage();
97 }
98
Adam Lesinski467f1712015-11-16 17:35:44 -080099 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 Lesinski1ab598f2015-08-14 14:26:04 -0700105
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 Lesinskie352b992015-11-16 11:59:14 -0800121 DiagMessage(source) << "'" << attr.value << "' "
122 << "is incompatible with attribute "
123 << package << ":" << attr.name << " "
124 << *attribute);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 mError = true;
126 }
127 } else {
Adam Lesinskie352b992015-11-16 11:59:14 -0800128 mContext->getDiagnostics()->error(DiagMessage(source)
129 << "attribute '" << package << ":"
Adam Lesinski467f1712015-11-16 17:35:44 -0800130 << attr.name << "' " << errStr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 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 Lesinskie352b992015-11-16 11:59:14 -0800141 attr.compiledValue->setSource(source);
Adam Lesinski467f1712015-11-16 17:35:44 -0800142 attr.compiledValue->accept(&mReferenceVisitor);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143 }
144 }
145
146 // Call the super implementation.
147 xml::PackageAwareVisitor::visit(el);
148 }
149
Adam Lesinski467f1712015-11-16 17:35:44 -0800150 bool hasError() {
151 return mError || mReferenceVisitor.hasError();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152 }
153};
154
155} // namespace
156
Adam Lesinski467f1712015-11-16 17:35:44 -0800157bool XmlReferenceLinker::consume(IAaptContext* context, xml::XmlResource* resource) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158 mSdkLevelsFound.clear();
Adam Lesinski467f1712015-11-16 17:35:44 -0800159 CallSite callSite = { resource->file.name };
160 XmlVisitor visitor(context, context->getExternalSymbols(), resource->file.source,
161 &mSdkLevelsFound, &callSite);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 if (resource->root) {
163 resource->root->accept(&visitor);
164 return !visitor.hasError();
165 }
166 return false;
167}
168
169} // namespace aapt