blob: a57d89dd02333fa49411d0c3e967bea639195f4e [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
Adam Lesinskid0f116b2016-07-08 15:00:32 -070031/**
32 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
33 */
34Maybe<ResourceName> toResourceName(const android::ResTable::resource_name& name);
35
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036/*
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 Lesinski7298bc9c2015-11-16 12:31:52 -080043 * Returns false if there was no package but a ':' was present.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070045bool extractResourceName(const StringPiece& str, StringPiece* outPackage,
46 StringPiece* outType, StringPiece* outEntry);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinski467f1712015-11-16 17:35:44 -080048/**
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 Lesinskid0f116b2016-07-08 15:00:32 -070053bool parseResourceName(const StringPiece& str, ResourceNameRef* outResource,
Adam Lesinski59e04c62016-02-04 15:59:23 -080054 bool* outPrivate = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080055
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056/*
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 Lesinskid0f116b2016-07-08 15:00:32 -070063bool tryParseReference(const StringPiece& str, ResourceNameRef* outReference,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 bool* outCreate = nullptr, bool* outPrivate = nullptr);
65
66/*
Adam Lesinski2ae4a872015-11-02 16:10:55 -080067 * Returns true if the string is in the form of a resource reference (@[+][package:]type/name).
68 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070069bool isReference(const StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080070
71/*
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080072 * Returns true if the string was parsed as an attribute reference (?[package:][type/]name),
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 * with `outReference` set to the parsed reference.
74 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070075bool tryParseAttributeReference(const StringPiece& str, ResourceNameRef* outReference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskib23f1e02015-11-03 12:24:17 -080077/**
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080078 * Returns true if the string is in the form of an attribute reference(?[package:][type/]name).
79 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070080bool isAttributeReference(const StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080081
82/**
Adam Lesinskib23f1e02015-11-03 12:24:17 -080083 * Returns true if the value is a boolean, putting the result in `outValue`.
84 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070085bool tryParseBool(const StringPiece& str, bool* outValue);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080086
Adam Lesinskifb6312f2016-06-28 14:40:32 -070087/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070088 * Returns an ID if it the string represented a valid ID.
89 */
90Maybe<ResourceId> tryParseResourceId(const StringPiece& str);
91
92/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -070093 * Parses an SDK version, which can be an integer, or a letter from A-Z.
94 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070095Maybe<int> tryParseSdkVersion(const StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070096
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097/*
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 Lesinskid0f116b2016-07-08 15:00:32 -0700106Maybe<Reference> parseStyleParentReference(const StringPiece& str, std::string* outError);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107
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 Lesinskid0f116b2016-07-08 15:00:32 -0700113std::unique_ptr<Reference> tryParseReference(const StringPiece& str, bool* outCreate = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114
115/*
116 * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed
117 * as one.
118 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700119std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120
121/*
122 * Returns a BinaryPrimitve object representing a color if the string was parsed
123 * as one.
124 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700125std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126
127/*
128 * Returns a BinaryPrimitve object representing a boolean if the string was parsed
129 * as one.
130 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700131std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132
133/*
134 * Returns a BinaryPrimitve object representing an integer if the string was parsed
135 * as one.
136 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700137std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138
139/*
140 * Returns a BinaryPrimitve object representing a floating point number
141 * (float, dimension, etc) if the string was parsed as one.
142 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700143std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144
145/*
146 * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed
147 * as one.
148 */
149std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700150 const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151
152/*
153 * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed
154 * as one.
155 */
156std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700157 const StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158/*
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 */
164std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700165 const StringPiece& value, const Attribute* attr,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700166 const std::function<void(const ResourceName&)>& onCreateReference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
168std::unique_ptr<Item> parseItemForAttribute(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700169 const StringPiece& value, uint32_t typeMask,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700170 const std::function<void(const ResourceName&)>& onCreateReference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
172uint32_t androidTypeToAttributeTypeMask(uint16_t type);
173
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800174/**
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 */
182std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler);
183
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184} // namespace ResourceUtils
185} // namespace aapt
186
187#endif /* AAPT_RESOURCEUTILS_H */