3
œVW”; ã @ s d Z dZdZddlZddlZejd dk rXejeƒd ZeefZ ej
dƒd Zdd „ ZneZ d
d „ Zddl
T ddlmZmZ dd
lmZmZmZmZmZmZmZmZmZmZ yddlZW n( ek
rà edejƒ d ƒ‚Y nX G dd„ dej ƒZ G dd„ dej!ƒZ"dd„ Z#dS )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 z0.3é Né Úutf8é c C s | d kr| 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 Zd ZdZdd„ Zdd„ Zdd„ Zdd „ Zd
d„ ZdS )
ÚLocatorz4SAX Locator adapter for libxml2.xmlTextReaderLocatorc C s
|| _ d S )N)Ú_Locator__locator)ÚselfÚlocatorr r r Ú__init__S s zLocator.__init__c C s dS )z6Return the column number where the current event ends.r éÿÿÿÿr )r r r r ÚgetColumnNumberV s zLocator.getColumnNumberc C s
| j jƒ S )z4Return the line number where the current event ends.)r Z
LineNumber)r r r r Ú
getLineNumberZ s zLocator.getLineNumberc C s dS )z3Return the public identifier for the current event.Nr )r r r r ÚgetPublicId^ s zLocator.getPublicIdc C s
| j jƒ S )z3Return the system identifier for the current event.)r ZBaseURI)r r r r ÚgetSystemIdb s zLocator.getSystemIdN) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r r r r r r r r r r P s r c @ s\ e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd d
„ Zdd„ Zd
d„ Z dd„ Z
dd„ Zdd„ ZdS )Ú
LibXml2Readerc C s@ t jj| ƒ 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 zLibXml2Reader.__init__c C s0 | j d krg | _ | j j|t|d t|ƒƒfƒ d S )N)r. ÚappendZSAXParseExceptionr )r ÚargÚmsgÚseverityr r r r Ú
_errorHandlerw s
zLibXml2Reader._errorHandlerc C sl x`| j D ]V\}}|tjtjfkr.| jj|ƒ q|rR|| j d d krR| jj|ƒ q| jj|ƒ qW d | _ d S )Nr r )r. Úlibxml2Z PARSER_SEVERITY_VALIDITY_WARNINGZPARSER_SEVERITY_WARNINGÚ_err_handlerZwarningÚ
fatalErrorÚerror)r Zfatalr2 Z exceptionr r r Ú
_reportErrors~ s
zLibXml2Reader._reportErrorsc C sJ d| _ �z4t|tƒr tj|ƒ}n&tj|ƒ}tj|jƒ ƒ}|j |j
ƒ ƒ}|j| jd ƒ | j
r–|jtjdƒ |jtjdƒ |jtjdƒ |jtj| jƒ n|jtjdƒ | jr¸tji i ƒ}n
tji ƒ}g }| jjƒ �xJ|jƒ }|dkrú| jd k rø| jdƒ nN|dk�r| jd k �r| jdƒ P n*| jd k �r6| jdƒ n| jjtdƒƒ P |j ƒ }|dk�r| j�ršt!|j"ƒ ƒt!|j#ƒ ƒf} t!|j$ƒ ƒ}
i |_%}i |_&}g }
x |j'ƒ �r>t!|j$ƒ ƒ}t!|j(ƒ ƒ}|j)dƒ�rt*|ƒdk�rê|dd … }nd }|
j+|ƒ | jj,||ƒ | j-�s�q t!|j"ƒ ƒt!|j#ƒ ƒf}|||<