• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
21 
22 #include "ResourceValues.h"
23 
24 namespace aapt {
25 namespace xml {
26 
27 constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
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";
31 constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
32 constexpr const char* kSchemaAapt = "http://schemas.android.com/aapt";
33 constexpr const char* kAttrFeatureFlag = "featureFlag";
34 
35 // Result of extracting a package name from a namespace URI declaration.
36 struct ExtractedPackage {
37   // The name of the package. This can be the empty string, which means that the package
38   // should be assumed to be the same as the CallSite it was defined in.
39   std::string package;
40 
41   // True if the package's private namespace was declared. This means that private resources
42   // are made visible.
43   bool private_namespace;
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   }
48 };
49 
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 //
54 // Special case: if namespaceUri is http://schemas.android.com/apk/res-auto, returns an empty
55 // package name.
56 std::optional<ExtractedPackage> ExtractPackageFromNamespace(const std::string& namespace_uri);
57 
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>
63 std::string BuildPackageNamespace(android::StringPiece package, bool private_reference = false);
64 
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.
67 struct IPackageDeclStack {
68   virtual ~IPackageDeclStack() = default;
69 
70   // Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
71   virtual std::optional<ExtractedPackage> TransformPackageAlias(
72       android::StringPiece alias) const = 0;
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 the
77 // package declaration was private.
78 void ResolvePackage(const IPackageDeclStack* decl_stack, Reference* in_ref);
79 
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 
86 }  // namespace xml
87 }  // namespace aapt
88 
89 #endif /* AAPT_XML_XMLUTIL_H */
90