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%)r namespaceURI 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)r1)rZeuriZenameZaurir&rrrisIdNSkszElementInfo.isIdNS)N) __name__ __module__ __qualname__ __slots__rrr r)r,r/r0r1r2rrrrr<s  rcCs |||Sr)_intern_setdefault)buildersrrr_internosr:cCsd|ks t|d}|j}t|dkr^|\}}}|||}d||f}|||}|||}n4t|dkr|\}}t}|||}}n td|||||||fS)N z%s:%sz4Unsupported syntax: spaces in URIs not supported: %r)AssertionErrorsplitr7lenr ValueError)r8namepartsinternuri localnameprefixqnamerrr_parse_ns_namers          rIc@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) rPrTrD 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_cdatarrrrrQs 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_handlerProcessingInstructionHandlerrKrentity_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_handlerZAttlistDeclHandlerrparserrrrrVs$ 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) rWreadParserYdocumentElement _setup_subset ParseEscaperQrP)rfileryZ first_bufferbufferdocrrr parseFiles"    zExpatBuilder.parseFilecCsP|}z||d||Wntk r6YnX|j}|d|_|S)z|j|}t|j||jr:|j|tkr:|j|dSr)rYZ createCommentr rZrNrrr)rrrrrrrjIs  zExpatBuilder.comment_handlercCsd|_d|_dS)NTFr\rrrrrrlOsz(ExpatBuilder.start_cdata_section_handlercCsd|_d|_dS)NFrrrrrrnSsz&ExpatBuilder.end_cdata_section_handlercCsdSNr!r)rcontextrrrrrrrsWsz(ExpatBuilder.external_entity_ref_handlercCs2|jdkr|jst|_|j|_|||dSr)rNr[rrstart_element_handlerrWr`)rrB attributesrrrr_Zs z"ExpatBuilder.first_element_handlercCs|j|}t|j|||_|rptdt|dD]<}t||tdt }||d}||_ |j|_ t ||q2||jj k r||dS)Nrr=r!)rYZ createElementr rZranger@rAttrrrrrr r}rO)rrBrriarrrrr`s      z"ExpatBuilder.start_element_handlercCsj|jrf||jjkrdS|j|}|tkr4t|n|tkrFt|ndS|j|_ |j || dSr) rNrYr}startContainerrRejecter FILTER_SKIPSkipper parentNoderZrunlink)rrZfiltrrrrOqs     z"ExpatBuilder._finish_start_elementcCs|j}|j|_||dSr)rZrr)rrBrZrrrrasz ExpatBuilder.end_element_handlercCs\|j|j}|r||||jrX||jjkr4dS|j|tkrX|j || dSr) r[getr_handle_white_text_nodesrNrYr}rrrZrr)rrZr'rrrrs   z ExpatBuilder._finish_end_elementcCsZ|jjs|sdSg}|jD]"}|jtkr|js||q|D]}| |qFdSr) rKZwhitespace_in_element_contentr/rrrrstriprr)rrr'Lrrrrrs  z%ExpatBuilder._handle_white_text_nodescCs>|j|}|dkr&t|||j|<n|jdks4t||_dSr)r[rrrr>)rrBrr'rrrrvs  z!ExpatBuilder.element_decl_handlerc CsF|j|}|dkr&t|}||j|<|jd|dd|d||gdSNr)r[rrrr)relemrBr.defaultZrequiredr'rrrrws  z!ExpatBuilder.attlist_decl_handlercCs2||j_||j_|dkr.|r&d|j_nd|j_dS)NrTF)rYversionencoding standalone)rrrrrrrrus  zExpatBuilder.xml_decl_handler)N) r3r4r5__doc__rrTrWrQrVrrr~r]rrcrprrrergrjrlrnrsr_rrOrarrrvrwrurrrrrJs:     rJc@seZdZdZdZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) rMzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.rLcCs ||_dSrr)rrLrrrrsz#FilterVisibilityController.__init__cCsT|j|j}|jj|@rL|j|}|tkr0t|tkrHtdt ||St SdS)Nz)startContainer() returned illegal value: ) _nodetype_maskrrL whatToShowrFILTER_INTERRUPTr_ALLOWED_FILTER_RETURNSrAreprr)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: )rrrLrrrrrrrZ appendChildrrrArr)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_NOTATIONrrrrrrMs< rMc@seZdZdZddZdS) FilterCrutch)_builder_level _old_start_old_endcCs6d|_||_|j}|j|_|j|_|j|_|j|_dSr) rrrPr`rrbrrra)rr8ryrrrrszFilterCrutch.__init__N)r3r4r5r6rrrrrrsrc@s(eZdZdZddZddZddZdS) rrcCs,t|||j}dD]}t||dqdS)N)rdrkrqrmrort)rrrPsetattr)rr8ryrBrrrrs zRejecter.__init__cGs|jd|_dSr)rrargsrrrrszRejecter.start_element_handlercGs@|jdkr0|jj}|j||j|_|j|_n |jd|_dSNrr!)rrrPrVrr`rrb)rrryrrrra s    zRejecter.end_element_handlerN)r3r4r5r6rrrarrrrrs rc@s eZdZdZddZddZdS)rrcGs.|jj}|j||jj|k r*|jd|_dSr)rrZrr)rrrrrrr-s  zSkipper.start_element_handlercGsD|jdkr*|j|jj_|j|jj_d|_n|jd|_|j|dSr)rrrrPr`rrbrrrrra3s     zSkipper.end_element_handlerN)r3r4r5r6rrarrrrr*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)rroriginalDocumentrrrJr)rrrRrrrr[s  zFragmentBuilder.__init__cCst|d|_dSr)rJrQfragmentrrrrrQds zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)rr{rrrrrrhszFragmentBuilder.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.rzzPUBLIC "%s" "%s"z SYSTEM "%s"r!) _sourcerWrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATEr|rQr) rrryrZidentrZnsattrsrYrrrrrms.  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. rzz z%sz%s SYSTEM "%s">z %s)rrrrrZlengthitemnodeNamerrrZ firstChildrr)rrr9rZnotationr rrrrs:      z FragmentBuilder._getDeclarationscCsdS)NrzrrrrrrszFragmentBuilder._getNSattrscCs~|tkrh|j}|j}|j|}|j|_|j|_|j|_z| |jdW5||_||_d|_XdSt |||||SdS)Nr!r) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrYrZrPZExternalEntityParserCreaterZcreateDocumentFragmentrrr|rJrs)rrrrrZ old_documentZ old_cur_noderyrrrrss(  z+FragmentBuilder.external_entity_ref_handler)N) r3r4r5rrrQrrrrrsrrrrrRs $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)rrSZnamespace_prefixesrxrrrrTs zNamespaces.createParsercCs t|||jjr|j|_dS)z.Insert the namespace-handlers onto the parser.N)rJrVrKZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerrxrrrrVs zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)rr)rrGrErrrrsz'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)Nr;xmlns:xmlnsrr=r!)rIrrrZElementrYrr rZrrr:rrr Z_ensure_attributes_attrs_attrsNSrr@Z ownerElement)rrBrrErFrGrHrrrrrr&rrrrrs^      z Namespaces.start_element_handlercCs|j}d|krHt||\}}}}|j|kr>|j|kr>|j|ksltdn$|j|ksZtd|jtksltd|j|_| |dS)Nr;z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rZrIr*r+rGr>rrrr)rrBrZrErFrGrHrrrras$   zNamespaces.end_element_handlerN) r3r4r5rrrTrVrrrarrrrrs5rc@seZdZdZddZdS)ExpatBuilderNSz*Document builder that supports namespaces.cCst||dSr)rJrQrrrrrrQ)s zExpatBuilderNS.resetN)r3r4r5rrQrrrrr&src@s eZdZdZddZddZdS)FragmentBuilderNSz*Fragment builder that supports namespaces.cCst||dSr)rrQrrrrrrQ1s 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.rz_ns_prefix_urirrz%s %s='%s'z %s='%s')rhasattrritemsrr)rattrsrrrGrEZdeclnamerrrr5s"   zFragmentBuilderNS._getNSattrsN)r3r4r5rrQrrrrrr.src@seZdZdZdS)rzEException raised to short-circuit parsing in InternalSubsetExtractor.N)r3r4r5rrrrrrSsrc@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)rJrrrrrrr`sz!InternalSubsetExtractor.parseFilecCs*zt||Wntk r$YnXdSr)rJrr)rrrrrrfsz#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr)r]r^rr`rxrrrrVlszInternalSubsetExtractor.installcCs0|r&|}g|_|jj|_|j|_ntdSr)rWrrZDefaultHandlerrrr)rrBrrrryrrrr]ps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs,d|jdddd}||_tdS)Nrzz   )joinrreplacer)rr9rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCs tdSr)r)rrBrrrrrsz-InternalSubsetExtractor.start_element_handler) r3r4r5rrrrrrVr]rrrrrrrWs 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)rrJ isinstancestropenr)r namespacesr8fpresultrrrparses   rcCs|r t}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrJr)rrr8rrrrsrc 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)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)rrrJ)rRrrr makeBuildersr)T)T)T)T)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomr r Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrXZTypeInfor$objectrr:rIrJrrMrrrrrrrrr Exceptionrrrrrrrrrrrsf            3C :   u_%,