
    4Ysh                        d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZmZmZmZ d	Zd
ez  Z e j0                  e      Z	  G d de      Z G d de      Zd Z G d de      Z G d d      Zy)    N)IOOptional)XMLGenerator)XML_NAMESPACE)AttributesNSImpl)etree)LiteralURIRefBNodeVariable)ResultResultParserResultSerializerResultExceptionz&http://www.w3.org/2005/sparql-results#z{%s}c                        e Zd Zddee   fdZy)XMLResultParserNcontent_typec                     t        |      S N)	XMLResult)selfsourcer   s      /var/www/sten-cake5-migrate2.hellocrow.space/lexinfo-master/env/lib/python3.12/site-packages/rdflib/plugins/sparql/results/xmlresults.pyparsezXMLResultParser.parse!   s          r   )__name__
__module____qualname__r   strr    r   r   r   r      s    !(3- !r   r   c                        e Zd Zddee   fdZy)r   Nr   c           	          	 t        j                  d      }t        j                  ||      }|j	                  t
        dz         }|j	                  t
        dz         }|d}n|d}nt        d      t        j                  | |       |dk(  rg | _	        |D ]O  }i }	|D ]+  }
t        |
d         |	t        |
j                  d	            <   - | j                  j                  |	       Q |j                  d
t
        dt
        d      D cg c]  }t        |j                  d	             c}| _        y |j                   j#                         j%                         dk(  | _        y # t        $ r t        j                  |      }Y Yw xY wc c}w )NT)	huge_treebooleanresultsASKSELECTz/No RDF result-bindings or boolean answer found!r   namez./zhead/variabletrue)r   	XMLParserr   	TypeErrorfindRESULTS_NS_ETr   r   __init__bindings	parseTermr   getappendfindallvarstextlowerstrip	askAnswer)r   r   r   parsertreer$   r%   type_resultrbindingxs               r   r/   zXMLResult.__init__&   se   	'__t4F;;vv.D ))MI56))MI56E E!"STTe$HDM! (% MG7@7LAhw{{6234M$$Q'	( -:MJ v'DI %\\//1779VCDN?  	';;v&D	'.s   ,E& !F&FFr   )r   r   r   r   r   r/   r    r   r   r   r   %   s    %DXc] %Dr   r   c                    | j                   | j                  }}|t        dz   k(  rv|d}d}d}| j                  dd      rt	        | j                  d            }n1| j                  dt
        z  d      r| j                  dt
        z        }t        |||      }|S |t        dz   k(  rt	        |      S |t        dz   k(  rt        |      S t        d	| z        )
zLrdflib object (Literal, URIRef, BNode) for the given
    elementtree elementliteralN datatypez{%s}lang)rD   languribnodezunknown binding type %r)	tagr6   r.   r2   r
   r   r	   r   r,   )elementrH   r6   rD   rE   rets         r   r1   r1   N   s     W\\C
mi''<D;;z4(gkk*56H[[m3T:;;zM9:DdXD9
	%	%d|	'	'T{1G;<<r   c                   $    e Zd Zd ZddedefdZy)XMLResultSerializerc                 0    t        j                  | |       y r   )r   r/   )r   r=   s     r   r/   zXMLResultSerializer.__init__h   s    !!$/r   streamencodingc                    t        ||      }| j                  j                  dk(  r7|j                  g        |j	                  | j                  j
                         n|j                  | j                  j                         |j                          | j                  j                  D ]L  }|j                          |j                         D ]  \  }}|j                  ||        |j                          N |j                          y )Nr&   )SPARQLXMLWriterr=   typewrite_header	write_askr9   r5   write_results_headerr0   write_start_resultitemswrite_bindingwrite_end_resultclose)r   rN   rO   kwargswriterbkeyvals           r   	serializezXMLResultSerializer.serializek   s     2;;u$#T[[223 0 01'')[[)) *))+ !	 3HC((c23 '')* 	r   Nzutf-8)r   r   r   r/   r   r   r`   r    r   r   rL   rL   g   s    0 c r   rL   c                   B    e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zy
)rQ   z1
    Python saxutils-based SPARQL XML Writer
    c                    t        ||      }|j                          |j                  dt               |j                  dt               |j                  t        dfdt        i i              || _        || _        || _	        d| _
        y )NrC   xmlsparqlF)r   startDocumentstartPrefixMappingSPARQL_XML_NAMESPACEr   startElementNSr   r\   _output	_encoding_results)r   outputrO   r\   s       r   r/   zSPARQLXMLWriter.__init__   s{    fh/!!"&:;!!%7!8,h8HR8P	
 !r   c           	         | j                   j                  t        dfdt        i i              t	        dt        |            D ]e  }dt        ||         i}ddi}| j                   j                  t        dfdt        ||             | j                   j                  t        dfd       g | j                   j                  t        dfd       y )Nheadr   Nr(   r(   r)   )r\   ri   rh   r   rangelenr   endElementNS)r   allvarsLi	attr_valsattr_qnamess        r   rS   zSPARQLXMLWriter.write_header   s    ""!6*F4DR4L	
 q#h-( 	UAHQK 0I K KK&&%z2 K8
 KK$$&:J%GT	U 	  "6!?Hr   c                    | j                   j                  t        dfdt        i i              | j                   j	                  t        |      j                                | j                   j                  t        dfd       y )Nr$   )r\   ri   rh   r   
charactersr   r7   rs   )r   r_   s     r   rT   zSPARQLXMLWriter.write_ask   sa    ""!9-y:J2r:R	
 	s3x~~/0  "6	!BINr   c                 l    | j                   j                  t        dfdt        i i              d| _        y )Nr%   T)r\   ri   rh   r   rl   r   s    r   rU   z$SPARQLXMLWriter.write_results_header   s1    ""!9-y:J2r:R	
 r   c                 l    | j                   j                  t        dfdt        i i              d| _        y )Nr=   T)r\   ri   rh   r   _resultStartedr{   s    r   rV   z"SPARQLXMLWriter.write_start_result   s2    ""!8,h8HR8P	
 #r   c                 r    | j                   sJ | j                  j                  t        dfd       d| _         y )Nr=   F)r}   r\   rs   rh   r{   s    r   rY   z SPARQLXMLWriter.write_end_result   s3    """"  "6!A8L#r   c                    | j                   sJ dt        |      i}ddi}| j                  j                  t        dfdt        ||             t        |t              rl| j                  j                  t        dfdt        i i              | j                  j                  |       | j                  j                  t        dfd       nUt        |t              rk| j                  j                  t        dfdt        i i              | j                  j                  |       | j                  j                  t        dfd       nt        |t              ri }i }|j                  r!|j                  |t        df<   d|t        df<   n |j                  r|j                  |d<   d	|d<   | j                  j                  t        d
fd
t        ||             | j                  j                  |       | j                  j                  t        d
fd
       nt        d|z        | j                  j                  t        dfd       y )Nrp   r(   r?   rF   rG   rE   zxml:lang)NrD   rD   rB   zUnsupported RDF term: %s)r}   r   r\   ri   rh   r   
isinstancer
   ry   rs   r   r	   languager   rD   	Exception)r   r(   r_   rv   rw   s        r   rX   zSPARQLXMLWriter.write_binding   s   """" CI
	 F
 	""!9-Y4	
 c6"KK&&%u-u6Fr26N KK""3'KK$$&:E%BEJU#KK&&%w/:J2r:R KK""3'KK$$&:G%DgNW%IK||58\\	=&127A]F3403	,-2<./KK&&%y1 K8
 KK""3'KK$$&:I%F	R 6<==  "6	!BINr   c                     | j                   r"| j                  j                  t        dfd       | j                  j                  t        dfd       | j                  j	                          y )Nr%   re   )rl   r\   rs   rh   endDocumentr{   s    r   rZ   zSPARQLXMLWriter.close   sM    ==KK$$&:I%F	R  "6!A8L!r   Nra   )r   r   r   __doc__r/   rS   rT   rU   rV   rY   rX   rZ   r    r   r   rQ   rQ      s3    I&O#$
0Od"r   rQ   ) loggingtypingr   r   xml.sax.saxutilsr   xml.domr   xml.sax.xmlreaderr   rdflib.compatr   rdflibr	   r
   r   r   rdflib.queryr   r   r   r   rh   r.   	getLoggerr   logr   r   r1   rL   rQ   r    r   r   <module>r      s      ) ! .  3 3 P P @ --g!!l !&D &DR=2* 0s" s"r   