a
^; @ s d Z dZdZddlZddlZejd dk rXeed ZeefZ e
dd Zdd ZneZ d
d Zddl
T ddlmZmZ dd
lmZmZmZmZmZmZmZmZmZmZ zddlZW n& ey ede d Y n0 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 u r| S t | d S d S )Nr )_decoders r 1/usr/lib64/python3.9/site-packages/drv_libxml2.py_d1 s r
c C s | S Nr r r r r r
9 s )*) xmlreadersaxutils)
feature_namespacesfeature_namespace_prefixesfeature_string_interningfeature_validationfeature_external_gesfeature_external_pesproperty_lexical_handlerproperty_declaration_handlerproperty_dom_nodeproperty_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 r )_Locator__locator)selflocatorr 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 getColumnNumberV s zLocator.getColumnNumberc C s
| j S )z4Return the line number where the current event ends.)r Z
LineNumberr 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 S )z3Return the system identifier for the current event.)r ZBaseURIr 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 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__errorsr r r r r h s zLibXml2Reader.__init__c C s0 | j d u rg | _ | j |t|d t|f d S r )r1 appendZSAXParseExceptionr )r argmsgseverityr r r r
_errorHandlerw s
zLibXml2Reader._errorHandlerc C sh | j D ]V\}}|tjtjfv r,| j| q|rP|| j d d u rP| j| q| j| qd | _ d S )Nr r )r1 libxml2Z PARSER_SEVERITY_VALIDITY_WARNINGZPARSER_SEVERITY_WARNING_err_handlerZwarning
fatalErrorerror)r Zfatalr5 exceptionr r r
_reportErrors~ s zLibXml2Reader._reportErrorsc C s> d| _ z(t|tr t|}n&t|}t| }| |
}|| jd | j
r|tjd |tjd |tjd |tj| j n|tjd | jrti i }n
ti }g }| j | }|dkr| jd ur| d nR|dkr| jd ur| d qn,| jd ur4| d n| jtd q| }|dkr | jrt!|" t!|# f} t!|$ }
i |_%}i |_&}g }
|' r