a Z^c @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) NodeFilterNZcdataZ enumerationentityentitiesidZidrefZidrefsZnmtokenZnmtokens) ZCDATAENUMZENTITYZENTITIESIDZIDREFZIDREFSZNMTOKENZNMTOKENSc@sZeZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS) ElementInfo _attr_info_modeltagNameNcCs||_g|_||_dSN)rrr)selfrmodelr,/usr/lib64/python3.9/xml/dom/expatbuilder.py__init__?szElementInfo.__init__cCs|j|j|jfSrrrrrr __getstate__DzElementInfo.__getstate__cCs|\|_|_|_dSrr)rZstaterrr __setstate__GrzElementInfo.__setstate__cCsN|jD]@}|d|kr|d}|ddkr6tdSt|dSqtjS)Nrz(r)r _typeinfo_mapr_no_type)ranameinfoZtrrrgetAttributeTypeJs    zElementInfo.getAttributeTypecCstjSr)rr")r namespaceURI localNamerrrgetAttributeTypeNSTrzElementInfo.getAttributeTypeNScCs,|jr$|jd}|tjjtjjfvSdSdSNrF)rrrZ XML_CTYPE_ANYZXML_CTYPE_MIXED)rtyperrrisElementContentWs  zElementInfo.isElementContentcCs |jr|jdtjjkSdSdSr))rrrZXML_CTYPE_EMPTYrrrrisEmpty_szElementInfo.isEmptycCs,|jD] }|d|kr|ddkSqdS)Nrr rF)r)rr#r$rrrisIdes  zElementInfo.isIdcCs|||fSr)r-)rZeuriZenameZaurir#rrrisIdNSkzElementInfo.isIdNSr) __name__ __module__ __qualname__ __slots__rrrr%r(r+r,r-r.rrrrr<s  rcCs |||Sr)_intern_setdefault)buildersrrr_internorr7cCsd|vs J|d}|j}t|dkr^|\}}}|||}d||f}|||}|||}n4t|dkr|\}}t}|||}}n td|||||||fS)N iz%s:%sz4Unsupported syntax: spaces in URIs not supported: %r)Zsplitr4lenr ValueError)r5nameZpartsinternuri localnameprefixqnamerrr_parse_ns_namers          rBc@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|durt}||_|jjdur2t|jj|_n d|_t|_d|_| dSr) rZOptions_optionsfilterFilterVisibilityController_filterr _finish_start_element_parserreset)roptionsrrrrs zExpatBuilder.__init__cCstS)zCreate a new parser object.)r ParserCreaterrrr createParserr/zExpatBuilder.createParsercCsF|js@||_|jjj|_d|j_d|j_d|j_||j|jS)z7Return the parser object, creating a new one if needed.T) rIrMr=Z setdefaultr4Z buffer_textZordered_attributesZspecified_attributesinstallrrrr getParsers   zExpatBuilder.getParsercCs,ttdd|_|j|_|jj|_d|_dS)z6Free all data structures used during DOM construction.NF)theDOMImplementationZcreateDocumentrdocumentcurNode _elem_info_cdatarrrrrJs  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_handlerProcessingInstructionHandlerrDr entity_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_handlerZAttlistDeclHandlerrparserrrrrNs$ zExpatBuilder.installcCs|}d}zH|d}|sqF||d|r@|jjr@||d}q|ddWntyfYn0|j}|d|_|S)zIParse a document from a file object, returning the document node.Ti@FsN) rOreadParserQdocumentElement _setup_subset ParseEscaperJrI)rfilerqZ first_bufferbufferdocrrr parseFiles"     zExpatBuilder.parseFilecCsN|}z||d||Wnty4Yn0|j}|d|_|S)z|j|}t|j||jr:|j|tkr:|j|dSr)rQZ createCommentrrRrGrrr)rrrrrrrbIs  zExpatBuilder.comment_handlercCsd|_d|_dS)NTFrTrrrrrrdOz(ExpatBuilder.start_cdata_section_handlercCsd|_d|_dS)NFrrrrrrfSrz&ExpatBuilder.end_cdata_section_handlercCdSNrr)rcontextrrrrrrrkWrz(ExpatBuilder.external_entity_ref_handlercCs2|jdur|jst|_|j|_|||dSr)rGrSr rstart_element_handlerrOrX)rr< attributesrrrrWZs z"ExpatBuilder.first_element_handlercCs|j|}t|j|||_|rptdt|dD]<}t||tdt }||d}||_ |j|_ t ||q2||jj ur||dS)Nrr9r)rQZ createElementrrRranger:rAttrrrrrr rtrH)rr<rriarrrrr`s      z"ExpatBuilder.start_element_handlercCsj|jrf||jjurdS|j|}|tkr4t|n|tkrFt|ndS|j|_ |j || dSr) rGrQrtstartContainerrRejecter FILTER_SKIPSkipper parentNoderRrunlink)rrZfiltrrrrHqs     z"ExpatBuilder._finish_start_elementcCs|j}|j|_||dSr)rRrr)rr<rRrrrrYsz ExpatBuilder.end_element_handlercCs\|j|j}|r||||jrX||jjur4dS|j|tkrX|j || dSr) rSgetr_handle_white_text_nodesrGrQrtrrrRrr)rrRr$rrrrs   z ExpatBuilder._finish_end_elementcCsZ|jjs|sdSg}|jD]"}|jtkr|js||q|D]}| |qFdSr) rDZwhitespace_in_element_contentr+rrrrZstriprr)rrr$Lrrrrrs  z%ExpatBuilder._handle_white_text_nodescCs>|j|}|dur&t|||j|<n|jdus4J||_dSr)rSrrr)rr<rr$rrrrns  z!ExpatBuilder.element_decl_handlerc CsF|j|}|dur&t|}||j|<|jd|dd|d||gdSNr)rSrrrr)rZelemr<r*ZdefaultZrequiredr$rrrros  z!ExpatBuilder.attlist_decl_handlercCs2||j_||j_|dkr.|r&d|j_nd|j_dS)NrTF)rQversionencoding standalone)rrrrrrrrms  zExpatBuilder.xml_decl_handlerr) r0r1r2__doc__rrMrOrJrNrzr|rurUrr[rhrjr]r_rbrdrfrkrWrrHrYrrrnrormrrrrrCs:     rCc@seZdZdZdZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) rFzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.rEcCs ||_dSrr)rrErrrrrz#FilterVisibilityController.__init__cCsT|j|j}|jj|@rL|j|}|tkr0t|tvrHtdt ||St SdS)Nz)startContainer() returned illegal value: ) _nodetype_maskrrE whatToShowrFILTER_INTERRUPTrv_ALLOWED_FILTER_RETURNSr;reprr)rrmaskvalrrrrs    z)FilterVisibilityController.startContainercCs|j|j}|jj|@r||j|}|tkr0t|tkr`|j}|j ddD]}| |qLt S|t vrxt dt||StSdS)Nz%acceptNode() returned illegal value: )rrrErrrrvrrrZ appendChildrrr;rr)rrrrZparentrrrrrs      z%FilterVisibilityController.acceptNodeN)#r0r1r2rr3rrrrZ 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_NOTATIONrrrrrrFs$ rFc@eZdZdZddZdS) FilterCrutch)_builder_level _old_start_old_endcCs6d|_||_|j}|j|_|j|_|j|_|j|_dSr) rrrIrXrrZrrrY)rr5rqrrrrszFilterCrutch.__init__N)r0r1r2r3rrrrrrrc@s(eZdZdZddZddZddZdS) rrcCs,t|||j}dD]}t||dqdS)N)r\rcrirergrl)rrrIZsetattr)rr5rqr<rrrrs zRejecter.__init__cGs|jd|_dSr)rrargsrrrrrzRejecter.start_element_handlercGs@|jdkr0|jj}|j||j|_|j|_n |jd|_dSNrr)rrrIrNrrXrrZ)rrrqrrrrY s    zRejecter.end_element_handlerN)r0r1r2r3rrrYrrrrrs rc@ eZdZdZddZddZdS)rrcGs.|jj}|j||jj|ur*|jd|_dSr)rrRrr)rrrrrrr-s  zSkipper.start_element_handlercGsD|jdkr*|j|jj_|j|jj_d|_n|jd|_|j|dSr)rrrrIrXrrZrrrrrY3s     zSkipper.end_element_handlerN)r0r1r2r3rrYrrrrr*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)rroriginalDocumentrrrCr)rrrKrrrr[s  zFragmentBuilder.__init__cCst|d|_dSr)rCrJfragmentrrrrrJd zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)r|rrrrwrrrrzhzFragmentBuilder.parseFilec Cs||_|}|jj}d}|rV|jp*|}|jrDd|j|jf}qZ|jrZd|j}nd}|}t |||f}z| |dWn| Yn0|j }| |S)zMParse a document fragment from a string, returning the fragment node.zPUBLIC "%s" "%s"z SYSTEM "%s"T) _sourcerOrr}r_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATErsrJr) rr{rqr}ZidentrZnsattrsrQrrrrr|ms.  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. rz z%sz%s SYSTEM "%s">z %s)rrr}rrZlengthZitemnodeNamerrr Z firstChildrr)rr}r6rZnotationr rrrrs:      z FragmentBuilder._getDeclarationscCr)NrrrrrrrrzFragmentBuilder._getNSattrscCs|tkrz|j}|j}|j|}|j|_|j|_|j|_z$||j dW||_||_d|_ n||_||_d|_ 0dSt |||||SdS)NTr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrQrRrIZExternalEntityParserCreaterZcreateDocumentFragmentrrsrrCrk)rrrrrZ old_documentZ old_cur_noderqrrrrks&   z+FragmentBuilder.external_entity_ref_handlerr) r0r1r2rrrJrzr|rrrkrrrrrRs $rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) Namespacesz7Mix-in class for builders; adds support for namespaces.cCs g|_dSr)_ns_ordered_prefixesrrrr_initNamespacesrzNamespaces._initNamespacescCstjdd}d|_|S)z'Create a new namespace-handling parser.r8)Znamespace_separatorT)rrLZnamespace_prefixesrprrrrMs zNamespaces.createParsercCs t|||jjr|j|_dS)z.Insert the namespace-handlers onto the parser.N)rCrNrDZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerrprrrrNs zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)rr)rr@r>rrrrr/z'Namespaces.start_namespace_decl_handlercCsd|vrt||\}}}}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| vrDt|| \}}}}t ||||}|| |<|| ||f<n$t | t| t}|| | <|| t| f<|j|_| |_ ||_qdS)Nr8xmlns:xmlnsrr9r)rBrrrZElementrQrrrRrrr7rrr Z_ensure_attributes_attrs_attrsNSrr:Z ownerElement)rr<rr>r?r@rArrrrrr#rrrrrsV      z Namespaces.start_element_handlercCs|j}d|vrHt||\}}}}|j|kr>|j|kr>|j|kslJdn$|j|ksZJd|jtkslJd|j|_||dS)Nr8z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rRrBr&r'r@rrrr)rr<rRr>r?r@rArrrrYs$   zNamespaces.end_element_handlerN) r0r1r2rrrMrNrrrYrrrrrs5rc@r)ExpatBuilderNSz*Document builder that supports namespaces.cCt||dSr)rCrJrrrrrrJ)rzExpatBuilderNS.resetN)r0r1r2rrJrrrrr&rrc@r)FragmentBuilderNSz*Fragment builder that supports namespaces.cCrr)rrJrrrrrrJ1rzFragmentBuilderNS.resetcCsd}|j}g}|r~t|drv|jD]N\}}||vr8q&|||rPd|}nd}|rhd|||f}q&d||f}q&|j}q|S)zNReturn string of namespace attributes from this element and ancestors.r_ns_prefix_urirrz%s %s='%s'z %s='%s')rZhasattrrZitemsrr)rattrsrrr@r>Zdeclnamerrrr5s"   zFragmentBuilderNS._getNSattrsN)r0r1r2rrJrrrrrr.src@seZdZdZdS)rvzEException raised to short-circuit parsing in InternalSubsetExtractor.N)r0r1r2rrrrrrvSsrvc@sLeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ dS)r~zBXML processor which can rip out the internal document type subset.NcCs|jS)z'Return the internal subset as a string.)rrrrrr\r/z!InternalSubsetExtractor.getSubsetcC(zt||Wnty"Yn0dSr)rCrzrvrrrrrz` z!InternalSubsetExtractor.parseFilecCrr)rCr|rv)rr{rrrr|frz#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr)rUrVrrXrprrrrNlszInternalSubsetExtractor.installcCs0|r&|}g|_|jj|_|j|_ntdSr)rOrrZDefaultHandlerrrrv)rr<rrrrqrrrrUps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs,d|jdddd}||_tdS)Nrz Z z )ZjoinrZreplacerv)rr6rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCs tdSr)rv)rr<rrrrrrz-InternalSubsetExtractor.start_element_handler) r0r1r2rrrrzr|rNrUrrrrrrr~Ws r~TcCs`|r t}nt}t|trRt|d}||}Wdq\1sF0Yn ||}|S)z{Parse a document, returning the resulting Document node. 'file' may be either a file name or an open file object. rbN)rrC isinstancestropenrz)rw namespacesr5fpresultrrrparses  * rcCs|r t}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrCr|)r{rr5rrrr|sr|cCsd|rt|}nt|}t|trVt|d}||}Wdq`1sJ0Yn ||}|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. rN)rrrrrrz)rwrrr5rrrrr 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|)r{rrr5rrrparseFragmentStrings rcCs|jrt|St|SdS)z,Create a builder based on an Options object.N)rrrC)rKrrr makeBuildersrTrrr)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomrr Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrPZTypeInfor!Zobjectrr7rBrCrrFrrrrrrrrrZ Exceptionrvr~rr|rrrrrrrZsf            3C :   u_%,