blob: f93a4c762706b6bff009ead713b08e840739469c [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
20#include "Resource.h"
21#include "ResourceValues.h"
22#include "util/StringPiece.h"
23
24#include <functional>
25#include <memory>
26
27namespace aapt {
28namespace ResourceUtils {
29
30/*
31 * Extracts the package, type, and name from a string of the format:
32 *
33 * [package:]type/name
34 *
35 * where the package can be empty. Validation must be performed on each
36 * individual extracted piece to verify that the pieces are valid.
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080037 * Returns false if there was no package but a ':' was present.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038 */
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080039bool extractResourceName(const StringPiece16& str, StringPiece16* outPackage,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040 StringPiece16* outType, StringPiece16* outEntry);
41
Adam Lesinski467f1712015-11-16 17:35:44 -080042/**
43 * Returns true if the string was parsed as a resource name ([*][package:]type/name), with
44 * `outResource` set to the parsed resource name and `outPrivate` set to true if a '*' prefix
45 * was present.
46 */
47bool parseResourceName(const StringPiece16& str, ResourceNameRef* outResource, bool* outPrivate);
48
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049/*
50 * Returns true if the string was parsed as a reference (@[+][package:]type/name), with
51 * `outReference` set to the parsed reference.
52 *
53 * If '+' was present in the reference, `outCreate` is set to true.
54 * If '*' was present in the reference, `outPrivate` is set to true.
55 */
56bool tryParseReference(const StringPiece16& str, ResourceNameRef* outReference,
57 bool* outCreate = nullptr, bool* outPrivate = nullptr);
58
59/*
Adam Lesinski2ae4a872015-11-02 16:10:55 -080060 * Returns true if the string is in the form of a resource reference (@[+][package:]type/name).
61 */
62bool isReference(const StringPiece16& str);
63
64/*
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080065 * Returns true if the string was parsed as an attribute reference (?[package:][type/]name),
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066 * with `outReference` set to the parsed reference.
67 */
68bool tryParseAttributeReference(const StringPiece16& str, ResourceNameRef* outReference);
69
Adam Lesinskib23f1e02015-11-03 12:24:17 -080070/**
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080071 * Returns true if the string is in the form of an attribute reference(?[package:][type/]name).
72 */
73bool isAttributeReference(const StringPiece16& str);
74
75/**
Adam Lesinskib23f1e02015-11-03 12:24:17 -080076 * Returns true if the value is a boolean, putting the result in `outValue`.
77 */
78bool tryParseBool(const StringPiece16& str, bool* outValue);
79
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080/*
81 * Returns a Reference, or None Maybe instance if the string `str` was parsed as a
82 * valid reference to a style.
83 * The format for a style parent is slightly more flexible than a normal reference:
84 *
85 * @[package:]style/<entry> or
86 * ?[package:]style/<entry> or
87 * <package>:[style/]<entry>
88 */
89Maybe<Reference> parseStyleParentReference(const StringPiece16& str, std::string* outError);
90
91/*
92 * Returns a Reference object if the string was parsed as a resource or attribute reference,
93 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true if
94 * the '+' was present in the string.
95 */
96std::unique_ptr<Reference> tryParseReference(const StringPiece16& str, bool* outCreate = nullptr);
97
98/*
99 * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed
100 * as one.
101 */
102std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str);
103
104/*
105 * Returns a BinaryPrimitve object representing a color if the string was parsed
106 * as one.
107 */
108std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str);
109
110/*
111 * Returns a BinaryPrimitve object representing a boolean if the string was parsed
112 * as one.
113 */
114std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str);
115
116/*
117 * Returns a BinaryPrimitve object representing an integer if the string was parsed
118 * as one.
119 */
120std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str);
121
122/*
123 * Returns a BinaryPrimitve object representing a floating point number
124 * (float, dimension, etc) if the string was parsed as one.
125 */
126std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str);
127
128/*
129 * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed
130 * as one.
131 */
132std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
133 const StringPiece16& str);
134
135/*
136 * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed
137 * as one.
138 */
139std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr,
140 const StringPiece16& str);
141/*
142 * Try to convert a string to an Item for the given attribute. The attribute will
143 * restrict what values the string can be converted to.
144 * The callback function onCreateReference is called when the parsed item is a
145 * reference to an ID that must be created (@+id/foo).
146 */
147std::unique_ptr<Item> parseItemForAttribute(
148 const StringPiece16& value, const Attribute* attr,
149 std::function<void(const ResourceName&)> onCreateReference = {});
150
151std::unique_ptr<Item> parseItemForAttribute(
152 const StringPiece16& value, uint32_t typeMask,
153 std::function<void(const ResourceName&)> onCreateReference = {});
154
155uint32_t androidTypeToAttributeTypeMask(uint16_t type);
156
157} // namespace ResourceUtils
158} // namespace aapt
159
160#endif /* AAPT_RESOURCEUTILS_H */