blob: 505a982e5eabf770ce1a7f6ec0c2b59ce46da496 [file] [log] [blame]
Adam Lesinski769de982015-04-10 19:43:55 -07001/*
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#ifndef AAPT_NAME_MANGLER_H
18#define AAPT_NAME_MANGLER_H
19
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "util/Maybe.h"
22
23#include <set>
Adam Lesinski769de982015-04-10 19:43:55 -070024#include <string>
25
26namespace aapt {
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028struct NameManglerPolicy {
Adam Lesinski769de982015-04-10 19:43:55 -070029 /**
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030 * Represents the package we are trying to build. References pointing
31 * to this package are not mangled, and mangled references inherit this package name.
Adam Lesinski769de982015-04-10 19:43:55 -070032 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070033 std::string targetPackageName;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
35 /**
36 * We must know which references to mangle, and which to keep (android vs. com.android.support).
37 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070038 std::set<std::string> packagesToMangle;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039};
40
41class NameMangler {
42private:
43 NameManglerPolicy mPolicy;
44
45public:
46 NameMangler(NameManglerPolicy policy) : mPolicy(policy) {
47 }
48
49 Maybe<ResourceName> mangleName(const ResourceName& name) {
50 if (mPolicy.targetPackageName == name.package ||
51 mPolicy.packagesToMangle.count(name.package) == 0) {
52 return {};
53 }
54
Adam Lesinskid0f116b2016-07-08 15:00:32 -070055 std::string mangledEntryName = mangleEntry(name.package, name.entry);
56 return ResourceName(mPolicy.targetPackageName, name.type, mangledEntryName);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057 }
58
Adam Lesinskid0f116b2016-07-08 15:00:32 -070059 bool shouldMangle(const std::string& package) const {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 if (package.empty() || mPolicy.targetPackageName == package) {
61 return false;
62 }
63 return mPolicy.packagesToMangle.count(package) != 0;
64 }
65
66 /**
67 * Returns a mangled name that is a combination of `name` and `package`.
68 * The mangled name should contain symbols that are illegal to define in XML,
69 * so that there will never be name mangling collisions.
70 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070071 static std::string mangleEntry(const std::string& package, const std::string& name) {
72 return package + "$" + name;
Adam Lesinski769de982015-04-10 19:43:55 -070073 }
74
75 /**
76 * Unmangles the name in `outName`, storing the correct name back in `outName`
77 * and the package in `outPackage`. Returns true if the name was unmangled or
78 * false if the name was never mangled to begin with.
79 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070080 static bool unmangle(std::string* outName, std::string* outPackage) {
81 size_t pivot = outName->find('$');
Adam Lesinski769de982015-04-10 19:43:55 -070082 if (pivot == std::string::npos) {
83 return false;
84 }
85
86 outPackage->assign(outName->data(), pivot);
87 outName->assign(outName->data() + pivot + 1, outName->size() - (pivot + 1));
88 return true;
89 }
90};
91
92} // namespace aapt
93
94#endif // AAPT_NAME_MANGLER_H