| 1 |
use std::fmt::{self, Display, Formatter, Write}; |
| 2 |
|
| 3 |
/// Simple but still over-engineered XML generator for a [`Formatter`], for generating |
| 4 |
/// Windows Manifest XML. This can easily generate invalid XML. |
| 5 |
/// |
| 6 |
/// When used correctly, this should generate the same output as MT’s `-canonicalize` |
| 7 |
/// option. |
| 8 |
pub struct XmlFormatter<'a, 'f> { |
| 9 |
f: &'f mut Formatter<'a>, |
| 10 |
state: State, |
| 11 |
depth: usize, |
| 12 |
} |
| 13 |
|
| 14 |
#[derive(Eq, PartialEq)] |
| 15 |
enum State { |
| 16 |
Init, |
| 17 |
StartTag, |
| 18 |
Text, |
| 19 |
} |
| 20 |
|
| 21 |
impl<'a, 'f> XmlFormatter<'a, 'f> { |
| 22 |
pub fn new(f: &'f mut Formatter<'a>) -> Self { |
| 23 |
Self { |
| 24 |
f, |
| 25 |
state: State::Init, |
| 26 |
depth: 0, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
fn pretty(&mut self) -> fmt::Result { |
| 31 |
if self.f.alternate() { |
| 32 |
self.f.write_str("\r\n")?; |
| 33 |
for _ in 0..self.depth { |
| 34 |
self.f.write_str(" ")?; |
| 35 |
} |
| 36 |
} |
| 37 |
Ok(()) |
| 38 |
} |
| 39 |
|
| 40 |
pub fn start_document(&mut self) -> fmt::Result { |
| 41 |
if !self.f.alternate() { |
| 42 |
self.f.write_char('\u{FEFF}')?; |
| 43 |
} |
| 44 |
self.f |
| 45 |
.write_str("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n") |
| 46 |
} |
| 47 |
|
| 48 |
pub fn element<F: FnOnce(&mut Self) -> fmt::Result>(&mut self, name: &str, attrs: &[(&str, &str)], f: F) -> fmt::Result { |
| 49 |
self.start_element(name, attrs)?; |
| 50 |
f(self)?; |
| 51 |
self.end_element(name) |
| 52 |
} |
| 53 |
|
| 54 |
pub fn empty_element(&mut self, name: &str, attrs: &[(&str, &str)]) -> fmt::Result { |
| 55 |
self.start_element(name, attrs)?; |
| 56 |
self.end_element(name) |
| 57 |
} |
| 58 |
|
| 59 |
pub fn start_element(&mut self, name: &str, attrs: &[(&str, &str)]) -> fmt::Result { |
| 60 |
if self.state == State::StartTag { |
| 61 |
self.f.write_char('>')?; |
| 62 |
} |
| 63 |
if self.depth != 0 { |
| 64 |
self.pretty()?; |
| 65 |
} |
| 66 |
write!(self.f, "<{}", name)?; |
| 67 |
for (name, value) in attrs { |
| 68 |
write!(self.f, " {}=\"{}\"", name, Xml(value))?; |
| 69 |
} |
| 70 |
self.depth += 1; |
| 71 |
self.state = State::StartTag; |
| 72 |
Ok(()) |
| 73 |
} |
| 74 |
|
| 75 |
pub fn end_element(&mut self, name: &str) -> fmt::Result { |
| 76 |
self.depth -= 1; |
| 77 |
match self.state { |
| 78 |
State::Init => { |
| 79 |
self.pretty()?; |
| 80 |
write!(self.f, "</{}>", name) |
| 81 |
} |
| 82 |
State::Text => { |
| 83 |
self.state = State::Init; |
| 84 |
write!(self.f, "</{}>", name) |
| 85 |
} |
| 86 |
State::StartTag => { |
| 87 |
self.state = State::Init; |
| 88 |
if self.f.alternate() { |
| 89 |
self.f.write_str("/>") |
| 90 |
} else { |
| 91 |
write!(self.f, "></{}>", name) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub fn text(&mut self, s: &str) -> fmt::Result { |
| 98 |
if self.state == State::StartTag { |
| 99 |
self.state = State::Text; |
| 100 |
self.f.write_char('>')?; |
| 101 |
} |
| 102 |
Xml(s).fmt(self.f) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/// Temporary wrapper for outputting a string with XML attribute encoding. |
| 107 |
/// This does not do anything with the control characters which are not |
| 108 |
/// valid in XML, encoded or not. |
| 109 |
struct Xml<'a>(&'a str); |
| 110 |
|
| 111 |
impl<'a> Display for Xml<'a> { |
| 112 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 113 |
// Process the string in blocks separated by special characters, so that the parts that |
| 114 |
// don't need encoding can be written all at once, not character by character, and with |
| 115 |
// no checks for whether string slices are aligned on character boundaries. |
| 116 |
for s in self.0.split_inclusive(&['<', '&', '>', '"', '\r'][..]) { |
| 117 |
// Check whether the last character in the substring needs encoding. This will be |
| 118 |
// `None` at the end of the input string. |
| 119 |
let mut iter = s.chars(); |
| 120 |
let ch = match iter.next_back() { |
| 121 |
Some('<') => Some("<"), |
| 122 |
Some('&') => Some("&"), |
| 123 |
Some('>') => Some(">"), |
| 124 |
Some('"') => Some("""), |
| 125 |
Some('\r') => Some(" "), |
| 126 |
_ => None, |
| 127 |
}; |
| 128 |
// Write the substring except the last character, then the encoded character; |
| 129 |
// or the entire substring if it is not terminated by a special character. |
| 130 |
match ch { |
| 131 |
Some(enc) => { |
| 132 |
f.write_str(iter.as_str())?; |
| 133 |
f.write_str(enc)?; |
| 134 |
} |
| 135 |
None => f.write_str(s)?, |
| 136 |
} |
| 137 |
} |
| 138 |
Ok(()) |
| 139 |
} |
| 140 |
} |