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