1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* 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 "Diagnostics.h"
#include "Resource.h"
#include "StringPool.h"
#include "io/File.h"
#include "util/Maybe.h"
#include <array>
#include <androidfw/ResourceTypes.h>
#include <ostream>
#include <vector>
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 mWeak;
}
void setWeak(bool val) {
mWeak = val;
}
/**
* Returns the source where this value was defined.
*/
const Source& getSource() const {
return mSource;
}
void setSource(const Source& source) {
mSource = source;
}
void setSource(Source&& source) {
mSource = std::move(source);
}
/**
* Returns the comment that was associated with this resource.
*/
StringPiece16 getComment() const {
return mComment;
}
void setComment(const StringPiece16& str) {
mComment = str.toString();
}
void setComment(std::u16string&& str) {
mComment = std::move(str);
}
/**
* Calls the appropriate overload of ValueVisitor.
*/
virtual void accept(RawValueVisitor* visitor) = 0;
/**
* Clone the value.
*/
virtual Value* clone(StringPool* newPool) const = 0;
/**
* Human readable printout of this value.
*/
virtual void print(std::ostream* out) const = 0;
protected:
Source mSource;
std::u16string mComment;
bool mWeak = false;
};
/**
* Inherit from this to get visitor accepting implementations for free.
*/
template <typename Derived>
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* newPool) 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* outValue) const = 0;
};
/**
* Inherit from this to get visitor accepting implementations for free.
*/
template <typename Derived>
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<Reference> {
enum class Type {
kResource,
kAttribute,
};
Maybe<ResourceName> name;
Maybe<ResourceId> id;
Reference::Type referenceType;
bool privateReference = false;
Reference();
explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
explicit Reference(const ResourceId& i, Type type = Type::kResource);
bool flatten(android::Res_value* outValue) const override;
Reference* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
/**
* An ID resource. Has no real value, just a place holder.
*/
struct Id : public BaseItem<Id> {
Id() { mWeak = true; }
bool flatten(android::Res_value* out) const override;
Id* clone(StringPool* newPool) 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<RawString> {
StringPool::Ref value;
RawString(const StringPool::Ref& ref);
bool flatten(android::Res_value* outValue) const override;
RawString* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
struct String : public BaseItem<String> {
StringPool::Ref value;
String(const StringPool::Ref& ref);
// Whether the string is marked as translateable. This does not persist when flattened.
// It is only used during compilation phase.
void setTranslateable(bool val);
bool isTranslateable() const;
bool flatten(android::Res_value* outValue) const override;
String* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
private:
bool mTranslateable;
};
struct StyledString : public BaseItem<StyledString> {
StringPool::StyleRef value;
StyledString(const StringPool::StyleRef& ref);
// Whether the string is marked as translateable. This does not persist when flattened.
// It is only used during compilation phase.
void setTranslateable(bool val);
bool isTranslateable() const;
bool flatten(android::Res_value* outValue) const override;
StyledString* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
private:
bool mTranslateable;
};
struct FileReference : public BaseItem<FileReference> {
StringPool::Ref path;
/**
* A handle to the file object from which this file can be read.
*/
io::IFile* file = nullptr;
FileReference() = default;
FileReference(const StringPool::Ref& path);
bool flatten(android::Res_value* outValue) const override;
FileReference* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
/**
* Represents any other android::Res_value.
*/
struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
android::Res_value value;
BinaryPrimitive() = default;
BinaryPrimitive(const android::Res_value& val);
BinaryPrimitive(uint8_t dataType, uint32_t data);
bool flatten(android::Res_value* outValue) const override;
BinaryPrimitive* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
struct Attribute : public BaseValue<Attribute> {
struct Symbol {
Reference symbol;
uint32_t value;
};
uint32_t typeMask;
int32_t minInt;
int32_t maxInt;
std::vector<Symbol> symbols;
Attribute(bool w, uint32_t t = 0u);
Attribute* clone(StringPool* newPool) const override;
void printMask(std::ostream* out) const;
void print(std::ostream* out) const override;
bool matches(const Item* item, DiagMessage* outMsg) const;
};
struct Style : public BaseValue<Style> {
struct Entry {
Reference key;
std::unique_ptr<Item> value;
};
Maybe<Reference> parent;
/**
* If set to true, the parent was auto inferred from the
* style's name.
*/
bool parentInferred = false;
std::vector<Entry> entries;
Style* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
struct Array : public BaseValue<Array> {
std::vector<std::unique_ptr<Item>> items;
Array* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
struct Plural : public BaseValue<Plural> {
enum {
Zero = 0,
One,
Two,
Few,
Many,
Other,
Count
};
std::array<std::unique_ptr<Item>, Count> values;
Plural* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
struct Styleable : public BaseValue<Styleable> {
std::vector<Reference> entries;
Styleable* clone(StringPool* newPool) const override;
void print(std::ostream* out) const override;
};
/**
* Stream operator for printing Value objects.
*/
inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
value.print(&out);
return out;
}
inline ::std::ostream& operator<<(::std::ostream& out, const Attribute::Symbol& s) {
if (s.symbol.name) {
out << s.symbol.name.value().entry;
} else {
out << "???";
}
return out << "=" << s.value;
}
} // namespace aapt
#endif // AAPT_RESOURCE_VALUES_H
|