| 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 |  | 
 | 20 | #include "ResourceValues.h" | 
 | 21 | #include "util/Maybe.h" | 
 | 22 |  | 
 | 23 | #include <string> | 
 | 24 |  | 
 | 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"; | 
 | 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 |  | 
 | 35 | /** | 
 | 36 |  * Result of extracting a package name from a namespace URI declaration. | 
 | 37 |  */ | 
 | 38 | struct ExtractedPackage { | 
 | 39 |     /** | 
 | 40 |      * The name of the package. This can be the empty string, which means that the package | 
 | 41 |      * should be assumed to be the package being compiled. | 
 | 42 |      */ | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 43 |     std::string package; | 
| Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 44 |  | 
 | 45 |     /** | 
 | 46 |      * True if the package's private namespace was declared. This means that private resources | 
 | 47 |      * are made visible. | 
 | 48 |      */ | 
 | 49 |     bool privateNamespace; | 
 | 50 | }; | 
 | 51 |  | 
 | 52 | /** | 
 | 53 |  * Returns an ExtractedPackage struct if the namespace URI is of the form: | 
 | 54 |  * http://schemas.android.com/apk/res/<package> or | 
 | 55 |  * http://schemas.android.com/apk/prv/res/<package> | 
 | 56 |  * | 
 | 57 |  * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto, | 
 | 58 |  * returns an empty package name. | 
 | 59 |  */ | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 60 | Maybe<ExtractedPackage> extractPackageFromNamespace(const std::string& namespaceUri); | 
 | 61 |  | 
 | 62 | /** | 
 | 63 |  * Returns an XML Android namespace for the given package of the form: | 
 | 64 |  * | 
 | 65 |  * http://schemas.android.com/apk/res/<package> | 
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 66 |  * | 
 | 67 |  * If privateReference == true, the package will be of the form: | 
 | 68 |  * | 
 | 69 |  * http://schemas.android.com/apk/prv/res/<package> | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 70 |  */ | 
| Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 71 | std::string buildPackageNamespace(const StringPiece& package, bool privateReference=false); | 
| Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 72 |  | 
 | 73 | /** | 
 | 74 |  * Interface representing a stack of XML namespace declarations. When looking up the package | 
 | 75 |  * for a namespace prefix, the stack is checked from top to bottom. | 
 | 76 |  */ | 
 | 77 | struct IPackageDeclStack { | 
 | 78 |     virtual ~IPackageDeclStack() = default; | 
 | 79 |  | 
 | 80 |     /** | 
 | 81 |      * Returns an ExtractedPackage struct if the alias given corresponds with a package declaration. | 
 | 82 |      */ | 
 | 83 |     virtual Maybe<ExtractedPackage> transformPackageAlias( | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 84 |             const StringPiece& alias, const StringPiece& localPackage) const = 0; | 
| Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 85 | }; | 
 | 86 |  | 
 | 87 | /** | 
 | 88 |  * Helper function for transforming the original Reference inRef to a fully qualified reference | 
 | 89 |  * via the IPackageDeclStack. This will also mark the Reference as private if the namespace of | 
 | 90 |  * the package declaration was private. | 
 | 91 |  */ | 
 | 92 | void transformReferenceFromNamespace(IPackageDeclStack* declStack, | 
| Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 93 |                                      const StringPiece& localPackage, Reference* inRef); | 
| Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 94 |  | 
 | 95 | } // namespace xml | 
 | 96 | } // namespace aapt | 
 | 97 |  | 
 | 98 | #endif /* AAPT_XML_XMLUTIL_H */ |