blob: bd3a8e3e0bc8da4ffc098d5e89c0e2691644de86 [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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <functional>
21#include <memory>
22
Adam Lesinskid5083f62017-01-16 15:07:21 -080023#include "androidfw/StringPiece.h"
24
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "Resource.h"
27#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029namespace aapt {
30namespace ResourceUtils {
31
32/*
33 * Extracts the package, type, and name from a string of the format:
34 *
35 * [package:]type/name
36 *
37 * where the package can be empty. Validation must be performed on each
38 * individual extracted piece to verify that the pieces are valid.
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080039 * Returns false if there was no package but a ':' was present.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080041bool ExtractResourceName(const android::StringPiece& str, android::StringPiece* out_package,
42 android::StringPiece* out_type, android::StringPiece* out_entry);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043
Adam Lesinski467f1712015-11-16 17:35:44 -080044/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 * Returns true if the string was parsed as a resource name
46 * ([*][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 * `out_resource` set to the parsed resource name and `out_private` set to true
48 * if a '*' prefix was present.
Adam Lesinski467f1712015-11-16 17:35:44 -080049 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080050bool ParseResourceName(const android::StringPiece& str, ResourceNameRef* out_resource,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 bool* out_private = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080052
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 * Returns true if the string was parsed as a reference
55 * (@[+][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 * `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 * If '+' was present in the reference, `out_create` is set to true.
59 * If '*' was present in the reference, `out_private` is set to true.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080061bool ParseReference(const android::StringPiece& str, ResourceNameRef* out_reference,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 bool* out_create = nullptr, bool* out_private = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
64/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 * Returns true if the string is in the form of a resource reference
66 * (@[+][package:]type/name).
Adam Lesinski2ae4a872015-11-02 16:10:55 -080067 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080068bool IsReference(const android::StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080069
70/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 * Returns true if the string was parsed as an attribute reference
72 * (?[package:][type/]name),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 * with `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080075bool ParseAttributeReference(const android::StringPiece& str, ResourceNameRef* out_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskib23f1e02015-11-03 12:24:17 -080077/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 * Returns true if the string is in the form of an attribute
79 * reference(?[package:][type/]name).
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080080 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080081bool IsAttributeReference(const android::StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080082
83/**
Adam Lesinski36c73a52016-08-11 13:39:24 -070084 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
Adam Lesinskib23f1e02015-11-03 12:24:17 -080085 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086Maybe<ResourceName> ToResourceName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 const android::ResTable::resource_name& name);
Adam Lesinski36c73a52016-08-11 13:39:24 -070088
89/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 * Returns a boolean value if the string is equal to TRUE, true, True, FALSE,
91 * false, or False.
Adam Lesinski36c73a52016-08-11 13:39:24 -070092 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080093Maybe<bool> ParseBool(const android::StringPiece& str);
Adam Lesinski36c73a52016-08-11 13:39:24 -070094
95/**
96 * Returns a uint32_t if the string is an integer.
97 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080098Maybe<uint32_t> ParseInt(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080099
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700100/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700101 * Returns an ID if it the string represented a valid ID.
102 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800103Maybe<ResourceId> ParseResourceId(const android::StringPiece& str);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700104
105/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700106 * Parses an SDK version, which can be an integer, or a letter from A-Z.
107 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800108Maybe<int> ParseSdkVersion(const android::StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700109
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 * Returns a Reference, or None Maybe instance if the string `str` was parsed as
112 * a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113 * valid reference to a style.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 * The format for a style parent is slightly more flexible than a normal
115 * reference:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 *
117 * @[package:]style/<entry> or
118 * ?[package:]style/<entry> or
119 * <package>:[style/]<entry>
120 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800121Maybe<Reference> ParseStyleParentReference(const android::StringPiece& str, std::string* out_error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122
123/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 * Returns a Reference if the string `str` was parsed as a valid XML attribute
125 * name.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700126 * The valid format for an XML attribute name is:
127 *
128 * package:entry
129 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800130Maybe<Reference> ParseXmlAttributeName(const android::StringPiece& str);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700131
132/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 * Returns a Reference object if the string was parsed as a resource or
134 * attribute reference,
135 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true
136 * if
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137 * the '+' was present in the string.
138 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800139std::unique_ptr<Reference> TryParseReference(const android::StringPiece& str,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 bool* out_create = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
142/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 * Returns a BinaryPrimitve object representing @null or @empty if the string
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 * was parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800146std::unique_ptr<BinaryPrimitive> TryParseNullOrEmpty(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
148/*
149 * Returns a BinaryPrimitve object representing a color if the string was parsed
150 * as one.
151 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800152std::unique_ptr<BinaryPrimitive> TryParseColor(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
154/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 * Returns a BinaryPrimitve object representing a boolean if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800158std::unique_ptr<BinaryPrimitive> TryParseBool(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
160/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 * Returns a BinaryPrimitve object representing an integer if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800164std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
166/*
167 * Returns a BinaryPrimitve object representing a floating point number
168 * (float, dimension, etc) if the string was parsed as one.
169 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800170std::unique_ptr<BinaryPrimitive> TryParseFloat(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
172/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 * Returns a BinaryPrimitve object representing an enum symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800177 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178
179/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 * Returns a BinaryPrimitve object representing a flag symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800184 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 * Try to convert a string to an Item for the given attribute. The attribute
187 * will
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 * restrict what values the string can be converted to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 * The callback function on_create_reference is called when the parsed item is a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190 * reference to an ID that must be created (@+id/foo).
191 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800193 const android::StringPiece& value, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800197 const android::StringPiece& value, uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800202/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 * Returns a string path suitable for use within an APK. The path will look
204 * like:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800205 *
206 * res/type[-config]/<name>.<ext>
207 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and
209 * the package
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800210 * requires mangling.
211 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212std::string BuildResourceFileName(const ResourceFile& res_file,
213 const NameMangler* mangler = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215} // namespace ResourceUtils
216} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700217
218#endif /* AAPT_RESOURCEUTILS_H */