From 25e71409d4975a37baa645ec2178466796300944 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Tue, 1 May 2018 19:25:06 -0700 Subject: Better match Apple plist output This changes the plist writer output to more closely match Apple's plist output. More specifically, we capitalize the encoding, emit a doctype, and use hard tabs for indentation. --- src/xml/writer.rs | 56 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 23 deletions(-) (limited to 'src/xml') diff --git a/src/xml/writer.rs b/src/xml/writer.rs index 67bb6a2..14392dd 100644 --- a/src/xml/writer.rs +++ b/src/xml/writer.rs @@ -2,10 +2,11 @@ use base64; use std::borrow::Cow; use std::io::Write; use xml_rs::attribute::Attribute; +use xml_rs::escape::escape_str_pcdata; use xml_rs::name::Name; use xml_rs::namespace::Namespace; -use xml_rs::writer::{EmitterConfig, Error as XmlWriterError, EventWriter as XmlEventWriter}; use xml_rs::writer::events::XmlEvent as WriteXmlEvent; +use xml_rs::writer::{EmitterConfig, Error as XmlWriterError, EventWriter as XmlEventWriter}; use {Error, EventWriter as PlistEventWriter, PlistEvent, Result}; @@ -38,15 +39,16 @@ pub struct EventWriter { impl EventWriter { pub fn new(writer: W) -> EventWriter { - let config = EmitterConfig::new() + let mut config = EmitterConfig::new() .line_separator("\n") - .indent_string(" ") + .indent_string("\t") .perform_indent(true) - .write_document_declaration(true) + .write_document_declaration(false) .normalize_empty_elements(true) .cdata_to_characters(true) .keep_element_names_stack(false) .autopad_comments(true); + config.perform_escaping = false; EventWriter { xml_writer: XmlEventWriter::new_with_config(writer, config), @@ -79,7 +81,8 @@ impl EventWriter { } fn write_value(&mut self, value: &str) -> Result<()> { - self.xml_writer.write(WriteXmlEvent::Characters(value))?; + self.xml_writer + .write(WriteXmlEvent::Characters(&escape_str_pcdata(value)))?; Ok(()) } @@ -123,6 +126,12 @@ impl PlistEventWriter for EventWriter { .push(Element::Dictionary(DictionaryState::ExpectKey)), Some(other) => self.stack.push(other), None => { + // Write prologue + let prologue = r#" + +"#; + self.xml_writer.write(WriteXmlEvent::Characters(prologue))?; + let version_name = Name::local("version"); let version_attr = Attribute::new(version_name, "1.0"); @@ -227,25 +236,26 @@ mod tests { } } - let comparison = " + let comparison = " + - - Author - William Shakespeare - Lines - - It is a tale told by an idiot, - Full of sound and fury, signifying nothing. - - Death - 1564 - Height - 1.6 - Data - AAAAvgAAAAMAAAAeAAAA - Birthdate - 1981-05-16T11:32:06Z - +\t +\t\tAuthor +\t\tWilliam Shakespeare +\t\tLines +\t\t +\t\t\tIt is a tale told by an idiot, +\t\t\tFull of sound and fury, signifying nothing. +\t\t +\t\tDeath +\t\t1564 +\t\tHeight +\t\t1.6 +\t\tData +\t\tAAAAvgAAAAMAAAAeAAAA +\t\tBirthdate +\t\t1981-05-16T11:32:06Z +\t "; let s = String::from_utf8(cursor.into_inner()).unwrap(); -- cgit v1.2.3