Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "flatten/XmlFlattener.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <map> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "android-base/logging.h" |
| 24 | #include "android-base/macros.h" |
| 25 | #include "androidfw/ResourceTypes.h" |
| 26 | #include "utils/misc.h" |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "SdkConstants.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "flatten/ChunkWriter.h" |
| 30 | #include "flatten/ResourceTypeExtensions.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 31 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | using namespace android; |
| 34 | |
| 35 | namespace aapt { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | constexpr uint32_t kLowPriority = 0xffffffffu; |
| 40 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 41 | static bool cmp_xml_attribute_by_id(const xml::Attribute* a, const xml::Attribute* b) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | if (a->compiled_attribute && a->compiled_attribute.value().id) { |
| 43 | if (b->compiled_attribute && b->compiled_attribute.value().id) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 44 | return a->compiled_attribute.value().id.value() < b->compiled_attribute.value().id.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | } |
| 46 | return true; |
| 47 | } else if (!b->compiled_attribute) { |
| 48 | int diff = a->namespace_uri.compare(b->namespace_uri); |
| 49 | if (diff < 0) { |
| 50 | return true; |
| 51 | } else if (diff > 0) { |
| 52 | return false; |
| 53 | } |
| 54 | return a->name < b->name; |
| 55 | } |
| 56 | return false; |
| 57 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | class XmlFlattenerVisitor : public xml::Visitor { |
| 60 | public: |
| 61 | using xml::Visitor::Visit; |
| 62 | |
| 63 | StringPool pool; |
| 64 | std::map<uint8_t, StringPool> package_pools; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | struct StringFlattenDest { |
| 67 | StringPool::Ref ref; |
| 68 | ResStringPool_ref* dest; |
| 69 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | std::vector<StringFlattenDest> string_refs; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | : buffer_(buffer), options_(options) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | void Visit(xml::Text* node) override { |
| 77 | if (util::TrimWhitespace(node->text).empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | // Skip whitespace only text nodes. |
| 79 | return; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | ChunkWriter writer(buffer_); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 83 | ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | flat_node->lineNumber = util::HostToDevice32(node->line_number); |
| 85 | flat_node->comment.index = util::HostToDevice32(-1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>(); |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 88 | |
| 89 | // Process plain strings to make sure they get properly escaped. |
| 90 | util::StringBuilder builder; |
| 91 | builder.Append(node->text); |
| 92 | AddString(builder.ToString(), kLowPriority, &flat_text->data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | void Visit(xml::Element* node) override { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 98 | for (const xml::NamespaceDecl& decl : node->namespace_decls) { |
| 99 | // Skip dedicated tools namespace. |
| 100 | if (decl.uri != xml::kSchemaTools) { |
| 101 | WriteNamespace(decl, android::RES_XML_START_NAMESPACE_TYPE); |
| 102 | } |
| 103 | } |
| 104 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | ChunkWriter start_writer(buffer_); |
| 107 | ResXMLTree_node* flat_node = |
| 108 | start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE); |
| 109 | flat_node->lineNumber = util::HostToDevice32(node->line_number); |
| 110 | flat_node->comment.index = util::HostToDevice32(-1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 112 | ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 114 | // A missing namespace must be null, not an empty string. Otherwise the runtime complains. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | AddString(node->namespace_uri, kLowPriority, &flat_elem->ns, |
| 116 | true /* treat_empty_string_as_null */); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 117 | AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem)); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 120 | flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | WriteAttributes(node, flat_elem, &start_writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | start_writer.Finish(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | xml::Visitor::Visit(node); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | |
| 129 | { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | ChunkWriter end_writer(buffer_); |
| 131 | ResXMLTree_node* flat_end_node = |
| 132 | end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE); |
| 133 | flat_end_node->lineNumber = util::HostToDevice32(node->line_number); |
| 134 | flat_end_node->comment.index = util::HostToDevice32(-1); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 136 | ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns, |
| 138 | true /* treat_empty_string_as_null */); |
| 139 | AddString(node->name, kLowPriority, &flat_end_elem->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | end_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 143 | |
| 144 | for (auto iter = node->namespace_decls.rbegin(); iter != node->namespace_decls.rend(); ++iter) { |
| 145 | // Skip dedicated tools namespace. |
| 146 | if (iter->uri != xml::kSchemaTools) { |
| 147 | WriteNamespace(*iter, android::RES_XML_END_NAMESPACE_TYPE); |
| 148 | } |
| 149 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | private: |
| 153 | DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor); |
| 154 | |
| 155 | void AddString(const StringPiece& str, uint32_t priority, |
| 156 | android::ResStringPool_ref* dest, |
| 157 | bool treat_empty_string_as_null = false) { |
| 158 | if (str.empty() && treat_empty_string_as_null) { |
| 159 | // Some parts of the runtime treat null differently than empty string. |
| 160 | dest->index = util::DeviceToHost32(-1); |
| 161 | } else { |
| 162 | string_refs.push_back(StringFlattenDest{ |
| 163 | pool.MakeRef(str, StringPool::Context(priority)), dest}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) { |
| 168 | string_refs.push_back(StringFlattenDest{ref, dest}); |
| 169 | } |
| 170 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 171 | void WriteNamespace(const xml::NamespaceDecl& decl, uint16_t type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | ChunkWriter writer(buffer_); |
| 173 | |
| 174 | ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 175 | flatNode->lineNumber = util::HostToDevice32(decl.line_number); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 176 | flatNode->comment.index = util::HostToDevice32(-1); |
| 177 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 178 | ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>(); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame^] | 179 | AddString(decl.prefix, kLowPriority, &flat_ns->prefix); |
| 180 | AddString(decl.uri, kLowPriority, &flat_ns->uri); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | |
| 182 | writer.Finish(); |
| 183 | } |
| 184 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 185 | void WriteAttributes(xml::Element* node, ResXMLTree_attrExt* flat_elem, ChunkWriter* writer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | filtered_attrs_.clear(); |
| 187 | filtered_attrs_.reserve(node->attributes.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | |
| 189 | // Filter the attributes. |
| 190 | for (xml::Attribute& attr : node->attributes) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 191 | if (attr.namespace_uri != xml::kSchemaTools) { |
| 192 | filtered_attrs_.push_back(&attr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | if (filtered_attrs_.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 199 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | const ResourceId kIdAttr(0x010100d0); |
| 201 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 202 | std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 206 | ResXMLTree_attribute* flat_attr = |
| 207 | writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size()); |
| 208 | uint16_t attribute_index = 1; |
| 209 | for (const xml::Attribute* xml_attr : filtered_attrs_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | // Assign the indices for specific attributes. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | if (xml_attr->compiled_attribute && |
| 212 | xml_attr->compiled_attribute.value().id && |
| 213 | xml_attr->compiled_attribute.value().id.value() == kIdAttr) { |
| 214 | flat_elem->idIndex = util::HostToDevice16(attribute_index); |
| 215 | } else if (xml_attr->namespace_uri.empty()) { |
| 216 | if (xml_attr->name == "class") { |
| 217 | flat_elem->classIndex = util::HostToDevice16(attribute_index); |
| 218 | } else if (xml_attr->name == "style") { |
| 219 | flat_elem->styleIndex = util::HostToDevice16(attribute_index); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | attribute_index++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 224 | // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | // is empty (doesn't exist). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns, |
| 227 | true /* treat_empty_string_as_null */); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | flat_attr->rawValue.index = util::HostToDevice32(-1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 231 | if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) { |
| 232 | // The attribute has no associated ResourceID, so the string order doesn't matter. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | AddString(xml_attr->name, kLowPriority, &flat_attr->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | } else { |
| 235 | // Attribute names are stored without packages, but we use |
| 236 | // their StringPool index to lookup their resource IDs. |
| 237 | // This will cause collisions, so we can't dedupe |
| 238 | // attribute names from different packages. We use separate |
| 239 | // pools that we later combine. |
| 240 | // |
| 241 | // Lookup the StringPool for this package and make the reference there. |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 242 | const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | StringPool::Ref name_ref = |
| 245 | package_pools[aapt_attr.id.value().package_id()].MakeRef( |
| 246 | xml_attr->name, StringPool::Context(aapt_attr.id.value().id)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | |
| 248 | // Add it to the list of strings to flatten. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | AddString(name_ref, &flat_attr->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 252 | // Process plain strings to make sure they get properly escaped. |
| 253 | StringPiece raw_value = xml_attr->value; |
Adam Lesinski | fba0cf2 | 2017-06-29 17:53:36 -0700 | [diff] [blame] | 254 | |
| 255 | util::StringBuilder str_builder(true /*preserve_spaces*/); |
| 256 | str_builder.Append(xml_attr->value); |
| 257 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 258 | if (!options_.keep_raw_values) { |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 259 | raw_value = str_builder.ToString(); |
| 260 | } |
| 261 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | if (options_.keep_raw_values || !xml_attr->compiled_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | // Keep raw values if the value is not compiled or |
| 264 | // if we're building a static library (need symbols). |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 265 | AddString(raw_value, kLowPriority, &flat_attr->rawValue); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | if (xml_attr->compiled_value) { |
| 269 | CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | } else { |
| 271 | // Flatten as a regular string type. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING; |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 273 | |
| 274 | AddString(str_builder.ToString(), kLowPriority, |
| 275 | (ResStringPool_ref*) &flat_attr->typedValue.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Adam Lesinski | 48448e8 | 2017-04-26 15:13:52 -0700 | [diff] [blame] | 278 | flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | flat_attr++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | |
| 283 | BigBuffer* buffer_; |
| 284 | XmlFlattenerOptions options_; |
| 285 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 286 | // Scratch vector to filter attributes. We avoid allocations making this a member. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | std::vector<xml::Attribute*> filtered_attrs_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | } // namespace |
| 291 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | bool XmlFlattener::Flatten(IAaptContext* context, xml::Node* node) { |
| 293 | BigBuffer node_buffer(1024); |
| 294 | XmlFlattenerVisitor visitor(&node_buffer, options_); |
| 295 | node->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | |
| 297 | // Merge the package pools into the main pool. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | for (auto& package_pool_entry : visitor.package_pools) { |
| 299 | visitor.pool.Merge(std::move(package_pool_entry.second)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Sort the string pool so that attribute resource IDs show up first. |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 303 | visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int { |
| 304 | return util::compare(a.priority, b.priority); |
| 305 | }); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | |
| 307 | // Now we flatten the string pool references into the correct places. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | for (const auto& ref_entry : visitor.string_refs) { |
| 309 | ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Write the XML header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | ChunkWriter xml_header_writer(buffer_); |
| 314 | xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 315 | |
| 316 | // Flatten the StringPool. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 317 | StringPool::FlattenUtf8(buffer_, visitor.pool); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | |
| 319 | { |
| 320 | // Write the array of resource IDs, indexed by StringPool order. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 321 | ChunkWriter res_id_map_writer(buffer_); |
| 322 | res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 323 | for (const auto& str : visitor.pool.strings()) { |
| 324 | ResourceId id(str->context.priority); |
| 325 | if (str->context.priority == kLowPriority || !id.is_valid()) { |
| 326 | // When we see the first non-resource ID, we're done. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 327 | break; |
| 328 | } |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 329 | *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 331 | res_id_map_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Move the nodeBuffer and append it to the out buffer. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | buffer_->AppendBuffer(std::move(node_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | |
| 337 | // Finish the xml header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | xml_header_writer.Finish(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 339 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | bool XmlFlattener::Consume(IAaptContext* context, xml::XmlResource* resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 343 | if (!resource->root) { |
| 344 | return false; |
| 345 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 346 | return Flatten(context, resource->root.get()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 349 | } // namespace aapt |