blob: d897941da6c462da1ec76b4a4ff2dce2c88d31ce [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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070064 public:
Adam Lesinskie59f0d82017-10-13 09:36:53 -070065 using xml::ConstVisitor::Visit;
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070082 if (util::TrimWhitespace(node->text).empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 // Skip whitespace only text nodes.
84 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085 }
86
Adam Lesinskice5e56e22016-10-21 17:56:45 -070087 ChunkWriter writer(buffer_);
Adam Lesinski6b372992017-08-09 10:54:23 -070088 ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
Adam Lesinskice5e56e22016-10-21 17:56:45 -070089 flat_node->lineNumber = util::HostToDevice32(node->line_number);
90 flat_node->comment.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091
Adam Lesinskice5e56e22016-10-21 17:56:45 -070092 ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
Adam Lesinski48448e82017-04-26 15:13:52 -070093
94 // Process plain strings to make sure they get properly escaped.
Adam Lesinski2eed52e2018-02-21 15:55:58 -080095 StringBuilder builder;
96 builder.AppendText(node->text);
97 AddString(builder.to_string(), kLowPriority, &flat_text->data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098
Adam Lesinskice5e56e22016-10-21 17:56:45 -070099 writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 }
101
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700102 void Visit(const xml::Element* node) override {
Adam Lesinski6b372992017-08-09 10:54:23 -0700103 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 Lesinskicacb28f2016-10-19 12:18:14 -0700110 {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700111 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 Lesinskicacb28f2016-10-19 12:18:14 -0700116
Adam Lesinski6b372992017-08-09 10:54:23 -0700117 ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118
Adam Lesinski6b372992017-08-09 10:54:23 -0700119 // A missing namespace must be null, not an empty string. Otherwise the runtime complains.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700120 AddString(node->namespace_uri, kLowPriority, &flat_elem->ns,
121 true /* treat_empty_string_as_null */);
Adam Lesinski6b372992017-08-09 10:54:23 -0700122 AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700124 flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
Adam Lesinski6b372992017-08-09 10:54:23 -0700125 flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700127 WriteAttributes(node, flat_elem, &start_writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700129 start_writer.Finish();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130 }
131
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700132 xml::ConstVisitor::Visit(node);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133
134 {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700135 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 Lesinski1ab598f2015-08-14 14:26:04 -0700140
Adam Lesinski6b372992017-08-09 10:54:23 -0700141 ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>();
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700142 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 Lesinskicacb28f2016-10-19 12:18:14 -0700145
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700146 end_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 }
Adam Lesinski6b372992017-08-09 10:54:23 -0700148
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 Lesinskicacb28f2016-10-19 12:18:14 -0700155 }
156
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700157 private:
158 DISALLOW_COPY_AND_ASSIGN(XmlFlattenerVisitor);
159
Adam Lesinskie1094a22018-02-22 17:27:17 -0800160 // 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 Lesinski46708052017-09-29 14:49:15 -0700163 void AddString(const StringPiece& str, uint32_t priority, android::ResStringPool_ref* dest,
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700164 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 Lesinski46708052017-09-29 14:49:15 -0700169 string_refs.push_back(
170 StringFlattenDest{pool.MakeRef(str, StringPool::Context(priority)), dest});
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 }
173
Adam Lesinskie1094a22018-02-22 17:27:17 -0800174 // 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 Lesinskice5e56e22016-10-21 17:56:45 -0700177 void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
178 string_refs.push_back(StringFlattenDest{ref, dest});
179 }
180
Adam Lesinski6b372992017-08-09 10:54:23 -0700181 void WriteNamespace(const xml::NamespaceDecl& decl, uint16_t type) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700182 ChunkWriter writer(buffer_);
183
184 ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
Adam Lesinski6b372992017-08-09 10:54:23 -0700185 flatNode->lineNumber = util::HostToDevice32(decl.line_number);
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700186 flatNode->comment.index = util::HostToDevice32(-1);
187
Adam Lesinski060b53d2017-07-28 17:10:35 -0700188 ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>();
Adam Lesinski6b372992017-08-09 10:54:23 -0700189 AddString(decl.prefix, kLowPriority, &flat_ns->prefix);
190 AddString(decl.uri, kLowPriority, &flat_ns->uri);
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700191
192 writer.Finish();
193 }
194
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700195 void WriteAttributes(const xml::Element* node, ResXMLTree_attrExt* flat_elem,
196 ChunkWriter* writer) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700197 filtered_attrs_.clear();
198 filtered_attrs_.reserve(node->attributes.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199
200 // Filter the attributes.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700201 for (const xml::Attribute& attr : node->attributes) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700202 if (attr.namespace_uri != xml::kSchemaTools) {
203 filtered_attrs_.push_back(&attr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205 }
206
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700207 if (filtered_attrs_.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 return;
209 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 const ResourceId kIdAttr(0x010100d0);
212
Adam Lesinski48448e82017-04-26 15:13:52 -0700213 std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700215 flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700217 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 Lesinskicacb28f2016-10-19 12:18:14 -0700221 // Assign the indices for specific attributes.
Adam Lesinski46708052017-09-29 14:49:15 -0700222 if (xml_attr->compiled_attribute && xml_attr->compiled_attribute.value().id &&
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700223 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 Lesinskicacb28f2016-10-19 12:18:14 -0700230 }
231 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700232 attribute_index++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233
Adam Lesinski48448e82017-04-26 15:13:52 -0700234 // Add the namespaceUri to the list of StringRefs to encode. Use null if the namespace
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 // is empty (doesn't exist).
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700236 AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
237 true /* treat_empty_string_as_null */);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700239 flat_attr->rawValue.index = util::HostToDevice32(-1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240
Adam Lesinski48448e82017-04-26 15:13:52 -0700241 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 Lesinskice5e56e22016-10-21 17:56:45 -0700243 AddString(xml_attr->name, kLowPriority, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 } 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 Lesinski48448e82017-04-26 15:13:52 -0700252 const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253
Adam Lesinski46708052017-09-29 14:49:15 -0700254 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 Lesinskicacb28f2016-10-19 12:18:14 -0700256
257 // Add it to the list of strings to flatten.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700258 AddString(name_ref, &flat_attr->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 }
260
Adam Lesinskie1094a22018-02-22 17:27:17 -0800261 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 Lesinskicacb28f2016-10-19 12:18:14 -0700274 } else {
Adam Lesinskie1094a22018-02-22 17:27:17 -0800275 // 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 Lesinski2eed52e2018-02-21 15:55:58 -0800278 StringBuilder(true /*preserve_spaces*/).AppendText(xml_attr->value).to_string();
Adam Lesinskie1094a22018-02-22 17:27:17 -0800279 compiled_text = StringPiece(processed_str);
280 }
Adam Lesinski48448e82017-04-26 15:13:52 -0700281
Adam Lesinskie1094a22018-02-22 17:27:17 -0800282 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 Lesinskicacb28f2016-10-19 12:18:14 -0700294 }
295
Adam Lesinski48448e82017-04-26 15:13:52 -0700296 flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700297 flat_attr++;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 }
299 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700300
301 BigBuffer* buffer_;
302 XmlFlattenerOptions options_;
303
Adam Lesinski060b53d2017-07-28 17:10:35 -0700304 // Scratch vector to filter attributes. We avoid allocations making this a member.
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700305 std::vector<const xml::Attribute*> filtered_attrs_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306};
307
308} // namespace
309
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700310bool XmlFlattener::Flatten(IAaptContext* context, const xml::Node* node) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700311 BigBuffer node_buffer(1024);
312 XmlFlattenerVisitor visitor(&node_buffer, options_);
313 node->Accept(&visitor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314
315 // Merge the package pools into the main pool.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700316 for (auto& package_pool_entry : visitor.package_pools) {
317 visitor.pool.Merge(std::move(package_pool_entry.second));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 }
319
320 // Sort the string pool so that attribute resource IDs show up first.
Adam Lesinski060b53d2017-07-28 17:10:35 -0700321 visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
322 return util::compare(a.priority, b.priority);
323 });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324
325 // Now we flatten the string pool references into the correct places.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700326 for (const auto& ref_entry : visitor.string_refs) {
327 ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 }
329
330 // Write the XML header.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700331 ChunkWriter xml_header_writer(buffer_);
332 xml_header_writer.StartChunk<ResXMLTree_header>(RES_XML_TYPE);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333
334 // Flatten the StringPool.
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700335 if (options_.use_utf16) {
Ryan Mitchell70414f22018-03-26 11:05:31 -0700336 StringPool::FlattenUtf16(buffer_, visitor.pool, context->GetDiagnostics());
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700337 } else {
Ryan Mitchell70414f22018-03-26 11:05:31 -0700338 StringPool::FlattenUtf8(buffer_, visitor.pool, context->GetDiagnostics());
Adam Lesinskiea13c1a2017-10-13 12:40:37 -0700339 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340
341 {
342 // Write the array of resource IDs, indexed by StringPool order.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700343 ChunkWriter res_id_map_writer(buffer_);
344 res_id_map_writer.StartChunk<ResChunk_header>(RES_XML_RESOURCE_MAP_TYPE);
Adam Lesinski060b53d2017-07-28 17:10:35 -0700345 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 Lesinskicacb28f2016-10-19 12:18:14 -0700349 break;
350 }
Adam Lesinski060b53d2017-07-28 17:10:35 -0700351 *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700353 res_id_map_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 }
355
356 // Move the nodeBuffer and append it to the out buffer.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700357 buffer_->AppendBuffer(std::move(node_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358
359 // Finish the xml header.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700360 xml_header_writer.Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700362}
363
Adam Lesinskie59f0d82017-10-13 09:36:53 -0700364bool XmlFlattener::Consume(IAaptContext* context, const xml::XmlResource* resource) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 if (!resource->root) {
366 return false;
367 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700368 return Flatten(context, resource->root.get());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700369}
370
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371} // namespace aapt