blob: 1f8548b5de751bcf8a0612256c62fa10e6ed1b20 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "link/Linkers.h"
18
Adam Lesinski38665542016-12-28 12:25:46 -050019#include "androidfw/ResourceTypes.h"
20
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "Diagnostics.h"
22#include "ResourceUtils.h"
23#include "SdkConstants.h"
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070024#include "ValueVisitor.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080025#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "process/IResourceTableConsumer.h"
27#include "process/SymbolTable.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080028#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080030#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031
32namespace aapt {
33
34namespace {
35
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070036// Visits each xml Element and compiles the attributes within.
Adam Lesinski467f1712015-11-16 17:35:44 -080037class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 using xml::PackageAwareVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070041 XmlVisitor(const Source& source, StringPool* pool, const CallSite& callsite,
42 IAaptContext* context, ResourceTable* table, SymbolTable* symbols)
Adam Lesinskif34b6f42017-03-03 16:33:26 -080043 : source_(source),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 callsite_(callsite),
Adam Lesinskif34b6f42017-03-03 16:33:26 -080045 context_(context),
46 symbols_(symbols),
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070047 reference_transformer_(callsite, context, symbols, pool, table, this) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070048 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 void Visit(xml::Element* el) override {
Adam Lesinski38665542016-12-28 12:25:46 -050051 // The default Attribute allows everything except enums or flags.
Adam Lesinski73bff1e2017-12-08 16:06:10 -080052 Attribute default_attribute(android::ResTable_map::TYPE_ANY);
53 default_attribute.SetWeak(true);
Adam Lesinski38665542016-12-28 12:25:46 -050054
Ryan Mitchell4d5833e2019-09-04 02:46:03 -070055 // The default orientation of gradients in android Q is different than previous android
56 // versions. Set the android:angle attribute to "0" to ensure that the default gradient
57 // orientation will remain left-to-right in android Q.
58 if (el->name == "gradient" && context_->GetMinSdkVersion() <= SDK_Q) {
59 if (!el->FindAttribute(xml::kSchemaAndroid, "angle")) {
60 el->attributes.push_back(xml::Attribute{xml::kSchemaAndroid, "angle", "0"});
61 }
62 }
63
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 const Source source = source_.WithLine(el->line_number);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski38665542016-12-28 12:25:46 -050066 // If the attribute has no namespace, interpret values as if
67 // they were assigned to the default Attribute.
68
Adam Lesinski73bff1e2017-12-08 16:06:10 -080069 const Attribute* attribute = &default_attribute;
Adam Lesinski38665542016-12-28 12:25:46 -050070
Ryan Mitchell4382e442021-07-14 12:53:01 -070071 if (std::optional<xml::ExtractedPackage> maybe_package =
Adam Lesinski38665542016-12-28 12:25:46 -050072 xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
73 // There is a valid package name for this attribute. We will look this up.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070074 Reference attr_ref(
75 ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 attr_ref.private_reference = maybe_package.value().private_namespace;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::string err_str;
Chris Warrington58e2fbf2018-07-23 14:12:20 +000079 attr.compiled_attribute =
Udam Sainib228df32019-06-18 16:50:34 -070080 ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, context_, symbols_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081
Adam Lesinski38665542016-12-28 12:25:46 -050082 if (!attr.compiled_attribute) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070083 DiagMessage error_msg(source);
84 error_msg << "attribute ";
85 ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
86 error_msg << " " << err_str;
87 context_->GetDiagnostics()->Error(error_msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 error_ = true;
Adam Lesinski38665542016-12-28 12:25:46 -050089 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 }
Adam Lesinski38665542016-12-28 12:25:46 -050091
Adam Lesinskic744ae82017-05-17 19:28:38 -070092 attribute = &attr.compiled_attribute.value().attribute;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
94
Adam Lesinski38665542016-12-28 12:25:46 -050095 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 if (attr.compiled_value) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070097 // With a compiledValue, we must resolve the reference and assign it an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 attr.compiled_value->SetSource(source);
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070099 attr.compiled_value = attr.compiled_value->Transform(reference_transformer_);
Adam Lesinski38665542016-12-28 12:25:46 -0500100 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
101 // We won't be able to encode this as a string.
102 DiagMessage msg(source);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700103 msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
104 << *attribute;
Adam Lesinski38665542016-12-28 12:25:46 -0500105 context_->GetDiagnostics()->Error(msg);
106 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108 }
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 // Call the super implementation.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800113
Adam Lesinski6b372992017-08-09 10:54:23 -0700114 bool HasError() {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700115 return error_ || reference_transformer_.HasError();
Adam Lesinski6b372992017-08-09 10:54:23 -0700116 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117
118 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
120
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800121 Source source_;
122 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 IAaptContext* context_;
124 SymbolTable* symbols_;
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800125
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700126 ReferenceLinkerTransformer reference_transformer_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 bool error_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128};
129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
Adam Lesinski38665542016-12-28 12:25:46 -0500132bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800133 TRACE_NAME("XmlReferenceLinker::Consume");
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700134 CallSite callsite{resource->file.name.package};
135
136 std::string out_name = resource->file.name.entry;
137 NameMangler::Unmangle(&out_name, &callsite.package);
138
139 if (callsite.package.empty()) {
140 // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
141 // for example.
142 callsite.package = context->GetCompilationPackage();
143 }
144
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700145 XmlVisitor visitor(resource->file.source, &resource->string_pool, callsite, context, table_,
146 context->GetExternalSymbols());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 if (resource->root) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 resource->root->Accept(&visitor);
149 return !visitor.HasError();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152}
153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154} // namespace aapt