Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write AXML decoded content to a file #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use std::cell::RefCell;
use std::io::{
Error,
Cursor,
Write,
};
use std::fs::File;

use byteorder::{
LittleEndian,
ReadBytesExt
};

use quick_xml::Writer;
use quick_xml::events::{Event, BytesEnd, BytesStart};
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, Event};
use quick_xml::events::attributes::Attribute;
use quick_xml::name::QName;

Expand All @@ -39,6 +41,54 @@ pub struct XmlElement {
pub children: Vec<Rc<RefCell<XmlElement>>>,
}

impl XmlElement {
pub fn write_to_file(&self, file: &mut File) -> Result<(), Error> {
let mut writer = Writer::new_with_indent(Vec::new(), b' ', 4);

writer
.write_event(Event::Decl(BytesDecl::new("1.0", Some("utf-8"), None)))
.unwrap();

self.write_element(&mut writer).unwrap();

file.write_all(&writer.into_inner()[..])
.expect("Couldn't write to file");

Ok(())
}

fn write_element<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), Error> {
let mut element = writer.create_element(&self.element_type);

element = if self.attributes.is_empty() {
element
} else {
element.with_attributes(
self.attributes
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect::<Vec<(&str, &str)>>(),
)
};

if self.children.is_empty() {
element.write_empty().unwrap();
} else {
element
.write_inner_content(|writer| -> Result<(), quick_xml::Error> {
for child in self.children.iter() {
child.as_ref().borrow().write_element(writer).unwrap();
}

Ok(())
})
.unwrap();
}

Ok(())
}
}

/// Parse the start of a namepace
pub fn parse_start_namespace(axml_buff: &mut Cursor<Vec<u8>>,
strings: &[String],
Expand Down