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 | #ifndef AAPT_XML_XMLUTIL_H |
| 18 | #define AAPT_XML_XMLUTIL_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 22 | #include "ResourceValues.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 23 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 24 | namespace aapt { |
| 25 | namespace xml { |
| 26 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 27 | constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto"; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 28 | constexpr const char* kSchemaPublicPrefix = "http://schemas.android.com/apk/res/"; |
| 29 | constexpr const char* kSchemaPrivatePrefix = "http://schemas.android.com/apk/prv/res/"; |
| 30 | constexpr const char* kSchemaAndroid = "http://schemas.android.com/apk/res/android"; |
Alexandria Cornwall | a9ff140 | 2016-08-03 09:44:10 -0700 | [diff] [blame] | 31 | constexpr const char* kSchemaTools = "http://schemas.android.com/tools"; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 32 | constexpr const char* kSchemaAapt = "http://schemas.android.com/aapt"; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 34 | // Result of extracting a package name from a namespace URI declaration. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 35 | struct ExtractedPackage { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 36 | // The name of the package. This can be the empty string, which means that the package |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 37 | // should be assumed to be the same as the CallSite it was defined in. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | std::string package; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 40 | // True if the package's private namespace was declared. This means that private resources |
| 41 | // are made visible. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | bool private_namespace; |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 43 | |
| 44 | friend inline bool operator==(const ExtractedPackage& a, const ExtractedPackage& b) { |
| 45 | return a.package == b.package && a.private_namespace == b.private_namespace; |
| 46 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 49 | // Returns an ExtractedPackage struct if the namespace URI is of the form: |
| 50 | // http://schemas.android.com/apk/res/<package> or |
| 51 | // http://schemas.android.com/apk/prv/res/<package> |
| 52 | // |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 53 | // Special case: if namespaceUri is http://schemas.android.com/apk/res-auto, returns an empty |
| 54 | // package name. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 55 | std::optional<ExtractedPackage> ExtractPackageFromNamespace(const std::string& namespace_uri); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 57 | // Returns an XML Android namespace for the given package of the form: |
| 58 | // http://schemas.android.com/apk/res/<package> |
| 59 | // |
| 60 | // If privateReference == true, the package will be of the form: |
| 61 | // http://schemas.android.com/apk/prv/res/<package> |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 62 | std::string BuildPackageNamespace(const android::StringPiece& package, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | bool private_reference = false); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 64 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 65 | // Interface representing a stack of XML namespace declarations. When looking up the package for a |
| 66 | // namespace prefix, the stack is checked from top to bottom. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 67 | struct IPackageDeclStack { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | virtual ~IPackageDeclStack() = default; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 69 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 70 | // Returns an ExtractedPackage struct if the alias given corresponds with a package declaration. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 71 | virtual std::optional<ExtractedPackage> TransformPackageAlias( |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 72 | const android::StringPiece& alias) const = 0; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 75 | // Helper function for transforming the original Reference inRef to a fully qualified reference |
| 76 | // via the IPackageDeclStack. This will also mark the Reference as private if the namespace of the |
| 77 | // package declaration was private. |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 78 | void ResolvePackage(const IPackageDeclStack* decl_stack, Reference* in_ref); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 80 | class Element; |
| 81 | |
| 82 | // Strips out any attributes in the http://schemas.android.com/tools namespace, which is owned by |
| 83 | // Android Studio and should not make it to the final APK. |
| 84 | void StripAndroidStudioAttributes(Element* el); |
| 85 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | } // namespace xml |
| 87 | } // namespace aapt |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 88 | |
| 89 | #endif /* AAPT_XML_XMLUTIL_H */ |