/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AAPT_RESOURCE_VALUES_H #define AAPT_RESOURCE_VALUES_H #include #include #include #include "androidfw/ResourceTypes.h" #include "Diagnostics.h" #include "Resource.h" #include "StringPool.h" #include "io/File.h" #include "util/Maybe.h" namespace aapt { struct RawValueVisitor; /** * A resource value. This is an all-encompassing representation * of Item and Map and their subclasses. The way to do * type specific operations is to check the Value's type() and * cast it to the appropriate subclass. This isn't super clean, * but it is the simplest strategy. */ struct Value { virtual ~Value() = default; /** * Whether this value is weak and can be overridden without * warning or error. Default is false. */ bool IsWeak() const { return weak_; } void SetWeak(bool val) { weak_ = val; } // Whether the value is marked as translateable. // This does not persist when flattened. // It is only used during compilation phase. void SetTranslateable(bool val) { translateable_ = val; } // Default true. bool IsTranslateable() const { return translateable_; } /** * Returns the source where this value was defined. */ const Source& GetSource() const { return source_; } void SetSource(const Source& source) { source_ = source; } void SetSource(Source&& source) { source_ = std::move(source); } /** * Returns the comment that was associated with this resource. */ const std::string& GetComment() const { return comment_; } void SetComment(const StringPiece& str) { comment_ = str.ToString(); } void SetComment(std::string&& str) { comment_ = std::move(str); } virtual bool Equals(const Value* value) const = 0; /** * Calls the appropriate overload of ValueVisitor. */ virtual void Accept(RawValueVisitor* visitor) = 0; /** * Clone the value. new_pool is the new StringPool that * any resources with strings should use when copying their string. */ virtual Value* Clone(StringPool* new_pool) const = 0; /** * Human readable printout of this value. */ virtual void Print(std::ostream* out) const = 0; protected: Source source_; std::string comment_; bool weak_ = false; bool translateable_ = true; }; /** * Inherit from this to get visitor accepting implementations for free. */ template struct BaseValue : public Value { void Accept(RawValueVisitor* visitor) override; }; /** * A resource item with a single value. This maps to android::ResTable_entry. */ struct Item : public Value { /** * Clone the Item. */ virtual Item* Clone(StringPool* new_pool) const override = 0; /** * Fills in an android::Res_value structure with this Item's binary * representation. * Returns false if an error occurred. */ virtual bool Flatten(android::Res_value* out_value) const = 0; }; /** * Inherit from this to get visitor accepting implementations for free. */ template struct BaseItem : public Item { void Accept(RawValueVisitor* visitor) override; }; /** * A reference to another resource. This maps to * android::Res_value::TYPE_REFERENCE. * * A reference can be symbolic (with the name set to a valid resource name) or * be * numeric (the id is set to a valid resource ID). */ struct Reference : public BaseItem { enum class Type { kResource, kAttribute, }; Maybe name; Maybe id; Reference::Type reference_type; bool private_reference = false; Reference(); explicit Reference(const ResourceNameRef& n, Type type = Type::kResource); explicit Reference(const ResourceId& i, Type type = Type::kResource); Reference(const ResourceNameRef& n, const ResourceId& i); bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out_value) const override; Reference* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; bool operator<(const Reference&, const Reference&); bool operator==(const Reference&, const Reference&); /** * An ID resource. Has no real value, just a place holder. */ struct Id : public BaseItem { Id() { weak_ = true; } bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out) const override; Id* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; /** * A raw, unprocessed string. This may contain quotations, * escape sequences, and whitespace. This shall *NOT* * end up in the final resource table. */ struct RawString : public BaseItem { StringPool::Ref value; explicit RawString(const StringPool::Ref& ref); bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out_value) const override; RawString* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; struct String : public BaseItem { StringPool::Ref value; explicit String(const StringPool::Ref& ref); bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out_value) const override; String* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; struct StyledString : public BaseItem { StringPool::StyleRef value; explicit StyledString(const StringPool::StyleRef& ref); bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out_value) const override; StyledString* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; struct FileReference : public BaseItem { StringPool::Ref path; /** * A handle to the file object from which this file can be read. */ io::IFile* file = nullptr; FileReference() = default; explicit FileReference(const StringPool::Ref& path); bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out_value) const override; FileReference* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; /** * Represents any other android::Res_value. */ struct BinaryPrimitive : public BaseItem { android::Res_value value; BinaryPrimitive() = default; explicit BinaryPrimitive(const android::Res_value& val); BinaryPrimitive(uint8_t dataType, uint32_t data); bool Equals(const Value* value) const override; bool Flatten(android::Res_value* out_value) const override; BinaryPrimitive* Clone(StringPool* new_pool) const override; void Print(std::ostream* out) const override; }; struct Attribute : public BaseValue { struct Symbol { Reference symbol; uint32_t value; }; uint32_t type_mask; int32_t min_int; int32_t max_int; std::vector symbols; explicit Attribute(bool w, uint32_t t = 0u); bool Equals(const Value* value) const override; Attribute* Clone(StringPool* new_pool) const override; void PrintMask(std::ostream* out) const; void Print(std::ostream* out) const override; bool Matches(const Item* item, DiagMessage* out_msg) const; }; struct Style : public BaseValue