Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 17 | #include "FQName.h" |
| 18 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 19 | #include <hidl-util/StringHelper.h> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 21 | #include <iostream> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 22 | #include <regex> |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 23 | #include <sstream> |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 24 | |
| 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 | |
| 29 | static const std::regex kRE1("(" RE_PATH ")(" RE_VERSION ")?::(" RE_PATH ")"); |
| 30 | static const std::regex kRE2("(" RE_VERSION ")::(" RE_PATH ")"); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 31 | static const std::regex kRE3("(" RE_PATH ")(" RE_VERSION ")"); |
| 32 | static const std::regex kRE4(RE_PATH); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 36 | FQName::FQName() |
| 37 | : mValid(false) { |
| 38 | } |
| 39 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 40 | FQName::FQName(const std::string &s) |
| 41 | : mValid(false) { |
| 42 | setTo(s); |
| 43 | } |
| 44 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 45 | FQName::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 Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 55 | bool FQName::isValid() const { |
| 56 | return mValid; |
| 57 | } |
| 58 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 59 | bool FQName::isFullyQualified() const { |
| 60 | return !mPackage.empty() && !mVersion.empty() && !mName.empty(); |
| 61 | } |
| 62 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 63 | bool 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 Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 83 | 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 Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 88 | mName = match.str(0); |
| 89 | } else { |
| 90 | mValid = false; |
| 91 | } |
| 92 | |
| 93 | return isValid(); |
| 94 | } |
| 95 | |
| 96 | std::string FQName::package() const { |
| 97 | return mPackage; |
| 98 | } |
| 99 | |
| 100 | std::string FQName::version() const { |
| 101 | return mVersion; |
| 102 | } |
| 103 | |
| 104 | std::string FQName::name() const { |
| 105 | return mName; |
| 106 | } |
| 107 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 108 | std::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 Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 118 | void 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 Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 130 | std::string FQName::string() const { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 131 | CHECK(mValid); |
| 132 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 133 | std::string out; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 134 | out.append(mPackage); |
| 135 | out.append(mVersion); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 136 | if (!mName.empty()) { |
| 137 | if (!mPackage.empty() || !mVersion.empty()) { |
| 138 | out.append("::"); |
| 139 | } |
| 140 | out.append(mName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 141 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 142 | |
| 143 | return out; |
| 144 | } |
| 145 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 146 | void FQName::print() const { |
| 147 | if (!mValid) { |
| 148 | LOG(INFO) << "INVALID"; |
| 149 | return; |
| 150 | } |
| 151 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 152 | LOG(INFO) << string(); |
| 153 | } |
| 154 | |
| 155 | bool FQName::operator<(const FQName &other) const { |
| 156 | return string() < other.string(); |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 159 | bool FQName::operator==(const FQName &other) const { |
| 160 | return string() == other.string(); |
| 161 | } |
| 162 | |
Steven Moreland | 197d56c | 2016-09-09 10:03:58 -0700 | [diff] [blame] | 163 | std::string FQName::getInterfaceBaseName() const { |
| 164 | CHECK(names().size() == 1) << "Must be a top level type"; |
| 165 | CHECK(!mName.empty() && mName[0] == 'I') << mName; |
| 166 | |
| 167 | // cut off the leading 'I'. |
| 168 | return mName.substr(1); |
| 169 | } |
| 170 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 171 | const FQName FQName::getTopLevelType() const { |
| 172 | auto idx = mName.find('.'); |
| 173 | |
| 174 | if (idx == std::string::npos) { |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | return FQName(mPackage, mVersion, mName.substr(0, idx)); |
| 179 | } |
| 180 | |
| 181 | std::string FQName::tokenName() const { |
| 182 | std::vector<std::string> components; |
| 183 | getPackageAndVersionComponents(&components, true /* cpp_compatible */); |
| 184 | |
| 185 | std::vector<std::string> nameComponents; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 186 | StringHelper::SplitString(mName, '.', &nameComponents); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 187 | |
| 188 | components.insert(components.end(), nameComponents.begin(), nameComponents.end()); |
| 189 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 190 | return StringHelper::JoinStrings(components, "_"); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 193 | std::string FQName::cppNamespace() const { |
| 194 | std::vector<std::string> components; |
| 195 | getPackageAndVersionComponents(&components, true /* cpp_compatible */); |
| 196 | |
| 197 | std::string out = "::"; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 198 | out += StringHelper::JoinStrings(components, "::"); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 199 | |
| 200 | return out; |
| 201 | } |
| 202 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 203 | std::string FQName::cppLocalName() const { |
| 204 | std::vector<std::string> components; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 205 | StringHelper::SplitString(mName, '.', &components); |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 206 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 207 | return StringHelper::JoinStrings(components, "::"); |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 210 | std::string FQName::cppName() const { |
| 211 | std::string out = cppNamespace(); |
| 212 | |
| 213 | std::vector<std::string> components; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 214 | StringHelper::SplitString(name(), '.', &components); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 215 | out += "::"; |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 216 | out += StringHelper::JoinStrings(components, "::"); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 217 | |
| 218 | return out; |
| 219 | } |
| 220 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 221 | std::string FQName::javaPackage() const { |
| 222 | std::vector<std::string> components; |
| 223 | getPackageAndVersionComponents(&components, true /* cpp_compatible */); |
| 224 | |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 225 | return StringHelper::JoinStrings(components, "."); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | std::string FQName::javaName() const { |
| 229 | return javaPackage() + "." + name(); |
| 230 | } |
| 231 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 232 | void FQName::getPackageComponents(std::vector<std::string> *components) const { |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 233 | StringHelper::SplitString(package(), '.', components); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void FQName::getPackageAndVersionComponents( |
| 237 | std::vector<std::string> *components, |
| 238 | bool cpp_compatible) const { |
| 239 | getPackageComponents(components); |
| 240 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 241 | if (!cpp_compatible) { |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 242 | components->push_back(getPackageMajorVersion() + |
| 243 | "." + getPackageMinorVersion()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 247 | // Form "Vmajor_minor". |
| 248 | std::string versionString = "V"; |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 249 | versionString.append(getPackageMajorVersion()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 250 | versionString.append("_"); |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 251 | versionString.append(getPackageMinorVersion()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 252 | |
| 253 | components->push_back(versionString); |
| 254 | } |
| 255 | |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 256 | std::string FQName::getPackageMajorVersion() 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(1, dotPos - 1); |
| 262 | } |
| 263 | |
| 264 | std::string FQName::getPackageMinorVersion() const { |
| 265 | const std::string packageVersion = version(); |
| 266 | CHECK(packageVersion[0] == '@'); |
| 267 | const size_t dotPos = packageVersion.find('.'); |
| 268 | CHECK(dotPos != std::string::npos); |
| 269 | return packageVersion.substr(dotPos + 1); |
| 270 | } |
| 271 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 272 | bool FQName::endsWith(const FQName &other) const { |
| 273 | std::string s1 = string(); |
| 274 | std::string s2 = other.string(); |
| 275 | |
| 276 | size_t pos = s1.rfind(s2); |
| 277 | if (pos == std::string::npos || pos + s2.size() != s1.size()) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (pos > 0) { |
| 282 | // A match is only a match if it is preceded by a "boundary", i.e. |
| 283 | // we perform a component-wise match from the end. |
| 284 | // "az" is not a match for "android.hardware.foo@1.0::IFoo.bar.baz", |
| 285 | // "baz", "bar.baz", "IFoo.bar.baz" are. |
| 286 | |
| 287 | char separator = s1[pos - 1]; |
| 288 | if (separator != '.' && separator != ':') { |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 296 | } // namespace android |
| 297 | |