Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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_RESOURCE_VALUES_H |
| 18 | #define AAPT_RESOURCE_VALUES_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <array> |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 21 | #include <limits> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include <ostream> |
| 23 | #include <vector> |
| 24 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 25 | #include "Resource.h" |
| 26 | #include "ValueTransformer.h" |
| 27 | #include "androidfw/IDiagnostics.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | #include "androidfw/StringPiece.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 30 | #include "androidfw/StringPool.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 31 | #include "io/File.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 32 | #include "text/Printer.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | namespace aapt { |
| 35 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 36 | class ValueVisitor; |
| 37 | class ConstValueVisitor; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 39 | // A resource value. This is an all-encompassing representation |
| 40 | // of Item and Map and their subclasses. The way to do |
| 41 | // type specific operations is to check the Value's type() and |
| 42 | // cast it to the appropriate subclass. This isn't super clean, |
| 43 | // but it is the simplest strategy. |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 44 | class Value { |
| 45 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | virtual ~Value() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 48 | // Whether this value is weak and can be overridden without warning or error. Default is false. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 49 | bool IsWeak() const { |
| 50 | return weak_; |
| 51 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 53 | void SetWeak(bool val) { |
| 54 | weak_ = val; |
| 55 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 57 | // Whether the value is marked as translatable. This does not persist when flattened to binary. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | // It is only used during compilation phase. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 59 | void SetTranslatable(bool val) { |
| 60 | translatable_ = val; |
| 61 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | // Default true. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 64 | bool IsTranslatable() const { |
| 65 | return translatable_; |
| 66 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 67 | |
Jeremy Meyer | 3d8d4a1 | 2024-08-23 17:29:03 -0700 | [diff] [blame] | 68 | void SetFlag(std::optional<FeatureFlagAttribute> val) { |
| 69 | flag_ = val; |
| 70 | } |
| 71 | |
| 72 | std::optional<FeatureFlagAttribute> GetFlag() const { |
| 73 | return flag_; |
| 74 | } |
| 75 | |
Jeremy Meyer | d52bd68 | 2024-08-14 11:16:58 -0700 | [diff] [blame] | 76 | void SetFlagStatus(FlagStatus val) { |
| 77 | flag_status_ = val; |
| 78 | } |
| 79 | |
Jeremy Meyer | 3d8d4a1 | 2024-08-23 17:29:03 -0700 | [diff] [blame] | 80 | // If the value is behind a flag this returns whether that flag was enabled when the value was |
| 81 | // parsed by comparing it to the flags passed on the command line to aapt2 (taking into account |
| 82 | // negation if necessary). If there was no flag, FlagStatus::NoFlag is returned instead. |
Jeremy Meyer | d52bd68 | 2024-08-14 11:16:58 -0700 | [diff] [blame] | 83 | FlagStatus GetFlagStatus() const { |
| 84 | return flag_status_; |
| 85 | } |
| 86 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 87 | // Returns the source where this value was defined. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 88 | const android::Source& GetSource() const { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 89 | return source_; |
| 90 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 91 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 92 | void SetSource(const android::Source& source) { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 93 | source_ = source; |
| 94 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 95 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 96 | void SetSource(android::Source&& source) { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 97 | source_ = std::move(source); |
| 98 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 100 | // Returns the comment that was associated with this resource. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 101 | const std::string& GetComment() const { |
| 102 | return comment_; |
| 103 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 104 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 105 | void SetComment(android::StringPiece str) { |
| 106 | comment_.assign(str); |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 107 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 109 | void SetComment(std::string&& str) { |
| 110 | comment_ = std::move(str); |
| 111 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | virtual bool Equals(const Value* value) const = 0; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 115 | // Calls the appropriate overload of ValueVisitor. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 116 | virtual void Accept(ValueVisitor* visitor) = 0; |
| 117 | |
| 118 | // Calls the appropriate overload of ConstValueVisitor. |
| 119 | virtual void Accept(ConstValueVisitor* visitor) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 121 | // Transform this Value into another Value using the transformer. |
| 122 | std::unique_ptr<Value> Transform(ValueTransformer& transformer) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 124 | // Human readable printout of this value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | virtual void Print(std::ostream* out) const = 0; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 126 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 127 | // Human readable printout of this value that may omit some information for the sake |
| 128 | // of brevity and readability. Default implementation just calls Print(). |
| 129 | virtual void PrettyPrint(text::Printer* printer) const; |
| 130 | |
Jeremy Meyer | d52bd68 | 2024-08-14 11:16:58 -0700 | [diff] [blame] | 131 | // Removes any part of the value that is beind a disabled flag. |
| 132 | virtual void RemoveFlagDisabledElements() { |
| 133 | } |
| 134 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 135 | friend std::ostream& operator<<(std::ostream& out, const Value& value); |
| 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | protected: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 138 | android::Source source_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | std::string comment_; |
| 140 | bool weak_ = false; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 141 | bool translatable_ = true; |
Jeremy Meyer | 3d8d4a1 | 2024-08-23 17:29:03 -0700 | [diff] [blame] | 142 | std::optional<FeatureFlagAttribute> flag_; |
Jeremy Meyer | d52bd68 | 2024-08-14 11:16:58 -0700 | [diff] [blame] | 143 | FlagStatus flag_status_ = FlagStatus::NoFlag; |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 149 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | template <typename Derived> |
| 151 | struct BaseValue : public Value { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 152 | void Accept(ValueVisitor* visitor) override; |
| 153 | void Accept(ConstValueVisitor* visitor) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 156 | // A resource item with a single value. This maps to android::ResTable_entry. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 157 | struct Item : public Value { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 158 | // Fills in an android::Res_value structure with this Item's binary representation. |
| 159 | // Returns false if an error occurred. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | virtual bool Flatten(android::Res_value* out_value) const = 0; |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 161 | |
| 162 | // Transform this Item into another Item using the transformer. |
| 163 | std::unique_ptr<Item> Transform(ValueTransformer& transformer) const; |
| 164 | |
| 165 | private: |
| 166 | virtual Item* TransformItemImpl(ValueTransformer& transformer) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 169 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 170 | template <typename Derived> |
| 171 | struct BaseItem : public Item { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 172 | void Accept(ValueVisitor* visitor) override; |
| 173 | void Accept(ConstValueVisitor* visitor) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 176 | // A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE. |
| 177 | // A reference can be symbolic (with the name set to a valid resource name) or be |
| 178 | // numeric (the id is set to a valid resource ID). |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 179 | struct Reference : public TransformableItem<Reference, BaseItem<Reference>> { |
Ryan Mitchell | a2b4fcd | 2021-05-03 15:37:00 -0700 | [diff] [blame] | 180 | enum class Type : uint8_t { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | kResource, |
| 182 | kAttribute, |
| 183 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 184 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 185 | std::optional<ResourceName> name; |
| 186 | std::optional<ResourceId> id; |
Ryan Mitchell | a2b4fcd | 2021-05-03 15:37:00 -0700 | [diff] [blame] | 187 | std::optional<uint32_t> type_flags; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | Reference::Type reference_type; |
| 189 | bool private_reference = false; |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 190 | bool is_dynamic = false; |
Ryan Mitchell | a2b4fcd | 2021-05-03 15:37:00 -0700 | [diff] [blame] | 191 | bool allow_raw = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 192 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | Reference(); |
| 194 | explicit Reference(const ResourceNameRef& n, Type type = Type::kResource); |
| 195 | explicit Reference(const ResourceId& i, Type type = Type::kResource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | Reference(const ResourceNameRef& n, const ResourceId& i); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 197 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | bool Equals(const Value* value) const override; |
| 199 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 200 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 201 | void PrettyPrint(text::Printer* printer) const override; |
| 202 | |
| 203 | // Prints the reference without a package name if the package name matches the one given. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 204 | void PrettyPrint(android::StringPiece package, text::Printer* printer) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 205 | }; |
| 206 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 207 | bool operator<(const Reference&, const Reference&); |
| 208 | bool operator==(const Reference&, const Reference&); |
| 209 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 210 | // An ID resource. Has no real value, just a place holder. |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 211 | struct Id : public TransformableItem<Id, BaseItem<Id>> { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 212 | Id() { |
| 213 | weak_ = true; |
| 214 | } |
| 215 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 216 | bool Equals(const Value* value) const override; |
| 217 | bool Flatten(android::Res_value* out) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 219 | }; |
| 220 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 221 | // A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace. |
| 222 | // This shall *NOT* end up in the final resource table. |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 223 | struct RawString : public TransformableItem<RawString, BaseItem<RawString>> { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 224 | android::StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 225 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 226 | explicit RawString(const android::StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | bool Equals(const Value* value) const override; |
| 229 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 231 | }; |
| 232 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 233 | // Identifies a range of characters in a string that are untranslatable. |
| 234 | // These should not be pseudolocalized. The start and end indices are measured in bytes. |
| 235 | struct UntranslatableSection { |
| 236 | // Start offset inclusive. |
| 237 | size_t start; |
| 238 | |
| 239 | // End offset exclusive. |
| 240 | size_t end; |
| 241 | }; |
| 242 | |
| 243 | inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 244 | return a.start == b.start && a.end == b.end; |
| 245 | } |
| 246 | |
| 247 | inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 248 | return a.start != b.start || a.end != b.end; |
| 249 | } |
| 250 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 251 | struct String : public TransformableItem<String, BaseItem<String>> { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 252 | android::StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 253 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 254 | // Sections of the string to NOT translate. Mainly used |
| 255 | // for pseudolocalization. This data is NOT persisted |
| 256 | // in any format. |
| 257 | std::vector<UntranslatableSection> untranslatable_sections; |
| 258 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 259 | explicit String(const android::StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | bool Equals(const Value* value) const override; |
| 262 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 263 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 264 | void PrettyPrint(text::Printer* printer) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 267 | struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 268 | android::StringPool::StyleRef value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 269 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 270 | // Sections of the string to NOT translate. Mainly used |
| 271 | // for pseudolocalization. This data is NOT persisted |
| 272 | // in any format. |
| 273 | std::vector<UntranslatableSection> untranslatable_sections; |
| 274 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 275 | explicit StyledString(const android::StringPool::StyleRef& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 276 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | bool Equals(const Value* value) const override; |
| 278 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 280 | }; |
| 281 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 282 | struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 283 | android::StringPool::Ref path; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 284 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 285 | // A handle to the file object from which this file can be read. |
| 286 | // This field is NOT persisted in any format. It is transient. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | io::IFile* file = nullptr; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 288 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 289 | // FileType of the file pointed to by `file`. This is used to know how to inflate the file, |
| 290 | // or if to inflate at all (just copy). |
| 291 | ResourceFile::Type type = ResourceFile::Type::kUnknown; |
| 292 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | FileReference() = default; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 294 | explicit FileReference(const android::StringPool::Ref& path); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | bool Equals(const Value* value) const override; |
| 297 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 299 | }; |
| 300 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 301 | // Represents any other android::Res_value. |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 302 | struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 303 | android::Res_value value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 304 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | BinaryPrimitive() = default; |
| 306 | explicit BinaryPrimitive(const android::Res_value& val); |
| 307 | BinaryPrimitive(uint8_t dataType, uint32_t data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 308 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | bool Equals(const Value* value) const override; |
| 310 | bool Flatten(android::Res_value* out_value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 311 | void Print(std::ostream* out) const override; |
Brandon Liu | c674d38 | 2023-03-31 22:37:42 +0000 | [diff] [blame] | 312 | static const char* DecideFormat(float f); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 313 | void PrettyPrint(text::Printer* printer) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 316 | struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 317 | struct Symbol { |
| 318 | Reference symbol; |
| 319 | uint32_t value; |
Ryan Mitchell | c167680 | 2019-05-20 16:47:08 -0700 | [diff] [blame] | 320 | uint8_t type; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 321 | |
| 322 | friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | uint32_t type_mask; |
| 326 | int32_t min_int; |
| 327 | int32_t max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 328 | std::vector<Symbol> symbols; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 329 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 330 | explicit Attribute(uint32_t t = 0u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 331 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | bool Equals(const Value* value) const override; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 333 | |
| 334 | // Returns true if this Attribute's format is compatible with the given Attribute. The basic |
| 335 | // rule is that TYPE_REFERENCE can be ignored for both of the Attributes, and TYPE_FLAGS and |
| 336 | // TYPE_ENUMS are never compatible. |
| 337 | bool IsCompatibleWith(const Attribute& attr) const; |
| 338 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 339 | std::string MaskString() const; |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 340 | static std::string MaskString(uint32_t type_mask); |
| 341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | void Print(std::ostream* out) const override; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 343 | bool Matches(const Item& item, android::DiagMessage* out_msg = nullptr) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 344 | }; |
| 345 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 346 | struct Style : public TransformableValue<Style, BaseValue<Style>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 347 | struct Entry { |
| 348 | Reference key; |
| 349 | std::unique_ptr<Item> value; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 350 | |
| 351 | friend std::ostream& operator<<(std::ostream& out, const Entry& entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 353 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 354 | std::optional<Reference> parent; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 355 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 356 | // If set to true, the parent was auto inferred from the style's name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | bool parent_inferred = false; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 358 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 359 | std::vector<Entry> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 360 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 361 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 362 | void Print(std::ostream* out) const override; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 363 | |
| 364 | // Merges `style` into this Style. All identical attributes of `style` take precedence, including |
| 365 | // the parent, if there is one. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 366 | void MergeWith(Style* style, android::StringPool* pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 367 | }; |
| 368 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 369 | struct Array : public TransformableValue<Array, BaseValue<Array>> { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 370 | std::vector<std::unique_ptr<Item>> elements; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 371 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 373 | void Print(std::ostream* out) const override; |
Jeremy Meyer | d52bd68 | 2024-08-14 11:16:58 -0700 | [diff] [blame] | 374 | void RemoveFlagDisabledElements() override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 375 | }; |
| 376 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 377 | struct Plural : public TransformableValue<Plural, BaseValue<Plural>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 378 | enum { Zero = 0, One, Two, Few, Many, Other, Count }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 379 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | std::array<std::unique_ptr<Item>, Count> values; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 381 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 383 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 384 | }; |
| 385 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 386 | struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 387 | std::vector<Reference> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 388 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 389 | bool Equals(const Value* value) const override; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 390 | void Print(std::ostream* out) const override; |
| 391 | void MergeWith(Styleable* styleable); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 392 | }; |
| 393 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 394 | struct Macro : public TransformableValue<Macro, BaseValue<Macro>> { |
| 395 | std::string raw_value; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 396 | android::StyleString style_string; |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 397 | std::vector<UntranslatableSection> untranslatable_sections; |
| 398 | |
| 399 | struct Namespace { |
| 400 | std::string alias; |
| 401 | std::string package_name; |
| 402 | bool is_private; |
| 403 | |
| 404 | bool operator==(const Namespace& right) const { |
| 405 | return alias == right.alias && package_name == right.package_name && |
| 406 | is_private == right.is_private; |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | std::vector<Namespace> alias_namespaces; |
| 411 | |
| 412 | bool Equals(const Value* value) const override; |
| 413 | void Print(std::ostream* out) const override; |
| 414 | }; |
| 415 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 416 | template <typename T> |
| 417 | typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<( |
| 418 | std::ostream& out, const std::unique_ptr<T>& value) { |
| 419 | if (value == nullptr) { |
| 420 | out << "NULL"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | } else { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 422 | value->Print(&out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 423 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 424 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 427 | struct CloningValueTransformer : public ValueTransformer { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 428 | explicit CloningValueTransformer(android::StringPool* new_pool); |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 429 | |
| 430 | std::unique_ptr<Reference> TransformDerived(const Reference* value) override; |
| 431 | std::unique_ptr<Id> TransformDerived(const Id* value) override; |
| 432 | std::unique_ptr<RawString> TransformDerived(const RawString* value) override; |
| 433 | std::unique_ptr<String> TransformDerived(const String* value) override; |
| 434 | std::unique_ptr<StyledString> TransformDerived(const StyledString* value) override; |
| 435 | std::unique_ptr<FileReference> TransformDerived(const FileReference* value) override; |
| 436 | std::unique_ptr<BinaryPrimitive> TransformDerived(const BinaryPrimitive* value) override; |
| 437 | std::unique_ptr<Attribute> TransformDerived(const Attribute* value) override; |
| 438 | std::unique_ptr<Style> TransformDerived(const Style* value) override; |
| 439 | std::unique_ptr<Array> TransformDerived(const Array* value) override; |
| 440 | std::unique_ptr<Plural> TransformDerived(const Plural* value) override; |
| 441 | std::unique_ptr<Styleable> TransformDerived(const Styleable* value) override; |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 442 | std::unique_ptr<Macro> TransformDerived(const Macro* value) override; |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 443 | }; |
| 444 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 445 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 446 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 447 | #endif // AAPT_RESOURCE_VALUES_H |