blob: 6d244aac9716ee20ed120f7df62aa3385742f1f8 [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 Lesinskicacb28f2016-10-19 12:18:14 -070029 /**
30 * Represents the package we are trying to build. References pointing
31 * to this package are not mangled, and mangled references inherit this
32 * package name.
33 */
34 std::string targetPackageName;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 /**
37 * We must know which references to mangle, and which to keep (android vs.
38 * com.android.support).
39 */
40 std::set<std::string> packagesToMangle;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041};
42
43class NameMangler {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 private:
45 NameManglerPolicy mPolicy;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 public:
48 explicit NameMangler(NameManglerPolicy policy) : mPolicy(policy) {}
49
50 Maybe<ResourceName> mangleName(const ResourceName& name) {
51 if (mPolicy.targetPackageName == name.package ||
52 mPolicy.packagesToMangle.count(name.package) == 0) {
53 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 }
55
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 std::string mangledEntryName = mangleEntry(name.package, name.entry);
57 return ResourceName(mPolicy.targetPackageName, name.type, mangledEntryName);
58 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 bool shouldMangle(const std::string& package) const {
61 if (package.empty() || mPolicy.targetPackageName == package) {
62 return false;
63 }
64 return mPolicy.packagesToMangle.count(package) != 0;
65 }
66
67 /**
68 * Returns a mangled name that is a combination of `name` and `package`.
69 * The mangled name should contain symbols that are illegal to define in XML,
70 * so that there will never be name mangling collisions.
71 */
72 static std::string mangleEntry(const std::string& package,
73 const std::string& name) {
74 return package + "$" + name;
75 }
76
77 /**
78 * Unmangles the name in `outName`, storing the correct name back in `outName`
79 * and the package in `outPackage`. Returns true if the name was unmangled or
80 * false if the name was never mangled to begin with.
81 */
82 static bool unmangle(std::string* outName, std::string* outPackage) {
83 size_t pivot = outName->find('$');
84 if (pivot == std::string::npos) {
85 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070086 }
87
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 outPackage->assign(outName->data(), pivot);
89 outName->assign(outName->data() + pivot + 1, outName->size() - (pivot + 1));
90 return true;
91 }
Adam Lesinski769de982015-04-10 19:43:55 -070092};
93
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094} // namespace aapt
Adam Lesinski769de982015-04-10 19:43:55 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096#endif // AAPT_NAME_MANGLER_H