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