<s> """<STR_LIT:U+0020>""" <EOL> from rdflib . namespace import Namespace <EOL> from rdflib . term import URIRef <EOL> from rdflib . term import BNode <EOL> from rdflib . term import Literal <EOL> from rdflib . graph import Graph , ConjunctiveGraph <EOL> from rdflib . exceptions import ParserError <EOL> from rdflib . parser import Parser <EOL> from xml . sax . saxutils import handler <EOL> from xml . sax import make_parser <EOL> from xml . sax . handler import ErrorHandler <EOL> TRIXNS = Namespace ( "<STR_LIT>" ) <EOL> XMLNS = Namespace ( "<STR_LIT>" ) <EOL> class TriXHandler ( handler . ContentHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , store ) : <EOL> self . store = store <EOL> self . preserve_bnode_ids = False <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> self . bnode = { } <EOL> self . graph = self . store <EOL> self . triple = None <EOL> self . state = <NUM_LIT:0> <EOL> self . lang = None <EOL> self . datatype = None <EOL> def setDocumentLocator ( self , locator ) : <EOL> self . locator = locator <EOL> def startDocument ( self ) : <EOL> pass <EOL> def startPrefixMapping ( self , prefix , namespace ) : <EOL> pass <EOL> def endPrefixMapping ( self , prefix ) : <EOL> pass <EOL> def startElementNS ( self , name , qname , attrs ) : <EOL> if name [ <NUM_LIT:0> ] != str ( TRIXNS ) : <EOL> self . error ( "<STR_LIT>" % ( name [ <NUM_LIT:0> ] , TRIXNS ) ) <EOL> if name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:0> : <EOL> self . state = <NUM_LIT:1> <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:1> : <EOL> self . state = <NUM_LIT:2> <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:2> : <EOL> self . state = <NUM_LIT:3> <EOL> elif self . state == <NUM_LIT:4> : <EOL> pass <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:2> : <EOL> if self . graph == None : <EOL> self . graph = Graph ( store = self . store . store ) <EOL> self . triple = [ ] <EOL> self . state = <NUM_LIT:4> <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:4> : <EOL> self . lang = None <EOL> self . datatype = None <EOL> try : <EOL> self . lang = attrs . getValue ( ( unicode ( XMLNS ) , u"<STR_LIT>" ) ) <EOL> except : <EOL> pass <EOL> try : <EOL> self . datatype = attrs . getValueByQName ( u"<STR_LIT>" ) <EOL> except KeyError : <EOL> self . error ( "<STR_LIT>" ) <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:4> : <EOL> self . lang = None <EOL> self . datatype = None <EOL> try : <EOL> self . lang = attrs . getValue ( ( unicode ( XMLNS ) , u"<STR_LIT>" ) ) <EOL> except : <EOL> pass <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT:id>" : <EOL> if self . state == <NUM_LIT:2> : <EOL> self . state = <NUM_LIT:3> <EOL> elif self . state == <NUM_LIT:4> : <EOL> pass <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> else : <EOL> self . error ( "<STR_LIT>" % name [ <NUM_LIT:1> ] ) <EOL> self . chars = "<STR_LIT>" <EOL> def endElementNS ( self , name , qname ) : <EOL> if name [ <NUM_LIT:0> ] != str ( TRIXNS ) : <EOL> self . error ( "<STR_LIT>" % ( name [ <NUM_LIT:0> ] , TRIXNS ) ) <EOL> if name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:3> : <EOL> self . graph = Graph ( store = self . store . store , identifier = URIRef ( self . chars . strip ( ) ) ) <EOL> self . state = <NUM_LIT:2> <EOL> elif self . state == <NUM_LIT:4> : <EOL> self . triple += [ URIRef ( self . chars . strip ( ) ) ] <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT:id>" : <EOL> if self . state == <NUM_LIT:3> : <EOL> self . graph = Graph ( self . store . store , identifier = self . get_bnode ( self . chars . strip ( ) ) ) <EOL> self . state = <NUM_LIT:2> <EOL> elif self . state == <NUM_LIT:4> : <EOL> self . triple += [ self . get_bnode ( self . chars . strip ( ) ) ] <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" or name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:4> : <EOL> self . triple += [ Literal ( self . chars , lang = self . lang , datatype = self . datatype ) ] <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if self . state == <NUM_LIT:4> : <EOL> if len ( self . triple ) != <NUM_LIT:3> : <EOL> self . error ( "<STR_LIT>" % ( len ( self . triple ) , self . triple ) ) <EOL> self . graph . add ( self . triple ) <EOL> self . state = <NUM_LIT:2> <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> self . graph = None <EOL> self . state = <NUM_LIT:1> <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> self . state = <NUM_LIT:0> <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> def get_bnode ( self , label ) : <EOL> if self . preserve_bnode_ids : <EOL> bn = BNode ( label ) <EOL> else : <EOL> if label in self . bnode : <EOL> bn = self . bnode [ label ] <EOL> else : <EOL> bn = BNode ( label ) <EOL> self . bnode [ label ] = bn <EOL> return bn <EOL> def characters ( self , content ) : <EOL> self . chars += content <EOL> def ignorableWhitespace ( self , content ) : <EOL> pass <EOL> def processingInstruction ( self , target , data ) : <EOL> pass <EOL> def error ( self , message ) : <EOL> locator = self . locator <EOL> info = "<STR_LIT>" % ( locator . getSystemId ( ) , <EOL> locator . getLineNumber ( ) , locator . getColumnNumber ( ) ) <EOL> raise ParserError ( info + message ) <EOL> def create_parser ( store ) : <EOL> parser = make_parser ( ) <EOL> parser . start_namespace_decl ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> parser . setFeature ( handler . feature_namespaces , <NUM_LIT:1> ) <EOL> trix = TriXHandler ( store ) <EOL> parser . setContentHandler ( trix ) <EOL> parser . setErrorHandler ( ErrorHandler ( ) ) <EOL> return parser <EOL> class TriXParser ( Parser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> def parse ( self , source , sink , ** args ) : <EOL> assert sink . store . context_aware <EOL> g = ConjunctiveGraph ( store = sink . store ) <EOL> self . _parser = create_parser ( g ) <EOL> content_handler = self . _parser . getContentHandler ( ) <EOL> preserve_bnode_ids = args . get ( "<STR_LIT>" , None ) <EOL> if preserve_bnode_ids is not None : <EOL> content_handler . preserve_bnode_ids = preserve_bnode_ids <EOL> self . _parser . parse ( source ) </s>
