Skip to content

Commit

Permalink
add indentation level to RdfXmlConfig
Browse files Browse the repository at this point in the history
fix #156
  • Loading branch information
pchampin committed May 28, 2024
1 parent ef36db2 commit b041da9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
9 changes: 7 additions & 2 deletions sophia/examples/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use sophia::turtle::serializer::{
turtle::{TurtleConfig, TurtleSerializer},
};
#[cfg(feature = "xml")]
use sophia::xml::serializer::RdfXmlSerializer;
use sophia::xml::serializer::{RdfXmlConfig, RdfXmlSerializer};

fn main() {
let input = BufReader::new(stdin());
Expand Down Expand Up @@ -63,7 +63,12 @@ fn main() {
JsonLdSerializer::new_with_options(out, JsonLdOptions::new().with_spaces(2)),
),
#[cfg(feature = "xml")]
"rdfxml" | "rdf" => serialize_triples(quad_source, RdfXmlSerializer::new(out)),
"rdfxml" | "rdf" => {
let indent = if pretty { 4 } else { 0 };
let config = RdfXmlConfig::new().with_identation(indent);
let ser = RdfXmlSerializer::new_with_config(out, config);
serialize_triples(quad_source, ser)
}
_ => {
eprintln!("Unrecognized format: {}", format);
std::process::exit(-1);
Expand Down
47 changes: 44 additions & 3 deletions xml/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@ use std::io;

/// RDF/XML serializer configuration.
#[derive(Clone, Debug, Default)]
pub struct RdfXmlConfig {}
pub struct RdfXmlConfig {
indentation: usize,
}

impl RdfXmlConfig {
// TODO add ways to customize namespaces
/// Size of the indentation to use in the serialization.
/// (defaults to 0, meaning no indentation nor linebreaks)
pub fn indentation(&self) -> usize {
self.indentation
}

/// Build a new default [`RdfXmlConfig`]
pub fn new() -> Self {
Default::default()
}

/// Transform an [`RdfXmlConfig`] by setting the [`indentation`](RdfXmlConfig::indentation).
pub fn with_identation(mut self, i: usize) -> Self {
self.indentation = i;
self
}
}

/// RDF/XML serializer.
Expand Down Expand Up @@ -65,7 +82,12 @@ where
TS: TripleSource,
{
// temporarily move out self.write
let mut tf = RdfXmlFormatter::new(&mut self.write).map_err(SinkError)?;
let res = if self.config.indentation > 0 {
RdfXmlFormatter::with_indentation(&mut self.write, self.config.indentation)
} else {
RdfXmlFormatter::new(&mut self.write)
};
let mut tf = res.map_err(SinkError)?;
rio_format_triples(&mut tf, source)?;
tf.finish().map_err(SinkError)?;
Ok(self)
Expand Down Expand Up @@ -132,4 +154,23 @@ pub(crate) mod test {
}
Ok(())
}

#[test]
fn roundtrip_with_ident() -> Result<(), Box<dyn std::error::Error>> {
let config = RdfXmlConfig::new().with_identation(4);
for rdfxml in TESTS {
println!("==========\n{}\n----------", rdfxml);
let g1: Vec<[SimpleTerm; 3]> = crate::parser::parse_str(rdfxml).collect_triples()?;

let out = RdfXmlSerializer::new_stringifier_with_config(config.clone())
.serialize_triples(g1.triples())?
.to_string();
println!("{}", &out);

let g2: Vec<[SimpleTerm; 3]> = crate::parser::parse_str(&out).collect_triples()?;

assert!(isomorphic_graphs(&g1, &g2)?);
}
Ok(())
}
}

0 comments on commit b041da9

Please sign in to comment.