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 | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 40 | bool extractResourceName(const StringPiece16& str, StringPiece16* outPackage, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | StringPiece16* outType, StringPiece16* outEntry); |
| 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 | */ |
| 48 | bool parseResourceName(const StringPiece16& str, ResourceNameRef* outResource, bool* outPrivate); |
| 49 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Returns true if the string was parsed as a reference (@[+][package:]type/name), with |
| 52 | * `outReference` set to the parsed reference. |
| 53 | * |
| 54 | * If '+' was present in the reference, `outCreate` is set to true. |
| 55 | * If '*' was present in the reference, `outPrivate` is set to true. |
| 56 | */ |
| 57 | bool tryParseReference(const StringPiece16& str, ResourceNameRef* outReference, |
| 58 | bool* outCreate = nullptr, bool* outPrivate = nullptr); |
| 59 | |
| 60 | /* |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 61 | * Returns true if the string is in the form of a resource reference (@[+][package:]type/name). |
| 62 | */ |
| 63 | bool isReference(const StringPiece16& str); |
| 64 | |
| 65 | /* |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 66 | * 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] | 67 | * with `outReference` set to the parsed reference. |
| 68 | */ |
| 69 | bool tryParseAttributeReference(const StringPiece16& str, ResourceNameRef* outReference); |
| 70 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 71 | /** |
Adam Lesinski | 7298bc9c | 2015-11-16 12:31:52 -0800 | [diff] [blame] | 72 | * Returns true if the string is in the form of an attribute reference(?[package:][type/]name). |
| 73 | */ |
| 74 | bool isAttributeReference(const StringPiece16& str); |
| 75 | |
| 76 | /** |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 77 | * Returns true if the value is a boolean, putting the result in `outValue`. |
| 78 | */ |
| 79 | bool tryParseBool(const StringPiece16& str, bool* outValue); |
| 80 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Returns a Reference, or None Maybe instance if the string `str` was parsed as a |
| 83 | * valid reference to a style. |
| 84 | * The format for a style parent is slightly more flexible than a normal reference: |
| 85 | * |
| 86 | * @[package:]style/<entry> or |
| 87 | * ?[package:]style/<entry> or |
| 88 | * <package>:[style/]<entry> |
| 89 | */ |
| 90 | Maybe<Reference> parseStyleParentReference(const StringPiece16& str, std::string* outError); |
| 91 | |
| 92 | /* |
| 93 | * Returns a Reference object if the string was parsed as a resource or attribute reference, |
| 94 | * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true if |
| 95 | * the '+' was present in the string. |
| 96 | */ |
| 97 | std::unique_ptr<Reference> tryParseReference(const StringPiece16& str, bool* outCreate = nullptr); |
| 98 | |
| 99 | /* |
| 100 | * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed |
| 101 | * as one. |
| 102 | */ |
| 103 | std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str); |
| 104 | |
| 105 | /* |
| 106 | * Returns a BinaryPrimitve object representing a color if the string was parsed |
| 107 | * as one. |
| 108 | */ |
| 109 | std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str); |
| 110 | |
| 111 | /* |
| 112 | * Returns a BinaryPrimitve object representing a boolean if the string was parsed |
| 113 | * as one. |
| 114 | */ |
| 115 | std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str); |
| 116 | |
| 117 | /* |
| 118 | * Returns a BinaryPrimitve object representing an integer if the string was parsed |
| 119 | * as one. |
| 120 | */ |
| 121 | std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str); |
| 122 | |
| 123 | /* |
| 124 | * Returns a BinaryPrimitve object representing a floating point number |
| 125 | * (float, dimension, etc) if the string was parsed as one. |
| 126 | */ |
| 127 | std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str); |
| 128 | |
| 129 | /* |
| 130 | * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed |
| 131 | * as one. |
| 132 | */ |
| 133 | std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr, |
| 134 | const StringPiece16& str); |
| 135 | |
| 136 | /* |
| 137 | * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed |
| 138 | * as one. |
| 139 | */ |
| 140 | std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr, |
| 141 | const StringPiece16& str); |
| 142 | /* |
| 143 | * Try to convert a string to an Item for the given attribute. The attribute will |
| 144 | * restrict what values the string can be converted to. |
| 145 | * The callback function onCreateReference is called when the parsed item is a |
| 146 | * reference to an ID that must be created (@+id/foo). |
| 147 | */ |
| 148 | std::unique_ptr<Item> parseItemForAttribute( |
| 149 | const StringPiece16& value, const Attribute* attr, |
| 150 | std::function<void(const ResourceName&)> onCreateReference = {}); |
| 151 | |
| 152 | std::unique_ptr<Item> parseItemForAttribute( |
| 153 | const StringPiece16& value, uint32_t typeMask, |
| 154 | std::function<void(const ResourceName&)> onCreateReference = {}); |
| 155 | |
| 156 | uint32_t androidTypeToAttributeTypeMask(uint16_t type); |
| 157 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 158 | /** |
| 159 | * Returns a string path suitable for use within an APK. The path will look like: |
| 160 | * |
| 161 | * res/type[-config]/<name>.<ext> |
| 162 | * |
| 163 | * Then name may be mangled if a NameMangler is supplied (can be nullptr) and the package |
| 164 | * requires mangling. |
| 165 | */ |
| 166 | std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler); |
| 167 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | } // namespace ResourceUtils |
| 169 | } // namespace aapt |
| 170 | |
| 171 | #endif /* AAPT_RESOURCEUTILS_H */ |