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