blob: d2e9bd770a3113c94ebc54ce840b61ed6ec05c09 [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 Lesinski1ab598f2015-08-14 14:26:04 -070017#include "ResourceUtils.h"
18#include "SdkConstants.h"
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070019#include "ValueVisitor.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000020#include "androidfw/IDiagnostics.h"
21#include "androidfw/ResourceTypes.h"
22#include "link/Linkers.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080023#include "link/ReferenceLinker.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include "process/IResourceTableConsumer.h"
25#include "process/SymbolTable.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080026#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080028#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
30namespace aapt {
31
32namespace {
33
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070034// Visits each xml Element and compiles the attributes within.
Adam Lesinski467f1712015-11-16 17:35:44 -080035class XmlVisitor : public xml::PackageAwareVisitor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 using xml::PackageAwareVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038
Jeremy Meyer56f36e82022-05-20 20:35:42 +000039 XmlVisitor(const android::Source& source, android::StringPool* pool, const CallSite& callsite,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070040 IAaptContext* context, ResourceTable* table, SymbolTable* symbols)
Adam Lesinskif34b6f42017-03-03 16:33:26 -080041 : source_(source),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 callsite_(callsite),
Adam Lesinskif34b6f42017-03-03 16:33:26 -080043 context_(context),
44 symbols_(symbols),
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070045 reference_transformer_(callsite, context, symbols, pool, table, this) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070046 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 void Visit(xml::Element* el) override {
Adam Lesinski38665542016-12-28 12:25:46 -050049 // The default Attribute allows everything except enums or flags.
Adam Lesinski73bff1e2017-12-08 16:06:10 -080050 Attribute default_attribute(android::ResTable_map::TYPE_ANY);
51 default_attribute.SetWeak(true);
Adam Lesinski38665542016-12-28 12:25:46 -050052
Ryan Mitchell4d5833e2019-09-04 02:46:03 -070053 // 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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000062 const android::Source source = source_.WithLine(el->line_number);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 for (xml::Attribute& attr : el->attributes) {
Adam Lesinski38665542016-12-28 12:25:46 -050064 // If the attribute has no namespace, interpret values as if
65 // they were assigned to the default Attribute.
66
Adam Lesinski73bff1e2017-12-08 16:06:10 -080067 const Attribute* attribute = &default_attribute;
Adam Lesinski38665542016-12-28 12:25:46 -050068
Ryan Mitchell4382e442021-07-14 12:53:01 -070069 if (std::optional<xml::ExtractedPackage> maybe_package =
Adam Lesinski38665542016-12-28 12:25:46 -050070 xml::ExtractPackageFromNamespace(attr.namespace_uri)) {
71 // There is a valid package name for this attribute. We will look this up.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070072 Reference attr_ref(
73 ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name));
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 attr_ref.private_reference = maybe_package.value().private_namespace;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 std::string err_str;
Chris Warrington58e2fbf2018-07-23 14:12:20 +000077 attr.compiled_attribute =
Udam Sainib228df32019-06-18 16:50:34 -070078 ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, context_, symbols_, &err_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079
Adam Lesinski38665542016-12-28 12:25:46 -050080 if (!attr.compiled_attribute) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000081 android::DiagMessage error_msg(source);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070082 error_msg << "attribute ";
83 ReferenceLinker::WriteAttributeName(attr_ref, callsite_, this, &error_msg);
84 error_msg << " " << err_str;
85 context_->GetDiagnostics()->Error(error_msg);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 error_ = true;
Adam Lesinski38665542016-12-28 12:25:46 -050087 continue;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 }
Adam Lesinski38665542016-12-28 12:25:46 -050089
Adam Lesinskic744ae82017-05-17 19:28:38 -070090 attribute = &attr.compiled_attribute.value().attribute;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 }
92
Adam Lesinski38665542016-12-28 12:25:46 -050093 attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 if (attr.compiled_value) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070095 // With a compiledValue, we must resolve the reference and assign it an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 attr.compiled_value->SetSource(source);
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070097 attr.compiled_value = attr.compiled_value->Transform(reference_transformer_);
Adam Lesinski38665542016-12-28 12:25:46 -050098 } else if ((attribute->type_mask & android::ResTable_map::TYPE_STRING) == 0) {
99 // We won't be able to encode this as a string.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000100 android::DiagMessage msg(source);
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700101 msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " "
102 << *attribute;
Adam Lesinski38665542016-12-28 12:25:46 -0500103 context_->GetDiagnostics()->Error(msg);
104 error_ = true;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 }
107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 // Call the super implementation.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 xml::PackageAwareVisitor::Visit(el);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800111
Adam Lesinski6b372992017-08-09 10:54:23 -0700112 bool HasError() {
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700113 return error_ || reference_transformer_.HasError();
Adam Lesinski6b372992017-08-09 10:54:23 -0700114 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115
116 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 DISALLOW_COPY_AND_ASSIGN(XmlVisitor);
118
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000119 android::Source source_;
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800120 const CallSite& callsite_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 IAaptContext* context_;
122 SymbolTable* symbols_;
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800123
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700124 ReferenceLinkerTransformer reference_transformer_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 bool error_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126};
127
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinski38665542016-12-28 12:25:46 -0500130bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800131 TRACE_NAME("XmlReferenceLinker::Consume");
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700132 CallSite callsite{resource->file.name.package};
133
134 std::string out_name = resource->file.name.entry;
135 NameMangler::Unmangle(&out_name, &callsite.package);
136
137 if (callsite.package.empty()) {
138 // Assume an empty package means that the XML file is local. This is true of AndroidManifest.xml
139 // for example.
140 callsite.package = context->GetCompilationPackage();
141 }
142
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700143 XmlVisitor visitor(resource->file.source, &resource->string_pool, callsite, context, table_,
144 context->GetExternalSymbols());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 if (resource->root) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 resource->root->Accept(&visitor);
147 return !visitor.HasError();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 }
149 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150}
151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152} // namespace aapt