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 | |
| 25 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 26 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 28 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include "Resource.h" |
| 30 | #include "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 | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 33 | #include "util/Maybe.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | namespace aapt { |
| 36 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 37 | class ValueVisitor; |
| 38 | class ConstValueVisitor; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 40 | // A resource value. This is an all-encompassing representation |
| 41 | // of Item and Map and their subclasses. The way to do |
| 42 | // type specific operations is to check the Value's type() and |
| 43 | // cast it to the appropriate subclass. This isn't super clean, |
| 44 | // but it is the simplest strategy. |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 45 | class Value { |
| 46 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | virtual ~Value() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 49 | // 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] | 50 | bool IsWeak() const { |
| 51 | return weak_; |
| 52 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 54 | void SetWeak(bool val) { |
| 55 | weak_ = val; |
| 56 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 58 | // 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] | 59 | // It is only used during compilation phase. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 60 | void SetTranslatable(bool val) { |
| 61 | translatable_ = val; |
| 62 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | // Default true. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 65 | bool IsTranslatable() const { |
| 66 | return translatable_; |
| 67 | } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 69 | // Returns the source where this value was defined. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 70 | const Source& GetSource() const { |
| 71 | return source_; |
| 72 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 74 | void SetSource(const Source& source) { |
| 75 | source_ = source; |
| 76 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 78 | void SetSource(Source&& source) { |
| 79 | source_ = std::move(source); |
| 80 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 82 | // Returns the comment that was associated with this resource. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 83 | const std::string& GetComment() const { |
| 84 | return comment_; |
| 85 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 87 | void SetComment(const android::StringPiece& str) { |
| 88 | comment_ = str.to_string(); |
| 89 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 91 | void SetComment(std::string&& str) { |
| 92 | comment_ = std::move(str); |
| 93 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | virtual bool Equals(const Value* value) const = 0; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 97 | // Calls the appropriate overload of ValueVisitor. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 98 | virtual void Accept(ValueVisitor* visitor) = 0; |
| 99 | |
| 100 | // Calls the appropriate overload of ConstValueVisitor. |
| 101 | virtual void Accept(ConstValueVisitor* visitor) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 103 | // Clone the value. `new_pool` is the new StringPool that |
| 104 | // any resources with strings should use when copying their string. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 105 | virtual Value* Clone(StringPool* new_pool) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 107 | // Human readable printout of this value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | virtual void Print(std::ostream* out) const = 0; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 110 | // Human readable printout of this value that may omit some information for the sake |
| 111 | // of brevity and readability. Default implementation just calls Print(). |
| 112 | virtual void PrettyPrint(text::Printer* printer) const; |
| 113 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 114 | friend std::ostream& operator<<(std::ostream& out, const Value& value); |
| 115 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | protected: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | Source source_; |
| 118 | std::string comment_; |
| 119 | bool weak_ = false; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 120 | bool translatable_ = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 123 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | template <typename Derived> |
| 125 | struct BaseValue : public Value { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 126 | void Accept(ValueVisitor* visitor) override; |
| 127 | void Accept(ConstValueVisitor* visitor) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 130 | // 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] | 131 | struct Item : public Value { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 132 | // Clone the Item. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | virtual Item* Clone(StringPool* new_pool) const override = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 135 | // Fills in an android::Res_value structure with this Item's binary representation. |
| 136 | // Returns false if an error occurred. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | virtual bool Flatten(android::Res_value* out_value) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 140 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 141 | template <typename Derived> |
| 142 | struct BaseItem : public Item { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 143 | void Accept(ValueVisitor* visitor) override; |
| 144 | void Accept(ConstValueVisitor* visitor) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 147 | // A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE. |
| 148 | // A reference can be symbolic (with the name set to a valid resource name) or be |
| 149 | // numeric (the id is set to a valid resource ID). |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | struct Reference : public BaseItem<Reference> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | enum class Type { |
| 152 | kResource, |
| 153 | kAttribute, |
| 154 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 155 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | Maybe<ResourceName> name; |
| 157 | Maybe<ResourceId> id; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | Reference::Type reference_type; |
| 159 | bool private_reference = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 160 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | Reference(); |
| 162 | explicit Reference(const ResourceNameRef& n, Type type = Type::kResource); |
| 163 | explicit Reference(const ResourceId& i, Type type = Type::kResource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | Reference(const ResourceNameRef& n, const ResourceId& i); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 165 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | bool Equals(const Value* value) const override; |
| 167 | bool Flatten(android::Res_value* out_value) const override; |
| 168 | Reference* Clone(StringPool* new_pool) const override; |
| 169 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 170 | void PrettyPrint(text::Printer* printer) const override; |
| 171 | |
| 172 | // Prints the reference without a package name if the package name matches the one given. |
| 173 | void PrettyPrint(const android::StringPiece& package, text::Printer* printer) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 176 | bool operator<(const Reference&, const Reference&); |
| 177 | bool operator==(const Reference&, const Reference&); |
| 178 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 179 | // An ID resource. Has no real value, just a place holder. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 180 | struct Id : public BaseItem<Id> { |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 181 | Id() { |
| 182 | weak_ = true; |
| 183 | } |
| 184 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | bool Equals(const Value* value) const override; |
| 186 | bool Flatten(android::Res_value* out) const override; |
| 187 | Id* Clone(StringPool* new_pool) const override; |
| 188 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 191 | // A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace. |
| 192 | // This shall *NOT* end up in the final resource table. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 193 | struct RawString : public BaseItem<RawString> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 195 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | explicit RawString(const StringPool::Ref& ref); |
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; |
| 200 | RawString* Clone(StringPool* new_pool) const override; |
| 201 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 202 | }; |
| 203 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 204 | // Identifies a range of characters in a string that are untranslatable. |
| 205 | // These should not be pseudolocalized. The start and end indices are measured in bytes. |
| 206 | struct UntranslatableSection { |
| 207 | // Start offset inclusive. |
| 208 | size_t start; |
| 209 | |
| 210 | // End offset exclusive. |
| 211 | size_t end; |
| 212 | }; |
| 213 | |
| 214 | inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 215 | return a.start == b.start && a.end == b.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 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 222 | struct String : public BaseItem<String> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 224 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 225 | // Sections of the string to NOT translate. Mainly used |
| 226 | // for pseudolocalization. This data is NOT persisted |
| 227 | // in any format. |
| 228 | std::vector<UntranslatableSection> untranslatable_sections; |
| 229 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | explicit String(const StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 231 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | bool Equals(const Value* value) const override; |
| 233 | bool Flatten(android::Res_value* out_value) const override; |
| 234 | String* Clone(StringPool* new_pool) const override; |
| 235 | void Print(std::ostream* out) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 236 | void PrettyPrint(text::Printer* printer) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | struct StyledString : public BaseItem<StyledString> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | StringPool::StyleRef 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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | explicit StyledString(const StringPool::StyleRef& 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; |
| 251 | StyledString* Clone(StringPool* new_pool) const override; |
| 252 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | struct FileReference : public BaseItem<FileReference> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | StringPool::Ref path; |
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 | // A handle to the file object from which this file can be read. |
| 259 | // This field is NOT persisted in any format. It is transient. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 260 | io::IFile* file = nullptr; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 261 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 262 | // FileType of the file pointed to by `file`. This is used to know how to inflate the file, |
| 263 | // or if to inflate at all (just copy). |
| 264 | ResourceFile::Type type = ResourceFile::Type::kUnknown; |
| 265 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | FileReference() = default; |
| 267 | explicit FileReference(const StringPool::Ref& path); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 268 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | bool Equals(const Value* value) const override; |
| 270 | bool Flatten(android::Res_value* out_value) const override; |
| 271 | FileReference* Clone(StringPool* new_pool) const override; |
| 272 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 275 | // Represents any other android::Res_value. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 276 | struct BinaryPrimitive : public BaseItem<BinaryPrimitive> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 277 | android::Res_value value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 278 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 279 | BinaryPrimitive() = default; |
| 280 | explicit BinaryPrimitive(const android::Res_value& val); |
| 281 | BinaryPrimitive(uint8_t dataType, uint32_t data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 282 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | bool Equals(const Value* value) const override; |
| 284 | bool Flatten(android::Res_value* out_value) const override; |
| 285 | BinaryPrimitive* Clone(StringPool* new_pool) const override; |
| 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 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 290 | struct Attribute : public BaseValue<Attribute> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | struct Symbol { |
| 292 | Reference symbol; |
| 293 | uint32_t value; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 294 | |
| 295 | friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 297 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | uint32_t type_mask; |
| 299 | int32_t min_int; |
| 300 | int32_t max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 | std::vector<Symbol> symbols; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 302 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 303 | Attribute(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | explicit Attribute(bool w, 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; |
| 307 | Attribute* Clone(StringPool* new_pool) const override; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 308 | std::string MaskString() const; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | void Print(std::ostream* out) const override; |
Adam Lesinski | 3124e7c | 2017-06-13 16:03:55 -0700 | [diff] [blame] | 310 | bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | struct Style : public BaseValue<Style> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | struct Entry { |
| 315 | Reference key; |
| 316 | std::unique_ptr<Item> value; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 317 | |
| 318 | friend std::ostream& operator<<(std::ostream& out, const Entry& entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 319 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 320 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 321 | Maybe<Reference> parent; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 322 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 323 | // 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] | 324 | bool parent_inferred = false; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 325 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 326 | std::vector<Entry> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | bool Equals(const Value* value) const override; |
| 329 | Style* Clone(StringPool* new_pool) const override; |
| 330 | void Print(std::ostream* out) const override; |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 331 | |
| 332 | // Merges `style` into this Style. All identical attributes of `style` take precedence, including |
| 333 | // the parent, if there is one. |
| 334 | void MergeWith(Style* style, StringPool* pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | struct Array : public BaseValue<Array> { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 338 | std::vector<std::unique_ptr<Item>> elements; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 339 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | bool Equals(const Value* value) const override; |
| 341 | Array* Clone(StringPool* new_pool) const override; |
| 342 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | struct Plural : public BaseValue<Plural> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 346 | enum { Zero = 0, One, Two, Few, Many, Other, Count }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 347 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 348 | std::array<std::unique_ptr<Item>, Count> values; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 349 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 350 | bool Equals(const Value* value) const override; |
| 351 | Plural* Clone(StringPool* new_pool) const override; |
| 352 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | struct Styleable : public BaseValue<Styleable> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 | std::vector<Reference> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 357 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | bool Equals(const Value* value) const override; |
| 359 | Styleable* Clone(StringPool* newPool) const override; |
| 360 | void Print(std::ostream* out) const override; |
| 361 | void MergeWith(Styleable* styleable); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 362 | }; |
| 363 | |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 364 | template <typename T> |
| 365 | typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<( |
| 366 | std::ostream& out, const std::unique_ptr<T>& value) { |
| 367 | if (value == nullptr) { |
| 368 | out << "NULL"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | } else { |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 370 | value->Print(&out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 371 | } |
Adam Lesinski | 5924d8c | 2017-05-30 15:15:58 -0700 | [diff] [blame] | 372 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 376 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | #endif // AAPT_RESOURCE_VALUES_H |