ResourceValues.h revision 3124e7ca0f582c8d54a9b4cf560c25dfef77ac2a
16f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski/*
26f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
36f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
46f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
56f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * you may not use this file except in compliance with the License.
66f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * You may obtain a copy of the License at
76f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
86f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
96f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Unless required by applicable law or agreed to in writing, software
116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * See the License for the specific language governing permissions and
146f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * limitations under the License.
156f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski */
166f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
176f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#ifndef AAPT_RESOURCE_VALUES_H
186f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#define AAPT_RESOURCE_VALUES_H
196f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
20ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include <array>
21c744ae8aca97edfb2422598ea620e8219449fa9bAdam Lesinski#include <limits>
22ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include <ostream>
23ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include <vector>
24ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski
25ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "androidfw/ResourceTypes.h"
26d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski#include "androidfw/StringPiece.h"
27ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski
28a587065721053ad54e34f484868142407d59512dAdam Lesinski#include "Diagnostics.h"
296f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include "Resource.h"
306f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include "StringPool.h"
31355f285ffd000f6cfe76680eb22d010546d124bbAdam Lesinski#include "io/File.h"
32a587065721053ad54e34f484868142407d59512dAdam Lesinski#include "util/Maybe.h"
336f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
346f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskinamespace aapt {
356f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
361ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskistruct RawValueVisitor;
376f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
387542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// A resource value. This is an all-encompassing representation
397542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// of Item and Map and their subclasses. The way to do
407542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// type specific operations is to check the Value's type() and
417542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// cast it to the appropriate subclass. This isn't super clean,
427542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// but it is the simplest strategy.
435924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinskiclass Value {
445924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski public:
45cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  virtual ~Value() = default;
46cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
477542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Whether this value is weak and can be overridden without warning or error. Default is false.
48ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool IsWeak() const { return weak_; }
49cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
50ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void SetWeak(bool val) { weak_ = val; }
51cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
527542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Whether the value is marked as translatable.
53cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  // This does not persist when flattened.
54cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  // It is only used during compilation phase.
557542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  void SetTranslatable(bool val) { translatable_ = val; }
56cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
57cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  // Default true.
587542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  bool IsTranslatable() const { return translatable_; }
59cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
607542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Returns the source where this value was defined.
61ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  const Source& GetSource() const { return source_; }
62cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
63ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void SetSource(const Source& source) { source_ = source; }
64cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
65ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void SetSource(Source&& source) { source_ = std::move(source); }
66cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
677542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Returns the comment that was associated with this resource.
68ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  const std::string& GetComment() const { return comment_; }
69cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
70d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski  void SetComment(const android::StringPiece& str) { comment_ = str.to_string(); }
71cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
72ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void SetComment(std::string&& str) { comment_ = std::move(str); }
73cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
74ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  virtual bool Equals(const Value* value) const = 0;
75cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
767542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Calls the appropriate overload of ValueVisitor.
77ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  virtual void Accept(RawValueVisitor* visitor) = 0;
78cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
797542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Clone the value. `new_pool` is the new StringPool that
807542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // any resources with strings should use when copying their string.
81ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  virtual Value* Clone(StringPool* new_pool) const = 0;
82cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
837542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Human readable printout of this value.
84ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  virtual void Print(std::ostream* out) const = 0;
85cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
865924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski  friend std::ostream& operator<<(std::ostream& out, const Value& value);
875924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski
88cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski protected:
89ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Source source_;
90ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  std::string comment_;
91ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool weak_ = false;
927542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  bool translatable_ = true;
936f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
946f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
957542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// Inherit from this to get visitor accepting implementations for free.
966f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskitemplate <typename Derived>
976f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct BaseValue : public Value {
98ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Accept(RawValueVisitor* visitor) override;
996f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
1006f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1017542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// A resource item with a single value. This maps to android::ResTable_entry.
1026f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Item : public Value {
1037542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Clone the Item.
104ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  virtual Item* Clone(StringPool* new_pool) const override = 0;
105cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
1067542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Fills in an android::Res_value structure with this Item's binary representation.
1077542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Returns false if an error occurred.
108ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  virtual bool Flatten(android::Res_value* out_value) const = 0;
1096f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
1106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1117542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// Inherit from this to get visitor accepting implementations for free.
1126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskitemplate <typename Derived>
1136f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct BaseItem : public Item {
114ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Accept(RawValueVisitor* visitor) override;
1156f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
1166f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1177542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
1187542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// A reference can be symbolic (with the name set to a valid resource name) or be
1197542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// numeric (the id is set to a valid resource ID).
1206f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Reference : public BaseItem<Reference> {
121cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  enum class Type {
122cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    kResource,
123cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    kAttribute,
124cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  };
125cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
126cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  Maybe<ResourceName> name;
127cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  Maybe<ResourceId> id;
128ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Reference::Type reference_type;
129ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool private_reference = false;
130cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
131cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  Reference();
132cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
133cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit Reference(const ResourceId& i, Type type = Type::kResource);
134ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Reference(const ResourceNameRef& n, const ResourceId& i);
135cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
136ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
137ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out_value) const override;
138ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Reference* Clone(StringPool* new_pool) const override;
139ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
1406f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
1416f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1428197cc460e02c6445434eace435e3d38ebe475c6Adam Lesinskibool operator<(const Reference&, const Reference&);
1438197cc460e02c6445434eace435e3d38ebe475c6Adam Lesinskibool operator==(const Reference&, const Reference&);
1448197cc460e02c6445434eace435e3d38ebe475c6Adam Lesinski
1457542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// An ID resource. Has no real value, just a place holder.
1466f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Id : public BaseItem<Id> {
147ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Id() { weak_ = true; }
148ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
149ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out) const override;
150ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Id* Clone(StringPool* new_pool) const override;
151ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
1526f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
1536f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1547542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace.
1557542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// This shall *NOT* end up in the final resource table.
1566f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct RawString : public BaseItem<RawString> {
157cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  StringPool::Ref value;
1586f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
159cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit RawString(const StringPool::Ref& ref);
1606f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
161ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
162ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out_value) const override;
163ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  RawString* Clone(StringPool* new_pool) const override;
164ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
1656f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
1666f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1677542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// Identifies a range of characters in a string that are untranslatable.
1687542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// These should not be pseudolocalized. The start and end indices are measured in bytes.
1697542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinskistruct UntranslatableSection {
1707542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Start offset inclusive.
1717542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  size_t start;
1727542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski
1737542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // End offset exclusive.
1747542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  size_t end;
1757542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski};
1767542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski
1777542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinskiinline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) {
1787542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  return a.start == b.start && a.end == b.end;
1797542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski}
1807542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski
1817542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinskiinline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) {
1827542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  return a.start != b.start || a.end != b.end;
1837542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski}
1847542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski
1856f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct String : public BaseItem<String> {
186cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  StringPool::Ref value;
1876f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1887542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Sections of the string to NOT translate. Mainly used
1897542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // for pseudolocalization. This data is NOT persisted
1907542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // in any format.
1917542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  std::vector<UntranslatableSection> untranslatable_sections;
1927542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski
193cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit String(const StringPool::Ref& ref);
1946f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
195ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
196ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out_value) const override;
197ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  String* Clone(StringPool* new_pool) const override;
198ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
1996f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
2006f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2016f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct StyledString : public BaseItem<StyledString> {
202cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  StringPool::StyleRef value;
2036f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2047542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // Sections of the string to NOT translate. Mainly used
2057542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // for pseudolocalization. This data is NOT persisted
2067542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // in any format.
2077542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  std::vector<UntranslatableSection> untranslatable_sections;
2087542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski
209cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit StyledString(const StringPool::StyleRef& ref);
2106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
211ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
212ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out_value) const override;
213ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  StyledString* Clone(StringPool* new_pool) const override;
214ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
2156f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
2166f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2176f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct FileReference : public BaseItem<FileReference> {
218cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  StringPool::Ref path;
2196f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2207542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // A handle to the file object from which this file can be read.
2217542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // This field is NOT persisted in any format. It is transient.
222cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  io::IFile* file = nullptr;
223355f285ffd000f6cfe76680eb22d010546d124bbAdam Lesinski
224cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  FileReference() = default;
225cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit FileReference(const StringPool::Ref& path);
2266f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
227ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
228ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out_value) const override;
229ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  FileReference* Clone(StringPool* new_pool) const override;
230ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
2316f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
2326f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2337542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski// Represents any other android::Res_value.
2346f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
235cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  android::Res_value value;
2366f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
237cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  BinaryPrimitive() = default;
238cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit BinaryPrimitive(const android::Res_value& val);
239cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  BinaryPrimitive(uint8_t dataType, uint32_t data);
2406f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
241ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
242ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Flatten(android::Res_value* out_value) const override;
243ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  BinaryPrimitive* Clone(StringPool* new_pool) const override;
244ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
2456f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
2466f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2476f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Attribute : public BaseValue<Attribute> {
248cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  struct Symbol {
249cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    Reference symbol;
250cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    uint32_t value;
2515924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski
2525924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski    friend std::ostream& operator<<(std::ostream& out, const Symbol& symbol);
253cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  };
254cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
255ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  uint32_t type_mask;
256ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  int32_t min_int;
257ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  int32_t max_int;
258cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::vector<Symbol> symbols;
259cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
260c744ae8aca97edfb2422598ea620e8219449fa9bAdam Lesinski  Attribute();
261cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  explicit Attribute(bool w, uint32_t t = 0u);
262cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
263ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
264ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Attribute* Clone(StringPool* new_pool) const override;
265ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void PrintMask(std::ostream* out) const;
266ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
2673124e7ca0f582c8d54a9b4cf560c25dfef77ac2aAdam Lesinski  bool Matches(const Item& item, DiagMessage* out_msg = nullptr) const;
2686f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
2696f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2706f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Style : public BaseValue<Style> {
271cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  struct Entry {
272cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    Reference key;
273cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski    std::unique_ptr<Item> value;
2745924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski
2755924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski    friend std::ostream& operator<<(std::ostream& out, const Entry& entry);
276cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  };
2776f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
278cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  Maybe<Reference> parent;
279bdaa092a193d8ddccbd9ad8434be97878e6ded59Adam Lesinski
2807542162cb1b1fd2ce8a26dd7f3fedc8de8160d38Adam Lesinski  // If set to true, the parent was auto inferred from the style's name.
281ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool parent_inferred = false;
282bdaa092a193d8ddccbd9ad8434be97878e6ded59Adam Lesinski
283cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::vector<Entry> entries;
2846f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
285ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
286ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Style* Clone(StringPool* new_pool) const override;
287ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
2885924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski
2895924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski  // Merges `style` into this Style. All identical attributes of `style` take precedence, including
2905924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski  // the parent, if there is one.
2915924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski  void MergeWith(Style* style, StringPool* pool);
2926f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
2936f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
2946f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Array : public BaseValue<Array> {
295cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::vector<std::unique_ptr<Item>> items;
2966f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
297ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
298ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Array* Clone(StringPool* new_pool) const override;
299ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
3006f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
3016f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
3026f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Plural : public BaseValue<Plural> {
303cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  enum { Zero = 0, One, Two, Few, Many, Other, Count };
304cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
305cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::array<std::unique_ptr<Item>, Count> values;
306cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
307ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
308ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Plural* Clone(StringPool* new_pool) const override;
309ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
3106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
3116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
3126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct Styleable : public BaseValue<Styleable> {
313cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  std::vector<Reference> entries;
3146f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
315ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  bool Equals(const Value* value) const override;
316ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  Styleable* Clone(StringPool* newPool) const override;
317ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void Print(std::ostream* out) const override;
318ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void MergeWith(Styleable* styleable);
3196f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
3206f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
3215924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinskitemplate <typename T>
3225924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinskitypename std::enable_if<std::is_base_of<Value, T>::value, std::ostream&>::type operator<<(
3235924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski    std::ostream& out, const std::unique_ptr<T>& value) {
3245924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski  if (value == nullptr) {
3255924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski    out << "NULL";
326cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  } else {
3275924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski    value->Print(&out);
328cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  }
3295924d8c9ab7bd8614e8bd99864903ce9d50f3bf7Adam Lesinski  return out;
3306f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
3316f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
332cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski}  // namespace aapt
3336f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
334cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski#endif  // AAPT_RESOURCE_VALUES_H
335