blob: 2fe24245f3d9828784ae23bd0365ac778249b46e [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski46708052017-09-29 14:49:15 -070017#include "format/binary/XmlFlattener.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
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 Lesinski2eed52e2018-02-21 15:55:58 -080028#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "SdkConstants.h"
Adam Lesinskie1094a22018-02-22 17:27:17 -080030#include "ValueVisitor.h"
Adam Lesinski46708052017-09-29 14:49:15 -070031#include "format/binary/ChunkWriter.h"
32#include "format/binary/ResourceTypeExtensions.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080033#include "xml/XmlDom.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035using namespace android;
36
Adam Lesinski2eed52e2018-02-21 15:55:58 -080037using ::aapt::ResourceUtils::StringBuilder;
38
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039namespace aapt {
40
41namespace {
42
43constexpr uint32_t kLowPriority = 0xffffffffu;
44
Adam Lesinski6b372992017-08-09 10:54:23 -070045static bool cmp_xml_attribute_by_id(const xml::Attribute* a, const xml::Attribute* b) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 if (a->compiled_attribute && a->compiled_attribute.value().id) {
47 if (b->compiled_attribute && b->compiled_attribute.value().id) {
Adam Lesinski6b372992017-08-09 10:54:23 -070048 return a->compiled_attribute.value().id.value() < b->compiled_attribute.value().id.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 }
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 Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskie59f0d82017-10-13 09:36:53 -070063class XmlFlattenerVisitor : public xml::ConstVisitor {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -070065 using xml::ConstVisitor::Visit;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066
67 StringPool pool;
68 std::map<uint8_t, StringPool> package_pools;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 struct StringFlattenDest {
71 StringPool::Ref ref;
72 ResStringPool_ref* dest;
73 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::vector<StringFlattenDest> string_refs;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 XmlFlattenerVisitor(BigBuffer* buffer, XmlFlattenerOptions options)
Adam Lesinski46708052017-09-29 14:49:15 -070078 : buffer_(buffer), options_(options) {
79 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080
Adam Lesinskie59f0d82017-10-13 09:36:53 -070081 void Visit(const xml::Text* node) override {
Ryan Mitchell0dcb20c2018-04-27 14:53:04 -070082 std::string text = util::TrimWhitespace(node->text).to_string();
83
84 // Skip whitespace only text nodes.
85 if (text.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 }
88
Ryan Mitchell0dcb20c2018-04-27 14:53:04 -070089 // Compact leading and trailing whitespace into a single space
90 if (isspace(node->text[0])) {
91 text = ' ' + text;
92 }
93 if (isspace(node->text[node->text.length() - 1])) {
94 text = text + ' ';
95 }
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 ChunkWriter writer(buffer_);
Adam Lesinski6b372992017-08-09 10:54:23 -070098 ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 flat_node->lineNumber = util::HostToDevice32(node->line_number);
100 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101
Adam Lesinski48448e82017-04-26 15:13:52 -0700102 // Process plain strings to make sure they get properly escaped.
Ryan Mitchell0dcb20c2018-04-27 14:53:04 -0700103 text = StringBuilder(true /*preserve_spaces*/).AppendText(text).to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104
Ryan Mitchell0dcb20c2018-04-27 14:53:04 -0700105 ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
106 AddString(text, kLowPriority, &flat_text->data);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 }
109
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700110 void Visit(const xml::Element* node) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700111 for (const xml::NamespaceDecl& decl : node->namespace_decls) {
112 // Skip dedicated tools namespace.
113 if (decl.uri != xml::kSchemaTools) {
114 WriteNamespace(decl, android::RES_XML_START_NAMESPACE_TYPE);
115 }
116 }
117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 ChunkWriter start_writer(buffer_);
120 ResXMLTree_node* flat_node =
121 start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE);
122 flat_node->lineNumber = util::HostToDevice32(node->line_number);
123 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124
Adam Lesinski6b372992017-08-09 10:54:23 -0700125 ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126
Adam Lesinski6b372992017-08-09 10:54:23 -0700127 // A missing namespace must be null, not an empty string. Otherwise the runtime complains.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 AddString(node->namespace_uri, kLowPriority, &flat_elem->ns,
129 true /* treat_empty_string_as_null */);
Adam Lesinski6b372992017-08-09 10:54:23 -0700130 AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
Adam Lesinski6b372992017-08-09 10:54:23 -0700133 flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 WriteAttributes(node, flat_elem, &start_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 start_writer.Finish();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138 }
139
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700140 xml::ConstVisitor::Visit(node);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
142 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 ChunkWriter end_writer(buffer_);
144 ResXMLTree_node* flat_end_node =
145 end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE);
146 flat_end_node->lineNumber = util::HostToDevice32(node->line_number);
147 flat_end_node->comment.index = util::HostToDevice32(-1);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Adam Lesinski6b372992017-08-09 10:54:23 -0700149 ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns,
151 true /* treat_empty_string_as_null */);
152 AddString(node->name, kLowPriority, &flat_end_elem->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 end_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700156
157 for (auto iter = node->namespace_decls.rbegin(); iter != node->namespace_decls.rend(); ++iter) {
158 // Skip dedicated tools namespace.
159 if (iter->uri != xml::kSchemaTools) {
160 WriteNamespace(*iter, android::RES_XML_END_NAMESPACE_TYPE);
161 }
162 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 }
164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 private:
166 DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor);
167
Adam Lesinskie1094a22018-02-22 17:27:17 -0800168 // We are adding strings to a StringPool whose strings will be sorted and merged with other
169 // string pools. That means we can't encode the ID of a string directly. Instead, we defer the
170 // writing of the ID here, until after the StringPool is merged and sorted.
Adam Lesinski46708052017-09-29 14:49:15 -0700171 void AddString(const StringPiece& str, uint32_t priority, android::ResStringPool_ref* dest,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 bool treat_empty_string_as_null = false) {
173 if (str.empty() && treat_empty_string_as_null) {
174 // Some parts of the runtime treat null differently than empty string.
175 dest->index = util::DeviceToHost32(-1);
176 } else {
Adam Lesinski46708052017-09-29 14:49:15 -0700177 string_refs.push_back(
178 StringFlattenDest{pool.MakeRef(str, StringPool::Context(priority)), dest});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
181
Adam Lesinskie1094a22018-02-22 17:27:17 -0800182 // We are adding strings to a StringPool whose strings will be sorted and merged with other
183 // string pools. That means we can't encode the ID of a string directly. Instead, we defer the
184 // writing of the ID here, until after the StringPool is merged and sorted.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
186 string_refs.push_back(StringFlattenDest{ref, dest});
187 }
188
Adam Lesinski6b372992017-08-09 10:54:23 -0700189 void WriteNamespace(const xml::NamespaceDecl& decl, uint16_t type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 ChunkWriter writer(buffer_);
191
192 ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
Adam Lesinski6b372992017-08-09 10:54:23 -0700193 flatNode->lineNumber = util::HostToDevice32(decl.line_number);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 flatNode->comment.index = util::HostToDevice32(-1);
195
Adam Lesinski060b53d2017-07-28 17:10:35 -0700196 ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700197 AddString(decl.prefix, kLowPriority, &flat_ns->prefix);
198 AddString(decl.uri, kLowPriority, &flat_ns->uri);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199
200 writer.Finish();
201 }
202
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700203 void WriteAttributes(const xml::Element* node, ResXMLTree_attrExt* flat_elem,
204 ChunkWriter* writer) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 filtered_attrs_.clear();
206 filtered_attrs_.reserve(node->attributes.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207
208 // Filter the attributes.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700209 for (const xml::Attribute& attr : node->attributes) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700210 if (attr.namespace_uri != xml::kSchemaTools) {
211 filtered_attrs_.push_back(&attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213 }
214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 return;
217 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 const ResourceId kIdAttr(0x010100d0);
220
Adam Lesinski48448e82017-04-26 15:13:52 -0700221 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 ResXMLTree_attribute* flat_attr =
226 writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
227 uint16_t attribute_index = 1;
228 for (const xml::Attribute* xml_attr : filtered_attrs_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 // Assign the indices for specific attributes.
Adam Lesinski46708052017-09-29 14:49:15 -0700230 if (xml_attr->compiled_attribute && xml_attr->compiled_attribute.value().id &&
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
232 flat_elem->idIndex = util::HostToDevice16(attribute_index);
233 } else if (xml_attr->namespace_uri.empty()) {
234 if (xml_attr->name == "class") {
235 flat_elem->classIndex = util::HostToDevice16(attribute_index);
236 } else if (xml_attr->name == "style") {
237 flat_elem->styleIndex = util::HostToDevice16(attribute_index);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 }
239 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241
Adam Lesinski48448e82017-04-26 15:13:52 -0700242 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 // is empty (doesn't exist).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
245 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248
Adam Lesinski48448e82017-04-26 15:13:52 -0700249 if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
250 // The attribute has no associated ResourceID, so the string order doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 } else {
253 // Attribute names are stored without packages, but we use
254 // their StringPool index to lookup their resource IDs.
255 // This will cause collisions, so we can't dedupe
256 // attribute names from different packages. We use separate
257 // pools that we later combine.
258 //
259 // Lookup the StringPool for this package and make the reference there.
Adam Lesinski48448e82017-04-26 15:13:52 -0700260 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261
Adam Lesinski46708052017-09-29 14:49:15 -0700262 StringPool::Ref name_ref = package_pools[aapt_attr.id.value().package_id()].MakeRef(
263 xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264
265 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 }
268
Adam Lesinskie1094a22018-02-22 17:27:17 -0800269 std::string processed_str;
270 Maybe<StringPiece> compiled_text;
271 if (xml_attr->compiled_value != nullptr) {
272 // Make sure we're not flattening a String. A String can be referencing a string from
273 // a different StringPool than we're using here to build the binary XML.
274 String* string_value = ValueCast<String>(xml_attr->compiled_value.get());
275 if (string_value != nullptr) {
276 // Mark the String's text as needing to be serialized.
277 compiled_text = StringPiece(*string_value->value);
278 } else {
279 // Serialize this compiled value safely.
280 CHECK(xml_attr->compiled_value->Flatten(&flat_attr->typedValue));
281 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 } else {
Adam Lesinskie1094a22018-02-22 17:27:17 -0800283 // There is no compiled value, so treat the raw string as compiled, once it is processed to
284 // make sure escape sequences are properly interpreted.
285 processed_str =
Adam Lesinski2eed52e2018-02-21 15:55:58 -0800286 StringBuilder(true /*preserve_spaces*/).AppendText(xml_attr->value).to_string();
Adam Lesinskie1094a22018-02-22 17:27:17 -0800287 compiled_text = StringPiece(processed_str);
288 }
Adam Lesinski48448e82017-04-26 15:13:52 -0700289
Adam Lesinskie1094a22018-02-22 17:27:17 -0800290 if (compiled_text) {
291 // Write out the compiled text and raw_text.
292 flat_attr->typedValue.dataType = android::Res_value::TYPE_STRING;
293 AddString(compiled_text.value(), kLowPriority,
294 reinterpret_cast<ResStringPool_ref*>(&flat_attr->typedValue.data));
295 if (options_.keep_raw_values) {
296 AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue);
297 } else {
298 AddString(compiled_text.value(), kLowPriority, &flat_attr->rawValue);
299 }
300 } else if (options_.keep_raw_values && !xml_attr->value.empty()) {
301 AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 }
303
Adam Lesinski48448e82017-04-26 15:13:52 -0700304 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 }
307 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308
309 BigBuffer* buffer_;
310 XmlFlattenerOptions options_;
311
Adam Lesinski060b53d2017-07-28 17:10:35 -0700312 // Scratch vector to filter attributes. We avoid allocations making this a member.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700313 std::vector<const xml::Attribute*> filtered_attrs_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314};
315
316} // namespace
317
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700318bool XmlFlattener::Flatten(IAaptContext* context, const xml::Node* node) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 BigBuffer node_buffer(1024);
320 XmlFlattenerVisitor visitor(&node_buffer, options_);
321 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322
323 // Merge the package pools into the main pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 for (auto& package_pool_entry : visitor.package_pools) {
325 visitor.pool.Merge(std::move(package_pool_entry.second));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 }
327
328 // Sort the string pool so that attribute resource IDs show up first.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700329 visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
330 return util::compare(a.priority, b.priority);
331 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332
333 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 for (const auto& ref_entry : visitor.string_refs) {
335 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 }
337
338 // Write the XML header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 ChunkWriter xml_header_writer(buffer_);
340 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341
342 // Flatten the StringPool.
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700343 if (options_.use_utf16) {
Ryan Mitchell70414f22018-03-26 11:05:31 -0700344 StringPool::FlattenUtf16(buffer_, visitor.pool, context->GetDiagnostics());
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700345 } else {
Ryan Mitchell70414f22018-03-26 11:05:31 -0700346 StringPool::FlattenUtf8(buffer_, visitor.pool, context->GetDiagnostics());
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700347 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348
349 {
350 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 ChunkWriter res_id_map_writer(buffer_);
352 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
Adam Lesinski060b53d2017-07-28 17:10:35 -0700353 for (const auto& str : visitor.pool.strings()) {
354 ResourceId id(str->context.priority);
355 if (str->context.priority == kLowPriority || !id.is_valid()) {
356 // When we see the first non-resource ID, we're done.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 break;
358 }
Adam Lesinski060b53d2017-07-28 17:10:35 -0700359 *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 }
363
364 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366
367 // Finish the xml header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700370}
371
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700372bool XmlFlattener::Consume(IAaptContext* context, const xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 if (!resource->root) {
374 return false;
375 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700377}
378
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379} // namespace aapt