509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
œVW”;  ã               @   s  d Z dZdZddlZddlZejd dk rXejeƒd ZeefZ	ej
dƒd Zdd	„ ZneZ	d
d	„ ZddlT ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ yddlZW n( ek
rà   edejƒ d  ƒ‚Y nX G dd„ dej ƒZ G dd„ dej!ƒZ"dd„ Z#dS )aí   A SAX2 driver for libxml2, on top of it's XmlReader API

USAGE
    # put this file (drv_libxml2.py) in PYTHONPATH
    import xml.sax
    reader = xml.sax.make_parser(["drv_libxml2"])
    # ...and the rest is standard python sax.

CAVEATS
    - Lexical handlers are supported, except for start/endEntity
      (waiting for XmlReader.ResolveEntity) and start/endDTD
    - Error callbacks are not exactly synchronous, they tend
      to be invoked before the corresponding content callback,
      because the underlying reader interface parses
      data by chunks of 512 bytes
    
TODO
    - search for TODO
    - some ErrorHandler events (warning)
    - some ContentHandler events (setDocumentLocator, skippedEntity)
    - EntityResolver (using libxml2.?)
    - DTDHandler (if/when libxml2 exposes such node types)
    - DeclHandler (if/when libxml2 exposes such node types)
    - property_xml_string?
    - feature_string_interning?
    - Incremental parser
    - additional performance tuning:
      - one might cache callbacks to avoid some name lookups
      - one might implement a smarter way to pass attributes to startElement
        (some kind of lazy evaluation?)
      - there might be room for improvement in start/endPrefixMapping
      - other?

u    StÃ©phane Bidoul <sbi@skynet.be>z0.3é    Né   Úutf8é   c             C   s   | d kr| S t | ƒd S d S )Nr   )Ú_decoder)Ús© r   ú#/usr/lib64/python3.6/drv_libxml2.pyÚ_d1   s    r	   c             C   s   | S )Nr   )r   r   r   r   r	   9   s    )Ú*)Ú	xmlreaderÚsaxutils)
Úfeature_namespacesÚfeature_namespace_prefixesÚfeature_string_interningÚfeature_validationÚfeature_external_gesÚfeature_external_pesÚproperty_lexical_handlerÚproperty_declaration_handlerÚproperty_dom_nodeÚproperty_xml_stringz+libxml2 not available: import error was: %sc               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚLocatorz4SAX Locator adapter for libxml2.xmlTextReaderLocatorc             C   s
   || _ d S )N)Ú_Locator__locator)ÚselfÚlocatorr   r   r   Ú__init__S   s    zLocator.__init__c             C   s   dS )z6Return the column number where the current event ends.r   éÿÿÿÿr   )r   r   r   r   ÚgetColumnNumberV   s    zLocator.getColumnNumberc             C   s
   | j jƒ S )z4Return the line number where the current event ends.)r   Z
LineNumber)r   r   r   r   ÚgetLineNumberZ   s    zLocator.getLineNumberc             C   s   dS )z3Return the public identifier for the current event.Nr   )r   r   r   r   ÚgetPublicId^   s    zLocator.getPublicIdc             C   s
   | j jƒ S )z3Return the system identifier for the current event.)r   ZBaseURI)r   r   r   r   ÚgetSystemIdb   s    zLocator.getSystemIdN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    r   r   r   r   r   P   s   r   c               @   s\   e Z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S )ÚLibXml2Readerc             C   s@   t jj| ƒ d| _d| _d| _d| _d| _d | _d | _	d | _
d S )Nr   r   )r   Ú	XMLReaderr   Ú_LibXml2Reader__nsÚ_LibXml2Reader__nspfxÚ_LibXml2Reader__validateÚ_LibXml2Reader__extparamsÚ_LibXml2Reader__parsingÚ_LibXml2Reader__lex_handlerÚ_LibXml2Reader__decl_handlerÚ_LibXml2Reader__errors)r   r   r   r   r   h   s    zLibXml2Reader.__init__c             C   s0   | j d krg | _ | j j|t|d t|ƒƒfƒ d S )N)r.   ÚappendZSAXParseExceptionr   )r   ÚargÚmsgÚseverityr   r   r   r   Ú_errorHandlerw   s
    
zLibXml2Reader._errorHandlerc             C   sl   x`| j D ]V\}}|tjtjfkr.| jj|ƒ q|rR|| j d d krR| jj|ƒ q| jj|ƒ qW d | _ d S )Nr   r   )r.   Úlibxml2Z PARSER_SEVERITY_VALIDITY_WARNINGZPARSER_SEVERITY_WARNINGÚ_err_handlerZwarningÚ
fatalErrorÚerror)r   Zfatalr2   Z	exceptionr   r   r   Ú_reportErrors~   s    
zLibXml2Reader._reportErrorsc             C   sJ  d| _ �z4t|tƒr tj|ƒ}n&tj|ƒ}tj|jƒ ƒ}|j	|j
ƒ ƒ}|j| jd ƒ | jr–|jtjdƒ |jtjdƒ |jtjdƒ |jtj| jƒ n|jtjdƒ | jr¸tji i ƒ}n
tji ƒ}g }| jjƒ  �xJ|jƒ }|dkrú| jd k	rø| jdƒ nN|dk�r| jd k	�r| jdƒ P n*| jd k	�r6| jdƒ n| jjtdƒƒ P |j ƒ }|dk�r| j�ršt!|j"ƒ ƒt!|j#ƒ ƒf}	t!|j$ƒ ƒ}
i  |_%}i  |_&}g }x |j'ƒ �r>t!|j$ƒ ƒ}t!|j(ƒ ƒ}|j)dƒ�rt*|ƒdk�rê|dd … }nd }|j+|ƒ | jj,||ƒ | j-�s�q t!|j"ƒ ƒt!|j#ƒ ƒf}|||< |||< �q W |j.ƒ  | jj/|	|
|ƒ |j0ƒ �rŽ| jj1|	|
ƒ x&|D ]}| jj2|ƒ �qvW n
|j+|ƒ npt!|j$ƒ ƒ}	i  |_%}x,|j'ƒ �rÜt!|j$ƒ ƒ}t!|j(ƒ ƒ||< �q²W |j.ƒ  | jj3|	|ƒ |j0ƒ �r| jj4|	ƒ qÔ|dk�r€| j�rj| jj1t!|j"ƒ ƒt!|j#ƒ ƒft!|j$ƒ ƒƒ x4|j5ƒ D ]}| jj2|ƒ �qRW n| jj4t!|j$ƒ ƒƒ qÔ|dk�r | jj6t!|j(ƒ ƒƒ qÔ|d	k�rÀ| jj7t!|j(ƒ ƒƒ qÔ|d
k�rà| jj6t!|j(ƒ ƒƒ qÔ|dk�r,| j8d k	�r | j8j9ƒ  | jj6t!|j(ƒ ƒƒ | j8d k	�r| j8j:ƒ  qÔ|dk�r^| j8d k	�rT| j;t!|j$ƒ ƒƒ |j<ƒ  qÔ|dk�rˆ| j8d k	�r| j=t!|j$ƒ ƒƒ qÔ|dk�r²| jj>t!|j$ƒ ƒt!|j(ƒ ƒƒ qÔ|dk�rÞ| j8d k	�r| j8j?t!|j(ƒ ƒƒ qÔ|dk�rêqÔ|dk�röqÔ|dk�rqÔ|dk�rqÔtd| ƒ‚qÔW |dk�r2| jj@ƒ  |jAƒ  W d d| _ X d S )Nr   r   z"Read failed (no details available)Zxmlnsé   é   é   r   é   é   é   é   é   é   é
   é   é   zUnexpected node type %d)Br+   Ú
isinstanceÚStringTypesr4   ZnewTextReaderFilenamer   Zprepare_input_sourceZinputBufferZgetByteStreamZnewTextReaderr    ZSetErrorHandlerr3   r*   ZSetParserPropZPARSER_LOADDTDZPARSER_DEFAULTATTRSZPARSER_SUBST_ENTITIESZPARSER_VALIDATEr)   r'   r   ZAttributesNSImplZAttributesImplZ_cont_handlerZstartDocumentZReadr.   r8   r5   r6   ZSAXExceptionZNodeTyper	   ZNamespaceUriZ	LocalNameÚNameZ_attrsZ_qnamesZMoveToNextAttributeZValueÚ
startswithÚlenr/   ZstartPrefixMappingr(   ZMoveToElementZstartElementNSZIsEmptyElementZendElementNSZendPrefixMappingZstartElementZ
endElementÚpopZ
charactersZignorableWhitespacer,   Z
startCDATAZendCDATAZstartEntityZResolveEntityZ	endEntityZprocessingInstructionZcommentZendDocumentZClose)r   ÚsourceÚreaderÚinputZattributesNSImplZattributesImplÚprefixesÚrZnodeTypeZeltNameZeltQNameZattrsZqnamesZnewPrefixesZqnameÚvalueZ	newPrefixZattNameÚprefixr   r   r   Úparse�   sô    



































zLibXml2Reader.parsec             C   s   t dƒ‚d S )NzDTDHandler not supported)ÚSAXNotSupportedException)r   Zhandlerr   r   r   ÚsetDTDHandler8  s    zLibXml2Reader.setDTDHandlerc             C   s   t dƒ‚d S )NzEntityResolver not supported)rS   )r   Zresolverr   r   r   ÚsetEntityResolver<  s    zLibXml2Reader.setEntityResolverc             C   sT   |t kr| jS |tkr| jS |tkr*| jS |tkr6dS |tkrD| jS t	d| ƒ‚d S )Nr   zFeature '%s' not recognized)
r   r'   r   r(   r   r)   r   r   r*   ÚSAXNotRecognizedException)r   Únamer   r   r   Ú
getFeature@  s    zLibXml2Reader.getFeaturec             C   s€   | j rtd| ƒ‚|tkr"|| _nZ|tkr2|| _nJ|tkrB|| _n:|tkr`|dkr|td| ƒ‚n|t	krp|| _
ntd| ƒ‚d S )Nz#Cannot set feature %s while parsingr   zFeature '%s' not supportedzFeature '%s' not recognized)r+   rS   r   r'   r   r(   r   r)   r   r   r*   rV   )r   rW   Ústater   r   r   Ú
setFeatureO  s"    
zLibXml2Reader.setFeaturec             C   s,   |t kr| jS |tkr| jS td| ƒ‚d S )NzProperty '%s' not recognized)r   r,   r   r-   rV   )r   rW   r   r   r   ÚgetPropertyd  s    zLibXml2Reader.getPropertyc             C   s<   |t kr|| _n(|tkr,td| ƒ‚|| _ntd| ƒ‚d S )NzProperty '%s' not supportedzProperty '%s' not recognized)r   r,   r   rS   r-   rV   )r   rW   rP   r   r   r   ÚsetPropertym  s    zLibXml2Reader.setPropertyN)r!   r"   r#   r   r3   r8   rR   rT   rU   rX   rZ   r[   r\   r   r   r   r   r%   f   s    ,	r%   c               C   s   t ƒ S )N)r%   r   r   r   r   Úcreate_parsery  s    r]   )$r$   Ú
__author__Ú__version__ÚsysÚcodecsÚversion_infoÚunicode_escape_decodeÚstrZunicoderF   Úlookupr   r	   Zxml.sax._exceptionsZxml.saxr   r   Zxml.sax.handlerr   r   r   r   r   r   r   r   r   r   r4   ÚImportErrorZSAXReaderNotAvailableÚexc_infor   r&   r%   r]   r   r   r   r   Ú<module>#   s0   
0  