blob: 871ed7ca1e3bc27da167b06dbaaea19d9b154000 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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_RESOURCEUTILS_H
18#define AAPT_RESOURCEUTILS_H
19
Adam Lesinskia6fe3452015-12-09 15:20:52 -080020#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "Resource.h"
22#include "ResourceValues.h"
23#include "util/StringPiece.h"
24
25#include <functional>
26#include <memory>
27
28namespace aapt {
29namespace ResourceUtils {
30
Adam Lesinskid0f116b2016-07-08 15:00:32 -070031/**
32 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
33 */
34Maybe<ResourceName> toResourceName(const android::ResTable::resource_name& name);
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036/*
37 * Extracts the package, type, and name from a string of the format:
38 *
39 * [package:]type/name
40 *
41 * where the package can be empty. Validation must be performed on each
42 * individual extracted piece to verify that the pieces are valid.
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080043 * Returns false if there was no package but a ':' was present.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070045bool extractResourceName(const StringPiece& str, StringPiece* outPackage,
46 StringPiece* outType, StringPiece* outEntry);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinski467f1712015-11-16 17:35:44 -080048/**
49 * Returns true if the string was parsed as a resource name ([*][package:]type/name), with
50 * `outResource` set to the parsed resource name and `outPrivate` set to true if a '*' prefix
51 * was present.
52 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070053bool parseResourceName(const StringPiece& str, ResourceNameRef* outResource,
Adam Lesinski59e04c62016-02-04 15:59:23 -080054 bool* outPrivate = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080055
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056/*
57 * Returns true if the string was parsed as a reference (@[+][package:]type/name), with
58 * `outReference` set to the parsed reference.
59 *
60 * If '+' was present in the reference, `outCreate` is set to true.
61 * If '*' was present in the reference, `outPrivate` is set to true.
62 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070063bool tryParseReference(const StringPiece& str, ResourceNameRef* outReference,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 bool* outCreate = nullptr, bool* outPrivate = nullptr);
65
66/*
Adam Lesinski2ae4a872015-11-02 16:10:55 -080067 * Returns true if the string is in the form of a resource reference (@[+][package:]type/name).
68 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069bool isReference(const StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080070
71/*
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080072 * Returns true if the string was parsed as an attribute reference (?[package:][type/]name),
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 * with `outReference` set to the parsed reference.
74 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070075bool tryParseAttributeReference(const StringPiece& str, ResourceNameRef* outReference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskib23f1e02015-11-03 12:24:17 -080077/**
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080078 * Returns true if the string is in the form of an attribute reference(?[package:][type/]name).
79 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070080bool isAttributeReference(const StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080081
82/**
Adam Lesinskib23f1e02015-11-03 12:24:17 -080083 * Returns true if the value is a boolean, putting the result in `outValue`.
84 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070085bool tryParseBool(const StringPiece& str, bool* outValue);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080086
Adam Lesinskifb6312f2016-06-28 14:40:32 -070087/**
88 * Parses an SDK version, which can be an integer, or a letter from A-Z.
89 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070090Maybe<int> tryParseSdkVersion(const StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070091
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092/*
93 * Returns a Reference, or None Maybe instance if the string `str` was parsed as a
94 * valid reference to a style.
95 * The format for a style parent is slightly more flexible than a normal reference:
96 *
97 * @[package:]style/<entry> or
98 * ?[package:]style/<entry> or
99 * <package>:[style/]<entry>
100 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700101Maybe<Reference> parseStyleParentReference(const StringPiece& str, std::string* outError);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102
103/*
104 * Returns a Reference object if the string was parsed as a resource or attribute reference,
105 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true if
106 * the '+' was present in the string.
107 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700108std::unique_ptr<Reference> tryParseReference(const StringPiece& str, bool* outCreate = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109
110/*
111 * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed
112 * as one.
113 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700114std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115
116/*
117 * Returns a BinaryPrimitve object representing a color if the string was parsed
118 * as one.
119 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700120std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121
122/*
123 * Returns a BinaryPrimitve object representing a boolean if the string was parsed
124 * as one.
125 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700126std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127
128/*
129 * Returns a BinaryPrimitve object representing an integer if the string was parsed
130 * as one.
131 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700132std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133
134/*
135 * Returns a BinaryPrimitve object representing a floating point number
136 * (float, dimension, etc) if the string was parsed as one.
137 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700138std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
140/*
141 * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed
142 * as one.
143 */
144std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700145 const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
147/*
148 * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed
149 * as one.
150 */
151std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700152 const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153/*
154 * Try to convert a string to an Item for the given attribute. The attribute will
155 * restrict what values the string can be converted to.
156 * The callback function onCreateReference is called when the parsed item is a
157 * reference to an ID that must be created (@+id/foo).
158 */
159std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700160 const StringPiece& value, const Attribute* attr,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161 std::function<void(const ResourceName&)> onCreateReference = {});
162
163std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700164 const StringPiece& value, uint32_t typeMask,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165 std::function<void(const ResourceName&)> onCreateReference = {});
166
167uint32_t androidTypeToAttributeTypeMask(uint16_t type);
168
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800169/**
170 * Returns a string path suitable for use within an APK. The path will look like:
171 *
172 * res/type[-config]/<name>.<ext>
173 *
174 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and the package
175 * requires mangling.
176 */
177std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler);
178
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179} // namespace ResourceUtils
180} // namespace aapt
181
182#endif /* AAPT_RESOURCEUTILS_H */