o
    éT•jo=  ã                   @   sž  d Z dZddlT ddlmZmZ ddlmZ ddlmZmZ ddlm	Z	 ddlm
Z
mZ dd	lZejd	d
… dkr>edd	ƒ‚[zddlmZ W n eyS   edd	ƒ‚w eedƒs^edd	ƒ‚ddlmZmZmZ ejZejZzdd	lZW n ey�   dd„ ZY n
w dd	lZejZ[[G dd„ dƒZG dd„ dejƒZG dd„ dej ejƒZ!dd„ Z"e#dkrÍdd	l$Z%e"ƒ Z&e& 'e%j(j )¡ ¡ e& *e%j( +¡ ¡ e& ,d¡ d	S d	S )z]
SAX driver for the pyexpat C module.  This driver works with
pyexpat.__version__ == '2.22'.
z0.20é    )Ú*)Úfeature_validationÚfeature_namespaces)Úfeature_namespace_prefixes)Úfeature_external_gesÚfeature_external_pes)Úfeature_string_interning)Úproperty_xml_stringÚproperty_interning_dictNé   Újavazexpat not available in Java)Úexpatzexpat not supportedÚParserCreate)Ú	xmlreaderÚsaxutilsÚhandlerc                 C   s   | S ©N© )Úor   r   ú*/usr/lib/python3.10/xml/sax/expatreader.pyÚ_mkproxy'   s   r   c                   @   s   e Zd ZdS )Ú_ClosedParserN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   .   s    r   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚExpatLocatorz¼Locator for use with the ExpatParser class.

    This uses a weak reference to the parser object to avoid creating
    a circular reference between the parser and the content handler.
    c                 C   s   t |ƒ| _d S r   )r   Ú_ref©ÚselfÚparserr   r   r   Ú__init__9   ó   zExpatLocator.__init__c                 C   s   | j }|jd u r
d S |jjS r   )r   Ú_parserÚErrorColumnNumberr   r   r   r   ÚgetColumnNumber<   ó   
zExpatLocator.getColumnNumberc                 C   s   | j }|jd u r
dS |jjS ©Né   )r   r"   ÚErrorLineNumberr   r   r   r   ÚgetLineNumberB   r%   zExpatLocator.getLineNumberc                 C   ó   | j }|d u r	d S |j ¡ S r   )r   Ú_sourceÚgetPublicIdr   r   r   r   r,   H   ó   
zExpatLocator.getPublicIdc                 C   r*   r   )r   r+   ÚgetSystemIdr   r   r   r   r.   N   r-   zExpatLocator.getSystemIdN)	r   r   r   Ú__doc__r    r$   r)   r,   r.   r   r   r   r   r   3   s    r   c                   @   s  e Zd ZdZdDdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdEdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Z d?d@„ Z!dAdB„ Z"dCS )FÚExpatParserz$SAX driver for the pyexpat C module.r   éìÿ  c                 C   sF   t j | |¡ t  ¡ | _d | _|| _d | _d| _g | _	d| _
d | _d S )NFr   )r   ÚIncrementalParserr    ÚInputSourcer+   r"   Ú_namespacesÚ_lex_handler_propÚ_parsingÚ_entity_stackÚ_external_gesÚ
_interning)r   ÚnamespaceHandlingÚbufsizer   r   r   r    Z   s   

zExpatParser.__init__c                 C   sN   t  |¡}|| _z|  ¡  | j t| ƒ¡ tj 	| |¡ W dS    |  
¡  ‚ )z3Parse an XML document from a URL or an InputSource.N)r   Úprepare_input_sourcer+   ÚresetÚ_cont_handlerÚsetDocumentLocatorr   r   r2   ÚparseÚ_close_source©r   Úsourcer   r   r   r@   g   s   
zExpatParser.parsec                 C   s$   |  ¡ d ur| j |  ¡ ¡ d S d S r   )r.   r"   ÚSetBaserB   r   r   r   ÚprepareParserw   s   ÿzExpatParser.prepareParserc                 C   s$   t j | |¡ | jr|  ¡  d S d S r   )r   r2   ÚsetContentHandlerr6   Ú_reset_cont_handler)r   r   r   r   r   rF   }   s   ÿzExpatParser.setContentHandlerc                 C   sL   |t kr| jS |tkr| jd uS |tttfv rdS |tkr | jS t	d| ƒ‚)Nr   úFeature '%s' not recognized)
r   r4   r   r9   r   r   r   r   r8   ÚSAXNotRecognizedException©r   Únamer   r   r   Ú
getFeature‚   s   
ÿzExpatParser.getFeaturec                 C   s´   | j rtdƒ‚|tkr|| _d S |tkr|| _d S |tkr0|r+| jd u r)i | _d S d S d | _d S |tkr<|r:tdƒ‚d S |t	krH|rFtdƒ‚d S |t
krT|rRtdƒ‚d S td| ƒ‚)Nz!Cannot set features while parsingz!expat does not support validationz/expat does not read external parameter entitiesz(expat does not report namespace prefixesrH   )r6   ÚSAXNotSupportedExceptionr   r4   r   r8   r   r9   r   r   r   rI   )r   rK   Ústater   r   r   Ú
setFeatureŽ   sB   



ÿ
ÿÿÿÿÿÿÿzExpatParser.setFeaturec                 C   s^   |t jkr| jS |tkr| jS |tkr)| jr%t| jdƒr!| j ¡ S t	dƒ‚t
dƒ‚t	d| ƒ‚)NÚGetInputContextz=This version of expat does not support getting the XML stringz.XML string cannot be returned when not parsingúProperty '%s' not recognized)r   Úproperty_lexical_handlerr5   r
   r9   r	   r"   ÚhasattrrP   rI   rM   rJ   r   r   r   ÚgetProperty¬   s   

ÿÿzExpatParser.getPropertyc                 C   sX   |t jkr|| _| jr|  ¡  d S d S |tkr|| _d S |tkr&td| ƒ‚t	d| ƒ‚)NzProperty '%s' cannot be setrQ   )
r   rR   r5   r6   Ú_reset_lex_handler_propr
   r9   r	   rM   rI   )r   rK   Úvaluer   r   r   ÚsetProperty¾   s   
ÿ
ÿÿzExpatParser.setPropertyFc              
   C   s|   | j s|  ¡  d| _ | j ¡  z
| j ||¡ W d S  tjy= } ztt 	|j
¡|| ƒ}| j |¡ W Y d }~d S d }~ww )NT)r6   r=   r>   ÚstartDocumentr"   ÚParser   ÚerrorÚSAXParseExceptionÚErrorStringÚcodeÚ_err_handlerÚ
fatalError)r   ÚdataÚisFinalÚeÚexcr   r   r   ÚfeedÎ   s   
€ýzExpatParser.feedc                 C   s^   | j }z| ¡ }|d ur| ¡  W | ¡ }|d ur| ¡  d S d S | ¡ }|d ur.| ¡  w w r   )r+   ÚgetCharacterStreamÚcloseÚgetByteStream)r   rC   Úfiler   r   r   rA   ß   s   €ÿÿ
ÿzExpatParser._close_sourcec                 C   s¾   | j s| jd u st| jtƒrd S z1| jddd� | j ¡  d| _d | _W d| _| jd ur<tƒ }| jj|_| jj	|_	|| _|  
¡  d S d| _| jd urZtƒ }| jj|_| jj	|_	|| _|  
¡  w )Nó    T)ra   F)r7   r"   Ú
isinstancer   rd   r>   ÚendDocumentr6   r#   r(   rA   r   r   r   r   rf   ê   s.   
ÿ



ù



zExpatParser.closec                 C   s   | j j| j_| j j| j_d S r   )r>   ÚprocessingInstructionr"   ÚProcessingInstructionHandlerÚ
charactersÚCharacterDataHandler©r   r   r   r   rG   ÿ   s   ÿzExpatParser._reset_cont_handlerc                 C   sb   | j }| j}|d u rd |_d |_d |_d |_d |_d S |j|_|j|_|j	|_| j
|_|j|_d S r   )r5   r"   ÚCommentHandlerÚStartCdataSectionHandlerÚEndCdataSectionHandlerÚStartDoctypeDeclHandlerÚEndDoctypeDeclHandlerÚcommentÚ
startCDATAÚendCDATAÚstart_doctype_declÚendDTD)r   Úlexr   r   r   r   rU     s   
z#ExpatParser._reset_lex_handler_propc                 C   sø   | j rtj| j ¡ d| jd�| _d| j_| j| j_	| j
| j_ntj| j ¡ | jd�| _| j| j_	| j| j_|  ¡  | j| j_| j| j_| j| j_| j| j_d | _| jrW|  ¡  | j| j_z| j| j_W n	 tyl   Y nw | j tj ¡ d| _!g | _"d S )Nú )Úinternr'   F)#r4   r   r   r+   ÚgetEncodingr9   r"   Únamespace_prefixesÚstart_element_nsÚStartElementHandlerÚend_element_nsÚEndElementHandlerÚstart_elementÚend_elementrG   Úunparsed_entity_declÚUnparsedEntityDeclHandlerÚnotation_declÚNotationDeclHandlerÚstart_namespace_declÚStartNamespaceDeclHandlerÚend_namespace_declÚEndNamespaceDeclHandlerÚ_decl_handler_propr5   rU   Úexternal_entity_refÚExternalEntityRefHandlerÚskipped_entity_handlerÚSkippedEntityHandlerÚAttributeErrorÚSetParamEntityParsingÚ*XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONEr6   r7   rp   r   r   r   r=     s>   ÿ
ÿ






þÿ
zExpatParser.resetc                 C   s   | j d u rd S | j jS r   )r"   r#   rp   r   r   r   r$   ;  ó   
zExpatParser.getColumnNumberc                 C   s   | j d u rdS | j jS r&   )r"   r(   rp   r   r   r   r)   @  r–   zExpatParser.getLineNumberc                 C   ó
   | j  ¡ S r   )r+   r,   rp   r   r   r   r,   E  ó   
zExpatParser.getPublicIdc                 C   r—   r   )r+   r.   rp   r   r   r   r.   H  r˜   zExpatParser.getSystemIdc                 C   s   | j  |t|ƒ¡ d S r   )r>   ÚstartElementÚAttributesImpl)r   rK   Úattrsr   r   r   r„   L  ó   zExpatParser.start_elementc                 C   ó   | j  |¡ d S r   )r>   Ú
endElementrJ   r   r   r   r…   O  ó   zExpatParser.end_elementc                 C   sì   |  ¡ }t|ƒdkrd |f}nt|ƒdkr|d |d f}nt|ƒ}i }i }| ¡ D ]>\}}|  ¡ }t|ƒ}	|	dkrA|}
d |f}n|	dkrXd|d |d f }
|d |d f}n|d }
t|ƒ}|||< |
||< q*| j |d t||ƒ¡ d S )Nr'   é   r   z%s:%sé   )ÚsplitÚlenÚtupleÚitemsr>   ÚstartElementNSÚAttributesNSImpl)r   rK   r›   ÚpairÚnewattrsÚqnamesÚanamerV   ÚpartsÚlengthÚqnameÚapairr   r   r   r€   R  s0   



ÿzExpatParser.start_element_nsc                 C   sV   |  ¡ }t|ƒdkrd |f}nt|ƒdkr|d |d f}nt|ƒ}| j |d ¡ d S )Nr'   r    r   )r¢   r£   r¤   r>   ÚendElementNS)r   rK   r¨   r   r   r   r‚   t  s   
zExpatParser.end_element_nsc                 C   ó   | j  ||¡ d S r   )r>   rl   )r   Útargetr`   r   r   r   Úprocessing_instruction€  ó   z"ExpatParser.processing_instructionc                 C   r�   r   )r>   rn   )r   r`   r   r   r   Úcharacter_data„  rŸ   zExpatParser.character_datac                 C   r±   r   )r>   ÚstartPrefixMapping)r   ÚprefixÚurir   r   r   rŠ   ‡  r´   z ExpatParser.start_namespace_declc                 C   r�   r   )r>   ÚendPrefixMapping)r   r·   r   r   r   rŒ   Š  rŸ   zExpatParser.end_namespace_declc                 C   s   | j  |||¡ d S r   )r5   ÚstartDTD)r   rK   ÚsysidÚpubidÚhas_internal_subsetr   r   r   ry   �  ó   zExpatParser.start_doctype_declc                 C   s   | j  ||||¡ d S r   )Ú_dtd_handlerÚunparsedEntityDecl)r   rK   Úbaser»   r¼   Únotation_namer   r   r   r†   �  rœ   z ExpatParser.unparsed_entity_declc                 C   s   | j  |||¡ d S r   )r¿   ÚnotationDecl)r   rK   rÁ   r»   r¼   r   r   r   rˆ   “  r¾   zExpatParser.notation_declc                 C   s”   | j sdS | j ||¡}t || j ¡ pd¡}| j | j	| jf¡ | j	 
|¡| _	|| _z	tj | |¡ W n   Y dS | jd \| _	| _| jd= dS )Nr'   Ú r   éÿÿÿÿ)r8   Ú_ent_handlerÚresolveEntityr   r<   r+   r.   r7   Úappendr"   ÚExternalEntityParserCreater   r2   r@   )r   ÚcontextrÁ   r»   r¼   rC   r   r   r   r�   –  s"   
þzExpatParser.external_entity_refc                 C   s   |rd| }| j  |¡ d S )Nú%)r>   ÚskippedEntity)r   rK   Úis_per   r   r   r‘   ¬  s   z"ExpatParser.skipped_entity_handlerN)r   r1   )F)#r   r   r   r/   r    r@   rE   rF   rL   rO   rT   rW   rd   rA   rf   rG   rU   r=   r$   r)   r,   r.   r„   r…   r€   r‚   r³   rµ   rŠ   rŒ   ry   r†   rˆ   r�   r‘   r   r   r   r   r0   W   sB    

'"r0   c                  O   s   t | i |¤ŽS r   )r0   )ÚargsÚkwargsr   r   r   Úcreate_parser´  r!   rÐ   Ú__main__z:http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml)-r/   ÚversionÚxml.sax._exceptionsÚxml.sax.handlerr   r   r   r   r   r   r	   r
   ÚsysÚplatformÚSAXReaderNotAvailableÚxml.parsersr   ÚImportErrorrS   Úxml.saxr   r   r   rš   r§   Ú_weakrefr   ÚweakrefÚproxyr   ÚLocatorr   r2   r0   rÐ   r   Úxml.sax.saxutilsÚxmlÚprF   ÚsaxÚXMLGeneratorÚsetErrorHandlerÚErrorHandlerr@   r   r   r   r   Ú<module>   sV    

ÿ

ÿ$  _û