blob: 709755e69292677bd8dd326bda3508248dd901a0 [file] [log] [blame]
Adam Lesinski467f1712015-11-16 17:35:44 -08001/*
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 Lesinski467f1712015-11-16 17:35:44 -080017#include "xml/XmlUtil.h"
18
Adam Lesinski00451162017-10-03 07:44:08 -070019#include <algorithm>
Adam Lesinski467f1712015-11-16 17:35:44 -080020#include <string>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "util/Util.h"
Adam Lesinski00451162017-10-03 07:44:08 -070023#include "xml/XmlDom.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024
Adam Lesinski00451162017-10-03 07:44:08 -070025using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080026
Adam Lesinski467f1712015-11-16 17:35:44 -080027namespace aapt {
28namespace xml {
29
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070030std::string BuildPackageNamespace(StringPiece package, bool private_reference) {
Adam Lesinski00451162017-10-03 07:44:08 -070031 std::string result = private_reference ? kSchemaPrivatePrefix : kSchemaPublicPrefix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 result.append(package.data(), package.size());
33 return result;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070034}
35
Ryan Mitchell4382e442021-07-14 12:53:01 -070036std::optional<ExtractedPackage> ExtractPackageFromNamespace(const std::string& namespace_uri) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 if (util::StartsWith(namespace_uri, kSchemaPublicPrefix)) {
38 StringPiece schema_prefix = kSchemaPublicPrefix;
39 StringPiece package = namespace_uri;
Adam Lesinski00451162017-10-03 07:44:08 -070040 package = package.substr(schema_prefix.size(), package.size() - schema_prefix.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 if (package.empty()) {
42 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080043 }
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070044 return ExtractedPackage{std::string(package), false /* is_private */};
Adam Lesinski467f1712015-11-16 17:35:44 -080045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 } else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) {
47 StringPiece schema_prefix = kSchemaPrivatePrefix;
48 StringPiece package = namespace_uri;
Adam Lesinski00451162017-10-03 07:44:08 -070049 package = package.substr(schema_prefix.size(), package.size() - schema_prefix.size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 if (package.empty()) {
51 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080052 }
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070053 return ExtractedPackage{std::string(package), true /* is_private */};
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054
55 } else if (namespace_uri == kSchemaAuto) {
56 return ExtractedPackage{std::string(), true /* is_private */};
57 }
58 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080059}
60
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070061void ResolvePackage(const IPackageDeclStack* decl_stack, Reference* in_ref) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 if (in_ref->name) {
Ryan Mitchell4382e442021-07-14 12:53:01 -070063 if (std::optional<ExtractedPackage> transformed_package =
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070064 decl_stack->TransformPackageAlias(in_ref->name.value().package)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 ExtractedPackage& extracted_package = transformed_package.value();
66 in_ref->name.value().package = std::move(extracted_package.package);
67
68 // If the reference was already private (with a * prefix) and the
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070069 // namespace is public, we keep the reference private.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 in_ref->private_reference |= extracted_package.private_namespace;
71 }
72 }
73}
74
Adam Lesinski00451162017-10-03 07:44:08 -070075namespace {
76
77class ToolsNamespaceRemover : public Visitor {
78 public:
79 using Visitor::Visit;
80
81 void Visit(Element* el) override {
82 auto new_end =
83 std::remove_if(el->namespace_decls.begin(), el->namespace_decls.end(),
84 [](const NamespaceDecl& decl) -> bool { return decl.uri == kSchemaTools; });
85 el->namespace_decls.erase(new_end, el->namespace_decls.end());
86
87 auto new_attr_end = std::remove_if(
88 el->attributes.begin(), el->attributes.end(),
89 [](const Attribute& attr) -> bool { return attr.namespace_uri == kSchemaTools; });
90 el->attributes.erase(new_attr_end, el->attributes.end());
91
92 Visitor::Visit(el);
93 }
94};
95
96} // namespace
97
98void StripAndroidStudioAttributes(Element* el) {
99 ToolsNamespaceRemover remover;
100 el->Accept(&remover);
101}
102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103} // namespace xml
104} // namespace aapt