blob: d00f7f2fe0aa437831ae954894a119a4e6328ca1 [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 Lesinski467f1712015-11-16 17:35:44 -080024namespace aapt {
25namespace xml {
26
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027std::string BuildPackageNamespace(const StringPiece& package,
28 bool private_reference) {
29 std::string result =
30 private_reference ? kSchemaPrivatePrefix : kSchemaPublicPrefix;
31 result.append(package.data(), package.size());
32 return result;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070033}
34
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035Maybe<ExtractedPackage> ExtractPackageFromNamespace(
36 const std::string& namespace_uri) {
37 if (util::StartsWith(namespace_uri, kSchemaPublicPrefix)) {
38 StringPiece schema_prefix = kSchemaPublicPrefix;
39 StringPiece package = namespace_uri;
40 package = package.substr(schema_prefix.size(),
41 package.size() - schema_prefix.size());
42 if (package.empty()) {
43 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080044 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 return ExtractedPackage{package.ToString(), false /* is_private */};
Adam Lesinski467f1712015-11-16 17:35:44 -080046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 } else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) {
48 StringPiece schema_prefix = kSchemaPrivatePrefix;
49 StringPiece package = namespace_uri;
50 package = package.substr(schema_prefix.size(),
51 package.size() - schema_prefix.size());
52 if (package.empty()) {
53 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080054 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 return ExtractedPackage{package.ToString(), true /* is_private */};
56
57 } else if (namespace_uri == kSchemaAuto) {
58 return ExtractedPackage{std::string(), true /* is_private */};
59 }
60 return {};
Adam Lesinski467f1712015-11-16 17:35:44 -080061}
62
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack,
64 const StringPiece& local_package,
65 Reference* in_ref) {
66 if (in_ref->name) {
67 if (Maybe<ExtractedPackage> transformed_package =
68 decl_stack->TransformPackageAlias(in_ref->name.value().package,
69 local_package)) {
70 ExtractedPackage& extracted_package = transformed_package.value();
71 in_ref->name.value().package = std::move(extracted_package.package);
72
73 // If the reference was already private (with a * prefix) and the
74 // namespace is public,
75 // we keep the reference private.
76 in_ref->private_reference |= extracted_package.private_namespace;
77 }
78 }
79}
80
81} // namespace xml
82} // namespace aapt