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