blob: 866b6dcd7a8802e1674821899f81d97043b691ee [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"
23#include "util/Maybe.h"
24
Adam Lesinski467f1712015-11-16 17:35:44 -080025namespace aapt {
26namespace xml {
27
Adam Lesinskid0f116b2016-07-08 15:00:32 -070028constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
Adam Lesinski6b372992017-08-09 10:54:23 -070029constexpr const char* kSchemaPublicPrefix = "http://schemas.android.com/apk/res/";
30constexpr const char* kSchemaPrivatePrefix = "http://schemas.android.com/apk/prv/res/";
31constexpr const char* kSchemaAndroid = "http://schemas.android.com/apk/res/android";
Alexandria Cornwalla9ff1402016-08-03 09:44:10 -070032constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070033constexpr const char* kSchemaAapt = "http://schemas.android.com/aapt";
Adam Lesinski467f1712015-11-16 17:35:44 -080034
Adam Lesinski6b372992017-08-09 10:54:23 -070035// Result of extracting a package name from a namespace URI declaration.
Adam Lesinski467f1712015-11-16 17:35:44 -080036struct ExtractedPackage {
Adam Lesinski6b372992017-08-09 10:54:23 -070037 // The name of the package. This can be the empty string, which means that the package
38 // should be assumed to be the package being compiled.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 std::string package;
Adam Lesinski467f1712015-11-16 17:35:44 -080040
Adam Lesinski6b372992017-08-09 10:54:23 -070041 // True if the package's private namespace was declared. This means that private resources
42 // are made visible.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 bool private_namespace;
Adam Lesinski6b372992017-08-09 10:54:23 -070044
45 friend inline bool operator==(const ExtractedPackage& a, const ExtractedPackage& b) {
46 return a.package == b.package && a.private_namespace == b.private_namespace;
47 }
Adam Lesinski467f1712015-11-16 17:35:44 -080048};
49
Adam Lesinski6b372992017-08-09 10:54:23 -070050// 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,
55// returns an empty package name.
56Maybe<ExtractedPackage> ExtractPackageFromNamespace(const std::string& namespace_uri);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070057
Adam Lesinski6b372992017-08-09 10:54:23 -070058// 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>
Adam Lesinskid5083f62017-01-16 15:07:21 -080063std::string BuildPackageNamespace(const android::StringPiece& package,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 bool private_reference = false);
Adam Lesinski467f1712015-11-16 17:35:44 -080065
Adam Lesinski6b372992017-08-09 10:54:23 -070066// Interface representing a stack of XML namespace declarations. When looking up the package
67// for a namespace prefix, the stack is checked from top to bottom.
Adam Lesinski467f1712015-11-16 17:35:44 -080068struct IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 virtual ~IPackageDeclStack() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080070
Adam Lesinski6b372992017-08-09 10:54:23 -070071 // Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 virtual Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -080073 const android::StringPiece& alias, const android::StringPiece& local_package) const = 0;
Adam Lesinski467f1712015-11-16 17:35:44 -080074};
75
Adam Lesinski6b372992017-08-09 10:54:23 -070076// Helper function for transforming the original Reference inRef to a fully qualified reference
77// via the IPackageDeclStack. This will also mark the Reference as private if the namespace of the
78// package declaration was private.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack,
Adam Lesinskid5083f62017-01-16 15:07:21 -080080 const android::StringPiece& local_package, Reference* in_ref);
Adam Lesinski467f1712015-11-16 17:35:44 -080081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082} // namespace xml
83} // namespace aapt
Adam Lesinski467f1712015-11-16 17:35:44 -080084
85#endif /* AAPT_XML_XMLUTIL_H */