3 \/@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddZ ifdd Z ifd d Z ifd d Z ddZGdddejZGddde jZdddZdS)znA library of useful helper classes to the SAX classes, for the convenience of application and driver writers. N)handler) xmlreadercCs&x |jD]\}}|j||}q W|S)z2Replace substrings of a string using a dictionary.)itemsreplace)sdkeyvaluer /usr/lib64/python3.6/saxutils.py__dict_replace sr cCs6|jdd}|jdd}|jdd}|r2t||}|S)zEscape &, <, and > in a string of data. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. &z&>z> in a string of data, then quote it for use as an attribute value. The " character will be escaped as well, if necessary. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. z z z )   "'z"%s"z"z'%s')copyupdaterr)rrr r r quoteattr0s   rcsdkrddl}|jSttjr&Sttjtjfr<SttjrlGfddd}|}dd|_ nDtj }dd|_ j |_ yj |_ j|_Wntk rYnXtj||ddd d S) NrcseZdZjZfddZdS)z _gettextwriter.._wrappercs t|S)N)getattr)selfname)outr r __getattr__[sz,_gettextwriter.._wrapper.__getattr__N)__name__ __module__ __qualname__ __class__r!r )r r r _wrapperYsr&cSsdS)Nr r r r r ^sz _gettextwriter..cSsdS)NTr r r r r r'csxmlcharrefreplacerT)encodingerrorsnewline write_through)sysstdout isinstanceio TextIOBasecodecs StreamWriterStreamReaderWriter RawIOBasecloseBufferedIOBasewritablewriteseekabletellAttributeError TextIOWrapper)r r)r-r&bufferr )r r _gettextwriterHs.     r?c@seZdZd ddZddZd!dd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZddZddZdS)" XMLGeneratorN iso-8859-1FcCsVtjj|t||}|j|_|j|_ig|_|jd|_ g|_ ||_ ||_ d|_ dS)NrF)rContentHandler__init__r?r9_writeflush_flush _ns_contexts_current_context_undeclared_ns_maps _encoding_short_empty_elements_pending_start_element)rr r)Zshort_empty_elementsr r r rDss   zXMLGenerator.__init__cCsJ|drBd|dkr d|dS|j|d}|rB|d|dS|dS)z7Builds a qualified name from a (ns_url, localname) pairrz$http://www.w3.org/XML/1998/namespacezxml:r:)rI)rrprefixr r r _qnames  zXMLGenerator._qnamecCs|jr|jdd|_dS)NrF)rMrE)r endElementr r r _finish_pending_start_elements z*XMLGenerator._finish_pending_start_elementcCs|jd|jdS)Nz$ )rErK)rr r r startDocumentszXMLGenerator.startDocumentcCs |jdS)N)rG)rr r r endDocumentszXMLGenerator.endDocumentcCs0|jj|jj||j|<|jj||fdS)N)rHappendrIrrJ)rrOurir r r startPrefixMappings zXMLGenerator.startPrefixMappingcCs|jd|_|jd=dS)NrrBrB)rHrI)rrOr r r endPrefixMappings zXMLGenerator.endPrefixMappingcCs^|j|jd|x*|jD]\}}|jd|t|fq W|jrPd|_n |jddS)Nrz %s=%sTr)rRrErrrLrM)rrattrsr r r r startElementszXMLGenerator.startElementcCs*|jr|jdd|_n|jd|dS)Nz/>Fz)rMrE)rrr r r rQs zXMLGenerator.endElementcCs|j|jd|j|x8|jD].\}}|rD|jd||fq$|jd|q$Wg|_x0|jD]$\}}|jd|j|t|fqfW|jrd|_n |jddS)Nrz xmlns:%s="%s"z xmlns="%s"z %s=%sTr)rRrErPrJrrrLrM)rrqnamerYrOrVr r r r startElementNSs zXMLGenerator.startElementNScCs0|jr|jdd|_n|jd|j|dS)Nz/>Fz)rMrErP)rrr[r r r endElementNSs zXMLGenerator.endElementNScCs4|r0|jt|ts"t||j}|jt|dS)N)rRr/strrKrEr)rcontentr r r characterss   zXMLGenerator.characterscCs0|r,|jt|ts"t||j}|j|dS)N)rRr/r^rKrE)rr_r r r ignorableWhitespaces   z XMLGenerator.ignorableWhitespacecCs|j|jd||fdS)Nz )rRrE)rtargetrr r r processingInstructionsz"XMLGenerator.processingInstruction)NrAF)F)r"r#r$rDrPrRrSrTrWrXrZrQr\r]r`rarcr r r r r@qs   r@c@seZdZdZd;ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:ZdS)< XMLFilterBaseaYThis class is designed to sit between an XMLReader and the client application's event handlers. By default, it does nothing but pass requests up to the reader and events on to the handlers unmodified, but subclasses can override specific methods to modify the event stream or the configuration requests as they pass through.NcCstjj|||_dS)N)r XMLReaderrD_parent)rparentr r r rDs zXMLFilterBase.__init__cCs|jj|dS)N) _err_handlererror)r exceptionr r r riszXMLFilterBase.errorcCs|jj|dS)N)rh fatalError)rrjr r r rkszXMLFilterBase.fatalErrorcCs|jj|dS)N)rhwarning)rrjr r r rlszXMLFilterBase.warningcCs|jj|dS)N) _cont_handlersetDocumentLocator)rZlocatorr r r rnsz XMLFilterBase.setDocumentLocatorcCs|jjdS)N)rmrS)rr r r rSszXMLFilterBase.startDocumentcCs|jjdS)N)rmrT)rr r r rTszXMLFilterBase.endDocumentcCs|jj||dS)N)rmrW)rrOrVr r r rWsz XMLFilterBase.startPrefixMappingcCs|jj|dS)N)rmrX)rrOr r r rX szXMLFilterBase.endPrefixMappingcCs|jj||dS)N)rmrZ)rrrYr r r rZ szXMLFilterBase.startElementcCs|jj|dS)N)rmrQ)rrr r r rQszXMLFilterBase.endElementcCs|jj|||dS)N)rmr\)rrr[rYr r r r\szXMLFilterBase.startElementNScCs|jj||dS)N)rmr])rrr[r r r r]szXMLFilterBase.endElementNScCs|jj|dS)N)rmr`)rr_r r r r`szXMLFilterBase.characterscCs|jj|dS)N)rmra)rcharsr r r rasz!XMLFilterBase.ignorableWhitespacecCs|jj||dS)N)rmrc)rrbrr r r rcsz#XMLFilterBase.processingInstructioncCs|jj|dS)N)rm skippedEntity)rrr r r rp!szXMLFilterBase.skippedEntitycCs|jj|||dS)N) _dtd_handler notationDecl)rrpublicIdsystemIdr r r rr&szXMLFilterBase.notationDeclcCs|jj||||dS)N)rqunparsedEntityDecl)rrrsrtZndatar r r ru)sz XMLFilterBase.unparsedEntityDeclcCs|jj||S)N)Z _ent_handler resolveEntity)rrsrtr r r rv.szXMLFilterBase.resolveEntitycCs@|jj||jj||jj||jj||jj|dS)N)rfZsetContentHandlerZsetErrorHandlerZsetEntityResolverZ setDTDHandlerparse)rsourcer r r rw3s     zXMLFilterBase.parsecCs|jj|dS)N)rf setLocale)rZlocaler r r ry:szXMLFilterBase.setLocalecCs |jj|S)N)rf getFeature)rrr r r rz=szXMLFilterBase.getFeaturecCs|jj||dS)N)rf setFeature)rrstater r r r{@szXMLFilterBase.setFeaturecCs |jj|S)N)rf getProperty)rrr r r r}CszXMLFilterBase.getPropertycCs|jj||dS)N)rf setProperty)rrr r r r r~FszXMLFilterBase.setPropertycCs|jS)N)rf)rr r r getParentKszXMLFilterBase.getParentcCs ||_dS)N)rf)rrgr r r setParentNszXMLFilterBase.setParent)N) r"r#r$__doc__rDrirkrlrnrSrTrWrXrZrQr\r]r`rarcrprrrurvrwryrzr{r}r~rrr r r r rds: rdcCs t|trtj|}n^t|drt|}tj}t|jdtrH|j|n |j|t|drtt|jtrt|j |j|j dko|j dkr|j }t jjt jj|}t jj||}t jj|r|j |t|d}n$|j tjj||tjj|j }|j||S)zThis function takes an InputSource and an optional base URL and returns a fully resolved InputSource object ready for reading.readrrNrb)r/r^rZ InputSourcehasattrrZsetCharacterStreamZ setByteStreamrZ setSystemIdZgetCharacterStreamZ getByteStreamZ getSystemIdospathdirnamenormpathjoinisfileopenurllibrwZurljoinZrequestZurlopen)rxbasefZsysidZbaseheadZ sysidfilenamer r r prepare_input_sourceSs*          r)r)rrZ urllib.parserZurllib.requestr0r2rrrr rrrr?rCr@rerdrr r r r s     )so