blob: 0e9d005dd57cb5af9ec1d507250450d23527f36d [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
17#include "util/Maybe.h"
18#include "util/Util.h"
19#include "xml/XmlUtil.h"
20
21#include <string>
22
23namespace aapt {
24namespace xml {
25
Adam Lesinskid0f116b2016-07-08 15:00:32 -070026std::string buildPackageNamespace(const StringPiece& package) {
27 std::string result = kSchemaPublicPrefix;
28 result.append(package.data(), package.size());
29 return result;
30}
31
32Maybe<ExtractedPackage> extractPackageFromNamespace(const std::string& namespaceUri) {
33 if (util::stringStartsWith(namespaceUri, kSchemaPublicPrefix)) {
34 StringPiece schemaPrefix = kSchemaPublicPrefix;
35 StringPiece package = namespaceUri;
Adam Lesinski467f1712015-11-16 17:35:44 -080036 package = package.substr(schemaPrefix.size(), package.size() - schemaPrefix.size());
37 if (package.empty()) {
38 return {};
39 }
40 return ExtractedPackage{ package.toString(), false /* isPrivate */ };
41
Adam Lesinskid0f116b2016-07-08 15:00:32 -070042 } else if (util::stringStartsWith(namespaceUri, kSchemaPrivatePrefix)) {
43 StringPiece schemaPrefix = kSchemaPrivatePrefix;
44 StringPiece package = namespaceUri;
Adam Lesinski467f1712015-11-16 17:35:44 -080045 package = package.substr(schemaPrefix.size(), package.size() - schemaPrefix.size());
46 if (package.empty()) {
47 return {};
48 }
49 return ExtractedPackage{ package.toString(), true /* isPrivate */ };
50
51 } else if (namespaceUri == kSchemaAuto) {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070052 return ExtractedPackage{ std::string(), true /* isPrivate */ };
Adam Lesinski467f1712015-11-16 17:35:44 -080053 }
54 return {};
55}
56
57void transformReferenceFromNamespace(IPackageDeclStack* declStack,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070058 const StringPiece& localPackage, Reference* inRef) {
Adam Lesinski467f1712015-11-16 17:35:44 -080059 if (inRef->name) {
60 if (Maybe<ExtractedPackage> transformedPackage =
61 declStack->transformPackageAlias(inRef->name.value().package, localPackage)) {
62 ExtractedPackage& extractedPackage = transformedPackage.value();
63 inRef->name.value().package = std::move(extractedPackage.package);
64
65 // If the reference was already private (with a * prefix) and the namespace is public,
66 // we keep the reference private.
67 inRef->privateReference |= extractedPackage.privateNamespace;
68 }
69 }
70}
71
72} // namespace xml
73} // namespace aapt