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

Emit doctype, better match Apple's plist output #27

Merged
merged 2 commits into from
May 2, 2018
Merged
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
69 changes: 37 additions & 32 deletions src/xml/writer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,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};

Expand Down Expand Up @@ -38,15 +38,16 @@ pub struct EventWriter<W: Write> {

impl<W: Write> EventWriter<W> {
pub fn new(writer: W) -> EventWriter<W> {
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),
Expand Down Expand Up @@ -79,13 +80,18 @@ impl<W: Write> EventWriter<W> {
}

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(())
}

fn maybe_end_plist(&mut self) -> Result<()> {
// If there are no more open tags then write the </plist> element
if self.stack.len() == 1 {
// We didn't tell the xml_writer about the <plist> tag so it thinks we're already at
// the root. As such, it's not going to prettify our output, so we need to include
// the newline ourselves.
self.xml_writer.write(WriteXmlEvent::Characters("\n"))?;
self.end_element("plist")?;
if let Some(Element::Root) = self.stack.pop() {
} else {
Expand Down Expand Up @@ -123,14 +129,12 @@ impl<W: Write> PlistEventWriter for EventWriter<W> {
.push(Element::Dictionary(DictionaryState::ExpectKey)),
Some(other) => self.stack.push(other),
None => {
let version_name = Name::local("version");
let version_attr = Attribute::new(version_name, "1.0");

self.xml_writer.write(WriteXmlEvent::StartElement {
name: Name::local("plist"),
attributes: Cow::Borrowed(&[version_attr]),
namespace: Cow::Borrowed(&self.empty_namespace),
})?;
// Write prologue
let prologue = r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
"#;
self.xml_writer.write(WriteXmlEvent::Characters(prologue))?;

self.stack.push(Element::Root);
}
Expand Down Expand Up @@ -227,25 +231,26 @@ mod tests {
}
}

let comparison = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
let comparison = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Author</key>
<string>William Shakespeare</string>
<key>Lines</key>
<array>
<string>It is a tale told by an idiot,</string>
<string>Full of sound and fury, signifying nothing.</string>
</array>
<key>Death</key>
<integer>1564</integer>
<key>Height</key>
<real>1.6</real>
<key>Data</key>
<data>AAAAvgAAAAMAAAAeAAAA</data>
<key>Birthdate</key>
<date>1981-05-16T11:32:06Z</date>
</dict>
<dict>
\t<key>Author</key>
\t<string>William Shakespeare</string>
\t<key>Lines</key>
\t<array>
\t\t<string>It is a tale told by an idiot,</string>
\t\t<string>Full of sound and fury, signifying nothing.</string>
\t</array>
\t<key>Death</key>
\t<integer>1564</integer>
\t<key>Height</key>
\t<real>1.6</real>
\t<key>Data</key>
\t<data>AAAAvgAAAAMAAAAeAAAA</data>
\t<key>Birthdate</key>
\t<date>1981-05-16T11:32:06Z</date>
</dict>
</plist>";

let s = String::from_utf8(cursor.into_inner()).unwrap();
Expand Down