Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace aapt { |
| 24 | namespace xml { |
| 25 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 26 | std::string buildPackageNamespace(const StringPiece& package) { |
| 27 | std::string result = kSchemaPublicPrefix; |
| 28 | result.append(package.data(), package.size()); |
| 29 | return result; |
| 30 | } |
| 31 | |
| 32 | Maybe<ExtractedPackage> extractPackageFromNamespace(const std::string& namespaceUri) { |
| 33 | if (util::stringStartsWith(namespaceUri, kSchemaPublicPrefix)) { |
| 34 | StringPiece schemaPrefix = kSchemaPublicPrefix; |
| 35 | StringPiece package = namespaceUri; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 36 | 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 42 | } else if (util::stringStartsWith(namespaceUri, kSchemaPrivatePrefix)) { |
| 43 | StringPiece schemaPrefix = kSchemaPrivatePrefix; |
| 44 | StringPiece package = namespaceUri; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 45 | 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 52 | return ExtractedPackage{ std::string(), true /* isPrivate */ }; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 53 | } |
| 54 | return {}; |
| 55 | } |
| 56 | |
| 57 | void transformReferenceFromNamespace(IPackageDeclStack* declStack, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 58 | const StringPiece& localPackage, Reference* inRef) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 59 | 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 |