blob: 2504ef54842790c0305bbfaa7757707ee349938c [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huber84f89de2016-07-28 15:39:51 -070017#include "FQName.h"
Steven Morelandaf440142016-09-07 10:09:11 -070018#include "StringHelper.h"
Andreas Huber84f89de2016-07-28 15:39:51 -070019
20#include <android-base/logging.h>
Iliyan Malchev800273d2016-09-02 15:25:07 -070021#include <iostream>
Andreas Huber84f89de2016-07-28 15:39:51 -070022#include <regex>
Iliyan Malchev800273d2016-09-02 15:25:07 -070023#include <sstream>
Andreas Huber84f89de2016-07-28 15:39:51 -070024
25#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
26#define RE_PATH RE_COMPONENT "([.]" RE_COMPONENT ")*"
27#define RE_VERSION "@[0-9]+[.][0-9]+"
28
29static const std::regex kRE1("(" RE_PATH ")(" RE_VERSION ")?::(" RE_PATH ")");
30static const std::regex kRE2("(" RE_VERSION ")::(" RE_PATH ")");
Andreas Huberda51b8e2016-07-28 16:00:57 -070031static const std::regex kRE3("(" RE_PATH ")(" RE_VERSION ")");
32static const std::regex kRE4(RE_PATH);
Andreas Huber84f89de2016-07-28 15:39:51 -070033
34namespace android {
35
Andreas Huberda51b8e2016-07-28 16:00:57 -070036FQName::FQName()
37 : mValid(false) {
38}
39
Andreas Huber84f89de2016-07-28 15:39:51 -070040FQName::FQName(const std::string &s)
41 : mValid(false) {
42 setTo(s);
43}
44
Andreas Huber68f24592016-07-29 14:53:48 -070045FQName::FQName(
46 const std::string &package,
47 const std::string &version,
48 const std::string &name)
49 : mValid(true),
50 mPackage(package),
51 mVersion(version),
52 mName(name) {
53}
54
Andreas Huber84f89de2016-07-28 15:39:51 -070055bool FQName::isValid() const {
56 return mValid;
57}
58
Andreas Huber68f24592016-07-29 14:53:48 -070059bool FQName::isFullyQualified() const {
60 return !mPackage.empty() && !mVersion.empty() && !mName.empty();
61}
62
Andreas Huber84f89de2016-07-28 15:39:51 -070063bool FQName::setTo(const std::string &s) {
64 mPackage.clear();
65 mVersion.clear();
66 mName.clear();
67
68 mValid = true;
69
70 std::smatch match;
71 if (std::regex_match(s, match, kRE1)) {
72 CHECK_EQ(match.size(), 6u);
73
74 mPackage = match.str(1);
75 mVersion = match.str(3);
76 mName = match.str(4);
77 } else if (std::regex_match(s, match, kRE2)) {
78 CHECK_EQ(match.size(), 4u);
79
80 mVersion = match.str(1);
81 mName = match.str(2);
82 } else if (std::regex_match(s, match, kRE3)) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070083 CHECK_EQ(match.size(), 4u);
84
85 mPackage = match.str(1);
86 mVersion = match.str(3);
87 } else if (std::regex_match(s, match, kRE4)) {
Andreas Huber84f89de2016-07-28 15:39:51 -070088 mName = match.str(0);
89 } else {
90 mValid = false;
91 }
92
93 return isValid();
94}
95
96std::string FQName::package() const {
97 return mPackage;
98}
99
100std::string FQName::version() const {
101 return mVersion;
102}
103
104std::string FQName::name() const {
105 return mName;
106}
107
Iliyan Malchev800273d2016-09-02 15:25:07 -0700108std::vector<std::string> FQName::names() const {
109 std::vector<std::string> res {};
110 std::istringstream ss(name());
111 std::string s;
112 while (std::getline(ss, s, '.')) {
113 res.push_back(s);
114 }
115 return res;
116}
117
Andreas Huber84f89de2016-07-28 15:39:51 -0700118void FQName::applyDefaults(
119 const std::string &defaultPackage,
120 const std::string &defaultVersion) {
121 if (mPackage.empty()) {
122 mPackage = defaultPackage;
123 }
124
125 if (mVersion.empty()) {
126 mVersion = defaultVersion;
127 }
128}
129
Andreas Huber68f24592016-07-29 14:53:48 -0700130std::string FQName::string() const {
Andreas Huber84f89de2016-07-28 15:39:51 -0700131 CHECK(mValid);
132
Andreas Huber5345ec22016-07-29 13:33:27 -0700133 std::string out;
Andreas Huber84f89de2016-07-28 15:39:51 -0700134 out.append(mPackage);
135 out.append(mVersion);
Andreas Huberda51b8e2016-07-28 16:00:57 -0700136 if (!mName.empty()) {
137 if (!mPackage.empty() || !mVersion.empty()) {
138 out.append("::");
139 }
140 out.append(mName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700141 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700142
143 return out;
144}
145
Andreas Huberda51b8e2016-07-28 16:00:57 -0700146void FQName::print() const {
147 if (!mValid) {
148 LOG(INFO) << "INVALID";
149 return;
150 }
151
Andreas Huber68f24592016-07-29 14:53:48 -0700152 LOG(INFO) << string();
153}
154
155bool FQName::operator<(const FQName &other) const {
156 return string() < other.string();
Andreas Huberda51b8e2016-07-28 16:00:57 -0700157}
158
Andreas Huberd2943e12016-08-05 11:59:31 -0700159bool FQName::operator==(const FQName &other) const {
160 return string() == other.string();
161}
162
Steven Moreland9c387612016-09-07 09:54:26 -0700163const FQName FQName::getTopLevelType() const {
164 auto idx = mName.find('.');
165
166 if (idx == std::string::npos) {
167 return *this;
168 }
169
170 return FQName(mPackage, mVersion, mName.substr(0, idx));
171}
172
173std::string FQName::tokenName() const {
174 std::vector<std::string> components;
175 getPackageAndVersionComponents(&components, true /* cpp_compatible */);
176
177 std::vector<std::string> nameComponents;
Steven Morelandaf440142016-09-07 10:09:11 -0700178 StringHelper::SplitString(mName, '.', &nameComponents);
Steven Moreland9c387612016-09-07 09:54:26 -0700179
180 components.insert(components.end(), nameComponents.begin(), nameComponents.end());
181
Steven Morelandaf440142016-09-07 10:09:11 -0700182 return StringHelper::JoinStrings(components, "_");
Steven Moreland9c387612016-09-07 09:54:26 -0700183}
184
Andreas Huber0e00de42016-08-03 09:56:02 -0700185std::string FQName::cppNamespace() const {
186 std::vector<std::string> components;
187 getPackageAndVersionComponents(&components, true /* cpp_compatible */);
188
189 std::string out = "::";
Steven Morelandaf440142016-09-07 10:09:11 -0700190 out += StringHelper::JoinStrings(components, "::");
Andreas Huber0e00de42016-08-03 09:56:02 -0700191
192 return out;
193}
194
Steven Moreland979e0992016-09-07 09:18:08 -0700195std::string FQName::cppLocalName() const {
196 std::vector<std::string> components;
Steven Morelandaf440142016-09-07 10:09:11 -0700197 StringHelper::SplitString(mName, '.', &components);
Steven Moreland979e0992016-09-07 09:18:08 -0700198
Steven Morelandaf440142016-09-07 10:09:11 -0700199 return StringHelper::JoinStrings(components, "::");
Steven Moreland979e0992016-09-07 09:18:08 -0700200}
201
Andreas Huber0e00de42016-08-03 09:56:02 -0700202std::string FQName::cppName() const {
203 std::string out = cppNamespace();
204
205 std::vector<std::string> components;
Steven Morelandaf440142016-09-07 10:09:11 -0700206 StringHelper::SplitString(name(), '.', &components);
Andreas Huber0e00de42016-08-03 09:56:02 -0700207 out += "::";
Steven Morelandaf440142016-09-07 10:09:11 -0700208 out += StringHelper::JoinStrings(components, "::");
Andreas Huber0e00de42016-08-03 09:56:02 -0700209
210 return out;
211}
212
Andreas Huber2831d512016-08-15 09:33:47 -0700213std::string FQName::javaPackage() const {
214 std::vector<std::string> components;
215 getPackageAndVersionComponents(&components, true /* cpp_compatible */);
216
Steven Morelandaf440142016-09-07 10:09:11 -0700217 return StringHelper::JoinStrings(components, ".");
Andreas Huber2831d512016-08-15 09:33:47 -0700218}
219
220std::string FQName::javaName() const {
221 return javaPackage() + "." + name();
222}
223
Andreas Huber0e00de42016-08-03 09:56:02 -0700224void FQName::getPackageComponents(std::vector<std::string> *components) const {
Steven Morelandaf440142016-09-07 10:09:11 -0700225 StringHelper::SplitString(package(), '.', components);
Andreas Huber0e00de42016-08-03 09:56:02 -0700226}
227
228void FQName::getPackageAndVersionComponents(
229 std::vector<std::string> *components,
230 bool cpp_compatible) const {
231 getPackageComponents(components);
232
Andreas Huber0e00de42016-08-03 09:56:02 -0700233 if (!cpp_compatible) {
Martijn Coenena21f1492016-09-08 15:55:14 +0200234 components->push_back(getPackageMajorVersion() +
235 "." + getPackageMinorVersion());
Andreas Huber0e00de42016-08-03 09:56:02 -0700236 return;
237 }
238
Andreas Huber0e00de42016-08-03 09:56:02 -0700239 // Form "Vmajor_minor".
240 std::string versionString = "V";
Martijn Coenena21f1492016-09-08 15:55:14 +0200241 versionString.append(getPackageMajorVersion());
Andreas Huber0e00de42016-08-03 09:56:02 -0700242 versionString.append("_");
Martijn Coenena21f1492016-09-08 15:55:14 +0200243 versionString.append(getPackageMinorVersion());
Andreas Huber0e00de42016-08-03 09:56:02 -0700244
245 components->push_back(versionString);
246}
247
Martijn Coenena21f1492016-09-08 15:55:14 +0200248std::string FQName::getPackageMajorVersion() const {
249 const std::string packageVersion = version();
250 CHECK(packageVersion[0] == '@');
251 const size_t dotPos = packageVersion.find('.');
252 CHECK(dotPos != std::string::npos);
253 return packageVersion.substr(1, dotPos - 1);
254}
255
256std::string FQName::getPackageMinorVersion() const {
257 const std::string packageVersion = version();
258 CHECK(packageVersion[0] == '@');
259 const size_t dotPos = packageVersion.find('.');
260 CHECK(dotPos != std::string::npos);
261 return packageVersion.substr(dotPos + 1);
262}
263
Andreas Huber39fa7182016-08-19 14:27:33 -0700264bool FQName::endsWith(const FQName &other) const {
265 std::string s1 = string();
266 std::string s2 = other.string();
267
268 size_t pos = s1.rfind(s2);
269 if (pos == std::string::npos || pos + s2.size() != s1.size()) {
270 return false;
271 }
272
273 if (pos > 0) {
274 // A match is only a match if it is preceded by a "boundary", i.e.
275 // we perform a component-wise match from the end.
276 // "az" is not a match for "android.hardware.foo@1.0::IFoo.bar.baz",
277 // "baz", "bar.baz", "IFoo.bar.baz" are.
278
279 char separator = s1[pos - 1];
280 if (separator != '.' && separator != ':') {
281 return false;
282 }
283 }
284
285 return true;
286}
287
Andreas Huber84f89de2016-07-28 15:39:51 -0700288} // namespace android
289