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 | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 20 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include "Resource.h" |
| 22 | #include "StringPool.h" |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 23 | #include "util/Maybe.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | |
| 25 | #include <array> |
| 26 | #include <androidfw/ResourceTypes.h> |
| 27 | #include <ostream> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace aapt { |
| 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | struct RawValueVisitor; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A resource value. This is an all-encompassing representation |
| 36 | * of Item and Map and their subclasses. The way to do |
| 37 | * type specific operations is to check the Value's type() and |
| 38 | * cast it to the appropriate subclass. This isn't super clean, |
| 39 | * but it is the simplest strategy. |
| 40 | */ |
| 41 | struct Value { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | virtual ~Value() = default; |
| 43 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | /** |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 45 | * Whether this value is weak and can be overridden without |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | * warning or error. Default for base class is false. |
| 47 | */ |
| 48 | virtual bool isWeak() const; |
| 49 | |
| 50 | /** |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 51 | * Returns the source where this value was defined. |
| 52 | */ |
| 53 | const Source& getSource() const { |
| 54 | return mSource; |
| 55 | } |
| 56 | |
| 57 | void setSource(const Source& source) { |
| 58 | mSource = source; |
| 59 | } |
| 60 | |
| 61 | void setSource(Source&& source) { |
| 62 | mSource = std::move(source); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the comment that was associated with this resource. |
| 67 | */ |
| 68 | StringPiece16 getComment() const { |
| 69 | return mComment; |
| 70 | } |
| 71 | |
| 72 | void setComment(const StringPiece16& str) { |
| 73 | mComment = str.toString(); |
| 74 | } |
| 75 | |
| 76 | void setComment(std::u16string&& str) { |
| 77 | mComment = std::move(str); |
| 78 | } |
| 79 | |
| 80 | /** |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | * Calls the appropriate overload of ValueVisitor. |
| 82 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | virtual void accept(RawValueVisitor* visitor) = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Clone the value. |
| 87 | */ |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 88 | virtual Value* clone(StringPool* newPool) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Human readable printout of this value. |
| 92 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 93 | virtual void print(std::ostream* out) const = 0; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 95 | protected: |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 96 | Source mSource; |
| 97 | std::u16string mComment; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * Inherit from this to get visitor accepting implementations for free. |
| 102 | */ |
| 103 | template <typename Derived> |
| 104 | struct BaseValue : public Value { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | void accept(RawValueVisitor* visitor) override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * A resource item with a single value. This maps to android::ResTable_entry. |
| 110 | */ |
| 111 | struct Item : public Value { |
| 112 | /** |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | * Clone the Item. |
| 114 | */ |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 115 | virtual Item* clone(StringPool* newPool) const override = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Fills in an android::Res_value structure with this Item's binary representation. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | * Returns false if an error occurred. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | virtual bool flatten(android::Res_value* outValue) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * Inherit from this to get visitor accepting implementations for free. |
| 126 | */ |
| 127 | template <typename Derived> |
| 128 | struct BaseItem : public Item { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 129 | void accept(RawValueVisitor* visitor) override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /** |
| 133 | * A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE. |
| 134 | * |
| 135 | * A reference can be symbolic (with the name set to a valid resource name) or be |
| 136 | * numeric (the id is set to a valid resource ID). |
| 137 | */ |
| 138 | struct Reference : public BaseItem<Reference> { |
| 139 | enum class Type { |
| 140 | kResource, |
| 141 | kAttribute, |
| 142 | }; |
| 143 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | Maybe<ResourceName> name; |
| 145 | Maybe<ResourceId> id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 146 | Reference::Type referenceType; |
| 147 | bool privateReference = false; |
| 148 | |
| 149 | Reference(); |
| 150 | Reference(const ResourceNameRef& n, Type type = Type::kResource); |
| 151 | Reference(const ResourceId& i, Type type = Type::kResource); |
| 152 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | bool flatten(android::Res_value* outValue) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 154 | Reference* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * An ID resource. Has no real value, just a place holder. |
| 160 | */ |
| 161 | struct Id : public BaseItem<Id> { |
| 162 | bool isWeak() const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 163 | bool flatten(android::Res_value* out) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 164 | Id* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /** |
| 169 | * A raw, unprocessed string. This may contain quotations, |
| 170 | * escape sequences, and whitespace. This shall *NOT* |
| 171 | * end up in the final resource table. |
| 172 | */ |
| 173 | struct RawString : public BaseItem<RawString> { |
| 174 | StringPool::Ref value; |
| 175 | |
| 176 | RawString(const StringPool::Ref& ref); |
| 177 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 178 | bool flatten(android::Res_value* outValue) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 179 | RawString* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 180 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | struct String : public BaseItem<String> { |
| 184 | StringPool::Ref value; |
| 185 | |
| 186 | String(const StringPool::Ref& ref); |
| 187 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | bool flatten(android::Res_value* outValue) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 189 | String* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | struct StyledString : public BaseItem<StyledString> { |
| 194 | StringPool::StyleRef value; |
| 195 | |
| 196 | StyledString(const StringPool::StyleRef& ref); |
| 197 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | bool flatten(android::Res_value* outValue) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 199 | StyledString* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 200 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | struct FileReference : public BaseItem<FileReference> { |
| 204 | StringPool::Ref path; |
| 205 | |
| 206 | FileReference() = default; |
| 207 | FileReference(const StringPool::Ref& path); |
| 208 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 209 | bool flatten(android::Res_value* outValue) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 210 | FileReference* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | /** |
| 215 | * Represents any other android::Res_value. |
| 216 | */ |
| 217 | struct BinaryPrimitive : public BaseItem<BinaryPrimitive> { |
| 218 | android::Res_value value; |
| 219 | |
| 220 | BinaryPrimitive() = default; |
| 221 | BinaryPrimitive(const android::Res_value& val); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 222 | BinaryPrimitive(uint8_t dataType, uint32_t data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 224 | bool flatten(android::Res_value* outValue) const override; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 225 | BinaryPrimitive* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 226 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 229 | struct Attribute : public BaseValue<Attribute> { |
| 230 | struct Symbol { |
| 231 | Reference symbol; |
| 232 | uint32_t value; |
| 233 | }; |
| 234 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 235 | bool weak; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 236 | uint32_t typeMask; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 237 | int32_t minInt; |
| 238 | int32_t maxInt; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 239 | std::vector<Symbol> symbols; |
| 240 | |
| 241 | Attribute(bool w, uint32_t t = 0u); |
| 242 | |
| 243 | bool isWeak() const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | Attribute* clone(StringPool* newPool) const override; |
| 245 | void printMask(std::ostream* out) const; |
| 246 | void print(std::ostream* out) const override; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 247 | bool matches(const Item* item, DiagMessage* outMsg) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | struct Style : public BaseValue<Style> { |
| 251 | struct Entry { |
| 252 | Reference key; |
| 253 | std::unique_ptr<Item> value; |
| 254 | }; |
| 255 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 256 | Maybe<Reference> parent; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * If set to true, the parent was auto inferred from the |
| 260 | * style's name. |
| 261 | */ |
| 262 | bool parentInferred = false; |
| 263 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 264 | std::vector<Entry> entries; |
| 265 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 266 | Style* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -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 | |
| 270 | struct Array : public BaseValue<Array> { |
| 271 | std::vector<std::unique_ptr<Item>> items; |
| 272 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 273 | Array* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 274 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | struct Plural : public BaseValue<Plural> { |
| 278 | enum { |
| 279 | Zero = 0, |
| 280 | One, |
| 281 | Two, |
| 282 | Few, |
| 283 | Many, |
| 284 | Other, |
| 285 | Count |
| 286 | }; |
| 287 | |
| 288 | std::array<std::unique_ptr<Item>, Count> values; |
| 289 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 290 | Plural* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 291 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | struct Styleable : public BaseValue<Styleable> { |
| 295 | std::vector<Reference> entries; |
| 296 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 297 | Styleable* clone(StringPool* newPool) const override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 298 | void print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * Stream operator for printing Value objects. |
| 303 | */ |
| 304 | inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 305 | value.print(&out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 306 | return out; |
| 307 | } |
| 308 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 309 | inline ::std::ostream& operator<<(::std::ostream& out, const Attribute::Symbol& s) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 310 | if (s.symbol.name) { |
| 311 | out << s.symbol.name.value().entry; |
| 312 | } else { |
| 313 | out << "???"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 314 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 315 | return out << "=" << s.value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | } // namespace aapt |
| 319 | |
| 320 | #endif // AAPT_RESOURCE_VALUES_H |