2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef AAPT_RESOURCEUTILS_H
18 #define AAPT_RESOURCEUTILS_H
20 #include "NameMangler.h"
22 #include "ResourceValues.h"
23 #include "util/StringPiece.h"
29 namespace ResourceUtils {
32 * Extracts the package, type, and name from a string of the format:
36 * where the package can be empty. Validation must be performed on each
37 * individual extracted piece to verify that the pieces are valid.
38 * Returns false if there was no package but a ':' was present.
40 bool extractResourceName(const StringPiece16& str, StringPiece16* outPackage,
41 StringPiece16* outType, StringPiece16* outEntry);
44 * Returns true if the string was parsed as a resource name ([*][package:]type/name), with
45 * `outResource` set to the parsed resource name and `outPrivate` set to true if a '*' prefix
48 bool parseResourceName(const StringPiece16& str, ResourceNameRef* outResource,
49 bool* outPrivate = nullptr);
52 * Returns true if the string was parsed as a reference (@[+][package:]type/name), with
53 * `outReference` set to the parsed reference.
55 * If '+' was present in the reference, `outCreate` is set to true.
56 * If '*' was present in the reference, `outPrivate` is set to true.
58 bool tryParseReference(const StringPiece16& str, ResourceNameRef* outReference,
59 bool* outCreate = nullptr, bool* outPrivate = nullptr);
62 * Returns true if the string is in the form of a resource reference (@[+][package:]type/name).
64 bool isReference(const StringPiece16& str);
67 * Returns true if the string was parsed as an attribute reference (?[package:][type/]name),
68 * with `outReference` set to the parsed reference.
70 bool tryParseAttributeReference(const StringPiece16& str, ResourceNameRef* outReference);
73 * Returns true if the string is in the form of an attribute reference(?[package:][type/]name).
75 bool isAttributeReference(const StringPiece16& str);
78 * Returns true if the value is a boolean, putting the result in `outValue`.
80 bool tryParseBool(const StringPiece16& str, bool* outValue);
83 * Returns a Reference, or None Maybe instance if the string `str` was parsed as a
84 * valid reference to a style.
85 * The format for a style parent is slightly more flexible than a normal reference:
87 * @[package:]style/<entry> or
88 * ?[package:]style/<entry> or
89 * <package>:[style/]<entry>
91 Maybe<Reference> parseStyleParentReference(const StringPiece16& str, std::string* outError);
94 * Returns a Reference object if the string was parsed as a resource or attribute reference,
95 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true if
96 * the '+' was present in the string.
98 std::unique_ptr<Reference> tryParseReference(const StringPiece16& str, bool* outCreate = nullptr);
101 * Returns a BinaryPrimitve object representing @null or @empty if the string was parsed
104 std::unique_ptr<BinaryPrimitive> tryParseNullOrEmpty(const StringPiece16& str);
107 * Returns a BinaryPrimitve object representing a color if the string was parsed
110 std::unique_ptr<BinaryPrimitive> tryParseColor(const StringPiece16& str);
113 * Returns a BinaryPrimitve object representing a boolean if the string was parsed
116 std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str);
119 * Returns a BinaryPrimitve object representing an integer if the string was parsed
122 std::unique_ptr<BinaryPrimitive> tryParseInt(const StringPiece16& str);
125 * Returns a BinaryPrimitve object representing a floating point number
126 * (float, dimension, etc) if the string was parsed as one.
128 std::unique_ptr<BinaryPrimitive> tryParseFloat(const StringPiece16& str);
131 * Returns a BinaryPrimitve object representing an enum symbol if the string was parsed
134 std::unique_ptr<BinaryPrimitive> tryParseEnumSymbol(const Attribute* enumAttr,
135 const StringPiece16& str);
138 * Returns a BinaryPrimitve object representing a flag symbol if the string was parsed
141 std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* enumAttr,
142 const StringPiece16& str);
144 * Try to convert a string to an Item for the given attribute. The attribute will
145 * restrict what values the string can be converted to.
146 * The callback function onCreateReference is called when the parsed item is a
147 * reference to an ID that must be created (@+id/foo).
149 std::unique_ptr<Item> parseItemForAttribute(
150 const StringPiece16& value, const Attribute* attr,
151 std::function<void(const ResourceName&)> onCreateReference = {});
153 std::unique_ptr<Item> parseItemForAttribute(
154 const StringPiece16& value, uint32_t typeMask,
155 std::function<void(const ResourceName&)> onCreateReference = {});
157 uint32_t androidTypeToAttributeTypeMask(uint16_t type);
160 * Returns a string path suitable for use within an APK. The path will look like:
162 * res/type[-config]/<name>.<ext>
164 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and the package
167 std::string buildResourceFileName(const ResourceFile& resFile, const NameMangler* mangler);
169 } // namespace ResourceUtils
172 #endif /* AAPT_RESOURCEUTILS_H */