diff --git a/Cargo.toml b/Cargo.toml index 8c694ca8..1d17d5b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ url = "1" uuid = { version = "0.7", features = ["v4"] } bzip2 = "0.3" byteorder = "1" -quick-xml = "0.12" +quick-xml = "0.13" [build-dependencies] peg = "0.5" diff --git a/src/rio/xml.rs b/src/rio/xml.rs index 109cfb0e..7142549c 100644 --- a/src/rio/xml.rs +++ b/src/rio/xml.rs @@ -136,62 +136,37 @@ impl Iterator for RdfXmlIterator { } //Read more XML - let (namespace, event) = match self + match self .reader .read_namespaced_event(&mut buffer, &mut self.namespace_buffer) { - Ok((Some(namespace), event)) => { - (Some(self.reader.decode(namespace).to_string()), event) - } - Ok((None, event)) => (None, event), - Err(error) => return Some(Err(error.into())), - }; - - match event { - Event::Start(event) => { - let uri = match namespace { - Some(namespace) => { - Url::parse(&(namespace + &self.reader.decode(&event.local_name()))) + Ok((_, event)) => match event { + Event::Start(event) => { + if let Err(error) = self.parse_start_event(&event) { + return Some(Err(error)); } - None => Url::parse(&self.reader.decode(&event.name())), - }; - let uri = match uri { - Ok(uri) => uri, - Err(error) => return Some(Err(error.into())), - }; - if let Err(error) = self.parse_start_event(&event, uri) { - return Some(Err(error)); } - } - Event::Text(event) => { - if let Err(error) = self.parse_text_event(&event) { - return Some(Err(error)); + Event::Text(event) => { + if let Err(error) = self.parse_text_event(&event) { + return Some(Err(error)); + } } - } - Event::End(event) => { - if let Err(error) = self.parse_end_event(&event) { - return Some(Err(error)); + Event::End(event) => { + if let Err(error) = self.parse_end_event(&event) { + return Some(Err(error)); + } } - } - Event::Eof => return None, - _ => (), + Event::Eof => return None, + _ => (), + }, + Err(error) => return Some(Err(error.into())), } } } } -/* -fn read_next_event<'a, R: BufRead>(reader: &'a mut Reader, namespace_buffer: &mut Vec) -> Result<(Option, Event<'a>)> { - let mut buffer = Vec::default(); - Ok(match reader.read_namespaced_event(&mut buffer, namespace_buffer)? { - (Some(namespace), event) => (Some(reader.decode(namespace).to_string()), event), - (None, event) => (None, event) - }) -} -*/ - impl RdfXmlIterator { - fn parse_start_event(&mut self, event: &BytesStart, uri: Url) -> Result<()> { + fn parse_start_event(&mut self, event: &BytesStart) -> Result<()> { #[derive(PartialEq, Eq)] enum RdfXmlParseType { Default, @@ -208,6 +183,8 @@ impl RdfXmlIterator { PropertyElt { subject: NamedOrBlankNode }, } + let uri = self.resolve_tag_name(event.name())?; + //We read attributes let mut language = String::default(); let mut base_uri = None; @@ -234,7 +211,7 @@ impl RdfXmlIterator { base_uri = Some(self.resolve_uri(&attribute.unescaped_value()?, &None)?) } key if !key.starts_with(b"xml") => { - let attribute_url = self.resolve_q_name(key)?; + let attribute_url = self.resolve_attribute_name(key)?; if attribute_url == *RDF_ID { let mut id = Vec::with_capacity(attribute.value.len() + 1); id.push(b'#'); @@ -417,8 +394,19 @@ impl RdfXmlIterator { Ok(()) } - fn resolve_q_name(&self, qname: &[u8]) -> Result { - let (namespace, local_name) = self.reader.resolve_namespace(qname, &self.namespace_buffer); + fn resolve_tag_name(&self, qname: &[u8]) -> Result { + let (namespace, local_name) = self.reader.event_namespace(qname, &self.namespace_buffer); + self.resolve_ns_name(namespace, local_name) + } + + fn resolve_attribute_name(&self, qname: &[u8]) -> Result { + let (namespace, local_name) = self + .reader + .attribute_namespace(qname, &self.namespace_buffer); + self.resolve_ns_name(namespace, local_name) + } + + fn resolve_ns_name(&self, namespace: Option<&[u8]>, local_name: &[u8]) -> Result { Ok(Url::parse( &(match namespace { Some(namespace) => self.reader.decode(namespace) + self.reader.decode(local_name),