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