xref: /aosp_15_r20/frameworks/base/tools/aapt2/link/XmlReferenceLinker.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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 "ResourceUtils.h"
18 #include "SdkConstants.h"
19 #include "ValueVisitor.h"
20 #include "androidfw/IDiagnostics.h"
21 #include "androidfw/ResourceTypes.h"
22 #include "link/Linkers.h"
23 #include "link/ReferenceLinker.h"
24 #include "process/IResourceTableConsumer.h"
25 #include "process/SymbolTable.h"
26 #include "trace/TraceBuffer.h"
27 #include "util/Util.h"
28 #include "xml/XmlDom.h"
29 
30 namespace aapt {
31 
32 namespace {
33 
34 // Visits each xml Element and compiles the attributes within.
35 class XmlVisitor : public xml::PackageAwareVisitor {
36  public:
37   using xml::PackageAwareVisitor::Visit;
38 
XmlVisitor(const android::Source & source,android::StringPool * pool,const CallSite & callsite,IAaptContext * context,ResourceTable * table,SymbolTable * symbols)39   XmlVisitor(const android::Source& source, android::StringPool* pool, const CallSite& callsite,
40              IAaptContext* context, ResourceTable* table, SymbolTable* symbols)
41       : source_(source),
42         callsite_(callsite),
43         context_(context),
44         symbols_(symbols),
45         reference_transformer_(callsite, context, symbols, pool, table, this) {
46   }
47 
Visit(xml::Element * el)48   void Visit(xml::Element* el) override {
49     // The default Attribute allows everything except enums or flags.
50     Attribute default_attribute(android::ResTable_map::TYPE_ANY);
51     default_attribute.SetWeak(true);
52 
53     // The default orientation of gradients in android Q is different than previous android
54     // versions. Set the android:angle attribute to "0" to ensure that the default gradient
55     // orientation will remain left-to-right in android Q.
56     if (el->name == "gradient" && context_->GetMinSdkVersion() <= SDK_Q) {
57       if (!el->FindAttribute(xml::kSchemaAndroid, "angle")) {
58         el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "angle", "0"});
59       }
60     }
61 
62     const android::Source source = source_.WithLine(el->line_number);
63     for (xml::Attribute& attr : el->attributes) {
64       // If the attribute has no namespace, interpret values as if
65       // they were assigned to the default Attribute.
66 
67       const Attribute* attribute = &default_attribute;
68 
69       if (std::optional<xml::ExtractedPackage> maybe_package =
70               xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
71         // There is a valid package name for this attribute. We will look this up.
72         Reference attr_ref(
73             ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
74         attr_ref.private_reference = maybe_package.value().private_namespace;
75 
76         std::string err_str;
77         attr.compiled_attribute =
78             ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, context_, symbols_, &err_str);
79 
80         if (!attr.compiled_attribute) {
81           android::DiagMessage error_msg(source);
82           error_msg << "attribute ";
83           ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
84           error_msg << " " << err_str;
85           context_->GetDiagnostics()->Error(error_msg);
86           error_ = true;
87           continue;
88         }
89 
90         attribute = &attr.compiled_attribute.value().attribute;
91       }
92 
93       attr.compiled_value = ResourceUtils::TryParseItemForAttribute(context_->GetDiagnostics(),
94                                                                     attr.value, attribute);
95       if (attr.compiled_value) {
96         // With a compiledValue, we must resolve the reference and assign it an ID.
97         attr.compiled_value->SetSource(source);
98         attr.compiled_value = attr.compiled_value->Transform(reference_transformer_);
99       } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
100         // We won't be able to encode this as a string.
101         android::DiagMessage msg(source);
102         msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
103             << *attribute;
104         context_->GetDiagnostics()->Error(msg);
105         error_ = true;
106       }
107     }
108 
109     // Call the super implementation.
110     xml::PackageAwareVisitor::Visit(el);
111   }
112 
HasError()113   bool HasError() {
114     return error_ || reference_transformer_.HasError();
115   }
116 
117  private:
118   DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
119 
120   android::Source source_;
121   const CallSite& callsite_;
122   IAaptContext* context_;
123   SymbolTable* symbols_;
124 
125   ReferenceLinkerTransformer reference_transformer_;
126   bool error_ = false;
127 };
128 
129 }  // namespace
130 
Consume(IAaptContext * context,xml::XmlResource * resource)131 bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
132   TRACE_NAME("XmlReferenceLinker::Consume");
133   CallSite callsite{resource->file.name.package};
134 
135   std::string out_name = resource->file.name.entry;
136   NameMangler::Unmangle(&out_name, &callsite.package);
137 
138   if (callsite.package.empty()) {
139     // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
140     // for example.
141     callsite.package = context->GetCompilationPackage();
142   }
143 
144   XmlVisitor visitor(resource->file.source, &resource->string_pool, callsite, context, table_,
145                      context->GetExternalSymbols());
146   if (resource->root) {
147     resource->root->Accept(&visitor);
148     return !visitor.HasError();
149   }
150   return false;
151 }
152 
153 }  // namespace aapt
154