a ^;@sdZdZdZddlZddlZejddkrXeedZeefZ e ddZ dd Z n eZ d d Z dd l Tdd lmZmZdd lmZmZmZmZmZmZmZmZmZmZz ddlZWn&eyededYn0Gdddej Z Gdddej!Z"ddZ#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.3Nutf8cCs|dur |St|dSdS)Nr)_decodersr1/usr/lib64/python3.9/site-packages/drv_libxml2.py_d1sr cCs|SNrrrrr r 9s)*) 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@s8eZdZdZddZddZddZdd Zd d Zd S) Locatorz4SAX Locator adapter for libxml2.xmlTextReaderLocatorcCs ||_dSr )_Locator__locator)selflocatorrrr __init__SszLocator.__init__cCsdS)z6Return the column number where the current event ends.rrrrr getColumnNumberVszLocator.getColumnNumbercCs |jS)z4Return the line number where the current event ends.)rZ LineNumberrrrr getLineNumberZszLocator.getLineNumbercCsdS)z3Return the public identifier for the current event.Nrrrrr getPublicId^szLocator.getPublicIdcCs |jS)z3Return the system identifier for the current event.)rZBaseURIrrrr getSystemIdbszLocator.getSystemIdN) __name__ __module__ __qualname____doc__rr r!r"r#rrrr rPs rc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) LibXml2ReadercCs@tj|d|_d|_d|_d|_d|_d|_d|_ d|_ dS)Nrr) r XMLReaderr_LibXml2Reader__ns_LibXml2Reader__nspfx_LibXml2Reader__validate_LibXml2Reader__extparams_LibXml2Reader__parsing_LibXml2Reader__lex_handler_LibXml2Reader__decl_handler_LibXml2Reader__errorsrrrr rhs zLibXml2Reader.__init__cCs0|jdurg|_|j|t|dt|fdSr )r1appendZSAXParseExceptionr)rargmsgseverityrrrr _errorHandlerws zLibXml2Reader._errorHandlercCsh|jD]V\}}|tjtjfvr,|j|q|rP||jddurP|j|q|j|qd|_dS)Nrr)r1libxml2Z PARSER_SEVERITY_VALIDITY_WARNINGZPARSER_SEVERITY_WARNING _err_handlerZwarning fatalErrorerror)rZfatalr5 exceptionrrr _reportErrors~szLibXml2Reader._reportErrorscCs>d|_z(t|tr t|}n&t|}t|}| | }| |j d|j r|tjd|tjd|tjd|tj|jn|tjd|jrtii}n ti}g}|j|}|dkr|jdur|dnR|dkr|jdur |dq n,|jdur4|dn|jtdq | }|dkr|jrt!|"t!|#f} t!|$} i|_%} i|_&} g} |'rt!|$t!|(q|dkr|j8dur |j8?t!|(q|dkrq|dkrq|dkrq|dkrqtd|q|dkr |j@|AWd|_nd|_0dS)Nrrz"Read failed (no details available)Zxmlnsr   zUnexpected node type %d)Br. isinstance StringTypesr7ZnewTextReaderFilenamerZprepare_input_sourceZ inputBufferZ getByteStreamZ newTextReaderr#ZSetErrorHandlerr6r-Z SetParserPropZPARSER_LOADDTDZPARSER_DEFAULTATTRSZPARSER_SUBST_ENTITIESZPARSER_VALIDATEr,r*r ZAttributesNSImplZAttributesImplZ _cont_handlerZ startDocumentZReadr1r<r8r9Z SAXExceptionZNodeTyper Z NamespaceUriZ LocalNameNameZ_attrsZ_qnamesZMoveToNextAttributeZValue startswithlenr2ZstartPrefixMappingr+Z MoveToElementZstartElementNSZIsEmptyElementZ endElementNSZendPrefixMappingZ startElementZ endElementpopZ charactersZignorableWhitespacer/Z startCDATAZendCDATAZ startEntityZ ResolveEntityZ endEntityZprocessingInstructionZcommentZ endDocumentZClose)rsourcereaderinputZattributesNSImplZattributesImplprefixesrZnodeTypeZeltNameZeltQNameattrsZqnamesZ newPrefixesZqnamevalueZ newPrefixZattNameprefixrrr parses                                                           zLibXml2Reader.parsecCs tddS)NzDTDHandler not supportedSAXNotSupportedException)rZhandlerrrr setDTDHandler8szLibXml2Reader.setDTDHandlercCs tddS)NzEntityResolver not supportedrX)rZresolverrrr setEntityResolver<szLibXml2Reader.setEntityResolvercCsT|tkr|jS|tkr|jS|tkr*|jS|tkr6dS|tkrD|jSt d|dS)NrFeature '%s' not recognized) rr*rr+rr,rrr-SAXNotRecognizedExceptionrnamerrr getFeature@szLibXml2Reader.getFeaturecCs|jrtd||tkr"||_nZ|tkr2||_nJ|tkrB||_n:|tkr`|dkr|td|n|t krp||_ n t d|dS)Nz#Cannot set feature %s while parsingrzFeature '%s' not supportedr\) r.rYrr*rr+rr,rrr-r])rr_staterrr setFeatureOs(zLibXml2Reader.setFeaturecCs,|tkr|jS|tkr|jStd|dS)NProperty '%s' not recognized)rr/rr0r]r^rrr getPropertydszLibXml2Reader.getPropertycCs<|tkr||_n(|tkr,td|||_n td|dS)NzProperty '%s' not supportedrc)rr/rrYr0r])rr_rUrrr setPropertymszLibXml2Reader.setPropertyN) r$r%r&rr6r<rWrZr[r`rbrdrerrrr r(fs, r(cCstSr )r(rrrr create_parserysrf)$r' __author__ __version__syscodecs version_infounicode_escape_decodestrZunicoderJlookuprr Zxml.sax._exceptionsZxml.saxr rZxml.sax.handlerrrrrrrrrrrr7 ImportErrorZSAXReaderNotAvailableexc_inforr)r(rfrrrr s2# 0