a Z^ @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.9/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%)r namespaceURI localNamerrrgetAttributeTypeNSTszElementInfo.getAttributeTypeNScCs,|jr$|jd}|tjjtjjfvSdSdSNrF)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)r1)rZeuriZenameZaurir&rrrisIdNSkszElementInfo.isIdNS)N) __name__ __module__ __qualname__ __slots__rrr r)r,r/r0r1r2rrrrr<s  rcCs |||Sr)_intern_setdefault)buildersrrr_internosr:cCsd|vs J|d}|j}t|dkr^|\}}}|||}d||f}|||}|||}n4t|dkr|\}}t}|||}}n td|||||||fS)N z%s:%sz4Unsupported syntax: spaces in URIs not supported: %r)splitr7lenr ValueError)r8namepartsinternuri localnameprefixqnamerrr_parse_ns_namers          rHc@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 createParserszExpatBuilder.createParsercCsF|js@||_|jjj|_d|j_d|j_d|j_||j|jS)z7Return the parser object, creating a new one if needed.T) rOrSrC setdefaultr7Z buffer_textZordered_attributesZspecified_attributesinstallrrrr getParsers   zExpatBuilder.getParsercCs,ttdd|_|j|_|jj|_d|_dS)z6Free all data structures used during DOM construction.NF)theDOMImplementationZcreateDocumentrdocumentcurNode _elem_info_cdatarrrrrPs  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_handlerProcessingInstructionHandlerrJrentity_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_handlerZAttlistDeclHandlerrparserrrrrUs$ 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@FN) rVreadParserXdocumentElement _setup_subset ParseEscaperPrO)rfilerxZ first_bufferbufferdocrrr parseFiles"     zExpatBuilder.parseFilecCsN|}z||d||Wnty4Yn0|j}|d|_|S)z|j|}t|j||jr:|j|tkr:|j|dSr)rXZ createCommentr rYrMrrr)rrrrrrriIs  zExpatBuilder.comment_handlercCsd|_d|_dS)NTFr[rrrrrrkOsz(ExpatBuilder.start_cdata_section_handlercCsd|_d|_dS)NFrrrrrrmSsz&ExpatBuilder.end_cdata_section_handlercCsdSNr!r)rcontextrrrrrrrrWsz(ExpatBuilder.external_entity_ref_handlercCs2|jdur|jst|_|j|_|||dSr)rMrZrrstart_element_handlerrVr_)rrA attributesrrrr^Zs z"ExpatBuilder.first_element_handlercCs|j|}t|j|||_|rptdt|dD]<}t||tdt }||d}||_ |j|_ t ||q2||jj ur||dS)Nrr=r!)rXZ createElementr rYranger?rAttrrrrrr r|rN)rrArriarrrrr`s      z"ExpatBuilder.start_element_handlercCsj|jrf||jjurdS|j|}|tkr4t|n|tkrFt|ndS|j|_ |j || dSr) rMrXr|startContainerrRejecter FILTER_SKIPSkipper parentNoderYrunlink)rrZfiltrrrrNqs     z"ExpatBuilder._finish_start_elementcCs|j}|j|_||dSr)rYrr)rrArYrrrr`sz ExpatBuilder.end_element_handlercCs\|j|j}|r||||jrX||jjur4dS|j|tkrX|j || dSr) rZgetr_handle_white_text_nodesrMrXr|rrrYrr)rrYr'rrrrs   z ExpatBuilder._finish_end_elementcCsZ|jjs|sdSg}|jD]"}|jtkr|js||q|D]}| |qFdSr) rJZwhitespace_in_element_contentr/rrrrstriprr)rrr'Lrrrrrs  z%ExpatBuilder._handle_white_text_nodescCs>|j|}|dur&t|||j|<n|jdus4J||_dSr)rZrrr)rrArr'rrrrus  z!ExpatBuilder.element_decl_handlerc CsF|j|}|dur&t|}||j|<|jd|dd|d||gdSNr)rZrrrr)relemrAr.defaultrequiredr'rrrrvs  z!ExpatBuilder.attlist_decl_handlercCs2||j_||j_|dkr.|r&d|j_nd|j_dS)NrTF)rXversionencoding standalone)rrrrrrrrts  zExpatBuilder.xml_decl_handler)N) r3r4r5__doc__rrSrVrPrUrrr}r\rrbrorqrdrfrirkrmrrr^rrNr`rrrurvrtrrrrrIs:     rIc@seZdZdZdZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) rLzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.rKcCs ||_dSrr)rrKrrrrsz#FilterVisibilityController.__init__cCsT|j|j}|jj|@rL|j|}|tkr0t|tvrHtdt ||St SdS)Nz)startContainer() returned illegal value: ) _nodetype_maskrrK whatToShowrFILTER_INTERRUPTr~_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: )rrrKrrrr~rrrZ appendChildrrr@rr)rrrrparentrrrrrs      z%FilterVisibilityController.acceptNodeN)#r3r4r5rr6rrrrZ 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_NOTATIONrrrrrrLs$ rLc@seZdZdZddZdS) FilterCrutch)_builder_level _old_start_old_endcCs6d|_||_|j}|j|_|j|_|j|_|j|_dSr) rrrOr_rrarrr`)rr8rxrrrrszFilterCrutch.__init__N)r3r4r5r6rrrrrrsrc@s(eZdZdZddZddZddZdS) rrcCs,t|||j}dD]}t||dqdS)N)rcrjrprlrnrs)rrrOsetattr)rr8rxrArrrrs zRejecter.__init__cGs|jd|_dSr)rrargsrrrrszRejecter.start_element_handlercGs@|jdkr0|jj}|j||j|_|j|_n |jd|_dSNrr!)rrrOrUrr_rra)rrrxrrrr` s    zRejecter.end_element_handlerN)r3r4r5r6rrr`rrrrrs rc@s eZdZdZddZddZdS)rrcGs.|jj}|j||jj|ur*|jd|_dSr)rrYrr)rrrrrrr-s  zSkipper.start_element_handlercGsD|jdkr*|j|jj_|j|jj_d|_n|jd|_|j|dSr)rrrrOr_rrarrrrr`3s     zSkipper.end_element_handlerN)r3r4r5r6rr`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)rroriginalDocumentrrrIr)rrrQrrrr[s  zFragmentBuilder.__init__cCst|d|_dSr)rIrPfragmentrrrrrPds zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)rrzrrrrrrhszFragmentBuilder.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) _sourcerVrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATEr{rPr) rrrxridentrZnsattrsrXrrrrrms.  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)rrrrrlengthitemnodeNamerrrZ firstChildrr)rrr9rZnotationr rrrrs:      z FragmentBuilder._getDeclarationscCsdS)NrrrrrrrszFragmentBuilder._getNSattrscCs|tkrz|j}|j}|j|}|j|_|j|_|j|_z$||j dW||_||_d|_ n||_||_d|_ 0dSt |||||SdS)NTr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrXrYrOZExternalEntityParserCreaterZcreateDocumentFragmentrr{rrIrr)rrrrrZ old_documentZ old_cur_noderxrrrrrs&   z+FragmentBuilder.external_entity_ref_handler)N) r3r4r5rrrPrrrrrrrrrrrRs $rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) 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.r;)Znamespace_separatorT)rrRZnamespace_prefixesrwrrrrSs zNamespaces.createParsercCs t|||jjr|j|_dS)z.Insert the namespace-handlers onto the parser.N)rIrUrJZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerrwrrrrUs zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)rr)rrFrDrrrrsz'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)Nr;xmlns:xmlnsrr=r!)rHrrrZElementrXrr rYrrr:rrr Z_ensure_attributes_attrs_attrsNSrr?Z ownerElement)rrArrDrErFrGrrrrrr&rrrrrsV      z Namespaces.start_element_handlercCs|j}d|vrHt||\}}}}|j|kr>|j|kr>|j|kslJdn$|j|ksZJd|jtkslJd|j|_||dS)Nr;z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rYrHr*r+rFrrrr)rrArYrDrErFrGrrrr`s$   zNamespaces.end_element_handlerN) r3r4r5rrrSrUrrr`rrrrrs5rc@seZdZdZddZdS)ExpatBuilderNSz*Document builder that supports namespaces.cCst||dSr)rIrPrrrrrrP)s zExpatBuilderNS.resetN)r3r4r5rrPrrrrr&src@s eZdZdZddZddZdS)FragmentBuilderNSz*Fragment builder that supports namespaces.cCst||dSr)rrPrrrrrrP1s zFragmentBuilderNS.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')rhasattrritemsrr)rattrsrrrFrDZdeclnamerrrr5s"   zFragmentBuilderNS._getNSattrsN)r3r4r5rrPrrrrrr.src@seZdZdZdS)r~zEException raised to short-circuit parsing in InternalSubsetExtractor.N)r3r4r5rrrrrr~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||Wnty"Yn0dSr)rIrr~rrrrr`s z!InternalSubsetExtractor.parseFilecCs(zt||Wnty"Yn0dSr)rIrr~)rrrrrrfs z#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr)r\r]rr_rwrrrrUlszInternalSubsetExtractor.installcCs0|r&|}g|_|jj|_|j|_ntdSr)rVrrZDefaultHandlerrrr~)rrArrrrxrrrr\ps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs,d|jdddd}||_tdS)Nrz   )joinrreplacer~)rr9rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCs tdSr)r~)rrArrrrrsz-InternalSubsetExtractor.start_element_handler) r3r4r5rrrrrrUr\rrrrrrrWs rTcCs`|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)rrI isinstancestropenr)r namespacesr8fpresultrrrparses  * rcCs|r t}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrIr)rrr8rrrrsrcCsd|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)rrrrrr)rrrr8rrrrr 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)rrrr8rrrparseFragmentStrings rcCs|jrt|St|SdS)z,Create a builder based on an Options object.N)rrrI)rQrrr makeBuildersr)T)T)T)T)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomr r Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrWZTypeInfor$objectrr:rHrIrrLrrrrrrrrr Exceptionr~rrrrrrrrrrsf            3C :   u_%,