|
|
@ -3,7 +3,7 @@ |
|
|
|
use crate::model::*; |
|
|
|
use crate::model::*; |
|
|
|
use crate::sparql::model::*; |
|
|
|
use crate::sparql::model::*; |
|
|
|
use crate::Result; |
|
|
|
use crate::Result; |
|
|
|
use failure::format_err; |
|
|
|
use anyhow::anyhow; |
|
|
|
use quick_xml::events::BytesDecl; |
|
|
|
use quick_xml::events::BytesDecl; |
|
|
|
use quick_xml::events::BytesEnd; |
|
|
|
use quick_xml::events::BytesEnd; |
|
|
|
use quick_xml::events::BytesStart; |
|
|
|
use quick_xml::events::BytesStart; |
|
|
@ -99,7 +99,7 @@ pub fn write_xml_results<W: Write>(results: QueryResult<'_>, sink: W) -> Result< |
|
|
|
writer.write_event(Event::End(BytesEnd::borrowed(b"sparql")))?; |
|
|
|
writer.write_event(Event::End(BytesEnd::borrowed(b"sparql")))?; |
|
|
|
} |
|
|
|
} |
|
|
|
QueryResult::Graph(_) => { |
|
|
|
QueryResult::Graph(_) => { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Graphs could not be formatted to SPARQL query results XML format" |
|
|
|
"Graphs could not be formatted to SPARQL query results XML format" |
|
|
|
)); |
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
@ -130,7 +130,7 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
let (ns, event) = reader.read_namespaced_event(&mut buffer, &mut namespace_buffer)?; |
|
|
|
let (ns, event) = reader.read_namespaced_event(&mut buffer, &mut namespace_buffer)?; |
|
|
|
if let Some(ns) = ns { |
|
|
|
if let Some(ns) = ns { |
|
|
|
if ns != b"http://www.w3.org/2005/sparql-results#".as_ref() { |
|
|
|
if ns != b"http://www.w3.org/2005/sparql-results#".as_ref() { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Unexpected namespace found in RDF/XML query result: {}", |
|
|
|
"Unexpected namespace found in RDF/XML query result: {}", |
|
|
|
reader.decode(ns)? |
|
|
|
reader.decode(ns)? |
|
|
|
)); |
|
|
|
)); |
|
|
@ -144,14 +144,14 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
if event.name() == b"sparql" { |
|
|
|
if event.name() == b"sparql" { |
|
|
|
state = State::Sparql; |
|
|
|
state = State::Sparql; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Expecting <sparql> tag, found {}", reader.decode(event.name())?)); |
|
|
|
return Err(anyhow!("Expecting <sparql> tag, found {}", reader.decode(event.name())?)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
State::Sparql => { |
|
|
|
State::Sparql => { |
|
|
|
if event.name() == b"head" { |
|
|
|
if event.name() == b"head" { |
|
|
|
state = State::Head; |
|
|
|
state = State::Head; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Expecting <head> tag, found {}", reader.decode(event.name())?)); |
|
|
|
return Err(anyhow!("Expecting <head> tag, found {}", reader.decode(event.name())?)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
State::Head => { |
|
|
|
State::Head => { |
|
|
@ -159,12 +159,12 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
let name = event.attributes() |
|
|
|
let name = event.attributes() |
|
|
|
.filter_map(|attr| attr.ok()) |
|
|
|
.filter_map(|attr| attr.ok()) |
|
|
|
.find(|attr| attr.key == b"name") |
|
|
|
.find(|attr| attr.key == b"name") |
|
|
|
.ok_or_else(|| format_err!("No name attribute found for the <variable> tag"))?; |
|
|
|
.ok_or_else(|| anyhow!("No name attribute found for the <variable> tag"))?; |
|
|
|
variables.push(name.unescape_and_decode_value(&reader)?); |
|
|
|
variables.push(name.unescape_and_decode_value(&reader)?); |
|
|
|
} else if event.name() == b"link" { |
|
|
|
} else if event.name() == b"link" { |
|
|
|
// no op
|
|
|
|
// no op
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Expecting <variable> or <link> tag, found {}", reader.decode(event.name())?)); |
|
|
|
return Err(anyhow!("Expecting <variable> or <link> tag, found {}", reader.decode(event.name())?)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
State::AfterHead => { |
|
|
|
State::AfterHead => { |
|
|
@ -186,17 +186,17 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
}), |
|
|
|
}), |
|
|
|
))); |
|
|
|
))); |
|
|
|
} else if event.name() != b"link" && event.name() != b"results" && event.name() != b"boolean" { |
|
|
|
} else if event.name() != b"link" && event.name() != b"results" && event.name() != b"boolean" { |
|
|
|
return Err(format_err!("Expecting sparql tag, found {}", reader.decode(event.name())?)); |
|
|
|
return Err(anyhow!("Expecting sparql tag, found {}", reader.decode(event.name())?)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
State::Boolean => return Err(format_err!("Unexpected tag inside of <boolean> tag: {}", reader.decode(event.name())?)) |
|
|
|
State::Boolean => return Err(anyhow!("Unexpected tag inside of <boolean> tag: {}", reader.decode(event.name())?)) |
|
|
|
}, |
|
|
|
}, |
|
|
|
Event::Empty(event) => match state { |
|
|
|
Event::Empty(event) => match state { |
|
|
|
State::Sparql => { |
|
|
|
State::Sparql => { |
|
|
|
if event.name() == b"head" { |
|
|
|
if event.name() == b"head" { |
|
|
|
state = State::AfterHead; |
|
|
|
state = State::AfterHead; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Expecting <head> tag, found {}", reader.decode(event.name())?)); |
|
|
|
return Err(anyhow!("Expecting <head> tag, found {}", reader.decode(event.name())?)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
State::Head => { |
|
|
|
State::Head => { |
|
|
@ -204,12 +204,12 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
let name = event.attributes() |
|
|
|
let name = event.attributes() |
|
|
|
.filter_map(|v| v.ok()) |
|
|
|
.filter_map(|v| v.ok()) |
|
|
|
.find(|attr| attr.key == b"name") |
|
|
|
.find(|attr| attr.key == b"name") |
|
|
|
.ok_or_else(|| format_err!("No name attribute found for the <variable> tag"))?; |
|
|
|
.ok_or_else(|| anyhow!("No name attribute found for the <variable> tag"))?; |
|
|
|
variables.push(name.unescape_and_decode_value(&reader)?); |
|
|
|
variables.push(name.unescape_and_decode_value(&reader)?); |
|
|
|
} else if event.name() == b"link" { |
|
|
|
} else if event.name() == b"link" { |
|
|
|
// no op
|
|
|
|
// no op
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Expecting <variable> or <link> tag, found {}", reader.decode(event.name())?)); |
|
|
|
return Err(anyhow!("Expecting <variable> or <link> tag, found {}", reader.decode(event.name())?)); |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
State::AfterHead => { |
|
|
|
State::AfterHead => { |
|
|
@ -219,10 +219,10 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
Box::new(empty()), |
|
|
|
Box::new(empty()), |
|
|
|
))) |
|
|
|
))) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Unexpected autoclosing tag <{}>", reader.decode(event.name())?)) |
|
|
|
return Err(anyhow!("Unexpected autoclosing tag <{}>", reader.decode(event.name())?)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
_ => return Err(format_err!("Unexpected autoclosing tag <{}>", reader.decode(event.name())?)) |
|
|
|
_ => return Err(anyhow!("Unexpected autoclosing tag <{}>", reader.decode(event.name())?)) |
|
|
|
}, |
|
|
|
}, |
|
|
|
Event::Text(event) => { |
|
|
|
Event::Text(event) => { |
|
|
|
let value = event.unescaped()?; |
|
|
|
let value = event.unescaped()?; |
|
|
@ -233,18 +233,18 @@ pub fn read_xml_results<'a>(source: impl BufRead + 'a) -> Result<QueryResult<'a> |
|
|
|
} else if value.as_ref() == b"false" { |
|
|
|
} else if value.as_ref() == b"false" { |
|
|
|
Ok(QueryResult::Boolean(false)) |
|
|
|
Ok(QueryResult::Boolean(false)) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Err(format_err!("Unexpected boolean value. Found {}", reader.decode(&value)?)) |
|
|
|
Err(anyhow!("Unexpected boolean value. Found {}", reader.decode(&value)?)) |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
_ => Err(format_err!("Unexpected textual value found: {}", reader.decode(&value)?)) |
|
|
|
_ => Err(anyhow!("Unexpected textual value found: {}", reader.decode(&value)?)) |
|
|
|
}; |
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
Event::End(_) => if let State::Head = state { |
|
|
|
Event::End(_) => if let State::Head = state { |
|
|
|
state = State::AfterHead; |
|
|
|
state = State::AfterHead; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("Unexpected early file end. All results file should have a <head> and a <result> or <boolean> tag")); |
|
|
|
return Err(anyhow!("Unexpected early file end. All results file should have a <head> and a <result> or <boolean> tag")); |
|
|
|
}, |
|
|
|
}, |
|
|
|
Event::Eof => return Err(format_err!("Unexpected early file end. All results file should have a <head> and a <result> or <boolean> tag")), |
|
|
|
Event::Eof => return Err(anyhow!("Unexpected early file end. All results file should have a <head> and a <result> or <boolean> tag")), |
|
|
|
_ => (), |
|
|
|
_ => (), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -292,7 +292,7 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
.read_namespaced_event(&mut self.buffer, &mut self.namespace_buffer)?; |
|
|
|
.read_namespaced_event(&mut self.buffer, &mut self.namespace_buffer)?; |
|
|
|
if let Some(ns) = ns { |
|
|
|
if let Some(ns) = ns { |
|
|
|
if ns != b"http://www.w3.org/2005/sparql-results#".as_ref() { |
|
|
|
if ns != b"http://www.w3.org/2005/sparql-results#".as_ref() { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Unexpected namespace found in RDF/XML query result: {}", |
|
|
|
"Unexpected namespace found in RDF/XML query result: {}", |
|
|
|
self.reader.decode(ns)? |
|
|
|
self.reader.decode(ns)? |
|
|
|
)); |
|
|
|
)); |
|
|
@ -304,7 +304,7 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
if event.name() == b"result" { |
|
|
|
if event.name() == b"result" { |
|
|
|
state = State::Result; |
|
|
|
state = State::Result; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Expecting <result>, found {}", |
|
|
|
"Expecting <result>, found {}", |
|
|
|
self.reader.decode(event.name())? |
|
|
|
self.reader.decode(event.name())? |
|
|
|
)); |
|
|
|
)); |
|
|
@ -319,14 +319,14 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
{ |
|
|
|
{ |
|
|
|
Some(attr) => current_var = Some(attr.unescaped_value()?.to_vec()), |
|
|
|
Some(attr) => current_var = Some(attr.unescaped_value()?.to_vec()), |
|
|
|
None => { |
|
|
|
None => { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"No name attribute found for the <binding> tag" |
|
|
|
"No name attribute found for the <binding> tag" |
|
|
|
)); |
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
state = State::Binding; |
|
|
|
state = State::Binding; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Expecting <binding>, found {}", |
|
|
|
"Expecting <binding>, found {}", |
|
|
|
self.reader.decode(event.name())? |
|
|
|
self.reader.decode(event.name())? |
|
|
|
)); |
|
|
|
)); |
|
|
@ -334,7 +334,7 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
} |
|
|
|
} |
|
|
|
State::Binding => { |
|
|
|
State::Binding => { |
|
|
|
if term.is_some() { |
|
|
|
if term.is_some() { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"There is already a value for the current binding" |
|
|
|
"There is already a value for the current binding" |
|
|
|
)); |
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
@ -356,7 +356,7 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
} |
|
|
|
} |
|
|
|
state = State::Literal; |
|
|
|
state = State::Literal; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Expecting <uri>, <bnode> or <literal> found {}", |
|
|
|
"Expecting <uri>, <bnode> or <literal> found {}", |
|
|
|
self.reader.decode(event.name())? |
|
|
|
self.reader.decode(event.name())? |
|
|
|
)); |
|
|
|
)); |
|
|
@ -390,7 +390,7 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
_ => { |
|
|
|
_ => { |
|
|
|
return Err(format_err!( |
|
|
|
return Err(anyhow!( |
|
|
|
"Unexpected textual value found: {}", |
|
|
|
"Unexpected textual value found: {}", |
|
|
|
self.reader.decode(&data)? |
|
|
|
self.reader.decode(&data)? |
|
|
|
)); |
|
|
|
)); |
|
|
@ -404,7 +404,7 @@ impl<R: BufRead> ResultsIterator<R> { |
|
|
|
if let Some(var) = ¤t_var { |
|
|
|
if let Some(var) = ¤t_var { |
|
|
|
new_bindings[self.mapping[var]] = term.clone() |
|
|
|
new_bindings[self.mapping[var]] = term.clone() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(format_err!("No name found for <binding> tag")); |
|
|
|
return Err(anyhow!("No name found for <binding> tag")); |
|
|
|
} |
|
|
|
} |
|
|
|
term = None; |
|
|
|
term = None; |
|
|
|
state = State::Result; |
|
|
|
state = State::Result; |
|
|
|