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 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 17 | #include "xml/XmlUtil.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 21 | #include "util/Maybe.h" |
| 22 | #include "util/Util.h" |
| 23 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 24 | namespace aapt { |
| 25 | namespace xml { |
| 26 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 27 | std::string BuildPackageNamespace(const StringPiece& package, |
| 28 | bool private_reference) { |
| 29 | std::string result = |
| 30 | private_reference ? kSchemaPrivatePrefix : kSchemaPublicPrefix; |
| 31 | result.append(package.data(), package.size()); |
| 32 | return result; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 35 | Maybe<ExtractedPackage> ExtractPackageFromNamespace( |
| 36 | const std::string& namespace_uri) { |
| 37 | if (util::StartsWith(namespace_uri, kSchemaPublicPrefix)) { |
| 38 | StringPiece schema_prefix = kSchemaPublicPrefix; |
| 39 | StringPiece package = namespace_uri; |
| 40 | package = package.substr(schema_prefix.size(), |
| 41 | package.size() - schema_prefix.size()); |
| 42 | if (package.empty()) { |
| 43 | return {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 44 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 45 | return ExtractedPackage{package.ToString(), false /* is_private */}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 47 | } else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) { |
| 48 | StringPiece schema_prefix = kSchemaPrivatePrefix; |
| 49 | StringPiece package = namespace_uri; |
| 50 | package = package.substr(schema_prefix.size(), |
| 51 | package.size() - schema_prefix.size()); |
| 52 | if (package.empty()) { |
| 53 | return {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 54 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 55 | return ExtractedPackage{package.ToString(), true /* is_private */}; |
| 56 | |
| 57 | } else if (namespace_uri == kSchemaAuto) { |
| 58 | return ExtractedPackage{std::string(), true /* is_private */}; |
| 59 | } |
| 60 | return {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 63 | void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack, |
| 64 | const StringPiece& local_package, |
| 65 | Reference* in_ref) { |
| 66 | if (in_ref->name) { |
| 67 | if (Maybe<ExtractedPackage> transformed_package = |
| 68 | decl_stack->TransformPackageAlias(in_ref->name.value().package, |
| 69 | local_package)) { |
| 70 | ExtractedPackage& extracted_package = transformed_package.value(); |
| 71 | in_ref->name.value().package = std::move(extracted_package.package); |
| 72 | |
| 73 | // If the reference was already private (with a * prefix) and the |
| 74 | // namespace is public, |
| 75 | // we keep the reference private. |
| 76 | in_ref->private_reference |= extracted_package.private_namespace; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } // namespace xml |
| 82 | } // namespace aapt |