blob: 552f42adc4649de7f3349045cc55defb26bccc0d [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 Lesinski5b7337f2017-06-26 14:57:22 -070023#include "SdkConstants.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080024#include "util/Maybe.h"
25
Adam Lesinski467f1712015-11-16 17:35:44 -080026namespace aapt {
27namespace xml {
28
Adam Lesinskid0f116b2016-07-08 15:00:32 -070029constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
Adam Lesinski5b7337f2017-06-26 14:57:22 -070030constexpr const char* kSchemaPublicPrefix = "http://schemas.android.com/apk/res/";
31constexpr const char* kSchemaPrivatePrefix = "http://schemas.android.com/apk/prv/res/";
32constexpr const char* kSchemaAndroid = "http://schemas.android.com/apk/res/android";
Alexandria Cornwalla9ff1402016-08-03 09:44:10 -070033constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070034constexpr const char* kSchemaAapt = "http://schemas.android.com/aapt";
Adam Lesinski467f1712015-11-16 17:35:44 -080035
36/**
37 * Result of extracting a package name from a namespace URI declaration.
38 */
39struct ExtractedPackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 /**
41 * The name of the package. This can be the empty string, which means that the
42 * package
43 * should be assumed to be the package being compiled.
44 */
45 std::string package;
Adam Lesinski467f1712015-11-16 17:35:44 -080046
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 /**
48 * True if the package's private namespace was declared. This means that
49 * private resources
50 * are made visible.
51 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 bool private_namespace;
Adam Lesinski467f1712015-11-16 17:35:44 -080053};
54
55/**
56 * Returns an ExtractedPackage struct if the namespace URI is of the form:
57 * http://schemas.android.com/apk/res/<package> or
58 * http://schemas.android.com/apk/prv/res/<package>
59 *
60 * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto,
61 * returns an empty package name.
62 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063Maybe<ExtractedPackage> ExtractPackageFromNamespace(
64 const std::string& namespace_uri);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070065
66/**
67 * Returns an XML Android namespace for the given package of the form:
68 *
69 * http://schemas.android.com/apk/res/<package>
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070070 *
71 * If privateReference == true, the package will be of the form:
72 *
73 * http://schemas.android.com/apk/prv/res/<package>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070074 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080075std::string BuildPackageNamespace(const android::StringPiece& package,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 bool private_reference = false);
Adam Lesinski467f1712015-11-16 17:35:44 -080077
78/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 * Interface representing a stack of XML namespace declarations. When looking up
80 * the package
Adam Lesinski467f1712015-11-16 17:35:44 -080081 * for a namespace prefix, the stack is checked from top to bottom.
82 */
83struct IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 virtual ~IPackageDeclStack() = default;
Adam Lesinski467f1712015-11-16 17:35:44 -080085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 /**
87 * Returns an ExtractedPackage struct if the alias given corresponds with a
88 * package declaration.
89 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 virtual Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -080091 const android::StringPiece& alias, const android::StringPiece& local_package) const = 0;
Adam Lesinski467f1712015-11-16 17:35:44 -080092};
93
94/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 * Helper function for transforming the original Reference inRef to a fully
96 * qualified reference
97 * via the IPackageDeclStack. This will also mark the Reference as private if
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 * the namespace of the package declaration was private.
Adam Lesinski467f1712015-11-16 17:35:44 -080099 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800101 const android::StringPiece& local_package, Reference* in_ref);
Adam Lesinski467f1712015-11-16 17:35:44 -0800102
Adam Lesinski5b7337f2017-06-26 14:57:22 -0700103struct TagApiVersionResult {
104 // If set, the API version to apply.
105 Maybe<ApiVersion> api_version;
106
107 // Whether to skip any auto-versioning.
108 bool skip_version;
109};
110
111enum TagVersionOptions : int {
112 // Skip versioning XML resources that deal with vector drawables.
113 kNoVersionVector,
114
115 // Skip versioning XML resources that deal with transitions.
116 kNoVersionTransition,
117};
118
119Maybe<TagApiVersionResult> GetXmlTagApiVersion(const android::StringPiece& tag_name, int options);
120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121} // namespace xml
122} // namespace aapt
Adam Lesinski467f1712015-11-16 17:35:44 -0800123
124#endif /* AAPT_XML_XMLUTIL_H */