Encode XML entities.

This commit is contained in:
Mark Story 2016-01-03 23:17:49 -05:00
parent c3632befb5
commit de10545906

View file

@ -84,11 +84,13 @@ pub fn output_checkstyle_file<T>(mut writer: T,
for line in mismatch.lines { for line in mismatch.lines {
match line { match line {
DiffLine::Expected(ref str) => { DiffLine::Expected(ref str) => {
let message = xml_escape_str(&str);
// TODO XML encode str here.
try!(write!(writer, try!(write!(writer,
"<error line=\"{}\" severity=\"error\" message=\"Should be \ "<error line=\"{}\" severity=\"error\" message=\"Should be \
`{}`\" />", `{}`\" />",
mismatch.line_number, mismatch.line_number,
str)); message));
} }
_ => { _ => {
// Do nothing with context and expected. // Do nothing with context and expected.
@ -100,6 +102,23 @@ pub fn output_checkstyle_file<T>(mut writer: T,
Ok(()) Ok(())
} }
// Convert special characters into XML entities.
// This is needed for checkstyle output.
fn xml_escape_str(string: &str) -> String {
let mut out = String::new();
for c in string.chars() {
match c {
'<' => out.push_str("&lt;"),
'>' => out.push_str("&gt;"),
'"' => out.push_str("&quot;"),
'\'' => out.push_str("&apos;"),
'&' => out.push_str("&amp;"),
_ => out.push(c),
}
}
out
}
// Prints all newlines either as `\n` or as `\r\n`. // Prints all newlines either as `\n` or as `\r\n`.
pub fn write_system_newlines<T>(writer: T, pub fn write_system_newlines<T>(writer: T,
text: &StringBuffer, text: &StringBuffer,