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 "link/Linkers.h"
18
19 #include "androidfw/ResourceTypes.h"
20
21 #include "Diagnostics.h"
22 #include "ResourceUtils.h"
23 #include "SdkConstants.h"
24 #include "ValueVisitor.h"
25 #include "link/ReferenceLinker.h"
26 #include "process/IResourceTableConsumer.h"
27 #include "process/SymbolTable.h"
28 #include "util/Util.h"
29 #include "xml/XmlDom.h"
30
31 namespace aapt {
32
33 namespace {
34
35 // Visits all references (including parents of styles, references in styles, arrays, etc) and
36 // links their symbolic name to their Resource ID, performing mangling and package aliasing
37 // as needed.
38 class ReferenceVisitor : public DescendingValueVisitor {
39 public:
40 using DescendingValueVisitor::Visit;
41
ReferenceVisitor(const CallSite & callsite,IAaptContext * context,SymbolTable * symbols,xml::IPackageDeclStack * decls)42 ReferenceVisitor(const CallSite& callsite, IAaptContext* context, SymbolTable* symbols,
43 xml::IPackageDeclStack* decls)
44 : callsite_(callsite), context_(context), symbols_(symbols), decls_(decls), error_(false) {}
45
Visit(Reference * ref)46 void Visit(Reference* ref) override {
47 if (!ReferenceLinker::LinkReference(callsite_, ref, context_, symbols_, decls_)) {
48 error_ = true;
49 }
50 }
51
HasError() const52 bool HasError() const {
53 return error_;
54 }
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(ReferenceVisitor);
58
59 const CallSite& callsite_;
60 IAaptContext* context_;
61 SymbolTable* symbols_;
62 xml::IPackageDeclStack* decls_;
63 bool error_;
64 };
65
66 // Visits each xml Element and compiles the attributes within.
67 class XmlVisitor : public xml::PackageAwareVisitor {
68 public:
69 using xml::PackageAwareVisitor::Visit;
70
XmlVisitor(const Source & source,const CallSite & callsite,IAaptContext * context,SymbolTable * symbols)71 XmlVisitor(const Source& source, const CallSite& callsite, IAaptContext* context,
72 SymbolTable* symbols)
73 : source_(source),
74 callsite_(callsite),
75 context_(context),
76 symbols_(symbols),
77 reference_visitor_(callsite, context, symbols, this) {
78 }
79
Visit(xml::Element * el)80 void Visit(xml::Element* el) override {
81 // The default Attribute allows everything except enums or flags.
82 Attribute default_attribute(android::ResTable_map::TYPE_ANY);
83 default_attribute.SetWeak(true);
84
85 const Source source = source_.WithLine(el->line_number);
86 for (xml::Attribute& attr : el->attributes) {
87 // If the attribute has no namespace, interpret values as if
88 // they were assigned to the default Attribute.
89
90 const Attribute* attribute = &default_attribute;
91
92 if (Maybe<xml::ExtractedPackage> maybe_package =
93 xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
94 // There is a valid package name for this attribute. We will look this up.
95 Reference attr_ref(
96 ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
97 attr_ref.private_reference = maybe_package.value().private_namespace;
98
99 std::string err_str;
100 attr.compiled_attribute =
101 ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, symbols_, &err_str);
102
103 if (!attr.compiled_attribute) {
104 DiagMessage error_msg(source);
105 error_msg << "attribute ";
106 ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
107 error_msg << " " << err_str;
108 context_->GetDiagnostics()->Error(error_msg);
109 error_ = true;
110 continue;
111 }
112
113 attribute = &attr.compiled_attribute.value().attribute;
114 }
115
116 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
117 if (attr.compiled_value) {
118 // With a compiledValue, we must resolve the reference and assign it an ID.
119 attr.compiled_value->SetSource(source);
120 attr.compiled_value->Accept(&reference_visitor_);
121 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
122 // We won't be able to encode this as a string.
123 DiagMessage msg(source);
124 msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
125 << *attribute;
126 context_->GetDiagnostics()->Error(msg);
127 error_ = true;
128 }
129 }
130
131 // Call the super implementation.
132 xml::PackageAwareVisitor::Visit(el);
133 }
134
HasError()135 bool HasError() {
136 return error_ || reference_visitor_.HasError();
137 }
138
139 private:
140 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
141
142 Source source_;
143 const CallSite& callsite_;
144 IAaptContext* context_;
145 SymbolTable* symbols_;
146
147 ReferenceVisitor reference_visitor_;
148 bool error_ = false;
149 };
150
151 } // namespace
152
Consume(IAaptContext * context,xml::XmlResource * resource)153 bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
154 CallSite callsite{resource->file.name.package};
155
156 std::string out_name = resource->file.name.entry;
157 NameMangler::Unmangle(&out_name, &callsite.package);
158
159 if (callsite.package.empty()) {
160 // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
161 // for example.
162 callsite.package = context->GetCompilationPackage();
163 }
164
165 XmlVisitor visitor(resource->file.source, callsite, context, context->GetExternalSymbols());
166 if (resource->root) {
167 resource->root->Accept(&visitor);
168 return !visitor.HasError();
169 }
170 return false;
171 }
172
173 } // namespace aapt
174