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 
28 constexpr const char16_t* kSchemaAuto = u"http://schemas.android.com/apk/res-auto";
29 constexpr const char16_t* kSchemaPublicPrefix = u"http://schemas.android.com/apk/res/";
30 constexpr const char16_t* kSchemaPrivatePrefix = u"http://schemas.android.com/apk/prv/res/";
31 constexpr const char16_t* kSchemaAndroid = u"http://schemas.android.com/apk/res/android";
32 
33 /**
34  * Result of extracting a package name from a namespace URI declaration.
35  */
36 struct ExtractedPackage {
37     /**
38      * The name of the package. This can be the empty string, which means that the package
39      * should be assumed to be the package being compiled.
40      */
41     std::u16string package;
42 
43     /**
44      * True if the package's private namespace was declared. This means that private resources
45      * are made visible.
46      */
47     bool privateNamespace;
48 };
49 
50 /**
51  * Returns an ExtractedPackage struct if the namespace URI is of the form:
52  * http://schemas.android.com/apk/res/<package> or
53  * http://schemas.android.com/apk/prv/res/<package>
54  *
55  * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto,
56  * returns an empty package name.
57  */
58 Maybe<ExtractedPackage> extractPackageFromNamespace(const std::u16string& namespaceUri);
59 
60 /**
61  * Interface representing a stack of XML namespace declarations. When looking up the package
62  * for a namespace prefix, the stack is checked from top to bottom.
63  */
64 struct IPackageDeclStack {
65     virtual ~IPackageDeclStack() = default;
66 
67     /**
68      * Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
69      */
70     virtual Maybe<ExtractedPackage> transformPackageAlias(
71             const StringPiece16& alias, const StringPiece16& localPackage) const = 0;
72 };
73 
74 /**
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
77  * the package declaration was private.
78  */
79 void transformReferenceFromNamespace(IPackageDeclStack* declStack,
80                                      const StringPiece16& localPackage, Reference* inRef);
81 
82 } // namespace xml
83 } // namespace aapt
84 
85 #endif /* AAPT_XML_XMLUTIL_H */
86