o  h<@s dZdZdZddlZddlZejddkr,eedZeefZ e ddZ dd Z neZ d d Z dd l Tdd lmZmZdd lmZmZmZmZmZmZmZmZmZmZzddlZWneymededwGdddej 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|dS)Nr)_decodersr-/usr/lib/python3/dist-packages/drv_libxml2.py_d1s r 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__Ss zLocator.__init__cCdS)z6Return the column number where the current event ends.rrrrr getColumnNumberVzLocator.getColumnNumbercC |jS)z4Return the line number where the current event ends.)r LineNumberr rrr getLineNumberZ zLocator.getLineNumbercCr)z3Return the public identifier for the current event.Nrr rrr getPublicId^r"zLocator.getPublicIdcCr#)z3Return the system identifier for the current event.)rBaseURIr rrr getSystemIdbr&zLocator.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__errorsr rrr rhs  zLibXml2Reader.__init__cCs0|jdurg|_|j|t|dt|fdSr )r7appendSAXParseExceptionr)rargmsgseverityrrrr _errorHandlerws  zLibXml2Reader._errorHandlercCsh|jD]+\}}|tjtjfvr|j|q|r(||jddur(|j|q|j|qd|_dS)Nrr)r7libxml2 PARSER_SEVERITY_VALIDITY_WARNINGPARSER_SEVERITY_WARNING _err_handlerwarning fatalErrorerror)rfatalr< exceptionrrr _reportErrors~s zLibXml2Reader._reportErrorscCsVd|_zt|trt|}nt|}|}|dur!|}t |}| | }| |j d|jrU|tjd|tjd|tjd|tj|jn|tjd|jrftii}nti}g}|j |}|dkr|jdur|dn%|dkr|jdur|dn|jdur|dn|jt dn|!} | dkr|jrRt"|#t"|$f} t"|%} i|_&} i|_'} g}|(r%t"|%}t"|)}|*dr t+|dkr|dd}nd}|,||j-|||j.s qt"|#t"|$f}|| |<|| |<|(s|/|j0| | ||1rK|j2| | |D] }|j3|q?nG|,|n@t"|%} i|_&} |(rut"|%}t"|)| |<|(sb|/|j4| ||1r|j5| n| dkr|jr|j2t"|#t"|$ft"|%|6D] }|j3|qn|j5t"|%n| dkr|j7t"|)n| d kr|j8t"|)n| d kr|j7t"|)n| d kr|j9dur|j9:|j7t"|)|j9dur|j9;nw| dkr4|j9dur/|t"|%nI| d kr^|j?t"|%t"|)n4| dkrt|j9durs|j9@t"|)n| dkrzn| dkrn| dkrn | dkrnt d| qs|dkr|jA|BWd|_dSd|_w)Nrrz"Read failed (no details available)xmlnsr   zUnexpected node type %d)Cr4 isinstance StringTypesr>newTextReaderFilenamerprepare_input_sourcegetCharacterStream getByteStream inputBuffer newTextReaderr)SetErrorHandlerr=r3 SetParserPropPARSER_LOADDTDPARSER_DEFAULTATTRSPARSER_SUBST_ENTITIESPARSER_VALIDATEr2r0r AttributesNSImplAttributesImpl _cont_handler startDocumentReadr7rGrArC SAXExceptionNodeTyper NamespaceUri LocalNameName_attrs_qnamesMoveToNextAttributeValue startswithlenr8startPrefixMappingr1 MoveToElementstartElementNSIsEmptyElement endElementNSendPrefixMapping startElement endElementpop charactersignorableWhitespacer5 startCDATAendCDATA startEntity ResolveEntity endEntityprocessingInstructioncomment endDocumentClose)rsourcereaderstreaminputattributesNSImplattributesImplprefixesrnodeTypeeltNameeltQNameattrsqnames newPrefixesqnamevalue newPrefixattNameprefixrrr parses"                                                            zLibXml2Reader.parsecCtd)NzDTDHandler not supportedSAXNotSupportedException)rhandlerrrr setDTDHandler;zLibXml2Reader.setDTDHandlercCr)NzEntityResolver not supportedr)rresolverrrr setEntityResolver?rzLibXml2Reader.setEntityResolvercCsP|tkr|jS|tkr|jS|tkr|jS|tkrdS|tkr"|jSt d|)NrFeature '%s' not recognized) rr0rr1rr2rrr3SAXNotRecognizedExceptionrnamerrr getFeatureCszLibXml2Reader.getFeaturecCs|jr td||tkr||_dS|tkr||_dS|tkr$||_dS|tkr4|dkr2td|dS|t kr=||_ dSt d|)Nz#Cannot set feature %s while parsingrzFeature '%s' not supportedr) r4rrr0rr1rr2rrr3r)rrstaterrr setFeatureRs*    zLibXml2Reader.setFeaturecCs(|tkr|jS|tkr|jStd|)NProperty '%s' not recognized)rr5rr6rrrrr getPropertygszLibXml2Reader.getPropertycCs2|tkr ||_dS|tkrtd|td|)NzProperty '%s' not supportedr)rr5rrr6r)rrrrrr setPropertyps zLibXml2Reader.setPropertyN) r*r+r,rr=rGrrrrrrrrrrr r.fs/ r.cCstSr )r.rrrr create_parser|sr)$r- __author__ __version__syscodecs version_infounicode_escape_decodestrunicoderVlookuprr xml.sax._exceptionsxml.saxr rxml.sax.handlerrrrrrrrrrrr> ImportErrorSAXReaderNotAvailableexc_inforr/r.rrrrr s6# 0