@ -1,7 +1,6 @@
//! Implementation of [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) syntax
//! Implementation of [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) syntax
use crate ::model ::vocab ::rdf ;
use crate ::model ::vocab ::rdf ;
use crate ::model ::Triple ;
use crate ::model ::* ;
use crate ::model ::* ;
use crate ::Result ;
use crate ::Result ;
use failure ::format_err ;
use failure ::format_err ;
@ -82,18 +81,18 @@ enum RdfXmlState {
} ,
} ,
RDF {
RDF {
base_uri : Option < Url > ,
base_uri : Option < Url > ,
language : String ,
language : Option < LanguageTag > ,
} ,
} ,
NodeElt {
NodeElt {
base_uri : Option < Url > ,
base_uri : Option < Url > ,
language : String ,
language : Option < LanguageTag > ,
subject : NamedOrBlankNode ,
subject : NamedOrBlankNode ,
} ,
} ,
PropertyElt {
PropertyElt {
//Resource, Literal or Empty property element
//Resource, Literal or Empty property element
uri : Url ,
uri : Url ,
base_uri : Option < Url > ,
base_uri : Option < Url > ,
language : String ,
language : Option < LanguageTag > ,
subject : NamedOrBlankNode ,
subject : NamedOrBlankNode ,
object : Option < NamedOrBlankNode > ,
object : Option < NamedOrBlankNode > ,
id_attr : Option < NamedNode > ,
id_attr : Option < NamedNode > ,
@ -102,7 +101,7 @@ enum RdfXmlState {
ParseTypeCollectionPropertyElt {
ParseTypeCollectionPropertyElt {
uri : NamedNode ,
uri : NamedNode ,
base_uri : Option < Url > ,
base_uri : Option < Url > ,
language : String ,
language : Option < LanguageTag > ,
subject : NamedOrBlankNode ,
subject : NamedOrBlankNode ,
id_attr : Option < NamedNode > ,
id_attr : Option < NamedNode > ,
} ,
} ,
@ -120,13 +119,13 @@ impl RdfXmlState {
}
}
}
}
fn language ( & self ) -> & str {
fn language ( & self ) -> Option < & LanguageTag > {
match self {
match self {
RdfXmlState ::Doc { .. } = > "" ,
RdfXmlState ::Doc { .. } = > None ,
RdfXmlState ::RDF { language , .. } = > language ,
RdfXmlState ::RDF { language , .. } = > language . as_ref ( ) ,
RdfXmlState ::NodeElt { language , .. } = > language ,
RdfXmlState ::NodeElt { language , .. } = > language . as_ref ( ) ,
RdfXmlState ::PropertyElt { language , .. } = > language ,
RdfXmlState ::PropertyElt { language , .. } = > language . as_ref ( ) ,
RdfXmlState ::ParseTypeCollectionPropertyElt { language , .. } = > language ,
RdfXmlState ::ParseTypeCollectionPropertyElt { language , .. } = > language . as_ref ( ) ,
}
}
}
}
}
}
@ -193,10 +192,10 @@ impl<R: BufRead> RdfXmlIterator<R> {
let uri = self . resolve_tag_name ( event . name ( ) ) ? ;
let uri = self . resolve_tag_name ( event . name ( ) ) ? ;
//We read attributes
//We read attributes
let mut language = String ::default ( ) ;
let mut language = None ;
let mut base_uri = None ;
let mut base_uri = None ;
if let Some ( current_state ) = self . state . last ( ) {
if let Some ( current_state ) = self . state . last ( ) {
language = current_state . language ( ) . to_string ( ) ;
language = current_state . language ( ) . cloned ( ) ;
base_uri = current_state . base_uri ( ) . clone ( ) ;
base_uri = current_state . base_uri ( ) . clone ( ) ;
}
}
let mut id_attr = None ;
let mut id_attr = None ;
@ -212,7 +211,9 @@ impl<R: BufRead> RdfXmlIterator<R> {
let attribute = attribute ? ;
let attribute = attribute ? ;
match attribute . key {
match attribute . key {
b" xml:lang " = > {
b" xml:lang " = > {
language = attribute . unescape_and_decode_value ( & self . reader ) ? ;
language = Some ( LanguageTag ::parse (
& attribute . unescape_and_decode_value ( & self . reader ) ? ,
) ? ) ;
}
}
b" xml:base " = > {
b" xml:base " = > {
base_uri = Some ( self . resolve_uri ( & attribute . unescaped_value ( ) ? , & None ) ? )
base_uri = Some ( self . resolve_uri ( & attribute . unescaped_value ( ) ? , & None ) ? )
@ -434,7 +435,7 @@ impl<R: BufRead> RdfXmlIterator<R> {
& mut self ,
& mut self ,
uri : NamedNode ,
uri : NamedNode ,
base_uri : Option < Url > ,
base_uri : Option < Url > ,
language : String ,
language : Option < LanguageTag > ,
id_attr : Option < NamedNode > ,
id_attr : Option < NamedNode > ,
node_id_attr : Option < BlankNode > ,
node_id_attr : Option < BlankNode > ,
about_attr : Option < NamedNode > ,
about_attr : Option < NamedNode > ,
@ -474,7 +475,7 @@ impl<R: BufRead> RdfXmlIterator<R> {
& mut self ,
& mut self ,
uri : NamedNode ,
uri : NamedNode ,
base_uri : Option < Url > ,
base_uri : Option < Url > ,
language : String ,
language : Option < LanguageTag > ,
subject : NamedOrBlankNode ,
subject : NamedOrBlankNode ,
id_attr : Option < NamedNode > ,
id_attr : Option < NamedNode > ,
) -> RdfXmlState {
) -> RdfXmlState {
@ -544,13 +545,18 @@ impl<R: BufRead> RdfXmlIterator<R> {
Ok ( ( ) )
Ok ( ( ) )
}
}
fn new_literal ( & self , text : String , language : String , datatype : Option < NamedNode > ) -> Literal {
fn new_literal (
& self ,
text : String ,
language : Option < LanguageTag > ,
datatype : Option < NamedNode > ,
) -> Literal {
if let Some ( datatype ) = datatype {
if let Some ( datatype ) = datatype {
Literal ::new_typed_literal ( text , datatype )
Literal ::new_typed_literal ( text , datatype )
} else if language . is_empty ( ) {
} else if let Some ( language ) = language {
Literal ::new_simple_literal ( text )
} else {
Literal ::new_language_tagged_literal ( text , language )
Literal ::new_language_tagged_literal ( text , language )
} else {
Literal ::new_simple_literal ( text )
}
}
}
}
@ -581,16 +587,16 @@ impl<R: BufRead> RdfXmlIterator<R> {
& mut self ,
& mut self ,
subject : & NamedOrBlankNode ,
subject : & NamedOrBlankNode ,
literal_attributes : Vec < ( NamedNode , String ) > ,
literal_attributes : Vec < ( NamedNode , String ) > ,
language : & str ,
language : & Option < LanguageTag > ,
) {
) {
for ( literal_predicate , literal_value ) in literal_attributes {
for ( literal_predicate , literal_value ) in literal_attributes {
self . triples_cache . push ( Triple ::new (
self . triples_cache . push ( Triple ::new (
subject . clone ( ) ,
subject . clone ( ) ,
literal_predicate ,
literal_predicate ,
if language . is_empty ( ) {
if let Some ( language ) = language {
Literal ::new_simp le_literal ( literal_value )
Literal ::new_languag e_tagged _literal ( literal_value , language . clone ( ) )
} else {
} else {
Literal ::new_languag e_tagged _literal ( literal_value , languag e )
Literal ::new_simp le_literal ( literal_value )
} ,
} ,
) ) ;
) ) ;
}
}