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