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 | #include "Resource.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 19 | #include "ValueVisitor.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 21 | #include "util/Util.h" |
| 22 | #include "flatten/ResourceTypeExtensions.h" |
| 23 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include <androidfw/ResourceTypes.h> |
| 25 | #include <limits> |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | template <typename Derived> |
| 30 | void BaseValue<Derived>::accept(RawValueVisitor* visitor) { |
| 31 | visitor->visit(static_cast<Derived*>(this)); |
| 32 | } |
| 33 | |
| 34 | template <typename Derived> |
| 35 | void BaseItem<Derived>::accept(RawValueVisitor* visitor) { |
| 36 | visitor->visit(static_cast<Derived*>(this)); |
| 37 | } |
| 38 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 39 | bool Value::isWeak() const { |
| 40 | return false; |
| 41 | } |
| 42 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | RawString::RawString(const StringPool::Ref& ref) : value(ref) { |
| 44 | } |
| 45 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 46 | RawString* RawString::clone(StringPool* newPool) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 47 | RawString* rs = new RawString(newPool->makeRef(*value)); |
| 48 | rs->mComment = mComment; |
| 49 | rs->mSource = mSource; |
| 50 | return rs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | bool RawString::flatten(android::Res_value* outValue) const { |
| 54 | outValue->dataType = ExtendedTypes::TYPE_RAW_STRING; |
| 55 | outValue->data = util::hostToDevice32(static_cast<uint32_t>(value.getIndex())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | void RawString::print(std::ostream* out) const { |
| 60 | *out << "(raw string) " << *value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Reference::Reference() : referenceType(Reference::Type::kResource) { |
| 64 | } |
| 65 | |
| 66 | Reference::Reference(const ResourceNameRef& n, Type t) : |
| 67 | name(n.toResourceName()), referenceType(t) { |
| 68 | } |
| 69 | |
| 70 | Reference::Reference(const ResourceId& i, Type type) : id(i), referenceType(type) { |
| 71 | } |
| 72 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | bool Reference::flatten(android::Res_value* outValue) const { |
| 74 | outValue->dataType = (referenceType == Reference::Type::kResource) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 75 | ? android::Res_value::TYPE_REFERENCE |
| 76 | : android::Res_value::TYPE_ATTRIBUTE; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | outValue->data = util::hostToDevice32(id ? id.value().id : 0); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 81 | Reference* Reference::clone(StringPool* /*newPool*/) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | Reference* ref = new Reference(); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 83 | ref->mComment = mComment; |
| 84 | ref->mSource = mSource; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 85 | ref->referenceType = referenceType; |
| 86 | ref->name = name; |
| 87 | ref->id = id; |
| 88 | return ref; |
| 89 | } |
| 90 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 91 | void Reference::print(std::ostream* out) const { |
| 92 | *out << "(reference) "; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | if (referenceType == Reference::Type::kResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 94 | *out << "@"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | *out << "?"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | if (name) { |
| 100 | *out << name.value(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | if (id && !Res_INTERNALID(id.value().id)) { |
| 104 | *out << " " << id.value(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | bool Id::isWeak() const { |
| 109 | return true; |
| 110 | } |
| 111 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | bool Id::flatten(android::Res_value* out) const { |
| 113 | out->dataType = android::Res_value::TYPE_INT_BOOLEAN; |
| 114 | out->data = util::hostToDevice32(0); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 118 | Id* Id::clone(StringPool* /*newPool*/) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 119 | Id* id = new Id(); |
| 120 | id->mComment = mComment; |
| 121 | id->mSource = mSource; |
| 122 | return id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | void Id::print(std::ostream* out) const { |
| 126 | *out << "(id)"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | String::String(const StringPool::Ref& ref) : value(ref) { |
| 130 | } |
| 131 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 132 | bool String::flatten(android::Res_value* outValue) const { |
| 133 | // Verify that our StringPool index is within encode-able limits. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | if (value.getIndex() > std::numeric_limits<uint32_t>::max()) { |
| 135 | return false; |
| 136 | } |
| 137 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 138 | outValue->dataType = android::Res_value::TYPE_STRING; |
| 139 | outValue->data = util::hostToDevice32(static_cast<uint32_t>(value.getIndex())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 143 | String* String::clone(StringPool* newPool) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 144 | String* str = new String(newPool->makeRef(*value)); |
| 145 | str->mComment = mComment; |
| 146 | str->mSource = mSource; |
| 147 | return str; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | void String::print(std::ostream* out) const { |
| 151 | *out << "(string) \"" << *value << "\""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) { |
| 155 | } |
| 156 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | bool StyledString::flatten(android::Res_value* outValue) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 158 | if (value.getIndex() > std::numeric_limits<uint32_t>::max()) { |
| 159 | return false; |
| 160 | } |
| 161 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | outValue->dataType = android::Res_value::TYPE_STRING; |
| 163 | outValue->data = util::hostToDevice32(static_cast<uint32_t>(value.getIndex())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 167 | StyledString* StyledString::clone(StringPool* newPool) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 168 | StyledString* str = new StyledString(newPool->makeRef(value)); |
| 169 | str->mComment = mComment; |
| 170 | str->mSource = mSource; |
| 171 | return str; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 174 | void StyledString::print(std::ostream* out) const { |
| 175 | *out << "(styled string) \"" << *value->str << "\""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | FileReference::FileReference(const StringPool::Ref& _path) : path(_path) { |
| 179 | } |
| 180 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | bool FileReference::flatten(android::Res_value* outValue) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 182 | if (path.getIndex() > std::numeric_limits<uint32_t>::max()) { |
| 183 | return false; |
| 184 | } |
| 185 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | outValue->dataType = android::Res_value::TYPE_STRING; |
| 187 | outValue->data = util::hostToDevice32(static_cast<uint32_t>(path.getIndex())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 191 | FileReference* FileReference::clone(StringPool* newPool) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 192 | FileReference* fr = new FileReference(newPool->makeRef(*path)); |
| 193 | fr->mComment = mComment; |
| 194 | fr->mSource = mSource; |
| 195 | return fr; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | void FileReference::print(std::ostream* out) const { |
| 199 | *out << "(file) " << *path; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) { |
| 203 | } |
| 204 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) { |
| 206 | value.dataType = dataType; |
| 207 | value.data = data; |
| 208 | } |
| 209 | |
| 210 | bool BinaryPrimitive::flatten(android::Res_value* outValue) const { |
| 211 | outValue->dataType = value.dataType; |
| 212 | outValue->data = util::hostToDevice32(value.data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 216 | BinaryPrimitive* BinaryPrimitive::clone(StringPool* /*newPool*/) const { |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 217 | BinaryPrimitive* bp = new BinaryPrimitive(value); |
| 218 | bp->mComment = mComment; |
| 219 | bp->mSource = mSource; |
| 220 | return bp; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 223 | void BinaryPrimitive::print(std::ostream* out) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 224 | switch (value.dataType) { |
| 225 | case android::Res_value::TYPE_NULL: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 226 | *out << "(null)"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | break; |
| 228 | case android::Res_value::TYPE_INT_DEC: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 229 | *out << "(integer) " << value.data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | break; |
| 231 | case android::Res_value::TYPE_INT_HEX: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 232 | *out << "(integer) " << std::hex << value.data << std::dec; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 233 | break; |
| 234 | case android::Res_value::TYPE_INT_BOOLEAN: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 235 | *out << "(boolean) " << (value.data != 0 ? "true" : "false"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 236 | break; |
| 237 | case android::Res_value::TYPE_INT_COLOR_ARGB8: |
| 238 | case android::Res_value::TYPE_INT_COLOR_RGB8: |
| 239 | case android::Res_value::TYPE_INT_COLOR_ARGB4: |
| 240 | case android::Res_value::TYPE_INT_COLOR_RGB4: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 241 | *out << "(color) #" << std::hex << value.data << std::dec; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 242 | break; |
| 243 | default: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | *out << "(unknown 0x" << std::hex << (int) value.dataType << ") 0x" |
| 245 | << std::hex << value.data << std::dec; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 250 | Attribute::Attribute(bool w, uint32_t t) : weak(w), typeMask(t) { |
| 251 | } |
| 252 | |
| 253 | bool Attribute::isWeak() const { |
| 254 | return weak; |
| 255 | } |
| 256 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 257 | Attribute* Attribute::clone(StringPool* /*newPool*/) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 258 | Attribute* attr = new Attribute(weak); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 259 | attr->mComment = mComment; |
| 260 | attr->mSource = mSource; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 261 | attr->typeMask = typeMask; |
| 262 | std::copy(symbols.begin(), symbols.end(), std::back_inserter(attr->symbols)); |
| 263 | return attr; |
| 264 | } |
| 265 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 266 | void Attribute::printMask(std::ostream* out) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 267 | if (typeMask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 268 | *out << "any"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | bool set = false; |
| 273 | if ((typeMask & android::ResTable_map::TYPE_REFERENCE) != 0) { |
| 274 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 275 | set = true; |
| 276 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 277 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 278 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 279 | *out << "reference"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if ((typeMask & android::ResTable_map::TYPE_STRING) != 0) { |
| 283 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 284 | set = true; |
| 285 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 286 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 287 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 288 | *out << "string"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if ((typeMask & android::ResTable_map::TYPE_INTEGER) != 0) { |
| 292 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 293 | set = true; |
| 294 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 295 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 296 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 297 | *out << "integer"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if ((typeMask & android::ResTable_map::TYPE_BOOLEAN) != 0) { |
| 301 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 302 | set = true; |
| 303 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 304 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 305 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 306 | *out << "boolean"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if ((typeMask & android::ResTable_map::TYPE_COLOR) != 0) { |
| 310 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 311 | set = true; |
| 312 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 313 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 314 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 315 | *out << "color"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if ((typeMask & android::ResTable_map::TYPE_FLOAT) != 0) { |
| 319 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 320 | set = true; |
| 321 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 322 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 323 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 324 | *out << "float"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if ((typeMask & android::ResTable_map::TYPE_DIMENSION) != 0) { |
| 328 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 329 | set = true; |
| 330 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 331 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 332 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 333 | *out << "dimension"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if ((typeMask & android::ResTable_map::TYPE_FRACTION) != 0) { |
| 337 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 338 | set = true; |
| 339 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 340 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 341 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 342 | *out << "fraction"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if ((typeMask & android::ResTable_map::TYPE_ENUM) != 0) { |
| 346 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 347 | set = true; |
| 348 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 349 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 350 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 351 | *out << "enum"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if ((typeMask & android::ResTable_map::TYPE_FLAGS) != 0) { |
| 355 | if (!set) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 356 | set = true; |
| 357 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 358 | *out << "|"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 359 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 360 | *out << "flags"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 361 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 364 | void Attribute::print(std::ostream* out) const { |
| 365 | *out << "(attr) "; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 366 | printMask(out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 367 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 368 | if (!symbols.empty()) { |
| 369 | *out << " [" |
| 370 | << util::joiner(symbols.begin(), symbols.end(), ", ") |
| 371 | << "]"; |
| 372 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 373 | |
| 374 | if (weak) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 375 | *out << " [weak]"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 379 | Style* Style::clone(StringPool* newPool) const { |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 380 | Style* style = new Style(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 381 | style->parent = parent; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 382 | style->parentInferred = parentInferred; |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 383 | style->mComment = mComment; |
| 384 | style->mSource = mSource; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 385 | for (auto& entry : entries) { |
| 386 | style->entries.push_back(Entry{ |
| 387 | entry.key, |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 388 | std::unique_ptr<Item>(entry.value->clone(newPool)) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 389 | }); |
| 390 | } |
| 391 | return style; |
| 392 | } |
| 393 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 394 | void Style::print(std::ostream* out) const { |
| 395 | *out << "(style) "; |
| 396 | if (parent && parent.value().name) { |
| 397 | *out << parent.value().name.value(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 398 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 399 | *out << " [" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 400 | << util::joiner(entries.begin(), entries.end(), ", ") |
| 401 | << "]"; |
| 402 | } |
| 403 | |
| 404 | static ::std::ostream& operator<<(::std::ostream& out, const Style::Entry& value) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 405 | if (value.key.name) { |
| 406 | out << value.key.name.value(); |
| 407 | } else { |
| 408 | out << "???"; |
| 409 | } |
| 410 | out << " = "; |
| 411 | value.value->print(&out); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 412 | return out; |
| 413 | } |
| 414 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 415 | Array* Array::clone(StringPool* newPool) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 416 | Array* array = new Array(); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 417 | array->mComment = mComment; |
| 418 | array->mSource = mSource; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 419 | for (auto& item : items) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 420 | array->items.emplace_back(std::unique_ptr<Item>(item->clone(newPool))); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 421 | } |
| 422 | return array; |
| 423 | } |
| 424 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 425 | void Array::print(std::ostream* out) const { |
| 426 | *out << "(array) [" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 427 | << util::joiner(items.begin(), items.end(), ", ") |
| 428 | << "]"; |
| 429 | } |
| 430 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 431 | Plural* Plural::clone(StringPool* newPool) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 432 | Plural* p = new Plural(); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 433 | p->mComment = mComment; |
| 434 | p->mSource = mSource; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 435 | const size_t count = values.size(); |
| 436 | for (size_t i = 0; i < count; i++) { |
| 437 | if (values[i]) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 438 | p->values[i] = std::unique_ptr<Item>(values[i]->clone(newPool)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | return p; |
| 442 | } |
| 443 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 444 | void Plural::print(std::ostream* out) const { |
| 445 | *out << "(plural)"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static ::std::ostream& operator<<(::std::ostream& out, const std::unique_ptr<Item>& item) { |
| 449 | return out << *item; |
| 450 | } |
| 451 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 452 | Styleable* Styleable::clone(StringPool* /*newPool*/) const { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 453 | Styleable* styleable = new Styleable(); |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame^] | 454 | styleable->mComment = mComment; |
| 455 | styleable->mSource = mSource; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 456 | std::copy(entries.begin(), entries.end(), std::back_inserter(styleable->entries)); |
| 457 | return styleable; |
| 458 | } |
| 459 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 460 | void Styleable::print(std::ostream* out) const { |
| 461 | *out << "(styleable) " << " [" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 462 | << util::joiner(entries.begin(), entries.end(), ", ") |
| 463 | << "]"; |
| 464 | } |
| 465 | |
| 466 | } // namespace aapt |