| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_RESOURCE_VALUES_H |
| 18 |
#define AAPT_RESOURCE_VALUES_H |
| 19 |
|
| 20 |
#include <array> |
| 21 |
#include <limits> |
| 22 |
#include <ostream> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "androidfw/ResourceTypes.h" |
| 26 |
#include "androidfw/StringPiece.h" |
| 27 |
|
| 28 |
#include "Diagnostics.h" |
| 29 |
#include "Resource.h" |
| 30 |
#include "StringPool.h" |
| 31 |
#include "ValueTransformer.h" |
| 32 |
#include "io/File.h" |
| 33 |
#include "text/Printer.h" |
| 34 |
|
| 35 |
namespace aapt { |
| 36 |
|
| 37 |
class ValueVisitor; |
| 38 |
class ConstValueVisitor; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class Value { |
| 46 |
public: |
| 47 |
virtual ~Value() = default; |
| 48 |
|
| 49 |
|
| 50 |
bool IsWeak() const { |
| 51 |
return weak_; |
| 52 |
} |
| 53 |
|
| 54 |
void SetWeak(bool val) { |
| 55 |
weak_ = val; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
void SetTranslatable(bool val) { |
| 61 |
translatable_ = val; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
bool IsTranslatable() const { |
| 66 |
return translatable_; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
const Source& GetSource() const { |
| 71 |
return source_; |
| 72 |
} |
| 73 |
|
| 74 |
void SetSource(const Source& source) { |
| 75 |
source_ = source; |
| 76 |
} |
| 77 |
|
| 78 |
void SetSource(Source&& source) { |
| 79 |
source_ = std::move(source); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
const std::string& GetComment() const { |
| 84 |
return comment_; |
| 85 |
} |
| 86 |
|
| 87 |
void SetComment(const android::StringPiece& str) { |
| 88 |
comment_ = str.to_string(); |
| 89 |
} |
| 90 |
|
| 91 |
void SetComment(std::string&& str) { |
| 92 |
comment_ = std::move(str); |
| 93 |
} |
| 94 |
|
| 95 |
virtual bool Equals(const Value* value) const = 0; |
| 96 |
|
| 97 |
|
| 98 |
virtual void Accept(ValueVisitor* visitor) = 0; |
| 99 |
|
| 100 |
|
| 101 |
virtual void Accept(ConstValueVisitor* visitor) const = 0; |
| 102 |
|
| 103 |
|
| 104 |
std::unique_ptr<Value> Transform(ValueTransformer& transformer) const; |
| 105 |
|
| 106 |
|
| 107 |
virtual void Print(std::ostream* out) const = 0; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
virtual void PrettyPrint(text::Printer* printer) const; |
| 112 |
|
| 113 |
friend std::ostream& operator<<(std::ostream& out, const Value& value); |
| 114 |
|
| 115 |
protected: |
| 116 |
Source source_; |
| 117 |
std::string comment_; |
| 118 |
bool weak_ = false; |
| 119 |
bool translatable_ = true; |
| 120 |
|
| 121 |
private: |
| 122 |
virtual Value* TransformValueImpl(ValueTransformer& transformer) const = 0; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
template <typename Derived> |
| 127 |
struct BaseValue : public Value { |
| 128 |
void Accept(ValueVisitor* visitor) override; |
| 129 |
void Accept(ConstValueVisitor* visitor) const override; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
struct Item : public Value { |
| 134 |
|
| 135 |
|
| 136 |
virtual bool Flatten(android::Res_value* out_value) const = 0; |
| 137 |
|
| 138 |
|
| 139 |
std::unique_ptr<Item> Transform(ValueTransformer& transformer) const; |
| 140 |
|
| 141 |
private: |
| 142 |
virtual Item* TransformItemImpl(ValueTransformer& transformer) const = 0; |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
template <typename Derived> |
| 147 |
struct BaseItem : public Item { |
| 148 |
void Accept(ValueVisitor* visitor) override; |
| 149 |
void Accept(ConstValueVisitor* visitor) const override; |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
struct Reference : public TransformableItem<Reference, BaseItem<Reference>> { |
| 156 |
enum class Type : uint8_t { |
| 157 |
kResource, |
| 158 |
kAttribute, |
| 159 |
}; |
| 160 |
|
| 161 |
std::optional<ResourceName> name; |
| 162 |
std::optional<ResourceId> id; |
| 163 |
std::optional<uint32_t> type_flags; |
| 164 |
Reference::Type reference_type; |
| 165 |
bool private_reference = false; |
| 166 |
bool is_dynamic = false; |
| 167 |
bool allow_raw = false; |
| 168 |
|
| 169 |
Reference(); |
| 170 |
explicit Reference(const ResourceNameRef& n, Type type = Type::kResource); |
| 171 |
explicit Reference(const ResourceId& i, Type type = Type::kResource); |
| 172 |
Reference(const ResourceNameRef& n, const ResourceId& i); |
| 173 |
|
| 174 |
bool Equals(const Value* value) const override; |
| 175 |
bool Flatten(android::Res_value* out_value) const override; |
| 176 |
void Print(std::ostream* out) const override; |
| 177 |
void PrettyPrint(text::Printer* printer) const override; |
| 178 |
|
| 179 |
|
| 180 |
void PrettyPrint(const android::StringPiece& package, text::Printer* printer) const; |
| 181 |
}; |
| 182 |
|
| 183 |
bool operator<(const Reference&, const Reference&); |
| 184 |
bool operator==(const Reference&, const Reference&); |
| 185 |
|
| 186 |
|
| 187 |
struct Id : public TransformableItem<Id, BaseItem<Id>> { |
| 188 |
Id() { |
| 189 |
weak_ = true; |
| 190 |
} |
| 191 |
|
| 192 |
bool Equals(const Value* value) const override; |
| 193 |
bool Flatten(android::Res_value* out) const override; |
| 194 |
void Print(std::ostream* out) const override; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
struct RawString : public TransformableItem<RawString, BaseItem<RawString>> { |
| 200 |
StringPool::Ref value; |
| 201 |
|
| 202 |
explicit RawString(const StringPool::Ref& ref); |
| 203 |
|
| 204 |
bool Equals(const Value* value) const override; |
| 205 |
bool Flatten(android::Res_value* out_value) const override; |
| 206 |
void Print(std::ostream* out) const override; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
struct UntranslatableSection { |
| 212 |
|
| 213 |
size_t start; |
| 214 |
|
| 215 |
|
| 216 |
size_t end; |
| 217 |
}; |
| 218 |
|
| 219 |
inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 220 |
return a.start == b.start && a.end == b.end; |
| 221 |
} |
| 222 |
|
| 223 |
inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 224 |
return a.start != b.start || a.end != b.end; |
| 225 |
} |
| 226 |
|
| 227 |
struct String : public TransformableItem<String, BaseItem<String>> { |
| 228 |
StringPool::Ref value; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
std::vector<UntranslatableSection> untranslatable_sections; |
| 234 |
|
| 235 |
explicit String(const StringPool::Ref& ref); |
| 236 |
|
| 237 |
bool Equals(const Value* value) const override; |
| 238 |
bool Flatten(android::Res_value* out_value) const override; |
| 239 |
void Print(std::ostream* out) const override; |
| 240 |
void PrettyPrint(text::Printer* printer) const override; |
| 241 |
}; |
| 242 |
|
| 243 |
struct StyledString : public TransformableItem<StyledString, BaseItem<StyledString>> { |
| 244 |
StringPool::StyleRef value; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
std::vector<UntranslatableSection> untranslatable_sections; |
| 250 |
|
| 251 |
explicit StyledString(const StringPool::StyleRef& ref); |
| 252 |
|
| 253 |
bool Equals(const Value* value) const override; |
| 254 |
bool Flatten(android::Res_value* out_value) const override; |
| 255 |
void Print(std::ostream* out) const override; |
| 256 |
}; |
| 257 |
|
| 258 |
struct FileReference : public TransformableItem<FileReference, BaseItem<FileReference>> { |
| 259 |
StringPool::Ref path; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
io::IFile* file = nullptr; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
ResourceFile::Type type = ResourceFile::Type::kUnknown; |
| 268 |
|
| 269 |
FileReference() = default; |
| 270 |
explicit FileReference(const StringPool::Ref& path); |
| 271 |
|
| 272 |
bool Equals(const Value* value) const override; |
| 273 |
bool Flatten(android::Res_value* out_value) const override; |
| 274 |
void Print(std::ostream* out) const override; |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
struct BinaryPrimitive : public TransformableItem<BinaryPrimitive, BaseItem<BinaryPrimitive>> { |
| 279 |
android::Res_value value; |
| 280 |
|
| 281 |
BinaryPrimitive() = default; |
| 282 |
explicit BinaryPrimitive(const android::Res_value& val); |
| 283 |
BinaryPrimitive(uint8_t dataType, uint32_t data); |
| 284 |
|
| 285 |
bool Equals(const Value* value) const override; |
| 286 |
bool Flatten(android::Res_value* out_value) const override; |
| 287 |
void Print(std::ostream* out) const override; |
| 288 |
void PrettyPrint(text::Printer* printer) const override; |
| 289 |
}; |
| 290 |
|
| 291 |
struct Attribute : public TransformableValue<Attribute, BaseValue<Attribute>> { |
| 292 |
struct Symbol { |
| 293 |
Reference symbol; |
| 294 |
uint32_t value; |
| 295 |
uint8_t type; |
| 296 |
|
| 297 |
friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol); |
| 298 |
}; |
| 299 |
|
| 300 |
uint32_t type_mask; |
| 301 |
int32_t min_int; |
| 302 |
int32_t max_int; |
| 303 |
std::vector<Symbol> symbols; |
| 304 |
|
| 305 |
explicit Attribute(uint32_t t = 0u); |
| 306 |
|
| 307 |
bool Equals(const Value* value) const override; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
bool IsCompatibleWith(const Attribute& attr) const; |
| 313 |
|
| 314 |
std::string MaskString() const; |
| 315 |
static std::string MaskString(uint32_t type_mask); |
| 316 |
|
| 317 |
void Print(std::ostream* out) const override; |
| 318 |
bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const; |
| 319 |
}; |
| 320 |
|
| 321 |
struct Style : public TransformableValue<Style, BaseValue<Style>> { |
| 322 |
struct Entry { |
| 323 |
Reference key; |
| 324 |
std::unique_ptr<Item> value; |
| 325 |
|
| 326 |
friend std::ostream& operator<<(std::ostream& out, const Entry& entry); |
| 327 |
}; |
| 328 |
|
| 329 |
std::optional<Reference> parent; |
| 330 |
|
| 331 |
|
| 332 |
bool parent_inferred = false; |
| 333 |
|
| 334 |
std::vector<Entry> entries; |
| 335 |
|
| 336 |
bool Equals(const Value* value) const override; |
| 337 |
void Print(std::ostream* out) const override; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
void MergeWith(Style* style, StringPool* pool); |
| 342 |
}; |
| 343 |
|
| 344 |
struct Array : public TransformableValue<Array, BaseValue<Array>> { |
| 345 |
std::vector<std::unique_ptr<Item>> elements; |
| 346 |
|
| 347 |
bool Equals(const Value* value) const override; |
| 348 |
void Print(std::ostream* out) const override; |
| 349 |
}; |
| 350 |
|
| 351 |
struct Plural : public TransformableValue<Plural, BaseValue<Plural>> { |
| 352 |
enum { Zero = 0, One, Two, Few, Many, Other, Count }; |
| 353 |
|
| 354 |
std::array<std::unique_ptr<Item>, Count> values; |
| 355 |
|
| 356 |
bool Equals(const Value* value) const override; |
| 357 |
void Print(std::ostream* out) const override; |
| 358 |
}; |
| 359 |
|
| 360 |
struct Styleable : public TransformableValue<Styleable, BaseValue<Styleable>> { |
| 361 |
std::vector<Reference> entries; |
| 362 |
|
| 363 |
bool Equals(const Value* value) const override; |
| 364 |
void Print(std::ostream* out) const override; |
| 365 |
void MergeWith(Styleable* styleable); |
| 366 |
}; |
| 367 |
|
| 368 |
struct Macro : public TransformableValue<Macro, BaseValue<Macro>> { |
| 369 |
std::string raw_value; |
| 370 |
StyleString style_string; |
| 371 |
std::vector<UntranslatableSection> untranslatable_sections; |
| 372 |
|
| 373 |
struct Namespace { |
| 374 |
std::string alias; |
| 375 |
std::string package_name; |
| 376 |
bool is_private; |
| 377 |
|
| 378 |
bool operator==(const Namespace& right) const { |
| 379 |
return alias == right.alias && package_name == right.package_name && |
| 380 |
is_private == right.is_private; |
| 381 |
} |
| 382 |
}; |
| 383 |
|
| 384 |
std::vector<Namespace> alias_namespaces; |
| 385 |
|
| 386 |
bool Equals(const Value* value) const override; |
| 387 |
void Print(std::ostream* out) const override; |
| 388 |
}; |
| 389 |
|
| 390 |
template <typename T> |
| 391 |
typename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<( |
| 392 |
std::ostream& out, const std::unique_ptr<T>& value) { |
| 393 |
if (value == nullptr) { |
| 394 |
out << "NULL"; |
| 395 |
} else { |
| 396 |
value->Print(&out); |
| 397 |
} |
| 398 |
return out; |
| 399 |
} |
| 400 |
|
| 401 |
struct CloningValueTransformer : public ValueTransformer { |
| 402 |
explicit CloningValueTransformer(StringPool* new_pool); |
| 403 |
|
| 404 |
std::unique_ptr<Reference> TransformDerived(const Reference* value) override; |
| 405 |
std::unique_ptr<Id> TransformDerived(const Id* value) override; |
| 406 |
std::unique_ptr<RawString> TransformDerived(const RawString* value) override; |
| 407 |
std::unique_ptr<String> TransformDerived(const String* value) override; |
| 408 |
std::unique_ptr<StyledString> TransformDerived(const StyledString* value) override; |
| 409 |
std::unique_ptr<FileReference> TransformDerived(const FileReference* value) override; |
| 410 |
std::unique_ptr<BinaryPrimitive> TransformDerived(const BinaryPrimitive* value) override; |
| 411 |
std::unique_ptr<Attribute> TransformDerived(const Attribute* value) override; |
| 412 |
std::unique_ptr<Style> TransformDerived(const Style* value) override; |
| 413 |
std::unique_ptr<Array> TransformDerived(const Array* value) override; |
| 414 |
std::unique_ptr<Plural> TransformDerived(const Plural* value) override; |
| 415 |
std::unique_ptr<Styleable> TransformDerived(const Styleable* value) override; |
| 416 |
std::unique_ptr<Macro> TransformDerived(const Macro* value) override; |
| 417 |
}; |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
#endif |