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 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "util/Maybe.h" |
| 23 | #include "util/Util.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 24 | #include "xml/XmlDom.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 26 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 28 | namespace aapt { |
| 29 | namespace xml { |
| 30 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 31 | std::string BuildPackageNamespace(const StringPiece& package, bool private_reference) { |
| 32 | std::string result = private_reference ? kSchemaPrivatePrefix : kSchemaPublicPrefix; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | result.append(package.data(), package.size()); |
| 34 | return result; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | Maybe<ExtractedPackage> ExtractPackageFromNamespace( |
| 38 | const std::string& namespace_uri) { |
| 39 | if (util::StartsWith(namespace_uri, kSchemaPublicPrefix)) { |
| 40 | StringPiece schema_prefix = kSchemaPublicPrefix; |
| 41 | StringPiece package = namespace_uri; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 42 | package = package.substr(schema_prefix.size(), package.size() - schema_prefix.size()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | if (package.empty()) { |
| 44 | return {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 45 | } |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 46 | return ExtractedPackage{package.to_string(), false /* is_private */}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | } else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) { |
| 49 | StringPiece schema_prefix = kSchemaPrivatePrefix; |
| 50 | StringPiece package = namespace_uri; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 51 | package = package.substr(schema_prefix.size(), package.size() - schema_prefix.size()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | if (package.empty()) { |
| 53 | return {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 54 | } |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 55 | return ExtractedPackage{package.to_string(), true /* is_private */}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 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 | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 63 | void ResolvePackage(const IPackageDeclStack* decl_stack, Reference* in_ref) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | if (in_ref->name) { |
| 65 | if (Maybe<ExtractedPackage> transformed_package = |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 66 | decl_stack->TransformPackageAlias(in_ref->name.value().package)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | ExtractedPackage& extracted_package = transformed_package.value(); |
| 68 | in_ref->name.value().package = std::move(extracted_package.package); |
| 69 | |
| 70 | // If the reference was already private (with a * prefix) and the |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 71 | // namespace is public, we keep the reference private. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | in_ref->private_reference |= extracted_package.private_namespace; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 77 | namespace { |
| 78 | |
| 79 | class ToolsNamespaceRemover : public Visitor { |
| 80 | public: |
| 81 | using Visitor::Visit; |
| 82 | |
| 83 | void Visit(Element* el) override { |
| 84 | auto new_end = |
| 85 | std::remove_if(el->namespace_decls.begin(), el->namespace_decls.end(), |
| 86 | [](const NamespaceDecl& decl) -> bool { return decl.uri == kSchemaTools; }); |
| 87 | el->namespace_decls.erase(new_end, el->namespace_decls.end()); |
| 88 | |
| 89 | auto new_attr_end = std::remove_if( |
| 90 | el->attributes.begin(), el->attributes.end(), |
| 91 | [](const Attribute& attr) -> bool { return attr.namespace_uri == kSchemaTools; }); |
| 92 | el->attributes.erase(new_attr_end, el->attributes.end()); |
| 93 | |
| 94 | Visitor::Visit(el); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 100 | void StripAndroidStudioAttributes(Element* el) { |
| 101 | ToolsNamespaceRemover remover; |
| 102 | el->Accept(&remover); |
| 103 | } |
| 104 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | } // namespace xml |
| 106 | } // namespace aapt |