blob: cebe47cee3a7c010fcdffa51ad254d222a945a40 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskia6fe3452015-12-09 15:20:52 -080020#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "Resource.h"
22#include "ResourceValues.h"
23#include "util/StringPiece.h"
24
25#include <functional>
26#include <memory>
27
28namespace aapt {
29namespace 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 Lesinski7298bc9c2015-11-16 12:31:52 -080038 * Returns false if there was no package but a ':' was present.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070040bool extractResourceName(const StringPiece& str, StringPiece* outPackage,
41 StringPiece* outType, StringPiece* outEntry);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042
Adam Lesinski467f1712015-11-16 17:35:44 -080043/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 * Returns true if the string was parsed as a resource name
45 * ([*][package:]type/name), with
46 * `outResource` set to the parsed resource name and `outPrivate` set to true if
47 * a '*' prefix
Adam Lesinski467f1712015-11-16 17:35:44 -080048 * was present.
49 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070050bool parseResourceName(const StringPiece& str, ResourceNameRef* outResource,
Adam Lesinski59e04c62016-02-04 15:59:23 -080051 bool* outPrivate = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080052
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 * Returns true if the string was parsed as a reference
55 * (@[+][package:]type/name), with
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 * `outReference` set to the parsed reference.
57 *
58 * If '+' was present in the reference, `outCreate` is set to true.
59 * If '*' was present in the reference, `outPrivate` is set to true.
60 */
Adam Lesinski36c73a52016-08-11 13:39:24 -070061bool parseReference(const StringPiece& str, ResourceNameRef* outReference,
62 bool* outCreate = nullptr, bool* outPrivate = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
64/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 * Returns true if the string is in the form of a resource reference
66 * (@[+][package:]type/name).
Adam Lesinski2ae4a872015-11-02 16:10:55 -080067 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070068bool isReference(const StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080069
70/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 * Returns true if the string was parsed as an attribute reference
72 * (?[package:][type/]name),
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 * with `outReference` set to the parsed reference.
74 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075bool parseAttributeReference(const StringPiece& str,
76 ResourceNameRef* outReference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskib23f1e02015-11-03 12:24:17 -080078/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 * Returns true if the string is in the form of an attribute
80 * reference(?[package:][type/]name).
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080081 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070082bool isAttributeReference(const StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080083
84/**
Adam Lesinski36c73a52016-08-11 13:39:24 -070085 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
Adam Lesinskib23f1e02015-11-03 12:24:17 -080086 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087Maybe<ResourceName> toResourceName(
88 const android::ResTable::resource_name& name);
Adam Lesinski36c73a52016-08-11 13:39:24 -070089
90/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 * Returns a boolean value if the string is equal to TRUE, true, True, FALSE,
92 * false, or False.
Adam Lesinski36c73a52016-08-11 13:39:24 -070093 */
94Maybe<bool> parseBool(const StringPiece& str);
95
96/**
97 * Returns a uint32_t if the string is an integer.
98 */
99Maybe<uint32_t> parseInt(const StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -0800100
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700101/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700102 * Returns an ID if it the string represented a valid ID.
103 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700104Maybe<ResourceId> parseResourceId(const StringPiece& str);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700105
106/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700107 * Parses an SDK version, which can be an integer, or a letter from A-Z.
108 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700109Maybe<int> parseSdkVersion(const StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700110
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 * Returns a Reference, or None Maybe instance if the string `str` was parsed as
113 * a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114 * valid reference to a style.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 * The format for a style parent is slightly more flexible than a normal
116 * reference:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 *
118 * @[package:]style/<entry> or
119 * ?[package:]style/<entry> or
120 * <package>:[style/]<entry>
121 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122Maybe<Reference> parseStyleParentReference(const StringPiece& str,
123 std::string* outError);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
125/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 * Returns a Reference if the string `str` was parsed as a valid XML attribute
127 * name.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700128 * The valid format for an XML attribute name is:
129 *
130 * package:entry
131 */
132Maybe<Reference> parseXmlAttributeName(const StringPiece& str);
133
134/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 * Returns a Reference object if the string was parsed as a resource or
136 * attribute reference,
137 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true
138 * if
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 * the '+' was present in the string.
140 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141std::unique_ptr<Reference> tryParseReference(const StringPiece& str,
142 bool* outCreate = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
144/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 * Returns a BinaryPrimitve object representing @null or @empty if the string
146 * was parsed
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147 * as one.
148 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700149std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150
151/*
152 * Returns a BinaryPrimitve object representing a color if the string was parsed
153 * as one.
154 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700155std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
157/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 * Returns a BinaryPrimitve object representing a boolean if the string was
159 * parsed
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160 * as one.
161 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700162std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163
164/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 * Returns a BinaryPrimitve object representing an integer if the string was
166 * parsed
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167 * as one.
168 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700169std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
171/*
172 * Returns a BinaryPrimitve object representing a floating point number
173 * (float, dimension, etc) if the string was parsed as one.
174 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700175std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700176
177/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 * Returns a BinaryPrimitve object representing an enum symbol if the string was
179 * parsed
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180 * as one.
181 */
182std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700183 const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184
185/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 * Returns a BinaryPrimitve object representing a flag symbol if the string was
187 * parsed
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 * as one.
189 */
190std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700191 const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 * Try to convert a string to an Item for the given attribute. The attribute
194 * will
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195 * restrict what values the string can be converted to.
196 * The callback function onCreateReference is called when the parsed item is a
197 * reference to an ID that must be created (@+id/foo).
198 */
Adam Lesinski36c73a52016-08-11 13:39:24 -0700199std::unique_ptr<Item> tryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 const StringPiece& value, const Attribute* attr,
201 const std::function<void(const ResourceName&)>& onCreateReference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700202
Adam Lesinski36c73a52016-08-11 13:39:24 -0700203std::unique_ptr<Item> tryParseItemForAttribute(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 const StringPiece& value, uint32_t typeMask,
205 const std::function<void(const ResourceName&)>& onCreateReference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206
207uint32_t androidTypeToAttributeTypeMask(uint16_t type);
208
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800209/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 * Returns a string path suitable for use within an APK. The path will look
211 * like:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800212 *
213 * res/type[-config]/<name>.<ext>
214 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and
216 * the package
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800217 * requires mangling.
218 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219std::string buildResourceFileName(const ResourceFile& resFile,
220 const NameMangler* mangler);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222} // namespace ResourceUtils
223} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224
225#endif /* AAPT_RESOURCEUTILS_H */