blob: 1650ac2124ac4539977c06a2bcedebeba48b8935 [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 Lesinskibb94f322017-07-12 07:41:55 -070029constexpr const char* kSchemaPublicPrefix =
30 "http://schemas.android.com/apk/res/";
31constexpr const char* kSchemaPrivatePrefix =
32 "http://schemas.android.com/apk/prv/res/";
33constexpr const char* kSchemaAndroid =
34 "http://schemas.android.com/apk/res/android";
Alexandria Cornwalla9ff1402016-08-03 09:44:10 -070035constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070036constexpr const char* kSchemaAapt = "http://schemas.android.com/aapt";
Adam Lesinski467f1712015-11-16 17:35:44 -080037
38/**
39 * Result of extracting a package name from a namespace URI declaration.
40 */
41struct ExtractedPackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 /**
43 * The name of the package. This can be the empty string, which means that the
44 * package
45 * should be assumed to be the package being compiled.
46 */
47 std::string package;
Adam Lesinski467f1712015-11-16 17:35:44 -080048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 /**
50 * True if the package's private namespace was declared. This means that
51 * private resources
52 * are made visible.
53 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 bool private_namespace;
Adam Lesinski467f1712015-11-16 17:35:44 -080055};
56
57/**
58 * Returns an ExtractedPackage struct if the namespace URI is of the form:
59 * http://schemas.android.com/apk/res/<package> or
60 * http://schemas.android.com/apk/prv/res/<package>
61 *
62 * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto,
63 * returns an empty package name.
64 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065Maybe<ExtractedPackage> ExtractPackageFromNamespace(
66 const std::string& namespace_uri);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070067
68/**
69 * Returns an XML Android namespace for the given package of the form:
70 *
71 * http://schemas.android.com/apk/res/<package>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070072 *
73 * If privateReference == true, the package will be of the form:
74 *
75 * http://schemas.android.com/apk/prv/res/<package>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070076 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080077std::string BuildPackageNamespace(const android::StringPiece& package,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 bool private_reference = false);
Adam Lesinski467f1712015-11-16 17:35:44 -080079
80/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 * Interface representing a stack of XML namespace declarations. When looking up
82 * the package
Adam Lesinski467f1712015-11-16 17:35:44 -080083 * for a namespace prefix, the stack is checked from top to bottom.
84 */
85struct IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 virtual ~IPackageDeclStack() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 /**
89 * Returns an ExtractedPackage struct if the alias given corresponds with a
90 * package declaration.
91 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 virtual Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -080093 const android::StringPiece& alias, const android::StringPiece& local_package) const = 0;
Adam Lesinski467f1712015-11-16 17:35:44 -080094};
95
96/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 * Helper function for transforming the original Reference inRef to a fully
98 * qualified reference
99 * via the IPackageDeclStack. This will also mark the Reference as private if
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 * the namespace of the package declaration was private.
Adam Lesinski467f1712015-11-16 17:35:44 -0800101 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800103 const android::StringPiece& local_package, Reference* in_ref);
Adam Lesinski467f1712015-11-16 17:35:44 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105} // namespace xml
106} // namespace aapt
Adam Lesinski467f1712015-11-16 17:35:44 -0800107
108#endif /* AAPT_XML_XMLUTIL_H */