blob: 36f6c2bda8f81e133b5140060fb0ccb0153a3603 [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 Lesinskid0f492d2017-04-03 18:12:45 -070023#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25
Adam Lesinskia6fe3452015-12-09 15:20:52 -080026#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "Resource.h"
28#include "ResourceValues.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070029#include "StringPool.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031namespace aapt {
32namespace ResourceUtils {
33
Adam Lesinski467f1712015-11-16 17:35:44 -080034/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 * Returns true if the string was parsed as a resource name
36 * ([*][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 * `out_resource` set to the parsed resource name and `out_private` set to true
38 * if a '*' prefix was present.
Adam Lesinski467f1712015-11-16 17:35:44 -080039 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080040bool ParseResourceName(const android::StringPiece& str, ResourceNameRef* out_resource,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 bool* out_private = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080042
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 * Returns true if the string was parsed as a reference
45 * (@[+][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 * `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 * If '+' was present in the reference, `out_create` is set to true.
49 * If '*' was present in the reference, `out_private` is set to true.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080051bool ParseReference(const android::StringPiece& str, ResourceNameRef* out_reference,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 bool* out_create = nullptr, bool* out_private = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
54/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 * Returns true if the string is in the form of a resource reference
56 * (@[+][package:]type/name).
Adam Lesinski2ae4a872015-11-02 16:10:55 -080057 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080058bool IsReference(const android::StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080059
60/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 * Returns true if the string was parsed as an attribute reference
62 * (?[package:][type/]name),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 * with `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080065bool ParseAttributeReference(const android::StringPiece& str, ResourceNameRef* out_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066
Adam Lesinskib23f1e02015-11-03 12:24:17 -080067/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 * Returns true if the string is in the form of an attribute
69 * reference(?[package:][type/]name).
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080070 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080071bool IsAttributeReference(const android::StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080072
73/**
Adam Lesinski36c73a52016-08-11 13:39:24 -070074 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
Adam Lesinskib23f1e02015-11-03 12:24:17 -080075 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076Maybe<ResourceName> ToResourceName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 const android::ResTable::resource_name& name);
Adam Lesinski36c73a52016-08-11 13:39:24 -070078
79/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 * Returns a boolean value if the string is equal to TRUE, true, True, FALSE,
81 * false, or False.
Adam Lesinski36c73a52016-08-11 13:39:24 -070082 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080083Maybe<bool> ParseBool(const android::StringPiece& str);
Adam Lesinski36c73a52016-08-11 13:39:24 -070084
85/**
86 * Returns a uint32_t if the string is an integer.
87 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080088Maybe<uint32_t> ParseInt(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080089
Adam Lesinskifb6312f2016-06-28 14:40:32 -070090/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070091 * Returns an ID if it the string represented a valid ID.
92 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080093Maybe<ResourceId> ParseResourceId(const android::StringPiece& str);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070094
95/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -070096 * Parses an SDK version, which can be an integer, or a letter from A-Z.
97 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080098Maybe<int> ParseSdkVersion(const android::StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070099
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 * Returns a Reference, or None Maybe instance if the string `str` was parsed as
102 * a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 * valid reference to a style.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 * The format for a style parent is slightly more flexible than a normal
105 * reference:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 *
107 * @[package:]style/<entry> or
108 * ?[package:]style/<entry> or
109 * <package>:[style/]<entry>
110 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800111Maybe<Reference> ParseStyleParentReference(const android::StringPiece& str, std::string* out_error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112
113/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 * Returns a Reference if the string `str` was parsed as a valid XML attribute
115 * name.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700116 * The valid format for an XML attribute name is:
117 *
118 * package:entry
119 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800120Maybe<Reference> ParseXmlAttributeName(const android::StringPiece& str);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700121
122/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 * Returns a Reference object if the string was parsed as a resource or
124 * attribute reference,
125 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true
126 * if
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 * the '+' was present in the string.
128 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800129std::unique_ptr<Reference> TryParseReference(const android::StringPiece& str,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 bool* out_create = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
132/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 * Returns a BinaryPrimitve object representing @null or @empty if the string
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 * was parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135 */
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700136std::unique_ptr<Item> TryParseNullOrEmpty(const android::StringPiece& str);
137
138// Returns a Reference representing @null.
139// Due to runtime compatibility issues, this is encoded as a reference with ID 0.
140// The runtime will convert this to TYPE_NULL.
141std::unique_ptr<Reference> MakeNull();
142
143// Returns a BinaryPrimitive representing @empty. This is encoded as a Res_value with
144// type Res_value::TYPE_NULL and data Res_value::DATA_NULL_EMPTY.
145std::unique_ptr<BinaryPrimitive> MakeEmpty();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146
147/*
148 * Returns a BinaryPrimitve object representing a color if the string was parsed
149 * as one.
150 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800151std::unique_ptr<BinaryPrimitive> TryParseColor(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152
153/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 * Returns a BinaryPrimitve object representing a boolean if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800157std::unique_ptr<BinaryPrimitive> TryParseBool(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700159// Returns a boolean BinaryPrimitive.
160std::unique_ptr<BinaryPrimitive> MakeBool(bool val);
161
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 * Returns a BinaryPrimitve object representing an integer if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800166std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
168/*
169 * Returns a BinaryPrimitve object representing a floating point number
170 * (float, dimension, etc) if the string was parsed as one.
171 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800172std::unique_ptr<BinaryPrimitive> TryParseFloat(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
174/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 * Returns a BinaryPrimitve object representing an enum symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800179 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180
181/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 * Returns a BinaryPrimitve object representing a flag symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800186 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 * Try to convert a string to an Item for the given attribute. The attribute
189 * will
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190 * restrict what values the string can be converted to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 * The callback function on_create_reference is called when the parsed item is a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192 * reference to an ID that must be created (@+id/foo).
193 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800195 const android::StringPiece& value, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800199 const android::StringPiece& value, uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800204/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 * Returns a string path suitable for use within an APK. The path will look
206 * like:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800207 *
208 * res/type[-config]/<name>.<ext>
209 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and
211 * the package
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800212 * requires mangling.
213 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214std::string BuildResourceFileName(const ResourceFile& res_file,
215 const NameMangler* mangler = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800216
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700217// Parses the binary form of a resource value. `type` is used as a hint to know when a value is
218// an ID versus a False boolean value, etc. `config` is for sorting strings in the string pool.
219std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config,
220 const android::ResStringPool& src_pool,
221 const android::Res_value& res_value,
222 StringPool* dst_pool);
223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224} // namespace ResourceUtils
225} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226
227#endif /* AAPT_RESOURCEUTILS_H */