blob: f77766ee906123fe11097adc2fbf376339d36aeb [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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <functional>
21#include <memory>
22
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080023#include "androidfw/AssetManager2.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020024#include "androidfw/ConfigDescription.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070025#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
27
Adam Lesinskia6fe3452015-12-09 15:20:52 -080028#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "Resource.h"
30#include "ResourceValues.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070031#include "StringPool.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace aapt {
34namespace ResourceUtils {
35
Adam Lesinski467f1712015-11-16 17:35:44 -080036/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 * Returns true if the string was parsed as a resource name
38 * ([*][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 * `out_resource` set to the parsed resource name and `out_private` set to true
40 * if a '*' prefix was present.
Adam Lesinski467f1712015-11-16 17:35:44 -080041 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080042bool ParseResourceName(const android::StringPiece& str, ResourceNameRef* out_resource,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 bool* out_private = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080044
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 * Returns true if the string was parsed as a reference
47 * (@[+][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 * `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 * If '+' was present in the reference, `out_create` is set to true.
51 * If '*' was present in the reference, `out_private` is set to true.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080053bool ParseReference(const android::StringPiece& str, ResourceNameRef* out_reference,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 bool* out_create = nullptr, bool* out_private = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055
56/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 * Returns true if the string is in the form of a resource reference
58 * (@[+][package:]type/name).
Adam Lesinski2ae4a872015-11-02 16:10:55 -080059 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080060bool IsReference(const android::StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080061
62/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 * Returns true if the string was parsed as an attribute reference
64 * (?[package:][type/]name),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 * with `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080067bool ParseAttributeReference(const android::StringPiece& str, ResourceNameRef* out_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskib23f1e02015-11-03 12:24:17 -080069/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 * Returns true if the string is in the form of an attribute
71 * reference(?[package:][type/]name).
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080072 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080073bool IsAttributeReference(const android::StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080074
75/**
Adam Lesinski36c73a52016-08-11 13:39:24 -070076 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
Adam Lesinskib23f1e02015-11-03 12:24:17 -080077 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078Maybe<ResourceName> ToResourceName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 const android::ResTable::resource_name& name);
Adam Lesinski36c73a52016-08-11 13:39:24 -070080
81/**
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080082 * Convert an android::AssetManager2::ResourceName to an aapt::ResourceName struct.
83 */
84Maybe<ResourceName> ToResourceName(
85 const android::AssetManager2::ResourceName& name_in);
86
87/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 * Returns a boolean value if the string is equal to TRUE, true, True, FALSE,
89 * false, or False.
Adam Lesinski36c73a52016-08-11 13:39:24 -070090 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080091Maybe<bool> ParseBool(const android::StringPiece& str);
Adam Lesinski36c73a52016-08-11 13:39:24 -070092
93/**
94 * Returns a uint32_t if the string is an integer.
95 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080096Maybe<uint32_t> ParseInt(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080097
Adam Lesinskifb6312f2016-06-28 14:40:32 -070098/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070099 * Returns an ID if it the string represented a valid ID.
100 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800101Maybe<ResourceId> ParseResourceId(const android::StringPiece& str);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700102
103/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700104 * Parses an SDK version, which can be an integer, or a letter from A-Z.
105 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800106Maybe<int> ParseSdkVersion(const android::StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700107
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 * Returns a Reference, or None Maybe instance if the string `str` was parsed as
110 * a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111 * valid reference to a style.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 * The format for a style parent is slightly more flexible than a normal
113 * reference:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114 *
115 * @[package:]style/<entry> or
116 * ?[package:]style/<entry> or
117 * <package>:[style/]<entry>
118 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800119Maybe<Reference> ParseStyleParentReference(const android::StringPiece& str, std::string* out_error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120
121/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 * Returns a Reference if the string `str` was parsed as a valid XML attribute
123 * name.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700124 * The valid format for an XML attribute name is:
125 *
126 * package:entry
127 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800128Maybe<Reference> ParseXmlAttributeName(const android::StringPiece& str);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700129
130/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 * Returns a Reference object if the string was parsed as a resource or
132 * attribute reference,
133 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true
134 * if
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135 * the '+' was present in the string.
136 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800137std::unique_ptr<Reference> TryParseReference(const android::StringPiece& str,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 bool* out_create = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
140/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 * Returns a BinaryPrimitve object representing @null or @empty if the string
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 * was parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143 */
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700144std::unique_ptr<Item> TryParseNullOrEmpty(const android::StringPiece& str);
145
146// Returns a Reference representing @null.
147// Due to runtime compatibility issues, this is encoded as a reference with ID 0.
148// The runtime will convert this to TYPE_NULL.
149std::unique_ptr<Reference> MakeNull();
150
151// Returns a BinaryPrimitive representing @empty. This is encoded as a Res_value with
152// type Res_value::TYPE_NULL and data Res_value::DATA_NULL_EMPTY.
153std::unique_ptr<BinaryPrimitive> MakeEmpty();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
155/*
156 * Returns a BinaryPrimitve object representing a color if the string was parsed
157 * as one.
158 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800159std::unique_ptr<BinaryPrimitive> TryParseColor(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160
161/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 * Returns a BinaryPrimitve object representing a boolean if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800165std::unique_ptr<BinaryPrimitive> TryParseBool(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700167// Returns a boolean BinaryPrimitive.
168std::unique_ptr<BinaryPrimitive> MakeBool(bool val);
169
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 * Returns a BinaryPrimitve object representing an integer if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800174std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Shane Farmerd05b9132018-02-14 15:40:35 -0800176// Returns an integer BinaryPrimitive.
177std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t value);
178
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179/*
180 * Returns a BinaryPrimitve object representing a floating point number
181 * (float, dimension, etc) if the string was parsed as one.
182 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800183std::unique_ptr<BinaryPrimitive> TryParseFloat(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184
185/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 * Returns a BinaryPrimitve object representing an enum symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800190 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191
192/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 * Returns a BinaryPrimitve object representing a flag symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800197 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 * Try to convert a string to an Item for the given attribute. The attribute
200 * will
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201 * restrict what values the string can be converted to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 * The callback function on_create_reference is called when the parsed item is a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203 * reference to an ID that must be created (@+id/foo).
204 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800206 const android::StringPiece& value, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800210 const android::StringPiece& value, uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700214
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800215/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 * Returns a string path suitable for use within an APK. The path will look
217 * like:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800218 *
219 * res/type[-config]/<name>.<ext>
220 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and
222 * the package
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800223 * requires mangling.
224 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225std::string BuildResourceFileName(const ResourceFile& res_file,
226 const NameMangler* mangler = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800227
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700228// Parses the binary form of a resource value. `type` is used as a hint to know when a value is
229// an ID versus a False boolean value, etc. `config` is for sorting strings in the string pool.
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200230std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type,
231 const android::ConfigDescription& config,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700232 const android::ResStringPool& src_pool,
233 const android::Res_value& res_value,
234 StringPool* dst_pool);
235
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800236// A string flattened from an XML hierarchy, which maintains tags and untranslatable sections
237// in parallel data structures.
238struct FlattenedXmlString {
239 std::string text;
240 std::vector<UntranslatableSection> untranslatable_sections;
241 std::vector<Span> spans;
242};
243
244// Flattens an XML hierarchy into a FlattenedXmlString, formatting the text, escaping characters,
245// and removing whitespace, all while keeping the untranslatable sections and spans in sync with the
246// transformations.
247//
248// Specifically, the StringBuilder will handle escaped characters like \t, \n, \\, \', etc.
249// Single quotes *must* be escaped, unless within a pair of double-quotes.
250// Pairs of double-quotes disable whitespace stripping of the enclosed text.
251// Unicode escape codes (\u0049) are interpreted and the represented Unicode character is inserted.
252//
253// A NOTE ON WHITESPACE:
254//
255// When preserve_spaces is false, and when text is not enclosed within double-quotes,
256// StringBuilder replaces a series of whitespace with a single space character. This happens at the
257// start and end of the string as well, so leading and trailing whitespace is possible.
258//
259// When a Span is started or stopped, the whitespace counter is reset, meaning if whitespace
260// is encountered directly after the span, it will be emitted. This leads to situations like the
261// following: "This <b> is </b> spaced" -> "This is spaced". Without spans, this would be properly
262// compressed: "This is spaced" -> "This is spaced".
263//
264// Untranslatable sections do not have the same problem:
265// "This <xliff:g> is </xliff:g> not spaced" -> "This is not spaced".
266//
267// NOTE: This is all the way it is because AAPT1 did it this way. Maintaining backwards
268// compatibility is important.
269//
270class StringBuilder {
271 public:
272 using SpanHandle = size_t;
273 using UntranslatableHandle = size_t;
274
275 // Creates a StringBuilder. If preserve_spaces is true, whitespace removal is not performed, and
276 // single quotations can be used without escaping them.
277 explicit StringBuilder(bool preserve_spaces = false);
278
Ryan Mitchell1d358ff2019-03-06 15:06:49 -0800279 // Appends a chunk of text.
280 StringBuilder& AppendText(const std::string& text);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800281
282 // Starts a Span (tag) with the given name. The name is expected to be of the form:
283 // "tag_name;attr1=value;attr2=value;"
284 // Which is how Spans are encoded in the ResStringPool.
285 // To end the span, pass back the SpanHandle received from this method to the EndSpan() method.
286 SpanHandle StartSpan(const std::string& name);
287
288 // Ends a Span (tag). Pass in the matching SpanHandle previously obtained from StartSpan().
289 void EndSpan(SpanHandle handle);
290
291 // Starts an Untranslatable section.
292 // To end the section, pass back the UntranslatableHandle received from this method to
293 // the EndUntranslatable() method.
294 UntranslatableHandle StartUntranslatable();
295
296 // Ends an Untranslatable section. Pass in the matching UntranslatableHandle previously obtained
297 // from StartUntranslatable().
298 void EndUntranslatable(UntranslatableHandle handle);
299
300 // Returns the flattened XML string, with all spans and untranslatable sections encoded as
301 // parallel data structures.
302 FlattenedXmlString GetFlattenedString() const;
303
304 // Returns just the flattened XML text, with no spans or untranslatable sections.
305 std::string to_string() const;
306
307 // Returns true if there was no error.
308 explicit operator bool() const;
309
310 std::string GetError() const;
311
312 private:
313 DISALLOW_COPY_AND_ASSIGN(StringBuilder);
314
315 void ResetTextState();
316
317 std::string error_;
318 FlattenedXmlString xml_string_;
319 uint32_t utf16_len_ = 0u;
320 bool preserve_spaces_;
321 bool quote_;
322 bool last_codepoint_was_space_ = false;
323};
324
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325} // namespace ResourceUtils
326} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700327
328#endif /* AAPT_RESOURCEUTILS_H */