blob: 1ab05a93d314dc08112e1766daa60b5fcc0b8a06 [file] [log] [blame]
Adam Lesinski467f1712015-11-16 17:35:44 -08001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <string>
21
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "ResourceValues.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080023
Adam Lesinski467f1712015-11-16 17:35:44 -080024namespace aapt {
25namespace xml {
26
Adam Lesinskid0f116b2016-07-08 15:00:32 -070027constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
Adam Lesinski6b372992017-08-09 10:54:23 -070028constexpr const char* kSchemaPublicPrefix = "http://schemas.android.com/apk/res/";
29constexpr const char* kSchemaPrivatePrefix = "http://schemas.android.com/apk/prv/res/";
30constexpr const char* kSchemaAndroid = "http://schemas.android.com/apk/res/android";
Alexandria Cornwalla9ff1402016-08-03 09:44:10 -070031constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070032constexpr const char* kSchemaAapt = "http://schemas.android.com/aapt";
Adam Lesinski467f1712015-11-16 17:35:44 -080033
Adam Lesinski6b372992017-08-09 10:54:23 -070034// Result of extracting a package name from a namespace URI declaration.
Adam Lesinski467f1712015-11-16 17:35:44 -080035struct ExtractedPackage {
Adam Lesinski6b372992017-08-09 10:54:23 -070036 // The name of the package. This can be the empty string, which means that the package
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070037 // should be assumed to be the same as the CallSite it was defined in.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 std::string package;
Adam Lesinski467f1712015-11-16 17:35:44 -080039
Adam Lesinski6b372992017-08-09 10:54:23 -070040 // True if the package's private namespace was declared. This means that private resources
41 // are made visible.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 bool private_namespace;
Adam Lesinski6b372992017-08-09 10:54:23 -070043
44 friend inline bool operator==(const ExtractedPackage& a, const ExtractedPackage& b) {
45 return a.package == b.package && a.private_namespace == b.private_namespace;
46 }
Adam Lesinski467f1712015-11-16 17:35:44 -080047};
48
Adam Lesinski6b372992017-08-09 10:54:23 -070049// 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//
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070053// Special case: if namespaceUri is http://schemas.android.com/apk/res-auto, returns an empty
54// package name.
Ryan Mitchell4382e442021-07-14 12:53:01 -070055std::optional<ExtractedPackage> ExtractPackageFromNamespace(const std::string& namespace_uri);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070056
Adam Lesinski6b372992017-08-09 10:54:23 -070057// 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>
Adam Lesinskid5083f62017-01-16 15:07:21 -080062std::string BuildPackageNamespace(const android::StringPiece& package,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 bool private_reference = false);
Adam Lesinski467f1712015-11-16 17:35:44 -080064
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070065// 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.
Adam Lesinski467f1712015-11-16 17:35:44 -080067struct IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 virtual ~IPackageDeclStack() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080069
Adam Lesinski6b372992017-08-09 10:54:23 -070070 // Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
Ryan Mitchell4382e442021-07-14 12:53:01 -070071 virtual std::optional<ExtractedPackage> TransformPackageAlias(
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070072 const android::StringPiece& alias) const = 0;
Adam Lesinski467f1712015-11-16 17:35:44 -080073};
74
Adam Lesinski6b372992017-08-09 10:54:23 -070075// 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.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070078void ResolvePackage(const IPackageDeclStack* decl_stack, Reference* in_ref);
Adam Lesinski467f1712015-11-16 17:35:44 -080079
Adam Lesinski00451162017-10-03 07:44:08 -070080class 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.
84void StripAndroidStudioAttributes(Element* el);
85
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086} // namespace xml
87} // namespace aapt
Adam Lesinski467f1712015-11-16 17:35:44 -080088
89#endif /* AAPT_XML_XMLUTIL_H */