From f9e8bb7c4ea09eb0b6c14bda4c5e4f418219271a Mon Sep 17 00:00:00 2001 From: Tpt Date: Sat, 12 Jun 2021 13:38:47 +0200 Subject: [PATCH] XML query results: fixes nested triples parsing --- lib/src/sparql/model.rs | 8 ++++++-- lib/src/sparql/xml_results.rs | 32 +++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/src/sparql/model.rs b/lib/src/sparql/model.rs index 2393bdad..e8e51e77 100644 --- a/lib/src/sparql/model.rs +++ b/lib/src/sparql/model.rs @@ -516,8 +516,8 @@ fn test_serialization_rountrip() -> Result<(), EvaluationError> { use std::str; for format in &[ - QueryResultsFormat::Xml, QueryResultsFormat::Json, + QueryResultsFormat::Xml, QueryResultsFormat::Tsv, ] { let results = vec![ @@ -563,7 +563,11 @@ fn test_serialization_rountrip() -> Result<(), EvaluationError> { Triple::new( NamedNode::new_unchecked("http://example.com/s"), NamedNode::new_unchecked("http://example.com/p"), - NamedNode::new_unchecked("http://example.com/o"), + Triple::new( + NamedNode::new_unchecked("http://example.com/os"), + NamedNode::new_unchecked("http://example.com/op"), + NamedNode::new_unchecked("http://example.com/oo"), + ), ) .into(), ), diff --git a/lib/src/sparql/xml_results.rs b/lib/src/sparql/xml_results.rs index 5f773ba1..d7b201cf 100644 --- a/lib/src/sparql/xml_results.rs +++ b/lib/src/sparql/xml_results.rs @@ -260,7 +260,10 @@ pub fn read_xml_results(source: impl BufRead + 'static) -> io::Result { namespace_buffer: Vec, mapping: BTreeMap, usize>, stack: Vec, + subject_stack: Vec, + predicate_stack: Vec, + object_stack: Vec, } impl Iterator for ResultsIterator { @@ -368,9 +374,6 @@ impl ResultsIterator { let mut term: Option = None; let mut lang = None; let mut datatype = None; - let mut subject = None; - let mut predicate = None; - let mut object = None; loop { let (ns, event) = self .reader @@ -547,15 +550,21 @@ impl ResultsIterator { state = State::Result; } State::Subject => { - subject = term.take(); + if let Some(subject) = term.take() { + self.subject_stack.push(subject) + } state = State::Triple; } State::Predicate => { - predicate = term.take(); + if let Some(predicate) = term.take() { + self.predicate_stack.push(predicate) + } state = State::Triple; } State::Object => { - object = term.take(); + if let Some(object) = term.take() { + self.object_stack.push(object) + } state = State::Triple; } State::Uri | State::BNode => state = self.stack.pop().unwrap(), @@ -567,10 +576,11 @@ impl ResultsIterator { state = self.stack.pop().unwrap(); } State::Triple => { - println!("foo"); - if let (Some(subject), Some(predicate), Some(object)) = - (subject.take(), predicate.take(), object.take()) - { + if let (Some(subject), Some(predicate), Some(object)) = ( + self.subject_stack.pop(), + self.predicate_stack.pop(), + self.object_stack.pop(), + ) { term = Some( Triple::new( match subject {