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