blob: e282fd58d26103e7ed5fd615ea49ee14aaeeb299 [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
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020023#include "androidfw/ConfigDescription.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070024#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
26
Adam Lesinskia6fe3452015-12-09 15:20:52 -080027#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "Resource.h"
29#include "ResourceValues.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070030#include "StringPool.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032namespace aapt {
33namespace ResourceUtils {
34
Adam Lesinski467f1712015-11-16 17:35:44 -080035/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 * Returns true if the string was parsed as a resource name
37 * ([*][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 * `out_resource` set to the parsed resource name and `out_private` set to true
39 * if a '*' prefix was present.
Adam Lesinski467f1712015-11-16 17:35:44 -080040 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080041bool ParseResourceName(const android::StringPiece& str, ResourceNameRef* out_resource,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 bool* out_private = nullptr);
Adam Lesinski467f1712015-11-16 17:35:44 -080043
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 * Returns true if the string was parsed as a reference
46 * (@[+][package:]type/name), with
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 * `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 * If '+' was present in the reference, `out_create` is set to true.
50 * If '*' was present in the reference, `out_private` is set to true.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080052bool ParseReference(const android::StringPiece& str, ResourceNameRef* out_reference,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 bool* out_create = nullptr, bool* out_private = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
55/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 * Returns true if the string is in the form of a resource reference
57 * (@[+][package:]type/name).
Adam Lesinski2ae4a872015-11-02 16:10:55 -080058 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080059bool IsReference(const android::StringPiece& str);
Adam Lesinski2ae4a872015-11-02 16:10:55 -080060
61/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 * Returns true if the string was parsed as an attribute reference
63 * (?[package:][type/]name),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 * with `out_reference` set to the parsed reference.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080066bool ParseAttributeReference(const android::StringPiece& str, ResourceNameRef* out_reference);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinskib23f1e02015-11-03 12:24:17 -080068/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 * Returns true if the string is in the form of an attribute
70 * reference(?[package:][type/]name).
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080071 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080072bool IsAttributeReference(const android::StringPiece& str);
Adam Lesinski7298bc9c2015-11-16 12:31:52 -080073
74/**
Adam Lesinski36c73a52016-08-11 13:39:24 -070075 * Convert an android::ResTable::resource_name to an aapt::ResourceName struct.
Adam Lesinskib23f1e02015-11-03 12:24:17 -080076 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077Maybe<ResourceName> ToResourceName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 const android::ResTable::resource_name& name);
Adam Lesinski36c73a52016-08-11 13:39:24 -070079
80/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 * Returns a boolean value if the string is equal to TRUE, true, True, FALSE,
82 * false, or False.
Adam Lesinski36c73a52016-08-11 13:39:24 -070083 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080084Maybe<bool> ParseBool(const android::StringPiece& str);
Adam Lesinski36c73a52016-08-11 13:39:24 -070085
86/**
87 * Returns a uint32_t if the string is an integer.
88 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080089Maybe<uint32_t> ParseInt(const android::StringPiece& str);
Adam Lesinskib23f1e02015-11-03 12:24:17 -080090
Adam Lesinskifb6312f2016-06-28 14:40:32 -070091/**
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070092 * Returns an ID if it the string represented a valid ID.
93 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080094Maybe<ResourceId> ParseResourceId(const android::StringPiece& str);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070095
96/**
Adam Lesinskifb6312f2016-06-28 14:40:32 -070097 * Parses an SDK version, which can be an integer, or a letter from A-Z.
98 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080099Maybe<int> ParseSdkVersion(const android::StringPiece& str);
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700100
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 * Returns a Reference, or None Maybe instance if the string `str` was parsed as
103 * a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 * valid reference to a style.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 * The format for a style parent is slightly more flexible than a normal
106 * reference:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107 *
108 * @[package:]style/<entry> or
109 * ?[package:]style/<entry> or
110 * <package>:[style/]<entry>
111 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800112Maybe<Reference> ParseStyleParentReference(const android::StringPiece& str, std::string* out_error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113
114/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 * Returns a Reference if the string `str` was parsed as a valid XML attribute
116 * name.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700117 * The valid format for an XML attribute name is:
118 *
119 * package:entry
120 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800121Maybe<Reference> ParseXmlAttributeName(const android::StringPiece& str);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700122
123/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 * Returns a Reference object if the string was parsed as a resource or
125 * attribute reference,
126 * ( @[+][package:]type/name | ?[package:]type/name ) setting outCreate to true
127 * if
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 * the '+' was present in the string.
129 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800130std::unique_ptr<Reference> TryParseReference(const android::StringPiece& str,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 bool* out_create = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132
133/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 * Returns a BinaryPrimitve object representing @null or @empty if the string
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 * was parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136 */
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700137std::unique_ptr<Item> TryParseNullOrEmpty(const android::StringPiece& str);
138
139// Returns a Reference representing @null.
140// Due to runtime compatibility issues, this is encoded as a reference with ID 0.
141// The runtime will convert this to TYPE_NULL.
142std::unique_ptr<Reference> MakeNull();
143
144// Returns a BinaryPrimitive representing @empty. This is encoded as a Res_value with
145// type Res_value::TYPE_NULL and data Res_value::DATA_NULL_EMPTY.
146std::unique_ptr<BinaryPrimitive> MakeEmpty();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
148/*
149 * Returns a BinaryPrimitve object representing a color if the string was parsed
150 * as one.
151 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800152std::unique_ptr<BinaryPrimitive> TryParseColor(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
154/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 * Returns a BinaryPrimitve object representing a boolean if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800158std::unique_ptr<BinaryPrimitive> TryParseBool(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700160// Returns a boolean BinaryPrimitive.
161std::unique_ptr<BinaryPrimitive> MakeBool(bool val);
162
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 * Returns a BinaryPrimitve object representing an integer if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800167std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168
Shane Farmerd05b9132018-02-14 15:40:35 -0800169// Returns an integer BinaryPrimitive.
170std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t value);
171
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172/*
173 * Returns a BinaryPrimitve object representing a floating point number
174 * (float, dimension, etc) if the string was parsed as one.
175 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800176std::unique_ptr<BinaryPrimitive> TryParseFloat(const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177
178/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 * Returns a BinaryPrimitve object representing an enum symbol if the string was
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 * parsed as one.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182std::unique_ptr<BinaryPrimitive> TryParseEnumSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800183 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 a flag 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> TryParseFlagSymbol(const Attribute* enum_attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800190 const android::StringPiece& str);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191/*
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 * Try to convert a string to an Item for the given attribute. The attribute
193 * will
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194 * restrict what values the string can be converted to.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 * The callback function on_create_reference is called when the parsed item is a
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196 * reference to an ID that must be created (@+id/foo).
197 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800199 const android::StringPiece& value, const Attribute* attr,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202std::unique_ptr<Item> TryParseItemForAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800203 const android::StringPiece& value, uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 const std::function<void(const ResourceName&)>& on_create_reference = {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800208/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 * Returns a string path suitable for use within an APK. The path will look
210 * like:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800211 *
212 * res/type[-config]/<name>.<ext>
213 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 * Then name may be mangled if a NameMangler is supplied (can be nullptr) and
215 * the package
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800216 * requires mangling.
217 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218std::string BuildResourceFileName(const ResourceFile& res_file,
219 const NameMangler* mangler = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800220
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700221// Parses the binary form of a resource value. `type` is used as a hint to know when a value is
222// 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 +0200223std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type,
224 const android::ConfigDescription& config,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700225 const android::ResStringPool& src_pool,
226 const android::Res_value& res_value,
227 StringPool* dst_pool);
228
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800229// A string flattened from an XML hierarchy, which maintains tags and untranslatable sections
230// in parallel data structures.
231struct FlattenedXmlString {
232 std::string text;
233 std::vector<UntranslatableSection> untranslatable_sections;
234 std::vector<Span> spans;
235};
236
237// Flattens an XML hierarchy into a FlattenedXmlString, formatting the text, escaping characters,
238// and removing whitespace, all while keeping the untranslatable sections and spans in sync with the
239// transformations.
240//
241// Specifically, the StringBuilder will handle escaped characters like \t, \n, \\, \', etc.
242// Single quotes *must* be escaped, unless within a pair of double-quotes.
243// Pairs of double-quotes disable whitespace stripping of the enclosed text.
244// Unicode escape codes (\u0049) are interpreted and the represented Unicode character is inserted.
245//
246// A NOTE ON WHITESPACE:
247//
248// When preserve_spaces is false, and when text is not enclosed within double-quotes,
249// StringBuilder replaces a series of whitespace with a single space character. This happens at the
250// start and end of the string as well, so leading and trailing whitespace is possible.
251//
252// When a Span is started or stopped, the whitespace counter is reset, meaning if whitespace
253// is encountered directly after the span, it will be emitted. This leads to situations like the
254// following: "This <b> is </b> spaced" -> "This is spaced". Without spans, this would be properly
255// compressed: "This is spaced" -> "This is spaced".
256//
257// Untranslatable sections do not have the same problem:
258// "This <xliff:g> is </xliff:g> not spaced" -> "This is not spaced".
259//
260// NOTE: This is all the way it is because AAPT1 did it this way. Maintaining backwards
261// compatibility is important.
262//
263class StringBuilder {
264 public:
265 using SpanHandle = size_t;
266 using UntranslatableHandle = size_t;
267
268 // Creates a StringBuilder. If preserve_spaces is true, whitespace removal is not performed, and
269 // single quotations can be used without escaping them.
270 explicit StringBuilder(bool preserve_spaces = false);
271
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700272 // Appends a chunk of text. If preserve_spaces is true, whitespace removal is not performed, and
273 // single quotations can be used without escaping them for this append. Otherwise, the
274 // StringBuilder will behave as it was constructed.
275 StringBuilder& AppendText(const std::string& text, bool preserve_spaces = false);
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800276
277 // Starts a Span (tag) with the given name. The name is expected to be of the form:
278 // "tag_name;attr1=value;attr2=value;"
279 // Which is how Spans are encoded in the ResStringPool.
280 // To end the span, pass back the SpanHandle received from this method to the EndSpan() method.
281 SpanHandle StartSpan(const std::string& name);
282
283 // Ends a Span (tag). Pass in the matching SpanHandle previously obtained from StartSpan().
284 void EndSpan(SpanHandle handle);
285
286 // Starts an Untranslatable section.
287 // To end the section, pass back the UntranslatableHandle received from this method to
288 // the EndUntranslatable() method.
289 UntranslatableHandle StartUntranslatable();
290
291 // Ends an Untranslatable section. Pass in the matching UntranslatableHandle previously obtained
292 // from StartUntranslatable().
293 void EndUntranslatable(UntranslatableHandle handle);
294
295 // Returns the flattened XML string, with all spans and untranslatable sections encoded as
296 // parallel data structures.
297 FlattenedXmlString GetFlattenedString() const;
298
299 // Returns just the flattened XML text, with no spans or untranslatable sections.
300 std::string to_string() const;
301
302 // Returns true if there was no error.
303 explicit operator bool() const;
304
305 std::string GetError() const;
306
307 private:
308 DISALLOW_COPY_AND_ASSIGN(StringBuilder);
309
310 void ResetTextState();
311
312 std::string error_;
313 FlattenedXmlString xml_string_;
314 uint32_t utf16_len_ = 0u;
315 bool preserve_spaces_;
316 bool quote_;
317 bool last_codepoint_was_space_ = false;
318};
319
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320} // namespace ResourceUtils
321} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700322
323#endif /* AAPT_RESOURCEUTILS_H */