U e5d @sdZddlmZmZmZddlmZmZmZddlm Z ddl m Z m Z ddl mZejZejZejZejjZejjZejjZejjZeZeddedd edd edd edd edd eddeddeddd ZGdddeZddZddZGdddZeeefZ GdddeZ!GdddeZ"Gddde"Z#Gd d!d!e"Z$d"Z%d#e%Z&Gd$d%d%eZ'Gd&d'd'Z(Gd(d)d)e(eZ)Gd*d+d+e(e'Z*Gd,d-d-e+Z,Gd.d/d/eZ-d;d1d2Z.dd7d8Z1d9d:Z2dS)?zFacility to use the Expat parser to load a minidom instance from a string or file. This avoids all the overhead of SAX and pulldom to gain performance. ) xmlbuilderminidomNode)EMPTY_NAMESPACE EMPTY_PREFIXXMLNS_NAMESPACE)expat) _append_child_set_attribute_node) NodeFilterNZcdata enumerationentityentitiesidZidrefZidrefsZnmtokenZnmtokens) ZCDATAENUMZENTITYZENTITIESIDZIDREFZIDREFSZNMTOKENZNMTOKENSc@sZeZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) ElementInfo _attr_info_modeltagNameNcCs||_g|_||_dSN)rrr)selfrmodelr,/usr/lib64/python3.8/xml/dom/expatbuilder.py__init__?szElementInfo.__init__cCs|j|j|jfSrrrrrr __getstate__DszElementInfo.__getstate__cCs|\|_|_|_dSrr)rstaterrr __setstate__GszElementInfo.__setstate__cCsN|jD]@}|d|kr|d}|ddkr6tdSt|dSqtjS)Nr(r)r _typeinfo_mapr_no_type)ranameinfotrrrgetAttributeTypeJs    zElementInfo.getAttributeTypecCstjSr)rr%)rZ namespaceURIZ localNamerrrgetAttributeTypeNSTszElementInfo.getAttributeTypeNScCs,|jr$|jd}|tjjtjjfkSdSdSNrF)rrrZ XML_CTYPE_ANYZXML_CTYPE_MIXED)rtyperrrisElementContentWs  zElementInfo.isElementContentcCs |jr|jdtjjkSdSdSr+)rrrZXML_CTYPE_EMPTYrrrrisEmpty_szElementInfo.isEmptycCs,|jD] }|d|kr|ddkSqdS)Nr!r"rF)r)rr&r'rrrisIdes  zElementInfo.isIdcCs|||fSr)r/)rZeuriZenameZaurir&rrrisIdNSkszElementInfo.isIdNS)N) __name__ __module__ __qualname__ __slots__rrr r)r*r-r.r/r0rrrrr<s  rcCs |||Sr)_intern_setdefault)buildersrrr_internosr8cCs|d}|j}t|dkrR|\}}}|||}d||f}|||}|||}n4t|dkrz|\}}t}|||}}n td|||||||fS)N z%s:%sz4Unsupported syntax: spaces in URIs not supported: %r)splitr5lenr ValueError)r6namepartsinternuri localnameprefixqnamerrr_parse_ns_namers         rFc@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)< ExpatBuilderzPDocument builder that uses Expat to build a ParsedXML.DOM document instance.NcCsP|dkrt}||_|jjdk r2t|jj|_n d|_t|_d|_| dSr) rZOptions_optionsfilterFilterVisibilityController_filterr_finish_start_element_parserreset)roptionsrrrrs zExpatBuilder.__init__cCstS)zCreate a new parser object.)r ParserCreaterrrr createParserszExpatBuilder.createParsercCsF|js@||_|jjj|_d|j_d|j_d|j_||j|jS)z7Return the parser object, creating a new one if needed.T) rMrQrA setdefaultr5Z buffer_textZordered_attributesZspecified_attributesinstallrrrr getParsers   zExpatBuilder.getParsercCs,ttdd|_|j|_|jj|_d|_dS)z6Free all data structures used during DOM construction.NF)theDOMImplementationZcreateDocumentrdocumentcurNode _elem_info_cdatarrrrrNs zExpatBuilder.resetcCs|j|_|j|_|j|_|j|_|jj r0|j |_ |j |_ |jjrH|j|_|jjrj|j|_|j|_|j|_n|j|_|j|_|j|_|j|_|j|_ dS)z>Install the callbacks needed to build the DOM into the parser.N)!start_doctype_decl_handlerStartDoctypeDeclHandlerfirst_element_handlerStartElementHandlerend_element_handlerEndElementHandler pi_handlerProcessingInstructionHandlerrHrentity_decl_handlerEntityDeclHandlernotation_decl_handlerNotationDeclHandlercommentscomment_handlerCommentHandlerZcdata_sectionsstart_cdata_section_handlerStartCdataSectionHandlerend_cdata_section_handlerEndCdataSectionHandlercharacter_data_handler_cdataCharacterDataHandlercharacter_data_handlerexternal_entity_ref_handlerExternalEntityRefHandlerxml_decl_handlerZXmlDeclHandlerelement_decl_handlerZElementDeclHandlerattlist_decl_handlerZAttlistDeclHandlerrparserrrrrSs$ zExpatBuilder.installcCs|}d}zH|d}|sqF||d|r@|jjr@||d}q|ddWntk rhYnX|j}|d|_|S)zIParse a document from a file object, returning the document node.Ti@rFN) rTreadParserVdocumentElement _setup_subset ParseEscaperNrM)rfilervZ first_bufferbufferdocrrr parseFiles"    zExpatBuilder.parseFilecCsP|}z||d||Wntk r6YnX|j}|d|_|S)z|j|}t|j||jr:|j|tkr:|j|dSr)rVZ createCommentr rWrKrrr)rrrrrrrgIs  zExpatBuilder.comment_handlercCsd|_d|_dS)NTFrYrrrrrriOsz(ExpatBuilder.start_cdata_section_handlercCsd|_d|_dS)NFrrrrrrkSsz&ExpatBuilder.end_cdata_section_handlercCsdSNr!r)rcontextrrrrrrrpWsz(ExpatBuilder.external_entity_ref_handlercCs2|jdkr|jst|_|j|_|||dSr)rKrXrrstart_element_handlerrTr])rr? attributesrrrr\Zs z"ExpatBuilder.first_element_handlercCs|j|}t|j|||_|rptdt|dD]<}t||tdt }||d}||_ |j|_ t ||q2||jj k r||dS)Nrr;r!)rVZ createElementr rWranger=rAttrrrrrr rzrL)rr?rriarrrrr`s      z"ExpatBuilder.start_element_handlercCsj|jrf||jjkrdS|j|}|tkr4t|n|tkrFt|ndS|j|_ |j || dSr) rKrVrzstartContainerrRejecter FILTER_SKIPSkipper parentNoderWrunlink)rrZfiltrrrrLqs     z"ExpatBuilder._finish_start_elementcCs|j}|j|_||dSr)rWrr)rr?rWrrrr^sz ExpatBuilder.end_element_handlercCs\|j|j}|r||||jrX||jjkr4dS|j|tkrX|j || dSr) rXgetr_handle_white_text_nodesrKrVrzrrrWrr)rrWr'rrrrs   z ExpatBuilder._finish_end_elementcCsZ|jjs|sdSg}|jD]"}|jtkr|js||q|D]}| |qFdSr) rHZwhitespace_in_element_contentr-rrrrstriprr)rrr'Lrrrrrs  z%ExpatBuilder._handle_white_text_nodescCs0|j|}|dkr&t|||j|<n||_dSr)rXrrr)rr?rr'rrrrss z!ExpatBuilder.element_decl_handlerc CsF|j|}|dkr&t|}||j|<|jd|dd|d||gdSNr)rXrrrr)relemr?r,defaultZrequiredr'rrrrts  z!ExpatBuilder.attlist_decl_handlercCs2||j_||j_|dkr.|r&d|j_nd|j_dS)NrTF)rVversionencoding standalone)rrrrrrrrrs  zExpatBuilder.xml_decl_handler)N) r1r2r3__doc__rrQrTrNrSrrr{rZrr`rmrorbrdrgrirkrpr\rrLr^rrrsrtrrrrrrrGs:     rGc@seZdZdZdZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) rJzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.rIcCs ||_dSrr)rrIrrrrsz#FilterVisibilityController.__init__cCsT|j|j}|jj|@rL|j|}|tkr0t|tkrHtdt ||St SdS)Nz)startContainer() returned illegal value: ) _nodetype_maskrrI whatToShowrFILTER_INTERRUPTr|_ALLOWED_FILTER_RETURNSr>reprr)rrmaskvalrrrrs    z)FilterVisibilityController.startContainercCs|j|j}|jj|@r||j|}|tkr0t|tkr`|j}|j ddD]}| |qLt S|t krxt dt||StSdS)Nz%acceptNode() returned illegal value: )rrrIrrrr|rrrZ appendChildrrr>rr)rrrrparentrrrrrs      z%FilterVisibilityController.acceptNodeN)#r1r2r3rr4rrrrZ ELEMENT_NODEr Z SHOW_ELEMENTZATTRIBUTE_NODEZSHOW_ATTRIBUTErZ SHOW_TEXTrZSHOW_CDATA_SECTIONZENTITY_REFERENCE_NODEZSHOW_ENTITY_REFERENCEZ ENTITY_NODEZ SHOW_ENTITYZPROCESSING_INSTRUCTION_NODEZSHOW_PROCESSING_INSTRUCTIONZ COMMENT_NODEZ SHOW_COMMENT DOCUMENT_NODEZ SHOW_DOCUMENTZDOCUMENT_TYPE_NODEZSHOW_DOCUMENT_TYPEZDOCUMENT_FRAGMENT_NODEZSHOW_DOCUMENT_FRAGMENTZ NOTATION_NODEZ SHOW_NOTATIONrrrrrrJs< rJc@seZdZdZddZdS) FilterCrutch)_builder_level _old_start_old_endcCs6d|_||_|j}|j|_|j|_|j|_|j|_dSr) rrrMr]rr_rrr^)rr6rvrrrrszFilterCrutch.__init__N)r1r2r3r4rrrrrrsrc@s(eZdZdZddZddZddZdS) rrcCs,t|||j}dD]}t||dqdS)N)rarhrnrjrlrq)rrrMsetattr)rr6rvr?rrrrs zRejecter.__init__cGs|jd|_dSr)rrargsrrrrszRejecter.start_element_handlercGs@|jdkr0|jj}|j||j|_|j|_n |jd|_dSNrr!)rrrMrSrr]rr_)rrrvrrrr^ s    zRejecter.end_element_handlerN)r1r2r3r4rrr^rrrrrs rc@s eZdZdZddZddZdS)rrcGs.|jj}|j||jj|k r*|jd|_dSr)rrWrr)rrrrrrr-s  zSkipper.start_element_handlercGsD|jdkr*|j|jj_|j|jj_d|_n|jd|_|j|dSr)rrrrMr]rr_rrrrr^3s     zSkipper.end_element_handlerN)r1r2r3r4rr^rrrrr*srz8http://xml.python.org/entities/fragment-builder/internalz %%s ]> &fragment-builder-internal;c@sJeZdZdZdddZddZddZd d Zd d Zd dZ ddZ dS)FragmentBuilderzBuilder which constructs document fragments given XML source text and a context node. The context node is expected to provide information about the namespace declarations which are in scope at the start of the fragment. NcCs6|jtkr||_||_n|j|_||_t||dSr)rroriginalDocumentrrrGr)rrrOrrrr[s  zFragmentBuilder.__init__cCst|d|_dSr)rGrNfragmentrrrrrNds zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)rrxrr}rrrrhszFragmentBuilder.parseFilec Cs||_|}|jj}d}|rV|jp*|}|jrDd|j|jf}qZ|jrZd|j}nd}|}t |||f}z| |dWn| YnX|j }| |S)zMParse a document fragment from a string, returning the fragment node.rwzPUBLIC "%s" "%s"z SYSTEM "%s"r!) _sourcerTrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATEryrNr) rrrvrZidentrZnsattrsrVrrrrrms.  zFragmentBuilder.parseStringcCs |jjj}d}|rt|jjD]R}|j|}|r<|d}d||jf}|jrdd||j|j f}q d||j f}q t|j jD]}|j |}|r|d}d||jf}|jrd||j|j f}n&|j rd||j f}nd ||j j f}|j rd ||j f}|d }q|S) zRe-create the internal subset from the DocumentType node. This is only needed if we don't already have the internalSubset as a string. rwz z%sz%s SYSTEM "%s">z %s)rrrrrZlengthitemZnodeNamerrrZ firstChildrr)rrr7rZnotationr rrrrs:      z FragmentBuilder._getDeclarationscCsdS)NrwrrrrrrszFragmentBuilder._getNSattrscCs~|tkrh|j}|j}|j|}|j|_|j|_|j|_z| |jdW5||_||_d|_XdSt |||||SdS)Nr!r) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrVrWrMZExternalEntityParserCreaterZcreateDocumentFragmentrrryrGrp)rrrrrZ old_documentZ old_cur_nodervrrrrps(  z+FragmentBuilder.external_entity_ref_handler)N) r1r2r3rrrNrrrrrprrrrrRs $rc@s8eZdZdZddZddZddZdd Zd d Zd S) Namespacesz7Mix-in class for builders; adds support for namespaces.cCs g|_dSr)_ns_ordered_prefixesrrrr_initNamespacesszNamespaces._initNamespacescCstjdd}d|_|S)z'Create a new namespace-handling parser.r9)Znamespace_separatorT)rrPZnamespace_prefixesrurrrrQs zNamespaces.createParsercCs t|||jjr|j|_dS)z.Insert the namespace-handlers onto the parser.N)rGrSrHZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerrurrrrSs zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)rr)rrDrBrrrrsz'Namespaces.start_namespace_decl_handlercCsd|krt||\}}}}nt}|}d}t}t||||}|j|_t|j|||_|j r|j D]P\}}|rt t |d|t |d}nt dt dt}||_ |j|_t||qb|j dd=|r~||j} |j} tdt|dD]} || } || d} d| krDt|| \}}}}t ||||}|| |<|| ||f<n$t | t| t}|| | <|| t| f<|j|_| |_ ||_qdS)Nr9xmlns:xmlnsrr;r!)rFrrrZElementrVrr rWrrr8rrr Z_ensure_attributes_attrs_attrsNSrr=Z ownerElement)rr?rrBrCrDrErrrrrr&rrrrrs^      z Namespaces.start_element_handlerN) r1r2r3rrrQrSrrr^rrrrrs5rc@seZdZdZddZdS)ExpatBuilderNSz*Document builder that supports namespaces.cCst||dSr)rGrNrrrrrrN)s zExpatBuilderNS.resetN)r1r2r3rrNrrrrr&src@s eZdZdZddZddZdS)FragmentBuilderNSz*Fragment builder that supports namespaces.cCst||dSr)rrNrrrrrrN1s zFragmentBuilderNS.resetcCsd}|j}g}|r~t|drv|jD]N\}}||kr8q&|||rPd|}nd}|rhd|||f}q&d||f}q&|j}q|S)zNReturn string of namespace attributes from this element and ancestors.rw_ns_prefix_urirrz%s %s='%s'z %s='%s')rhasattrritemsrr)rattrsrrrDrBZdeclnamerrrr5s"   zFragmentBuilderNS._getNSattrsN)r1r2r3rrNrrrrrr.src@seZdZdZdS)r|zEException raised to short-circuit parsing in InternalSubsetExtractor.N)r1r2r3rrrrrr|Ssr|c@sLeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ dS)rzBXML processor which can rip out the internal document type subset.NcCs|jS)z'Return the internal subset as a string.)rrrrrr\sz!InternalSubsetExtractor.getSubsetcCs*zt||Wntk r$YnXdSr)rGrr|rrrrr`sz!InternalSubsetExtractor.parseFilecCs*zt||Wntk r$YnXdSr)rGrr|)rrrrrrfsz#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr)rZr[rr]rurrrrSlszInternalSubsetExtractor.installcCs0|r&|}g|_|jj|_|j|_ntdSr)rTrrZDefaultHandlerrrr|)rr?rrrrvrrrrZps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs,d|jdddd}||_tdS)Nrwz   )joinrreplacer|)rr7rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCs tdSr)r|)rr?rrrrrsz-InternalSubsetExtractor.start_element_handler) r1r2r3rrrrrrSrZrrrrrrrWs rTc CsL|r t}nt}t|tr>t|d}||}W5QRXn ||}|S)z{Parse a document, returning the resulting Document node. 'file' may be either a file name or an open file object. rb)rrG isinstancestropenr)r} namespacesr6fpresultrrrparses   rcCs|r t}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrGr)rrr6rrrrsrc CsP|rt|}nt|}t|trBt|d}||}W5QRXn ||}|S)zParse a fragment of a document, given the context from which it was originally extracted. context should be the parent of the node(s) which are in the fragment. 'file' may be either a file name or an open file object. r)rrrrrr)r}rrr6rrrrr parseFragments    rcCs |rt|}nt|}||S)zParse a fragment of a document from a string, given the context from which it was originally extracted. context should be the parent of the node(s) which are in the fragment. )rrr)rrrr6rrrparseFragmentStrings rcCs|jrt|St|SdS)z,Create a builder based on an Options object.N)rrrG)rOrrr makeBuildersr)T)T)T)T)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomr r Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrUZTypeInfor$objectrr8rFrGrrJrrrrrrrrr Exceptionr|rrrrrrrrrrsf            3C :   u_%,