Uses new quick-xml APIs for XML results serialization

pull/435/head
Tpt 2 years ago committed by Thomas Tanon
parent 0f43ef19e3
commit 20dc1f26df
  1. 112
      lib/sparesults/src/xml.rs

@ -18,19 +18,17 @@ pub fn write_boolean_xml_result<W: Write>(sink: W, value: bool) -> io::Result<W>
fn do_write_boolean_xml_result<W: Write>(sink: W, value: bool) -> Result<W, quick_xml::Error> { fn do_write_boolean_xml_result<W: Write>(sink: W, value: bool) -> Result<W, quick_xml::Error> {
let mut writer = Writer::new(sink); let mut writer = Writer::new(sink);
writer.write_event(Event::Decl(BytesDecl::new("1.0", None, None)))?; writer.write_event(Event::Decl(BytesDecl::new("1.0", None, None)))?;
let mut sparql_open = BytesStart::new("sparql"); writer
sparql_open.push_attribute(("xmlns", "http://www.w3.org/2005/sparql-results#")); .create_element("sparql")
writer.write_event(Event::Start(sparql_open))?; .with_attribute(("xmlns", "http://www.w3.org/2005/sparql-results#"))
writer.write_event(Event::Start(BytesStart::new("head")))?; .write_inner_content(|writer| {
writer.write_event(Event::End(BytesEnd::new("head")))?; writer
writer.write_event(Event::Start(BytesStart::new("boolean")))?; .create_element("head")
writer.write_event(Event::Text(BytesText::new(if value { .write_text_content(BytesText::new(""))?
"true" .create_element("boolean")
} else { .write_text_content(BytesText::new(if value { "true" } else { "false" }))?;
"false" Ok(())
})))?; })?;
writer.write_event(Event::End(BytesEnd::new("boolean")))?;
writer.write_event(Event::End(BytesEnd::new("sparql")))?;
Ok(writer.into_inner()) Ok(writer.into_inner())
} }
@ -49,13 +47,17 @@ impl<W: Write> XmlSolutionsWriter<W> {
let mut sparql_open = BytesStart::new("sparql"); let mut sparql_open = BytesStart::new("sparql");
sparql_open.push_attribute(("xmlns", "http://www.w3.org/2005/sparql-results#")); sparql_open.push_attribute(("xmlns", "http://www.w3.org/2005/sparql-results#"));
writer.write_event(Event::Start(sparql_open))?; writer.write_event(Event::Start(sparql_open))?;
writer.write_event(Event::Start(BytesStart::new("head")))?; writer
for variable in &variables { .create_element("head")
let mut variable_tag = BytesStart::new("variable"); .write_inner_content(|writer| {
variable_tag.push_attribute(("name", variable.as_str())); for variable in &variables {
writer.write_event(Event::Empty(variable_tag))?; writer
} .create_element("variable")
writer.write_event(Event::End(BytesEnd::new("head")))?; .with_attribute(("name", variable.as_str()))
.write_empty()?;
}
Ok(())
})?;
writer.write_event(Event::Start(BytesStart::new("results")))?; writer.write_event(Event::Start(BytesStart::new("results")))?;
Ok(Self { writer }) Ok(Self { writer })
} }
@ -74,12 +76,13 @@ impl<W: Write> XmlSolutionsWriter<W> {
self.writer self.writer
.write_event(Event::Start(BytesStart::new("result")))?; .write_event(Event::Start(BytesStart::new("result")))?;
for (variable, value) in solution { for (variable, value) in solution {
let mut binding_tag = BytesStart::new("binding");
binding_tag.push_attribute(("name", variable.as_str()));
self.writer.write_event(Event::Start(binding_tag))?;
write_xml_term(value, &mut self.writer)?;
self.writer self.writer
.write_event(Event::End(BytesEnd::new("binding")))?; .create_element("binding")
.with_attribute(("name", variable.as_str()))
.write_inner_content(|writer| {
write_xml_term(value, writer)?;
Ok(())
})?;
} }
self.writer.write_event(Event::End(BytesEnd::new("result"))) self.writer.write_event(Event::End(BytesEnd::new("result")))
} }
@ -105,39 +108,48 @@ fn write_xml_term(
) -> Result<(), quick_xml::Error> { ) -> Result<(), quick_xml::Error> {
match term { match term {
TermRef::NamedNode(uri) => { TermRef::NamedNode(uri) => {
writer.write_event(Event::Start(BytesStart::new("uri")))?; writer
writer.write_event(Event::Text(BytesText::new(uri.as_str())))?; .create_element("uri")
writer.write_event(Event::End(BytesEnd::new("uri")))?; .write_text_content(BytesText::new(uri.as_str()))?;
} }
TermRef::BlankNode(bnode) => { TermRef::BlankNode(bnode) => {
writer.write_event(Event::Start(BytesStart::new("bnode")))?; writer
writer.write_event(Event::Text(BytesText::new(bnode.as_str())))?; .create_element("bnode")
writer.write_event(Event::End(BytesEnd::new("bnode")))?; .write_text_content(BytesText::new(bnode.as_str()))?;
} }
TermRef::Literal(literal) => { TermRef::Literal(literal) => {
let mut literal_tag = BytesStart::new("literal"); let element = writer.create_element("literal");
if let Some(language) = literal.language() { let element = if let Some(language) = literal.language() {
literal_tag.push_attribute(("xml:lang", language)); element.with_attribute(("xml:lang", language))
} else if !literal.is_plain() { } else if !literal.is_plain() {
literal_tag.push_attribute(("datatype", literal.datatype().as_str())); element.with_attribute(("datatype", literal.datatype().as_str()))
} } else {
writer.write_event(Event::Start(literal_tag))?; element
writer.write_event(Event::Text(BytesText::new(literal.value())))?; };
writer.write_event(Event::End(BytesEnd::new("literal")))?; element.write_text_content(BytesText::new(literal.value()))?;
} }
#[cfg(feature = "rdf-star")] #[cfg(feature = "rdf-star")]
TermRef::Triple(triple) => { TermRef::Triple(triple) => {
writer.write_event(Event::Start(BytesStart::new("triple")))?; writer
writer.write_event(Event::Start(BytesStart::new("subject")))?; .create_element("triple")
write_xml_term(triple.subject.as_ref().into(), writer)?; .write_inner_content(|writer| {
writer.write_event(Event::End(BytesEnd::new("subject")))?; writer
writer.write_event(Event::Start(BytesStart::new("predicate")))?; .create_element("subject")
write_xml_term(triple.predicate.as_ref().into(), writer)?; .write_inner_content(|writer| {
writer.write_event(Event::End(BytesEnd::new("predicate")))?; write_xml_term(triple.subject.as_ref().into(), writer)
writer.write_event(Event::Start(BytesStart::new("object")))?; })?;
write_xml_term(triple.object.as_ref(), writer)?; writer
writer.write_event(Event::End(BytesEnd::new("object")))?; .create_element("predicate")
writer.write_event(Event::End(BytesEnd::new("triple")))?; .write_inner_content(|writer| {
write_xml_term(triple.predicate.as_ref().into(), writer)
})?;
writer
.create_element("object")
.write_inner_content(|writer| {
write_xml_term(triple.object.as_ref(), writer)
})?;
Ok(())
})?;
} }
} }
Ok(()) Ok(())

Loading…
Cancel
Save