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 | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 20 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "Resource.h" |
| 22 | #include "ResourceValues.h" |
| 23 | #include "util/StringPiece.h" |
| 24 | |
| 25 | #include <functional> |
| 26 | #include <memory> |
| 27 | |
| 28 | namespace aapt { |
| 29 | namespace ResourceUtils { |
| 30 | |
| 31 | /* |
| 32 | * Extracts the package, type, and name from a string of the format: |
| 33 | * |
| 34 | * [package:]type/name |
| 35 | * |
| 36 | * where the package can be empty. Validation must be performed on each |
| 37 | * individual extracted piece to verify that the pieces are valid. |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 38 | * Returns false if there was no package but a ':' was present. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 40 | bool extractResourceName(const StringPiece& str, StringPiece* outPackage, |
| 41 | StringPiece* outType, StringPiece* outEntry); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Returns true if the string was parsed as a resource name ([*][package:]type/name), with |
| 45 | * `outResource` set to the parsed resource name and `outPrivate` set to true if a '*' prefix |
| 46 | * was present. |
| 47 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 48 | bool parseResourceName(const StringPiece& str, ResourceNameRef* outResource, |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 49 | bool* outPrivate = nullptr); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Returns true if the string was parsed as a reference (@[+][package:]type/name), with |
| 53 | * `outReference` set to the parsed reference. |
| 54 | * |
| 55 | * If '+' was present in the reference, `outCreate` is set to true. |
| 56 | * If '*' was present in the reference, `outPrivate` is set to true. |
| 57 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 58 | bool parseReference(const StringPiece& str, ResourceNameRef* outReference, |
| 59 | bool* outCreate = nullptr, bool* outPrivate = nullptr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | |
| 61 | /* |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 62 | * Returns true if the string is in the form of a resource reference (@[+][package:]type/name). |
| 63 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 64 | bool isReference(const StringPiece& str); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 67 | * Returns true if the string was parsed as an attribute reference (?[package:][type/]name), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | * with `outReference` set to the parsed reference. |
| 69 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 70 | bool parseAttributeReference(const StringPiece& str, ResourceNameRef* outReference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 72 | /** |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 73 | * Returns true if the string is in the form of an attribute reference(?[package:][type/]name). |
| 74 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 75 | bool isAttributeReference(const StringPiece& str); |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 76 | |
| 77 | /** |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 78 | * Convert an android::ResTable::resource_name to an aapt::ResourceName struct. |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 79 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 80 | Maybe<ResourceName> toResourceName(const android::ResTable::resource_name& name); |
| 81 | |
| 82 | /** |
| 83 | * Returns a boolean value if the string is equal to TRUE, true, True, FALSE, false, or False. |
| 84 | */ |
| 85 | Maybe<bool> parseBool(const StringPiece& str); |
| 86 | |
| 87 | /** |
| 88 | * Returns a uint32_t if the string is an integer. |
| 89 | */ |
| 90 | Maybe<uint32_t> parseInt(const StringPiece& str); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 92 | /** |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 93 | * Returns an ID if it the string represented a valid ID. |
| 94 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 95 | Maybe<ResourceId> parseResourceId(const StringPiece& str); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 98 | * Parses an SDK version, which can be an integer, or a letter from A-Z. |
| 99 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 100 | Maybe<int> parseSdkVersion(const StringPiece& str); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Returns a Reference, or None Maybe instance if the string `str` was parsed as a |
| 104 | * valid reference to a style. |
| 105 | * The format for a style parent is slightly more flexible than a normal reference: |
| 106 | * |
| 107 | * @[package:]style/<entry> or |
| 108 | * ?[package:]style/<entry> or |
| 109 | * <package>:[style/]<entry> |
| 110 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 111 | Maybe<Reference> parseStyleParentReference(const StringPiece& str, std::string* outError); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Returns a Reference object if the string was parsed as a resource or attribute reference, |
| 115 | * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true if |
| 116 | * the '+' was present in the string. |
| 117 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 118 | std::unique_ptr<Reference> tryParseReference(const StringPiece& str, bool* outCreate = nullptr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed |
| 122 | * as one. |
| 123 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 124 | std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * Returns a BinaryPrimitve object representing a color if the string was parsed |
| 128 | * as one. |
| 129 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 130 | std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Returns a BinaryPrimitve object representing a boolean if the string was parsed |
| 134 | * as one. |
| 135 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 136 | std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Returns a BinaryPrimitve object representing an integer if the string was parsed |
| 140 | * as one. |
| 141 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 142 | std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Returns a BinaryPrimitve object representing a floating point number |
| 146 | * (float, dimension, etc) if the string was parsed as one. |
| 147 | */ |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 148 | std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed |
| 152 | * as one. |
| 153 | */ |
| 154 | std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 155 | const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed |
| 159 | * as one. |
| 160 | */ |
| 161 | std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr, |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 162 | const StringPiece& str); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 163 | /* |
| 164 | * Try to convert a string to an Item for the given attribute. The attribute will |
| 165 | * restrict what values the string can be converted to. |
| 166 | * The callback function onCreateReference is called when the parsed item is a |
| 167 | * reference to an ID that must be created (@+id/foo). |
| 168 | */ |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 169 | std::unique_ptr<Item> tryParseItemForAttribute( |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 170 | const StringPiece& value, const Attribute* attr, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | std::function<void(const ResourceName&)> onCreateReference = {}); |
| 172 | |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame^] | 173 | std::unique_ptr<Item> tryParseItemForAttribute( |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 174 | const StringPiece& value, uint32_t typeMask, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 175 | std::function<void(const ResourceName&)> onCreateReference = {}); |
| 176 | |
| 177 | uint32_t androidTypeToAttributeTypeMask(uint16_t type); |
| 178 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 179 | /** |
| 180 | * Returns a string path suitable for use within an APK. The path will look like: |
| 181 | * |
| 182 | * res/type[-config]/<name>.<ext> |
| 183 | * |
| 184 | * Then name may be mangled if a NameMangler is supplied (can be nullptr) and the package |
| 185 | * requires mangling. |
| 186 | */ |
| 187 | std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler); |
| 188 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 189 | } // namespace ResourceUtils |
| 190 | } // namespace aapt |
| 191 | |
| 192 | #endif /* AAPT_RESOURCEUTILS_H */ |