blob: b75d8ac1506a57e5fbba0504327c2275c3dfc455 [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";
Alexandria Cornwalla9ff1402016-08-03 09:44:10 -070032constexpr const char* kSchemaTools = "http://schemas.android.com/tools";
Adam Lesinski467f1712015-11-16 17:35:44 -080033
34/**
35 * Result of extracting a package name from a namespace URI declaration.
36 */
37struct ExtractedPackage {
38 /**
39 * The name of the package. This can be the empty string, which means that the package
40 * should be assumed to be the package being compiled.
41 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070042 std::string package;
Adam Lesinski467f1712015-11-16 17:35:44 -080043
44 /**
45 * True if the package's private namespace was declared. This means that private resources
46 * are made visible.
47 */
48 bool privateNamespace;
49};
50
51/**
52 * Returns an ExtractedPackage struct if the namespace URI is of the form:
53 * http://schemas.android.com/apk/res/<package> or
54 * http://schemas.android.com/apk/prv/res/<package>
55 *
56 * Special case: if namespaceUri is http://schemas.android.com/apk/res-auto,
57 * returns an empty package name.
58 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070059Maybe<ExtractedPackage> extractPackageFromNamespace(const std::string& namespaceUri);
60
61/**
62 * Returns an XML Android namespace for the given package of the form:
63 *
64 * http://schemas.android.com/apk/res/<package>
65 */
66std::string buildPackageNamespace(const StringPiece& package);
Adam Lesinski467f1712015-11-16 17:35:44 -080067
68/**
69 * Interface representing a stack of XML namespace declarations. When looking up the package
70 * for a namespace prefix, the stack is checked from top to bottom.
71 */
72struct IPackageDeclStack {
73 virtual ~IPackageDeclStack() = default;
74
75 /**
76 * Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
77 */
78 virtual Maybe<ExtractedPackage> transformPackageAlias(
Adam Lesinskid0f116b2016-07-08 15:00:32 -070079 const StringPiece& alias, const StringPiece& localPackage) const = 0;
Adam Lesinski467f1712015-11-16 17:35:44 -080080};
81
82/**
83 * Helper function for transforming the original Reference inRef to a fully qualified reference
84 * via the IPackageDeclStack. This will also mark the Reference as private if the namespace of
85 * the package declaration was private.
86 */
87void transformReferenceFromNamespace(IPackageDeclStack* declStack,
Adam Lesinskid0f116b2016-07-08 15:00:32 -070088 const StringPiece& localPackage, Reference* inRef);
Adam Lesinski467f1712015-11-16 17:35:44 -080089
90} // namespace xml
91} // namespace aapt
92
93#endif /* AAPT_XML_XMLUTIL_H */