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 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 23 | #include "NameMangler.h" |
| 24 | #include "Resource.h" |
| 25 | #include "ResourceValues.h" |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 26 | #include "androidfw/AssetManager2.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 27 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 28 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | #include "androidfw/StringPiece.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 30 | #include "androidfw/StringPool.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | namespace ResourceUtils { |
| 34 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 35 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | * Returns true if the string was parsed as a resource name |
| 37 | * ([*][package:]type/name), with |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | * `out_resource` set to the parsed resource name and `out_private` set to true |
| 39 | * if a '*' prefix was present. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 40 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 41 | bool ParseResourceName(android::StringPiece str, ResourceNameRef* out_resource, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | bool* out_private = nullptr); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | * Returns true if the string was parsed as a reference |
| 46 | * (@[+][package:]type/name), with |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | * `out_reference` set to the parsed reference. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | * |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | * If '+' was present in the reference, `out_create` is set to true. |
| 50 | * If '*' was present in the reference, `out_private` is set to true. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 52 | bool ParseReference(android::StringPiece str, ResourceNameRef* out_reference, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | bool* out_create = nullptr, bool* out_private = nullptr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | * Returns true if the string is in the form of a resource reference |
| 57 | * (@[+][package:]type/name). |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 58 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 59 | bool IsReference(android::StringPiece str); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 60 | |
| 61 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | * Returns true if the string was parsed as an attribute reference |
| 63 | * (?[package:][type/]name), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | * with `out_reference` set to the parsed reference. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 66 | bool ParseAttributeReference(android::StringPiece str, ResourceNameRef* out_reference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 68 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | * Returns true if the string is in the form of an attribute |
| 70 | * reference(?[package:][type/]name). |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 71 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 72 | bool IsAttributeReference(android::StringPiece str); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 75 | * Convert an android::ResTable::resource_name to an aapt::ResourceName struct. |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 76 | */ |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 77 | std::optional<ResourceName> ToResourceName(const android::ResTable::resource_name& name); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 80 | * Convert an android::AssetManager2::ResourceName to an aapt::ResourceName struct. |
| 81 | */ |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 82 | std::optional<ResourceName> ToResourceName(const android::AssetManager2::ResourceName& name_in); |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | * Returns a boolean value if the string is equal to TRUE, true, True, FALSE, |
| 86 | * false, or False. |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 87 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 88 | std::optional<bool> ParseBool(android::StringPiece str); |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Returns a uint32_t if the string is an integer. |
| 92 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 93 | std::optional<uint32_t> ParseInt(android::StringPiece str); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 95 | /** |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 96 | * Returns an ID if it the string represented a valid ID. |
| 97 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 98 | std::optional<ResourceId> ParseResourceId(android::StringPiece str); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 101 | * Parses an SDK version, which can be an integer, or a letter from A-Z. |
| 102 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 103 | std::optional<int> ParseSdkVersion(android::StringPiece str); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | * Returns a Reference, or None Maybe instance if the string `str` was parsed as |
| 107 | * a |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 108 | * valid reference to a style. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | * The format for a style parent is slightly more flexible than a normal |
| 110 | * reference: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | * |
| 112 | * @[package:]style/<entry> or |
| 113 | * ?[package:]style/<entry> or |
| 114 | * <package>:[style/]<entry> |
| 115 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 116 | std::optional<Reference> ParseStyleParentReference(android::StringPiece str, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 117 | std::string* out_error); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | * Returns a Reference if the string `str` was parsed as a valid XML attribute |
| 121 | * name. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 122 | * The valid format for an XML attribute name is: |
| 123 | * |
| 124 | * package:entry |
| 125 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 126 | std::optional<Reference> ParseXmlAttributeName(android::StringPiece str); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 127 | |
| 128 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 129 | * Returns a Reference object if the string was parsed as a resource or |
| 130 | * attribute reference, |
| 131 | * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true |
| 132 | * if |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 133 | * the '+' was present in the string. |
| 134 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 135 | std::unique_ptr<Reference> TryParseReference(android::StringPiece str, bool* out_create = nullptr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 136 | |
| 137 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 138 | * Returns a BinaryPrimitve object representing @null or @empty if the string |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | * was parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 141 | std::unique_ptr<Item> TryParseNullOrEmpty(android::StringPiece str); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 142 | |
| 143 | // Returns a Reference representing @null. |
| 144 | // Due to runtime compatibility issues, this is encoded as a reference with ID 0. |
| 145 | // The runtime will convert this to TYPE_NULL. |
| 146 | std::unique_ptr<Reference> MakeNull(); |
| 147 | |
| 148 | // Returns a BinaryPrimitive representing @empty. This is encoded as a Res_value with |
| 149 | // type Res_value::TYPE_NULL and data Res_value::DATA_NULL_EMPTY. |
| 150 | std::unique_ptr<BinaryPrimitive> MakeEmpty(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Returns a BinaryPrimitve object representing a color if the string was parsed |
| 154 | * as one. |
| 155 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 156 | std::unique_ptr<BinaryPrimitive> TryParseColor(android::StringPiece str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | |
| 158 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | * Returns a BinaryPrimitve object representing a boolean if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 161 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 162 | std::unique_ptr<BinaryPrimitive> TryParseBool(android::StringPiece str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 163 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 164 | // Returns a boolean BinaryPrimitive. |
| 165 | std::unique_ptr<BinaryPrimitive> MakeBool(bool val); |
| 166 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 167 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | * Returns a BinaryPrimitve object representing an integer if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 170 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 171 | std::unique_ptr<BinaryPrimitive> TryParseInt(android::StringPiece str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | |
Shane Farmer | d05b913 | 2018-02-14 15:40:35 -0800 | [diff] [blame] | 173 | // Returns an integer BinaryPrimitive. |
| 174 | std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t value); |
| 175 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 176 | /* |
| 177 | * Returns a BinaryPrimitve object representing a floating point number |
| 178 | * (float, dimension, etc) if the string was parsed as one. |
| 179 | */ |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 180 | std::unique_ptr<BinaryPrimitive> TryParseFloat(android::StringPiece str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | |
| 182 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 183 | * Returns a BinaryPrimitve object representing an enum symbol if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | * parsed as one. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 187 | android::StringPiece str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | * Returns a BinaryPrimitve object representing a flag symbol if the string was |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | * parsed as one. |
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 | std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 194 | android::StringPiece str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 195 | /* |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | * Try to convert a string to an Item for the given attribute. The attribute |
| 197 | * will |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | * restrict what values the string can be converted to. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | * 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] | 200 | * reference to an ID that must be created (@+id/foo). |
| 201 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | std::unique_ptr<Item> TryParseItemForAttribute( |
Brandon Liu | 48d229d | 2023-05-04 23:54:03 +0000 | [diff] [blame] | 203 | android::IDiagnostics* diag, android::StringPiece value, const Attribute* attr, |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 204 | const std::function<bool(const ResourceName&)>& on_create_reference = {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | std::unique_ptr<Item> TryParseItemForAttribute( |
Brandon Liu | 48d229d | 2023-05-04 23:54:03 +0000 | [diff] [blame] | 207 | android::IDiagnostics* diag, android::StringPiece value, uint32_t type_mask, |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 208 | const std::function<bool(const ResourceName&)>& on_create_reference = {}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | uint32_t AndroidTypeToAttributeTypeMask(uint16_t type); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 212 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | * Returns a string path suitable for use within an APK. The path will look |
| 214 | * like: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 215 | * |
| 216 | * res/type[-config]/<name>.<ext> |
| 217 | * |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | * Then name may be mangled if a NameMangler is supplied (can be nullptr) and |
| 219 | * the package |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 220 | * requires mangling. |
| 221 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | std::string BuildResourceFileName(const ResourceFile& res_file, |
| 223 | const NameMangler* mangler = nullptr); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 224 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 225 | // Parses the binary form of a resource value. `type` is used as a hint to know when a value is |
| 226 | // an ID versus a False boolean value, etc. `config` is for sorting strings in the string pool. |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 227 | std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, |
| 228 | const android::ConfigDescription& config, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 229 | const android::ResStringPool& src_pool, |
| 230 | const android::Res_value& res_value, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 231 | android::StringPool* dst_pool); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 232 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 233 | // A string flattened from an XML hierarchy, which maintains tags and untranslatable sections |
| 234 | // in parallel data structures. |
| 235 | struct FlattenedXmlString { |
| 236 | std::string text; |
| 237 | std::vector<UntranslatableSection> untranslatable_sections; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 238 | std::vector<android::Span> spans; |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | // Flattens an XML hierarchy into a FlattenedXmlString, formatting the text, escaping characters, |
| 242 | // and removing whitespace, all while keeping the untranslatable sections and spans in sync with the |
| 243 | // transformations. |
| 244 | // |
| 245 | // Specifically, the StringBuilder will handle escaped characters like \t, \n, \\, \', etc. |
| 246 | // Single quotes *must* be escaped, unless within a pair of double-quotes. |
| 247 | // Pairs of double-quotes disable whitespace stripping of the enclosed text. |
| 248 | // Unicode escape codes (\u0049) are interpreted and the represented Unicode character is inserted. |
| 249 | // |
| 250 | // A NOTE ON WHITESPACE: |
| 251 | // |
| 252 | // When preserve_spaces is false, and when text is not enclosed within double-quotes, |
| 253 | // StringBuilder replaces a series of whitespace with a single space character. This happens at the |
| 254 | // start and end of the string as well, so leading and trailing whitespace is possible. |
| 255 | // |
| 256 | // When a Span is started or stopped, the whitespace counter is reset, meaning if whitespace |
| 257 | // is encountered directly after the span, it will be emitted. This leads to situations like the |
| 258 | // following: "This <b> is </b> spaced" -> "This is spaced". Without spans, this would be properly |
| 259 | // compressed: "This is spaced" -> "This is spaced". |
| 260 | // |
| 261 | // Untranslatable sections do not have the same problem: |
| 262 | // "This <xliff:g> is </xliff:g> not spaced" -> "This is not spaced". |
| 263 | // |
| 264 | // NOTE: This is all the way it is because AAPT1 did it this way. Maintaining backwards |
| 265 | // compatibility is important. |
| 266 | // |
| 267 | class StringBuilder { |
| 268 | public: |
| 269 | using SpanHandle = size_t; |
| 270 | using UntranslatableHandle = size_t; |
| 271 | |
| 272 | // Creates a StringBuilder. If preserve_spaces is true, whitespace removal is not performed, and |
| 273 | // single quotations can be used without escaping them. |
| 274 | explicit StringBuilder(bool preserve_spaces = false); |
| 275 | |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 276 | // Appends a chunk of text. |
| 277 | StringBuilder& AppendText(const std::string& text); |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 278 | |
| 279 | // Starts a Span (tag) with the given name. The name is expected to be of the form: |
| 280 | // "tag_name;attr1=value;attr2=value;" |
| 281 | // Which is how Spans are encoded in the ResStringPool. |
| 282 | // To end the span, pass back the SpanHandle received from this method to the EndSpan() method. |
| 283 | SpanHandle StartSpan(const std::string& name); |
| 284 | |
| 285 | // Ends a Span (tag). Pass in the matching SpanHandle previously obtained from StartSpan(). |
| 286 | void EndSpan(SpanHandle handle); |
| 287 | |
| 288 | // Starts an Untranslatable section. |
| 289 | // To end the section, pass back the UntranslatableHandle received from this method to |
| 290 | // the EndUntranslatable() method. |
| 291 | UntranslatableHandle StartUntranslatable(); |
| 292 | |
| 293 | // Ends an Untranslatable section. Pass in the matching UntranslatableHandle previously obtained |
| 294 | // from StartUntranslatable(). |
| 295 | void EndUntranslatable(UntranslatableHandle handle); |
| 296 | |
| 297 | // Returns the flattened XML string, with all spans and untranslatable sections encoded as |
| 298 | // parallel data structures. |
| 299 | FlattenedXmlString GetFlattenedString() const; |
| 300 | |
| 301 | // Returns just the flattened XML text, with no spans or untranslatable sections. |
| 302 | std::string to_string() const; |
| 303 | |
| 304 | // Returns true if there was no error. |
| 305 | explicit operator bool() const; |
| 306 | |
| 307 | std::string GetError() const; |
| 308 | |
| 309 | private: |
| 310 | DISALLOW_COPY_AND_ASSIGN(StringBuilder); |
| 311 | |
| 312 | void ResetTextState(); |
| 313 | |
| 314 | std::string error_; |
| 315 | FlattenedXmlString xml_string_; |
| 316 | uint32_t utf16_len_ = 0u; |
| 317 | bool preserve_spaces_; |
| 318 | bool quote_; |
| 319 | bool last_codepoint_was_space_ = false; |
| 320 | }; |
| 321 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 322 | } // namespace ResourceUtils |
| 323 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 324 | |
| 325 | #endif /* AAPT_RESOURCEUTILS_H */ |