Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <memory> |
| 22 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 23 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
| 25 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 26 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | #include "Resource.h" |
| 28 | #include "ResourceValues.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 29 | #include "StringPool.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | namespace aapt { |
| 32 | namespace ResourceUtils { |
| 33 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 34 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | * Returns true if the string was parsed as a resource name |
| 36 | * ([*][package:]type/name), with |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | * `out_resource` set to the parsed resource name and `out_private` set to true |
| 38 | * if a '*' prefix was present. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 39 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 40 | bool ParseResourceName(const android::StringPiece& str, ResourceNameRef* out_resource, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | bool* out_private = nullptr); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 42 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | * Returns true if the string was parsed as a reference |
| 45 | * (@[+][package:]type/name), with |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | * `out_reference` set to the parsed reference. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | * |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | * 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 51 | bool ParseReference(const android::StringPiece& str, ResourceNameRef* out_reference, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | bool* out_create = nullptr, bool* out_private = nullptr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | * Returns true if the string is in the form of a resource reference |
| 56 | * (@[+][package:]type/name). |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 57 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 58 | bool IsReference(const android::StringPiece& str); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 59 | |
| 60 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | * Returns true if the string was parsed as an attribute reference |
| 62 | * (?[package:][type/]name), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | * with `out_reference` set to the parsed reference. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 65 | bool ParseAttributeReference(const android::StringPiece& str, ResourceNameRef* out_reference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 67 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | * Returns true if the string is in the form of an attribute |
| 69 | * reference(?[package:][type/]name). |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 70 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 71 | bool IsAttributeReference(const android::StringPiece& str); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 74 | * Convert an android::ResTable::resource_name to an aapt::ResourceName struct. |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 75 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | Maybe<ResourceName> ToResourceName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | const android::ResTable::resource_name& name); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | * Returns a boolean value if the string is equal to TRUE, true, True, FALSE, |
| 81 | * false, or False. |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 82 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 83 | Maybe<bool> ParseBool(const android::StringPiece& str); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Returns a uint32_t if the string is an integer. |
| 87 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 88 | Maybe<uint32_t> ParseInt(const android::StringPiece& str); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 89 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 90 | /** |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 91 | * Returns an ID if it the string represented a valid ID. |
| 92 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 93 | Maybe<ResourceId> ParseResourceId(const android::StringPiece& str); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 96 | * Parses an SDK version, which can be an integer, or a letter from A-Z. |
| 97 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 98 | Maybe<int> ParseSdkVersion(const android::StringPiece& str); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | * Returns a Reference, or None Maybe instance if the string `str` was parsed as |
| 102 | * a |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | * valid reference to a style. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | * The format for a style parent is slightly more flexible than a normal |
| 105 | * reference: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | * |
| 107 | * @[package:]style/<entry> or |
| 108 | * ?[package:]style/<entry> or |
| 109 | * <package>:[style/]<entry> |
| 110 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 111 | Maybe<Reference> ParseStyleParentReference(const android::StringPiece& str, std::string* out_error); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | * Returns a Reference if the string `str` was parsed as a valid XML attribute |
| 115 | * name. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 116 | * The valid format for an XML attribute name is: |
| 117 | * |
| 118 | * package:entry |
| 119 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 120 | Maybe<Reference> ParseXmlAttributeName(const android::StringPiece& str); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | * 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | * the '+' was present in the string. |
| 128 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 129 | std::unique_ptr<Reference> TryParseReference(const android::StringPiece& str, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | bool* out_create = nullptr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | * Returns a BinaryPrimitve object representing @null or @empty if the string |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | * was parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 136 | std::unique_ptr<BinaryPrimitive> TryParseNullOrEmpty(const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Returns a BinaryPrimitve object representing a color if the string was parsed |
| 140 | * as one. |
| 141 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 142 | std::unique_ptr<BinaryPrimitive> TryParseColor(const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | * Returns a BinaryPrimitve object representing a boolean if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 148 | std::unique_ptr<BinaryPrimitive> TryParseBool(const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame^] | 150 | // Returns a boolean BinaryPrimitive. |
| 151 | std::unique_ptr<BinaryPrimitive> MakeBool(bool val); |
| 152 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | * Returns a BinaryPrimitve object representing an integer if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 157 | std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * Returns a BinaryPrimitve object representing a floating point number |
| 161 | * (float, dimension, etc) if the string was parsed as one. |
| 162 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 163 | std::unique_ptr<BinaryPrimitive> TryParseFloat(const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | * Returns a BinaryPrimitve object representing an enum symbol if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 170 | const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | |
| 172 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | * Returns a BinaryPrimitve object representing a flag symbol if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 175 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 177 | const android::StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 178 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | * Try to convert a string to an Item for the given attribute. The attribute |
| 180 | * will |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | * restrict what values the string can be converted to. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | * The callback function on_create_reference is called when the parsed item is a |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | * reference to an ID that must be created (@+id/foo). |
| 184 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | std::unique_ptr<Item> TryParseItemForAttribute( |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 186 | const android::StringPiece& value, const Attribute* attr, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | const std::function<void(const ResourceName&)>& on_create_reference = {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | std::unique_ptr<Item> TryParseItemForAttribute( |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 190 | const android::StringPiece& value, uint32_t type_mask, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | const std::function<void(const ResourceName&)>& on_create_reference = {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 192 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | uint32_t AndroidTypeToAttributeTypeMask(uint16_t type); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 195 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | * Returns a string path suitable for use within an APK. The path will look |
| 197 | * like: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 198 | * |
| 199 | * res/type[-config]/<name>.<ext> |
| 200 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | * Then name may be mangled if a NameMangler is supplied (can be nullptr) and |
| 202 | * the package |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 203 | * requires mangling. |
| 204 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | std::string BuildResourceFileName(const ResourceFile& res_file, |
| 206 | const NameMangler* mangler = nullptr); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 207 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 208 | // Parses the binary form of a resource value. `type` is used as a hint to know when a value is |
| 209 | // an ID versus a False boolean value, etc. `config` is for sorting strings in the string pool. |
| 210 | std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const ConfigDescription& config, |
| 211 | const android::ResStringPool& src_pool, |
| 212 | const android::Res_value& res_value, |
| 213 | StringPool* dst_pool); |
| 214 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | } // namespace ResourceUtils |
| 216 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 217 | |
| 218 | #endif /* AAPT_RESOURCEUTILS_H */ |