blob: 59b78f4b3f33feee55700db9ba709459babb3a79 [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
Adam Lesinski467f1712015-11-16 17:35:44 -080032/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 * Returns true if the string was parsed as a resource name
34 * ([*][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 * `out_resource` set to the parsed resource name and `out_private` set to true
36 * if a '*' prefix was present.
Adam Lesinski467f1712015-11-16 17:35:44 -080037 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080038bool ParseResourceName(const android::StringPiece& str, ResourceNameRef* out_resource,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 bool* out_private = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080040
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 * Returns true if the string was parsed as a reference
43 * (@[+][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 * `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 * If '+' was present in the reference, `out_create` is set to true.
47 * If '*' was present in the reference, `out_private` is set to true.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080049bool ParseReference(const android::StringPiece& str, ResourceNameRef* out_reference,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 bool* out_create = nullptr, bool* out_private = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
52/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 * Returns true if the string is in the form of a resource reference
54 * (@[+][package:]type/name).
Adam Lesinski2ae4a872015-11-02 16:10:55 -080055 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080056bool IsReference(const android::StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080057
58/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 * Returns true if the string was parsed as an attribute reference
60 * (?[package:][type/]name),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 * with `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080063bool ParseAttributeReference(const android::StringPiece& str, ResourceNameRef* out_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskib23f1e02015-11-03 12:24:17 -080065/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 * Returns true if the string is in the form of an attribute
67 * reference(?[package:][type/]name).
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080068 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080069bool IsAttributeReference(const android::StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080070
71/**
Adam Lesinski36c73a52016-08-11 13:39:24 -070072 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
Adam Lesinskib23f1e02015-11-03 12:24:17 -080073 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074Maybe<ResourceName> ToResourceName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 const android::ResTable::resource_name& name);
Adam Lesinski36c73a52016-08-11 13:39:24 -070076
77/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 * Returns a boolean value if the string is equal to TRUE, true, True, FALSE,
79 * false, or False.
Adam Lesinski36c73a52016-08-11 13:39:24 -070080 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080081Maybe<bool> ParseBool(const android::StringPiece& str);
Adam Lesinski36c73a52016-08-11 13:39:24 -070082
83/**
84 * Returns a uint32_t if the string is an integer.
85 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080086Maybe<uint32_t> ParseInt(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080087
Adam Lesinskifb6312f2016-06-28 14:40:32 -070088/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070089 * Returns an ID if it the string represented a valid ID.
90 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080091Maybe<ResourceId> ParseResourceId(const android::StringPiece& str);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070092
93/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -070094 * Parses an SDK version, which can be an integer, or a letter from A-Z.
95 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080096Maybe<int> ParseSdkVersion(const android::StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070097
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 * Returns a Reference, or None Maybe instance if the string `str` was parsed as
100 * a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101 * valid reference to a style.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 * The format for a style parent is slightly more flexible than a normal
103 * reference:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 *
105 * @[package:]style/<entry> or
106 * ?[package:]style/<entry> or
107 * <package>:[style/]<entry>
108 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109Maybe<Reference> ParseStyleParentReference(const android::StringPiece& str, std::string* out_error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110
111/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 * Returns a Reference if the string `str` was parsed as a valid XML attribute
113 * name.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700114 * The valid format for an XML attribute name is:
115 *
116 * package:entry
117 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800118Maybe<Reference> ParseXmlAttributeName(const android::StringPiece& str);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700119
120/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 * Returns a Reference object if the string was parsed as a resource or
122 * attribute reference,
123 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true
124 * if
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 * the '+' was present in the string.
126 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800127std::unique_ptr<Reference> TryParseReference(const android::StringPiece& str,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 bool* out_create = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
130/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 * Returns a BinaryPrimitve object representing @null or @empty if the string
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 * was parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800134std::unique_ptr<BinaryPrimitive> TryParseNullOrEmpty(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
136/*
137 * Returns a BinaryPrimitve object representing a color if the string was parsed
138 * as one.
139 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800140std::unique_ptr<BinaryPrimitive> TryParseColor(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
142/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 * Returns a BinaryPrimitve object representing a boolean if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800146std::unique_ptr<BinaryPrimitive> TryParseBool(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
148/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 * Returns a BinaryPrimitve object representing an integer if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800152std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
154/*
155 * Returns a BinaryPrimitve object representing a floating point number
156 * (float, dimension, etc) if the string was parsed as one.
157 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800158std::unique_ptr<BinaryPrimitive> TryParseFloat(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 enum symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800165 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
167/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 * Returns a BinaryPrimitve object representing a flag symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800172 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 * Try to convert a string to an Item for the given attribute. The attribute
175 * will
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176 * restrict what values the string can be converted to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 * The callback function on_create_reference is called when the parsed item is a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178 * reference to an ID that must be created (@+id/foo).
179 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800181 const android::StringPiece& value, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800185 const android::StringPiece& value, uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800190/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 * Returns a string path suitable for use within an APK. The path will look
192 * like:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800193 *
194 * res/type[-config]/<name>.<ext>
195 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and
197 * the package
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800198 * requires mangling.
199 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200std::string BuildResourceFileName(const ResourceFile& res_file,
201 const NameMangler* mangler = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800202
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203} // namespace ResourceUtils
204} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205
206#endif /* AAPT_RESOURCEUTILS_H */