3 VW; @sdZdZdZddlZddlZejddkrXejedZeefZ ej ddZ dd Z n eZ d d Z dd l Tdd lmZmZdd lmZmZmZmZmZmZmZmZmZmZy ddlZWn(ek redejdYnXGdddej 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|dkr |St|dSdS)Nr)_decoder)sr#/usr/lib64/python3.6/drv_libxml2.py_d1sr cCs|S)Nr)rrrrr 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 ||_dS)N)_Locator__locator)selflocatorrrr__init__SszLocator.__init__cCsdS)z6Return the column number where the current event ends.rr)rrrrgetColumnNumberVszLocator.getColumnNumbercCs |jjS)z4Return the line number where the current event ends.)rZ LineNumber)rrrr getLineNumberZszLocator.getLineNumbercCsdS)z3Return the public identifier for the current event.Nr)rrrr getPublicId^szLocator.getPublicIdcCs |jjS)z3Return the system identifier for the current event.)rZBaseURI)rrrr getSystemIdbszLocator.getSystemIdN) __name__ __module__ __qualname____doc__rrrrr rrrrrPs rc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) LibXml2ReadercCs@tjj|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__errors)rrrrrhs zLibXml2Reader.__init__cCs0|jdkrg|_|jj|t|dt|fdS)N)r.appendZSAXParseExceptionr)rargmsgseverityrrrr _errorHandlerws  zLibXml2Reader._errorHandlercCslx`|jD]V\}}|tjtjfkr.|jj|q|rR||jddkrR|jj|q|jj|qWd|_dS)Nrr)r.libxml2Z PARSER_SEVERITY_VALIDITY_WARNINGZPARSER_SEVERITY_WARNING _err_handlerZwarning fatalErrorerror)rZfatalr2Z exceptionrrr _reportErrors~s zLibXml2Reader._reportErrorsc CsJd|_z4t|tr tj|}n&tj|}tj|j}|j |j }|j |j d|j r|jtjd|jtjd|jtjd|jtj|jn|jtjd|jrtjii}n tji}g}|jjxJ|j}|dkr|jdk r|jdnN|dkr|jdk r|jdPn*|jdk r6|jdn|jjtdP|j }|dkr |jrt!|j"t!|j#f} t!|j$} i|_%} i|_&} g} x|j'r>t!|j$}t!|j(}|j)drt*|dkr|dd}nd}| j+||jj,|||j-sqt!|j"t!|j#f}|| |<|| |<qW|j.|jj/| | ||j0r|jj1| | x&| D]}|jj2|qvWn |j+| npt!|j$} i|_%} x,|j'rt!|j$}t!|j(| |<qW|j.|jj3| ||j0r|jj4| q|dkr|jrj|jj1t!|j"t!|j#ft!|j$x4|j5D]}|jj2|qRWn|jj4t!|j$q|dkr|jj6t!|j(q|d kr|jj7t!|j(q|d kr|jj6t!|j(q|d kr,|j8dk r|j8j9|jj6t!|j(|j8dk r|j8j:q|dkr^|j8dk rT|j;t!|j$|j<q|d kr|j8dk r|j=t!|j$q|d kr|jj>t!|j$t!|j(q|dkr|j8dk r|j8j?t!|j(q|dkrq|dkrq|dkrq|dkrqtd|qW|dkr2|jj@|jAWdd|_XdS)Nrrz"Read failed (no details available)Zxmlnsr   zUnexpected node type %d)Br+ isinstance StringTypesr4ZnewTextReaderFilenamer Zprepare_input_sourceZ inputBufferZ getByteStreamZ newTextReaderr ZSetErrorHandlerr3r*Z SetParserPropZPARSER_LOADDTDZPARSER_DEFAULTATTRSZPARSER_SUBST_ENTITIESZPARSER_VALIDATEr)r'r ZAttributesNSImplZAttributesImplZ _cont_handlerZ startDocumentZReadr.r8r5r6Z SAXExceptionZNodeTyper Z NamespaceUriZ LocalNameNameZ_attrsZ_qnamesZMoveToNextAttributeZValue startswithlenr/ZstartPrefixMappingr(Z MoveToElementZstartElementNSZIsEmptyElementZ endElementNSZendPrefixMappingZ startElementZ endElementpopZ charactersZignorableWhitespacer,Z startCDATAZendCDATAZ startEntityZ ResolveEntityZ endEntityZprocessingInstructionZcommentZ endDocumentZClose)rsourcereaderinputZattributesNSImplZattributesImplprefixesrZnodeTypeZeltNameZeltQNameZattrsZqnamesZ newPrefixesZqnamevalueZ newPrefixZattNameprefixrrrparses                                                         zLibXml2Reader.parsecCs tddS)NzDTDHandler not supported)SAXNotSupportedException)rZhandlerrrr setDTDHandler8szLibXml2Reader.setDTDHandlercCs tddS)NzEntityResolver not supported)rS)rZresolverrrrsetEntityResolver<szLibXml2Reader.setEntityResolvercCsT|tkr|jS|tkr|jS|tkr*|jS|tkr6dS|tkrD|jSt d|dS)NrzFeature '%s' not recognized) r r'rr(rr)rrr*SAXNotRecognizedException)rnamerrr 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 supportedzFeature '%s' not recognized) r+rSr r'rr(rr)rrr*rV)rrWstaterrr setFeatureOs" zLibXml2Reader.setFeaturecCs,|tkr|jS|tkr|jStd|dS)NzProperty '%s' not recognized)rr,rr-rV)rrWrrr getPropertyds zLibXml2Reader.getPropertycCs<|tkr||_n(|tkr,td|||_n td|dS)NzProperty '%s' not supportedzProperty '%s' not recognized)rr,rrSr-rV)rrWrPrrr setPropertymszLibXml2Reader.setPropertyN) r!r"r#rr3r8rRrTrUrXrZr[r\rrrrr%fs, r%cCstS)N)r%rrrr create_parserysr])$r$ __author__ __version__syscodecs version_infounicode_escape_decodestrZunicoderFlookuprr Zxml.sax._exceptionsZxml.saxr r Zxml.sax.handlerr rrrrrrrrrr4 ImportErrorZSAXReaderNotAvailableexc_inforr&r%r]rrrr#s0 0