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