ResourceUtils.h revision 7298bc9c857541b444b2f1639dbed17599cbe5e9
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 "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.
37 * Returns false if there was no package but a ':' was present.
38 */
39bool extractResourceName(const StringPiece16& str, StringPiece16* outPackage,
40                         StringPiece16* outType, StringPiece16* outEntry);
41
42/*
43 * Returns true if the string was parsed as a reference (@[+][package:]type/name), with
44 * `outReference` set to the parsed reference.
45 *
46 * If '+' was present in the reference, `outCreate` is set to true.
47 * If '*' was present in the reference, `outPrivate` is set to true.
48 */
49bool tryParseReference(const StringPiece16& str, ResourceNameRef* outReference,
50                       bool* outCreate = nullptr, bool* outPrivate = nullptr);
51
52/*
53 * Returns true if the string is in the form of a resource reference (@[+][package:]type/name).
54 */
55bool isReference(const StringPiece16& str);
56
57/*
58 * Returns true if the string was parsed as an attribute reference (?[package:][type/]name),
59 * with `outReference` set to the parsed reference.
60 */
61bool tryParseAttributeReference(const StringPiece16& str, ResourceNameRef* outReference);
62
63/**
64 * Returns true if the string is in the form of an attribute reference(?[package:][type/]name).
65 */
66bool isAttributeReference(const StringPiece16& str);
67
68/**
69 * Returns true if the value is a boolean, putting the result in `outValue`.
70 */
71bool tryParseBool(const StringPiece16& str, bool* outValue);
72
73/*
74 * Returns a Reference, or None Maybe instance if the string `str` was parsed as a
75 * valid reference to a style.
76 * The format for a style parent is slightly more flexible than a normal reference:
77 *
78 * @[package:]style/<entry> or
79 * ?[package:]style/<entry> or
80 * <package>:[style/]<entry>
81 */
82Maybe<Reference> parseStyleParentReference(const StringPiece16& str, std::string* outError);
83
84/*
85 * Returns a Reference object if the string was parsed as a resource or attribute reference,
86 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true if
87 * the '+' was present in the string.
88 */
89std::unique_ptr<Reference> tryParseReference(const StringPiece16& str, bool* outCreate = nullptr);
90
91/*
92 * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed
93 * as one.
94 */
95std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str);
96
97/*
98 * Returns a BinaryPrimitve object representing a color if the string was parsed
99 * as one.
100 */
101std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str);
102
103/*
104 * Returns a BinaryPrimitve object representing a boolean if the string was parsed
105 * as one.
106 */
107std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str);
108
109/*
110 * Returns a BinaryPrimitve object representing an integer if the string was parsed
111 * as one.
112 */
113std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str);
114
115/*
116 * Returns a BinaryPrimitve object representing a floating point number
117 * (float, dimension, etc) if the string was parsed as one.
118 */
119std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str);
120
121/*
122 * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed
123 * as one.
124 */
125std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
126                                                    const StringPiece16& str);
127
128/*
129 * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed
130 * as one.
131 */
132std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr,
133                                                    const StringPiece16& str);
134/*
135 * Try to convert a string to an Item for the given attribute. The attribute will
136 * restrict what values the string can be converted to.
137 * The callback function onCreateReference is called when the parsed item is a
138 * reference to an ID that must be created (@+id/foo).
139 */
140std::unique_ptr<Item> parseItemForAttribute(
141        const StringPiece16& value, const Attribute* attr,
142        std::function<void(const ResourceName&)> onCreateReference = {});
143
144std::unique_ptr<Item> parseItemForAttribute(
145        const StringPiece16& value, uint32_t typeMask,
146        std::function<void(const ResourceName&)> onCreateReference = {});
147
148uint32_t androidTypeToAttributeTypeMask(uint16_t type);
149
150} // namespace ResourceUtils
151} // namespace aapt
152
153#endif /* AAPT_RESOURCEUTILS_H */
154