blob: fa1b0f04967840cdc305624e710924369c4a54f5 [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
19#include <string>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "util/Maybe.h"
22#include "util/Util.h"
23
Adam Lesinskid5083f62017-01-16 15:07:21 -080024using android::StringPiece;
25
Adam Lesinski467f1712015-11-16 17:35:44 -080026namespace aapt {
27namespace xml {
28
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029std::string BuildPackageNamespace(const StringPiece& package,
30 bool private_reference) {
31 std::string result =
32 private_reference ? kSchemaPrivatePrefix : kSchemaPublicPrefix;
33 result.append(package.data(), package.size());
34 return result;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070035}
36
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037Maybe<ExtractedPackage> ExtractPackageFromNamespace(
38 const std::string& namespace_uri) {
39 if (util::StartsWith(namespace_uri, kSchemaPublicPrefix)) {
40 StringPiece schema_prefix = kSchemaPublicPrefix;
41 StringPiece package = namespace_uri;
42 package = package.substr(schema_prefix.size(),
43 package.size() - schema_prefix.size());
44 if (package.empty()) {
45 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080046 }
Adam Lesinskid5083f62017-01-16 15:07:21 -080047 return ExtractedPackage{package.to_string(), false /* is_private */};
Adam Lesinski467f1712015-11-16 17:35:44 -080048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 } else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) {
50 StringPiece schema_prefix = kSchemaPrivatePrefix;
51 StringPiece package = namespace_uri;
52 package = package.substr(schema_prefix.size(),
53 package.size() - schema_prefix.size());
54 if (package.empty()) {
55 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080056 }
Adam Lesinskid5083f62017-01-16 15:07:21 -080057 return ExtractedPackage{package.to_string(), true /* is_private */};
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058
59 } else if (namespace_uri == kSchemaAuto) {
60 return ExtractedPackage{std::string(), true /* is_private */};
61 }
62 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack,
66 const StringPiece& local_package,
67 Reference* in_ref) {
68 if (in_ref->name) {
69 if (Maybe<ExtractedPackage> transformed_package =
70 decl_stack->TransformPackageAlias(in_ref->name.value().package,
71 local_package)) {
72 ExtractedPackage& extracted_package = transformed_package.value();
73 in_ref->name.value().package = std::move(extracted_package.package);
74
75 // If the reference was already private (with a * prefix) and the
76 // namespace is public,
77 // we keep the reference private.
78 in_ref->private_reference |= extracted_package.private_namespace;
79 }
80 }
81}
82
Adam Lesinski5b7337f2017-06-26 14:57:22 -070083namespace {
84
85struct TagCompat {
86 ApiVersion api_version;
87
88 enum class XmlType {
89 kVector,
90 kTransition,
91 kAdaptiveIcon,
92 };
93
94 XmlType type;
95};
96
97std::unordered_map<StringPiece, TagCompat> sTagVersions = {
98 {"fade", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
99 {"changeBounds", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
100 {"slide", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
101 {"explode", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
102 {"changeImageTransform", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
103 {"changeTransform", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
104 {"changeClipBounds", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
105 {"autoTransition", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
106 {"recolor", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
107 {"changeScroll", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
108 {"transitionSet", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
109 {"transition", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
110 {"transitionManager", {SDK_LOLLIPOP, TagCompat::XmlType::kTransition}},
111
112 {"vector", {SDK_LOLLIPOP, TagCompat::XmlType::kVector}},
113 {"animated-vector", {SDK_LOLLIPOP, TagCompat::XmlType::kVector}},
114 {"pathInterpolator", {SDK_LOLLIPOP, TagCompat::XmlType::kVector}},
115 {"objectAnimator", {SDK_LOLLIPOP, TagCompat::XmlType::kVector}},
116
117 {"adaptive-icon", {SDK_O, TagCompat::XmlType::kAdaptiveIcon}},
118};
119
120} // namespace
121
122Maybe<TagApiVersionResult> GetXmlTagApiVersion(const StringPiece& tag_name, int options) {
123 auto iter = sTagVersions.find(tag_name);
124 if (iter == sTagVersions.end()) {
125 return {};
126 }
127
128 const TagCompat& tag_compat = iter->second;
129 if (options & kNoVersionVector) {
130 if (tag_compat.type == TagCompat::XmlType::kVector) {
131 return TagApiVersionResult{{}, true /*skip_version*/};
132 }
133 }
134
135 if (options & kNoVersionTransition) {
136 if (tag_compat.type == TagCompat::XmlType::kTransition) {
137 return TagApiVersionResult{{}, true /*skip_version*/};
138 }
139 }
140 return TagApiVersionResult{tag_compat.api_version, false /*skip_version*/};
141}
142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143} // namespace xml
144} // namespace aapt