blob: f0d59b7346b29a20a0c7f68f7b42c49524074294 [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
20#include "ResourceValues.h"
21#include "util/Maybe.h"
22
23#include <string>
24
25namespace aapt {
26namespace xml {
27
Adam Lesinskid0f116b2016-07-08 15:00:32 -070028constexpr const char* kSchemaAuto = "http://schemas.android.com/apk/res-auto";
29constexpr 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";
Adam Lesinski467f1712015-11-16 17:35:44 -080032
33/**
34 * Result of extracting a package name from a namespace URI declaration.
35 */
36struct ExtractedPackage {
37 /**
38 * The name of the package. This can be the empty string, which means that the package
39 * should be assumed to be the package being compiled.
40 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070041 std::string package;
Adam Lesinski467f1712015-11-16 17:35:44 -080042
43 /**
44 * True if the package's private namespace was declared. This means that private resources
45 * are made visible.
46 */
47 bool privateNamespace;
48};
49
50/**
51 * Returns an ExtractedPackage struct if the namespace URI is of the form:
52 * http://schemas.android.com/apk/res/<package> or
53 * http://schemas.android.com/apk/prv/res/<package>
54 *
55 * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto,
56 * returns an empty package name.
57 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070058Maybe<ExtractedPackage> extractPackageFromNamespace(const std::string& namespaceUri);
59
60/**
61 * Returns an XML Android namespace for the given package of the form:
62 *
63 * http://schemas.android.com/apk/res/<package>
64 */
65std::string buildPackageNamespace(const StringPiece& package);
Adam Lesinski467f1712015-11-16 17:35:44 -080066
67/**
68 * Interface representing a stack of XML namespace declarations. When looking up the package
69 * for a namespace prefix, the stack is checked from top to bottom.
70 */
71struct IPackageDeclStack {
72 virtual ~IPackageDeclStack() = default;
73
74 /**
75 * Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
76 */
77 virtual Maybe<ExtractedPackage> transformPackageAlias(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070078 const StringPiece& alias, const StringPiece& localPackage) const = 0;
Adam Lesinski467f1712015-11-16 17:35:44 -080079};
80
81/**
82 * Helper function for transforming the original Reference inRef to a fully qualified reference
83 * via the IPackageDeclStack. This will also mark the Reference as private if the namespace of
84 * the package declaration was private.
85 */
86void transformReferenceFromNamespace(IPackageDeclStack* declStack,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070087 const StringPiece& localPackage, Reference* inRef);
Adam Lesinski467f1712015-11-16 17:35:44 -080088
89} // namespace xml
90} // namespace aapt
91
92#endif /* AAPT_XML_XMLUTIL_H */