use std::collections::BTreeMap;
use std::io::Read;
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::{Reader, Writer as XmlWriterInner};
use crate::exceptions::{Error, Result};
use crate::xml::constants::*;
pub const REGISTERED_PREFIXES: [(&str, &str); 10] = [
(DCTERMS_PREFIX, DCTERMS_NS),
("dcmitype", "http://purl.org/dc/dcmitype/"),
("cp", COREPROPS_NS),
("c", CHART_NS),
("a", DRAWING_NS),
("s", SHEET_MAIN_NS),
("r", REL_NS),
("vt", VTYPES_NS),
("xdr", SHEET_DRAWING_NS),
("cdr", CHART_DRAWING_NS),
];
pub type QName = String;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Element {
pub tag: QName,
pub attributes: Vec<(String, String)>,
pub text: Option<String>,
pub tail: Option<String>,
pub children: Vec<Element>,
}
impl Element {
pub fn new(tag: impl Into<QName>) -> Self {
Element {
tag: tag.into(),
..Default::default()
}
}
pub fn with_attributes<T, I, K, V>(tag: T, attrs: I) -> Self
where
T: std::fmt::Display,
I: IntoIterator<Item = (K, V)>,
K: std::fmt::Display,
V: std::fmt::Display,
{
let mut el = Element::new(tag.to_string());
for (k, v) in attrs {
el.set(k.to_string(), v.to_string());
}
el
}
pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
let key = key.into();
let value = value.into();
match self.attributes.iter_mut().find(|(k, _)| *k == key) {
Some(slot) => slot.1 = value,
None => self.attributes.push((key, value)),
}
self
}
pub fn get(&self, key: impl AsRef<str>) -> Option<&str> {
let key = key.as_ref();
self.attributes
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v.as_str())
}
pub fn set_text(&mut self, text: impl Into<String>) -> &mut Self {
self.text = Some(text.into());
self
}
pub fn append(&mut self, child: Element) -> &mut Self {
self.children.push(child);
self
}
pub fn insert(&mut self, index: usize, child: Element) -> &mut Self {
self.children.insert(index, child);
self
}
pub fn find(&self, path: impl AsRef<str>) -> Option<&Element> {
let path = path.as_ref();
let (ancestor, tag) = match split_path(path) {
Some((a, t)) => (Some(a), t),
None => (None, path),
};
if let Some(ancestor) = ancestor {
if !self.matches_step(ancestor) {
return None;
}
}
self.children.iter().find(|child| child.matches_tag(tag))
}
pub fn find_mut(&mut self, path: impl AsRef<str>) -> Option<&mut Element> {
let path = path.as_ref();
let (ancestor, tag) = match split_path(path) {
Some((a, t)) => (Some(a), t),
None => (None, path),
};
if let Some(ancestor) = ancestor {
if !self.matches_step(ancestor) {
return None;
}
}
self.children
.iter_mut()
.find(|child| child.matches_tag(tag))
}
fn matches_step(&self, step: &str) -> bool {
step == "*" || self.matches_tag(step)
}
fn matches_tag(&self, tag: &str) -> bool {
self.tag == tag
|| (super::constants::namespace_of(tag).is_none()
&& super::constants::local_name(&self.tag) == tag)
}
pub fn find_all(&self, tag: impl AsRef<str>) -> Vec<&Element> {
let tag = tag.as_ref();
self.children
.iter()
.filter(|c| c.matches_tag(tag))
.collect()
}
pub fn find_all_mut(&mut self, tag: impl AsRef<str>) -> Vec<&mut Element> {
let tag = tag.as_ref();
self.children
.iter_mut()
.filter(|c| c.matches_tag(tag))
.collect()
}
pub fn children(&self) -> Vec<&Element> {
self.children.iter().collect()
}
pub fn children_mut(&mut self) -> Vec<&mut Element> {
self.children.iter_mut().collect()
}
pub fn find_text(&self, path: impl AsRef<str>, default: &str) -> String {
match self.find(path.as_ref()) {
Some(node) => node.text.clone().unwrap_or_else(|| default.to_string()),
None => default.to_string(),
}
}
pub fn find_text_opt(&self, path: impl AsRef<str>) -> Option<String> {
self.find(path.as_ref()).and_then(|node| node.text.clone())
}
pub fn iter_tag(&self, tag: impl AsRef<str>) -> Vec<&Element> {
self.find_all(tag)
}
pub fn to_string_bytes(&self) -> Vec<u8> {
serialize(self)
}
pub fn to_pretty_string(&self) -> String {
let mut namespaces: Vec<String> = Vec::new();
collect_namespaces(self, &mut namespaces);
let mut prefixes: BTreeMap<String, String> = BTreeMap::new();
let mut declarations: Vec<(String, String)> = Vec::new();
for ns in &namespaces {
if ns == super::constants::XML_NS {
prefixes.insert(ns.clone(), "xml".to_string());
continue;
}
let prefix = REGISTERED_PREFIXES
.iter()
.find(|(_, uri)| *uri == ns)
.map(|(prefix, _)| (*prefix).to_string())
.unwrap_or_else(|| format!("ns{}", prefixes.len()));
declarations.push((prefix.clone(), ns.clone()));
prefixes.insert(ns.clone(), prefix);
}
let mut out = String::new();
write_pretty(&mut out, self, &prefixes, &declarations, 0, &mut false);
out
}
}
fn collect_namespaces(el: &Element, out: &mut Vec<String>) {
if let Some(ns) = namespace_of(&el.tag) {
if !out.iter().any(|x| x == ns) {
out.push(ns.to_string());
}
}
for (key, _) in &el.attributes {
if let Some(ns) = namespace_of(key) {
if !out.iter().any(|x| x == ns) {
out.push(ns.to_string());
}
}
}
for child in &el.children {
collect_namespaces(child, out);
}
}
fn write_pretty(
out: &mut String,
el: &Element,
prefixes: &BTreeMap<String, String>,
declarations: &[(String, String)],
depth: usize,
_is_root: &mut bool,
) {
const INDENT: &str = " ";
for _ in 0..depth {
out.push_str(INDENT);
}
out.push('<');
out.push_str(&render_qname(&el.tag, prefixes, true));
if depth == 0 {
for (prefix, uri) in declarations {
out.push_str(&format!(" xmlns:{prefix}=\"{}\"", escape_attribute(uri)));
}
}
for (key, value) in &el.attributes {
out.push_str(&format!(
" {}=\"{}\"",
render_qname(key, prefixes, false),
escape_attribute(value)
));
}
let has_children = !el.children.is_empty();
let text = el.text.as_deref().filter(|text| !text.is_empty());
match (has_children, text) {
(false, None) => out.push_str("/>"),
(false, Some(text)) => {
out.push('>');
out.push_str(&escape_text(text));
out.push_str("</");
out.push_str(&render_qname(&el.tag, prefixes, true));
out.push('>');
}
(true, text) => {
out.push('>');
if let Some(text) = text {
out.push_str(&escape_text(text));
}
for child in &el.children {
out.push('\n');
write_pretty(out, child, prefixes, declarations, depth + 1, _is_root);
}
out.push('\n');
for _ in 0..depth {
out.push_str(INDENT);
}
out.push_str("</");
out.push_str(&render_qname(&el.tag, prefixes, true));
out.push('>');
}
}
}
fn render_qname(name: &str, prefixes: &BTreeMap<String, String>, element: bool) -> String {
match namespace_of(name) {
Some(ns) => {
let local = super::constants::local_name(name);
match prefixes.get(ns) {
Some(prefix) => {
if element {
format!("{prefix}:{local}")
} else {
format!("{prefix}:{local}")
}
}
None => local.to_string(),
}
}
None => name.to_string(),
}
}
pub fn escape_text(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for ch in text.chars() {
match ch {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
_ => out.push(ch),
}
}
out
}
pub fn escape_attribute(value: &str) -> String {
let mut out = String::with_capacity(value.len());
for ch in value.chars() {
match ch {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
'\n' => out.push_str(" "),
'\r' => out.push_str(" "),
'\t' => out.push_str("	"),
_ => out.push(ch),
}
}
out
}
#[derive(Debug, Default)]
pub struct XmlWriter {
out: String,
}
impl XmlWriter {
pub fn new() -> Self {
XmlWriter { out: String::new() }
}
pub fn into_string(self) -> String {
self.out
}
pub fn as_str(&self) -> &str {
&self.out
}
pub fn raw(&mut self, text: &str) -> &mut Self {
self.out.push_str(text);
self
}
pub fn start_tag<I, K, V>(&mut self, name: &str, attrs: I) -> &mut Self
where
I: IntoIterator<Item = (K, V)>,
K: std::fmt::Display,
V: std::fmt::Display,
{
self.out.push('<');
self.out.push_str(name);
for (k, v) in attrs {
self.out.push(' ');
self.out.push_str(&k.to_string());
self.out.push_str("=\"");
self.out.push_str(&escape_attribute(&v.to_string()));
self.out.push('"');
}
self.out.push('>');
self
}
pub fn end_tag(&mut self, name: &str) -> &mut Self {
self.out.push_str("</");
self.out.push_str(name);
self.out.push('>');
self
}
pub fn tag<I, K, V>(&mut self, name: &str, attrs: I, body: Option<&str>) -> &mut Self
where
I: IntoIterator<Item = (K, V)>,
K: std::fmt::Display,
V: std::fmt::Display,
{
self.start_tag(name, attrs);
if let Some(body) = body {
if !body.is_empty() {
self.out.push_str(&escape_text(body));
}
}
self.end_tag(name)
}
pub fn end_document(&mut self) -> &mut Self {
self
}
}
pub fn start_tag<I, K, V>(doc: &mut XmlWriter, name: &str, attrs: I, body: Option<&str>)
where
I: IntoIterator<Item = (K, V)>,
K: std::fmt::Display,
V: std::fmt::Display,
{
doc.start_tag(name, attrs);
if let Some(body) = body {
if !body.is_empty() {
doc.raw(&escape_text(body));
}
}
}
pub fn end_tag(doc: &mut XmlWriter, name: &str) {
doc.end_tag(name);
}
pub fn tag<I, K, V>(doc: &mut XmlWriter, name: &str, attrs: I, body: Option<&str>)
where
I: IntoIterator<Item = (K, V)>,
K: std::fmt::Display,
V: std::fmt::Display,
{
doc.tag(name, attrs, body);
}
pub fn conditional_element<'a>(
parent: &'a mut Element,
tag: &str,
condition: bool,
attr: Option<&str>,
) -> Option<&'a mut Element> {
if !condition {
return None;
}
let mut child = Element::new(tag);
if let Some(name) = attr {
child.set(name, "1");
}
parent.append(child);
parent.children.last_mut()
}
fn append_text(top: Option<&mut Element>, chunk: &str) {
let Some(top) = top else {
return;
};
match top.text.as_mut() {
Some(existing) => existing.push_str(chunk),
None => top.text = Some(chunk.to_string()),
}
}
fn decode_character_reference(text: &str) -> Option<String> {
let digits = text.strip_prefix('#')?;
let code = match digits.strip_prefix(['x', 'X']) {
Some(hex) => u32::from_str_radix(hex, 16).ok()?,
None => digits.parse::<u32>().ok()?,
};
char::from_u32(code).map(String::from)
}
fn split_path(path: &str) -> Option<(&str, &str)> {
let mut depth = 0i32;
for (index, ch) in path.char_indices() {
match ch {
'{' => depth += 1,
'}' => depth -= 1,
'/' if depth == 0 => return Some((&path[..index], &path[index + 1..])),
_ => {}
}
}
None
}
pub fn fromstring(data: &[u8]) -> Result<Element> {
let text = std::str::from_utf8(data)
.map_err(|e| Error::Xml(format!("input is not valid UTF-8: {e}")))?;
let mut reader = Reader::from_str(text);
reader.config_mut().trim_text(false);
reader.config_mut().check_end_names = true;
let mut stack: Vec<Element> = vec![Element::new("__document__")];
let mut bindings: Vec<(String, String)> = Vec::new();
let mut scope_marks: Vec<usize> = vec![0];
let mut buffer = Vec::new();
loop {
match reader.read_event_into(&mut buffer) {
Ok(Event::Start(start)) => {
scope_marks.push(bindings.len());
let el = start_element(start, &mut bindings)?;
stack.push(el);
}
Ok(Event::Empty(start)) => {
let el = start_element(start, &mut bindings)?;
let parent = stack.last_mut().expect("document root");
parent.children.push(el);
}
Ok(Event::End(BytesEnd { .. })) => {
if stack.len() > 1 {
if let Some(mark) = scope_marks.pop() {
bindings.truncate(mark);
}
let el = stack.pop().expect("element underflow");
let parent = stack.last_mut().expect("document root");
parent.children.push(el);
}
}
Ok(Event::Text(text)) => {
let decoded =
quick_xml::escape::unescape(&text).map_err(|e| Error::Xml(e.to_string()))?;
append_text(stack.last_mut(), &decoded);
}
Ok(Event::GeneralRef(reference)) => {
let name = reference.as_ref();
let resolved = match name {
"amp" => "&".to_string(),
"lt" => "<".to_string(),
"gt" => ">".to_string(),
"quot" => "\"".to_string(),
"apos" => "'".to_string(),
_ => match decode_character_reference(name) {
Some(character) => character,
None => {
return Err(Error::Xml(format!(
"undefined entity reference &{name}; in content this is not \
valid XML"
)))
}
},
};
append_text(stack.last_mut(), &resolved);
}
Ok(Event::CData(cdata)) => {
let decoded = cdata
.escape()
.map_err(|e| Error::Xml(e.to_string()))?
.to_string();
append_text(stack.last_mut(), &decoded);
}
Ok(Event::Eof) => break,
Ok(_) => {}
Err(e) => return Err(Error::Xml(e.to_string())),
}
buffer.clear();
}
if stack.len() == 1 {
let mut document = stack.pop().expect("document sentinel");
return match document.children.pop() {
Some(root) => Ok(root),
None => Err(Error::Xml("document has no root element".into())),
};
}
Err(Error::Xml("no root element found".into()))
}
fn start_element(start: BytesStart<'_>, bindings: &mut Vec<(String, String)>) -> Result<Element> {
let mut raw: Vec<(String, String)> = Vec::new();
for attr in start.attributes().with_checks(false) {
let attr = attr.map_err(|e| Error::Xml(e.to_string()))?;
let value = attr
.normalized_value(quick_xml::XmlVersion::Implicit1_0)?
.into_owned();
raw.push((attr.key.as_ref().to_string(), value));
}
for (key, value) in &raw {
if key == "xmlns" {
bindings.push((String::new(), value.clone()));
} else if let Some(prefix) = key.strip_prefix("xmlns:") {
bindings.push((prefix.to_string(), value.clone()));
}
}
let name = start.name();
let tag = qualify(name.as_ref(), bindings, false);
let mut el = Element::new(tag);
for (key, value) in raw {
if key == "xmlns" || key.starts_with("xmlns:") {
continue;
}
el.attributes.push((qualify(&key, bindings, true), value));
}
Ok(el)
}
fn qualify(name: &str, bindings: &[(String, String)], is_attribute: bool) -> String {
let (prefix, local) = match name.split_once(':') {
Some((prefix, local)) => (prefix, local),
None => ("", name),
};
let resolved = if is_attribute && prefix.is_empty() {
None
} else {
lookup(bindings, prefix)
};
match resolved {
Some(uri) => format!("{{{uri}}}{local}"),
None => local.to_string(),
}
}
fn lookup<'a>(bindings: &'a [(String, String)], prefix: &str) -> Option<&'a str> {
if prefix == "xml" {
return Some(super::constants::XML_NS);
}
bindings
.iter()
.rev()
.find(|(declared, _)| declared == prefix)
.map(|(_, uri)| uri.as_str())
}
pub fn serialize(root: &Element) -> Vec<u8> {
let mut writer = XmlWriterInner::new(Vec::new());
write_element_with(&mut writer, root);
writer.into_inner()
}
fn write_element_with<W: std::io::Write>(writer: &mut XmlWriterInner<W>, el: &Element) {
let mut start = BytesStart::new(super::constants::local_name(&el.tag).to_string());
if let Some(ns) = namespace_of(&el.tag) {
start = start.with_attributes([("xmlns", ns)]);
}
for (key, value) in &el.attributes {
let attr_name = if let Some(ns) = namespace_of(key) {
BytesStart::new(format!(
"xmlns:{}",
REGISTERED_PREFIXES
.iter()
.find(|(_, u)| *u == ns)
.map(|(p, _)| *p)
.unwrap_or("ns")
))
} else {
BytesStart::new(key.clone())
};
let _ = attr_name;
start = start.with_attributes([(key.as_str(), value.as_str())]);
}
if el.children.is_empty() && el.text.is_none() {
let _ = writer.write_event(Event::Empty(start));
} else {
let _ = writer.write_event(Event::Start(start));
if let Some(text) = &el.text {
let _ = writer.write_event(Event::Text(BytesText::new(text)));
}
for child in &el.children {
write_element_with(writer, child);
}
let _ = writer.write_event(Event::End(BytesEnd::new(
super::constants::local_name(&el.tag).to_string(),
)));
}
}
pub fn read_all<R: Read>(mut reader: R) -> Result<Vec<u8>> {
let mut buf = Vec::new();
reader.read_to_end(&mut buf)?;
Ok(buf)
}
pub fn repr_float(value: f64) -> String {
if value == value.trunc() && value.abs() < 1e16 {
format!("{}", value as i64)
} else {
let mut s = format!("{value}");
if s.contains('e') || s.contains("inf") || s.contains("NaN") {
s = format!("{value:?}");
}
s
}
}
pub fn safe_string(value: f64) -> String {
if value == 0.0 {
return "0".to_string();
}
let exp = value.abs().log10().floor() as i32;
if !(-4..15).contains(&exp) {
let mut s = format!("{:.14e}", value);
if let Some(pos) = s.find('e') {
let (mantissa, exponent) = s.split_at(pos);
let mut mantissa = mantissa.trim_end_matches('0').to_string();
if mantissa.ends_with('.') {
mantissa.pop();
}
let exponent = &exponent[1..];
let (sign, digits) = match exponent.strip_prefix('-') {
Some(d) => ('-', d),
None => ('+', exponent),
};
s = format!("{mantissa}e{sign}{:0>2}", digits);
}
s
} else {
let decimals = (14 - exp).max(0) as usize;
let mut s = format!("{:.*}", decimals, value);
if s.contains('.') {
s = s.trim_end_matches('0').trim_end_matches('.').to_string();
}
s
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_and_find() {
let xml =
br#"<?xml version="1.0"?><root xmlns="urn:x"><child a="1">hi</child><child/></root>"#;
let root = fromstring(xml).unwrap();
assert_eq!(root.tag, "{urn:x}root");
assert_eq!(root.find_text("{urn:x}child", ""), "hi");
assert_eq!(root.find("{urn:x}child").unwrap().get("a"), Some("1"));
assert_eq!(root.find_all("{urn:x}child").len(), 2);
assert!(root.find("{urn:x}missing").is_none());
}
#[test]
fn parse_namespaced_attributes() {
let xml =
br#"<w:workbook xmlns:w="urn:w" xmlns:r="urn:r"><w:sheet r:id="rId1"/></w:workbook>"#;
let root = fromstring(xml).unwrap();
let sheet = root.find("{urn:w}sheet").unwrap();
assert_eq!(sheet.get("{urn:r}id"), Some("rId1"));
}
#[test]
fn serialise_declares_namespaces() {
let mut el = Element::new(format!("{{{SHEET_MAIN_NS}}}workbook"));
el.append(Element::new(format!("{{{SHEET_MAIN_NS}}}sheets")));
let out = el.to_pretty_string();
assert!(out.starts_with("<s:workbook xmlns:s="));
assert!(out.contains(SHEET_MAIN_NS));
assert!(out.contains("<s:sheets/>"));
assert!(out.trim_end().ends_with("</s:workbook>"));
}
#[test]
fn streaming_writer_matches_python_shape() {
let mut doc = XmlWriter::new();
doc.start_tag("worksheet", [("xmlns", SHEET_MAIN_NS)]);
doc.tag("dimension", [("ref", "A1:B2")], None);
doc.start_tag("row", [("r", "1")]);
doc.tag("c", [("r", "A1"), ("t", "s")], Some("0"));
doc.end_tag("row");
doc.end_tag("worksheet");
assert_eq!(
doc.as_str(),
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><dimension ref=\"A1:B2\"></dimension><row r=\"1\"><c r=\"A1\" t=\"s\">0</c></row></worksheet>"
);
}
#[test]
fn escaping_is_applied() {
let mut doc = XmlWriter::new();
doc.tag("t", [("a", "x\"&<>")], Some("a<b&c>d"));
assert_eq!(
doc.as_str(),
"<t a=\"x"&<>\">a<b&c>d</t>"
);
}
#[test]
fn an_inline_string_is_read_from_is_not_from_v() {
let cell = fromstring(br#"<c r="A1" t="inlineStr"><is><t>plain & simple</t></is></c>"#)
.expect("parses");
let text = cell
.find("is")
.and_then(|is| is.find("t"))
.and_then(|t| t.text.clone());
assert_eq!(
text.as_deref(),
Some("plain & simple"),
"an inline string carries its text in <is><t>, not in <v>"
);
}
#[test]
fn an_entity_reference_in_text_is_resolved_rather_than_dropped() {
for (xml, expected) in [
("<t>a & b</t>", "a & b"),
("<t>Page &P of &N</t>", "Page &P of &N"),
("<t><tag></t>", "<tag>"),
("<t>"quoted"</t>", "\"quoted\""),
("<t>it's</t>", "it's"),
("<t>AB</t>", "AB"),
("<t>AB</t>", "AB"),
("<t>Ben & Jerry's</t>", "Ben & Jerry's"),
] {
let element = fromstring(xml.as_bytes()).expect("parses");
assert_eq!(
element.text.as_deref(),
Some(expected),
"{xml} should read back as {expected:?}"
);
}
}
#[test]
fn an_entity_reference_in_an_attribute_is_kept() {
let element = fromstring(br#"<t name="a & b"/>"#).expect("parses");
assert_eq!(element.get("name"), Some("a & b"));
}
#[test]
fn an_undeclared_entity_is_an_error_rather_than_a_silent_omission() {
assert!(fromstring(b"<t> </t>").is_err());
}
#[test]
fn safe_string_matches_python_g_format() {
assert_eq!(safe_string(0.0), "0");
assert_eq!(safe_string(1.0), "1");
assert_eq!(safe_string(1.5), "1.5");
assert_eq!(safe_string(1234567890123456.0), "1.23456789012346e+15");
assert_eq!(safe_string(0.0001), "0.0001");
}
#[test]
fn repr_float_drops_trailing_dot() {
assert_eq!(repr_float(1.0), "1");
assert_eq!(repr_float(1.5), "1.5");
}
}