• 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 
34 // Result of extracting a package name from a namespace URI declaration.
35 struct ExtractedPackage {
36   // The name of the package. This can be the empty string, which means that the package
37   // should be assumed to be the same as the CallSite it was defined in.
38   std::string package;
39 
40   // True if the package's private namespace was declared. This means that private resources
41   // are made visible.
42   bool private_namespace;
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   }
47 };
48 
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 //
53 // Special case: if namespaceUri is http://schemas.android.com/apk/res-auto, returns an empty
54 // package name.
55 std::optional<ExtractedPackage> ExtractPackageFromNamespace(const std::string& namespace_uri);
56 
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>
62 std::string BuildPackageNamespace(android::StringPiece package, bool private_reference = false);
63 
64 // Interface representing a stack of XML namespace declarations. When looking up the package for a
65 // namespace prefix, the stack is checked from top to bottom.
66 struct IPackageDeclStack {
67   virtual ~IPackageDeclStack() = default;
68 
69   // Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
70   virtual std::optional<ExtractedPackage> TransformPackageAlias(
71       android::StringPiece alias) const = 0;
72 };
73 
74 // Helper function for transforming the original Reference inRef to a fully qualified reference
75 // via the IPackageDeclStack. This will also mark the Reference as private if the namespace of the
76 // package declaration was private.
77 void ResolvePackage(const IPackageDeclStack* decl_stack, Reference* in_ref);
78 
79 class Element;
80 
81 // Strips out any attributes in the http://schemas.android.com/tools namespace, which is owned by
82 // Android Studio and should not make it to the final APK.
83 void StripAndroidStudioAttributes(Element* el);
84 
85 }  // namespace xml
86 }  // namespace aapt
87 
88 #endif /* AAPT_XML_XMLUTIL_H */
89