| 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 "trace/TraceBuffer.h" |
| 29 | #include "util/Util.h" |
| 30 | #include "xml/XmlDom.h" |
| 31 | |
| 32 | namespace aapt { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | // Visits each xml Element and compiles the attributes within. |
| 37 | class XmlVisitor : public xml::PackageAwareVisitor { |
| 38 | public: |
| 39 | using xml::PackageAwareVisitor::Visit; |
| 40 | |
| 41 | XmlVisitor(const Source& source, StringPool* pool, const CallSite& callsite, |
| 42 | IAaptContext* context, ResourceTable* table, SymbolTable* symbols) |
| 43 | : source_(source), |
| 44 | callsite_(callsite), |
| 45 | context_(context), |
| 46 | symbols_(symbols), |
| 47 | reference_transformer_(callsite, context, symbols, pool, table, this) { |
| 48 | } |
| 49 | |
| 50 | void Visit(xml::Element* el) override { |
| 51 | // The default Attribute allows everything except enums or flags. |
| 52 | Attribute default_attribute(android::ResTable_map::TYPE_ANY); |
| 53 | default_attribute.SetWeak(true); |
| 54 | |
| 55 | // 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 | |
| 64 | const Source source = source_.WithLine(el->line_number); |
| 65 | for (xml::Attribute& attr : el->attributes) { |
| 66 | // If the attribute has no namespace, interpret values as if |
| 67 | // they were assigned to the default Attribute. |
| 68 | |
| 69 | const Attribute* attribute = &default_attribute; |
| 70 | |
| 71 | if (std::optional<xml::ExtractedPackage> maybe_package = |
| 72 | xml::ExtractPackageFromNamespace(attr.namespace_uri)) { |
| 73 | // There is a valid package name for this attribute. We will look this up. |
| 74 | Reference attr_ref( |
| 75 | ResourceNameRef(maybe_package.value().package, ResourceType::kAttr, attr.name)); |
| 76 | attr_ref.private_reference = maybe_package.value().private_namespace; |
| 77 | |
| 78 | std::string err_str; |
| 79 | attr.compiled_attribute = |
| 80 | ReferenceLinker::CompileXmlAttribute(attr_ref, callsite_, context_, symbols_, &err_str); |
| 81 | |
| 82 | if (!attr.compiled_attribute) { |
| 83 | 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); |
| 88 | error_ = true; |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | attribute = &attr.compiled_attribute.value().attribute; |
| 93 | } |
| 94 | |
| 95 | attr.compiled_value = ResourceUtils::TryParseItemForAttribute(attr.value, attribute); |
| 96 | if (attr.compiled_value) { |
| 97 | // With a compiledValue, we must resolve the reference and assign it an ID. |
| 98 | attr.compiled_value->SetSource(source); |
| 99 | attr.compiled_value = attr.compiled_value->Transform(reference_transformer_); |
| 100 | } 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); |
| 103 | msg << "'" << attr.value << "' is incompatible with attribute " << attr.name << " " |
| 104 | << *attribute; |
| 105 | context_->GetDiagnostics()->Error(msg); |
| 106 | error_ = true; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Call the super implementation. |
| 111 | xml::PackageAwareVisitor::Visit(el); |
| 112 | } |
| 113 | |
| 114 | bool HasError() { |
| 115 | return error_ || reference_transformer_.HasError(); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | DISALLOW_COPY_AND_ASSIGN(XmlVisitor); |
| 120 | |
| 121 | Source source_; |
| 122 | const CallSite& callsite_; |
| 123 | IAaptContext* context_; |
| 124 | SymbolTable* symbols_; |
| 125 | |
| 126 | ReferenceLinkerTransformer reference_transformer_; |
| 127 | bool error_ = false; |
| 128 | }; |
| 129 | |
| 130 | } // namespace |
| 131 | |
| 132 | bool XmlReferenceLinker::Consume(IAaptContext* context, xml::XmlResource* resource) { |
| 133 | TRACE_NAME("XmlReferenceLinker::Consume"); |
| 134 | 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 | |
| 145 | XmlVisitor visitor(resource->file.source, &resource->string_pool, callsite, context, table_, |
| 146 | context->GetExternalSymbols()); |
| 147 | if (resource->root) { |
| 148 | resource->root->Accept(&visitor); |
| 149 | return !visitor.HasError(); |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | } // namespace aapt |