Vc dZddlmZmZmZddlmZmZmZddlm Z ddl m Z m Z ddl mZejZejZejZejjZejjZejjZejjZejZejddejdd ejdd ejdd ejdd ejdd ejddejddejddd ZGddeZdZdZGddZeeefZ GddeZ!GddeZ"Gdde"Z#Gdde"Z$d Z%d!e%zZ&Gd"d#eZ'Gd$d%Z(Gd&d'e(eZ)Gd(d)e(e'Z*Gd*d+e+Z,Gd,d-eZ-d4d/Z.d4d0Z/d4d1Z0d4d2Z1d3Z2dS)5zFacility 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) NodeFilterNcdata enumerationentityentitiesididrefidrefsnmtokennmtokens) CDATAENUMENTITYENTITIESIDIDREFIDREFSNMTOKENNMTOKENScJeZdZdZd dZdZdZdZdZdZ d Z d Z d Z dS) ElementInfo _attr_info_modeltagNameNc0||_g|_||_dSN)r$r"r#)selfr$models -/usr/lib64/python3.11/xml/dom/expatbuilder.py__init__zElementInfo.__init__?s  c*|j|j|jfSr&r!r's r) __getstate__zElementInfo.__getstate__Ds T\99r+c0|\|_|_|_dSr&r!)r'states r) __setstate__zElementInfo.__setstate__Gs5:2dlllr+c|jD]F}|d|kr8|d}|ddkrtdcSt|dcSGtjS)Nr(r)r" _typeinfo_mapr_no_type)r'anameinfots r)getAttributeTypezElementInfo.getAttributeTypeJssO 3 3DAw% 3HQ43;3(0000(b2222  3 r+ctjSr&)rr7)r' namespaceURI localNames r)getAttributeTypeNSzElementInfo.getAttributeTypeNSTs r+cv|jr1|jd}|tjjtjjfvSdSNrF)r#r r( XML_CTYPE_ANYXML_CTYPE_MIXED)r'types r)isElementContentzElementInfo.isElementContentWs? ; ;q>D 9 % ; == =5r+cT|jr |jdtjjkSdSrA)r#r r(XML_CTYPE_EMPTYr-s r)isEmptyzElementInfo.isEmpty_s' ; ;q>U[%@@ @5r+cN|jD]}|d|kr|ddkcSdS)Nr3r4rF)r")r'r8r9s r)isIdzElementInfo.isIdesCO ( (DAw% (Bx4'''' (ur+c0|||fSr&)rJ)r'eurienameaurir8s r)isIdNSzElementInfo.isIdNSksyy$'''r+r&) __name__ __module__ __qualname__ __slots__r*r.r1r;r?rErHrJrOr+r)r r <s1I :::;;;        (((((r+r c.|||Sr&)_intern_setdefault)builderss r)_internrYos  % %a + ++r+cpd|vsJ|d}|j}t|dkr2|\}}}|||}|d|}|||}|||}n@t|dkr|\}}t}|||x}}nt d|z||||||fS)N :z4Unsupported syntax: spaces in URIs not supported: %r)splitrVlenr ValueError)rWnamepartsinternuri localnameprefixqnames r)_parse_ns_namerirs $; JJsOOE  'F 5zzQ X!&Y''!6699-ue$$F9i00 UqXY"F9i888 ORVVWWW 6#s  Y 55r+ceZdZdZddZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdS) ExpatBuilderzPDocument builder that uses Expat to build a ParsedXML.DOM document instance.Nc|tj}||_|jjt |jj|_nd|_t |_d|_| dSr&) rOptions_optionsfilterFilterVisibilityController_filterr_finish_start_element_parserreset)r'optionss r)r*zExpatBuilder.__init__sk  + (**G =  ,5dm6JKKDLLDL*,D &  r+c(tjS)zCreate a new parser object.)r ParserCreater-s r) createParserzExpatBuilder.createParsers!###r+c|jsm||_|jjj|_d|j_d|j_d|j_||j|jS)z7Return the parser object, creating a new one if needed.T) rsrxrd setdefaultrV buffer_textordered_attributesspecified_attributesinstallr-s r) getParserzExpatBuilder.getParsersi| ',,..DL&*l&9&DD #'+DL $.2DL +04DL - LL & & &|r+cttdd|_|j|_|jj|_d|_dS)z6Free all data structures used during DOM construction.NF)theDOMImplementationcreateDocumentrdocumentcurNode _elem_info_cdatar-s r)rtzExpatBuilder.resets@,;; T4)) } -2 r+c|j|_|j|_|j|_|j|_|jj r |j |_ |j |_ |jjr |j|_|jjr%|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_handlerProcessingInstructionHandlerrnrentity_decl_handlerEntityDeclHandlernotation_decl_handlerNotationDeclHandlercommentscomment_handlerCommentHandlercdata_sectionsstart_cdata_section_handlerStartCdataSectionHandlerend_cdata_section_handlerEndCdataSectionHandlercharacter_data_handler_cdataCharacterDataHandlercharacter_data_handlerexternal_entity_ref_handlerExternalEntityRefHandlerxml_decl_handlerXmlDeclHandlerelement_decl_handlerElementDeclHandlerattlist_decl_handlerAttlistDeclHandlerr'parsers r)r~zExpatBuilder.installs*.)H&%)%?"#'#; .2o+ = ! @'+'?F $%)%?" = ! 9$($8F ! = ' F.2.NF +,0,JF )*.*KF ' '*.*EF '*.*J' $ 5$($=!$($=!!!r+cp|}d} |d}|sn<||d|r!|jjr||d}T|ddn#t $rYnwxYw|j}|d|_|S)zIParse a document from a file object, returning the document node.Tr3i@Fr+N) rreadParserdocumentElement _setup_subset ParseEscapertrs)r'filer first_bufferbufferdocs r) parseFilezExpatBuilder.parseFiles!!   %7++ VU+++/DM$A/&&v...$  % LLd # # # #    D m   sA+B BBc|} ||d||n#t$rYnwxYw|j}|d|_|S)zDL '48O 1 *4< *')D $ $ $ * * * *r+c|j||}t|j||jr?|j|t kr|j|dSdSdSr&)rcreateProcessingInstructionr rrqrr removeChild)r'targetdatanodes r)rzExpatBuilder.pi_handler s}88FFdlD))) < +DL33D99]J + L $ $T * * * * * + + + +r+c|jj}|jr\|jr3|djt kr|d|dS|j|}d|_nY|r1|djtkr|d}|j |z}||_ dStj }||_ |j|_ t|j|dS)NrT)rrr_cdata_continuenodeTypeCDATA_SECTION_NODE appendDatarcreateCDATASection TEXT_NODErrTextrr )r'rrrvalues r)rz)ExpatBuilder.character_data_handler_cdatas\, ; /&  n-1CC 2))$///=33D99D#'D  /JrN3y@ /b>DI$EDI F<>>DDI!%D dlD)))))r+c|jj}|r/|djtkr|d}|j|z|_dSt j}|j|z|_|j|_t|j|dSr) rrrrrrrrrr )r'rrrs r)rz#ExpatBuilder.character_data_handler&s~\,  *R.1Y> b>D D(DI F|~~I$ !]dlD)))))r+c|rdS|jjsdS|j||||}|4|j|} |j| |jjjj||j r<|j |tkr|jjjjd=dSdSdSr) rnrr_create_entitycreateTextNoderappendrrrqrr) r' entityNameis_parameter_entityrbaserr notationNamerchilds r)rz ExpatBuilder.entity_decl_handler1s   F}%  F}++J,4lDD  *M0077E O " "5 ) ) ) &+224888 < 8DL33D99]J 8 %.3B777 8 8 8 8r+c|j|||}|jjjj||jr<|j|tkr|jjjjd=dSdSdSr) r_create_notationrrrrrqr FILTER_ACCEPT)r'rrrrrs r)rz"ExpatBuilder.notation_decl_handlerCs}--lHhOO ',33D999 < 9DL33D99]J 9 %/4R888 9 9 9 9r+c|j|}t|j||jr?|j|t kr|j|dSdSdSr&)r createCommentr rrqrrr)r'rrs r)rzExpatBuilder.comment_handlerIs}**400dlD))) < +DL33D99]J + L $ $T * * * * * + + + +r+c"d|_d|_dS)NTFrrr-s r)rz(ExpatBuilder.start_cdata_section_handlerOs $r+c"d|_d|_dS)NFrr-s r)rz&ExpatBuilder.end_cdata_section_handlerSs $r+cdSNr3rT)r'contextrrrs r)rz(ExpatBuilder.external_entity_ref_handlerWsqr+c|j|js t|_|j|_|||dSr&)rqrrrstart_element_handlerrr)r'rb attributess r)rz"ExpatBuilder.first_element_handlerZsN < * *')D $/3/I, ""444444r+c|j|}t|j|||_|rvt dt |dD]W}t j||tdt}||dz}||_ |j|_ t||X||jj ur||dSdS)Nrr^r3)r createElementr rranger`rAttrrrrrr rrr)r'rbrriars r)rz"ExpatBuilder.start_element_handler`s}**400dlD)))  -1c*ooq11 - -LA!%|55"1Q3"&-#D!,,,, t}4 4 -  & &t , , , , , - -r+cP|jr||jjurdS|j|}|tkrt |n|t krt|ndS|j|_ |j || dSdSr&) rqrrstartContainerrRejecter FILTER_SKIPSkipper parentNoderrunlink)r'rfilts r)rrz"ExpatBuilder._finish_start_elementqs < t}44 <..t44D}$ $  ?DL O ' ' - - - KKMMMMM#  r+cV|j}|j|_||dSr&)rrr)r'rbrs r)rz ExpatBuilder.end_element_handlers-,)    )))))r+cL|j|j}|r||||jrc||jjurdS|j|tkr2|j || dSdSdSr&) rgetr$_handle_white_text_nodesrqrrrrrrr)r'rr9s r)rz ExpatBuilder._finish_end_elements""7?33  9  ) )'4 8 8 8 < !$-77 |&&w//=@ ! ((111      ! ! ! !r+c|jjs|sdSg}|jD]@}|jt kr.|js||A|D]}| |dSr&) rnwhitespace_in_element_contentrErrrrstriprr)r'rr9Lrs r)r z%ExpatBuilder._handle_white_text_nodess M 7 ((**  F _  E~* 5:3C3C3E3E  $ $E   U # # # # $ $r+c|j|}|t|||j|<dS|jJ||_dSr&)rrr r#)r'rbr(r9s r)rz!ExpatBuilder.element_decl_handlersT""4((  $/e$<$>>.,   :::OOO****+++ ***( * * *888$999 +++ %%%%%%555 ---".*** ! ! !$$$"   BBB11111r+rkcLeZdZdZdZdZdZdZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"dS)rpzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.roc||_dSr&r)r'ros r)r*z#FilterVisibilityController.__init__s  r+c|j|j}|jj|zrV|j|}|t krt |tvrtdt|z|StS)Nz)startContainer() returned illegal value: ) _nodetype_maskrro whatToShowrFILTER_INTERRUPTr_ALLOWED_FILTER_RETURNSrareprr)r'rmaskvals r)rz)FilterVisibilityController.startContainers"4=1 ; !D ( !+,,T22C&& "!!11 O ADIIMOOOJ r+c~|j|j}|jj|zr|j|}|t krt |tkr5|j}|j ddD]}| |tS|tvrtdt|z|StS)Nz%acceptNode() returned illegal value: )rrrorrrrrrr appendChildrr rar!r)r'rr"r#parentrs r)rz%FilterVisibilityController.acceptNodes"4=1 ; !D ( !+((..C&& "!!k! %!_QQQ/..E&&u----$$11 K =S IKKKJ r+N)#rPrQrRrrSr*rrr ELEMENT_NODEr SHOW_ELEMENTATTRIBUTE_NODESHOW_ATTRIBUTEr SHOW_TEXTrSHOW_CDATA_SECTIONENTITY_REFERENCE_NODESHOW_ENTITY_REFERENCE ENTITY_NODE SHOW_ENTITYPROCESSING_INSTRUCTION_NODESHOW_PROCESSING_INSTRUCTION COMMENT_NODE SHOW_COMMENT DOCUMENT_NODE SHOW_DOCUMENTDOCUMENT_TYPE_NODESHOW_DOCUMENT_TYPEDOCUMENT_FRAGMENT_NODESHOW_DOCUMENT_FRAGMENT NOTATION_NODE SHOW_NOTATIONrrTr+r)rprps55I ! ! !!!!* **A **C **> **G "**J **@ (**P **A **B **G #**K **B  NNNr+rpceZdZdZdZdS) FilterCrutch)_builder_level _old_start_old_endcd|_||_|j}|j|_|j|_|j|_|j|_dSr) r@r?rsrrArrBrr)r'rWrs r)r*zFilterCrutch.__init__sG   40 %)%?"#'#;   r+N)rPrQrRrSr*rTr+r)r>r>s(>I<<<<c$eZdZdZdZdZdZdS)rrTcvt|||j}dD]}t||ddS)N)rrrrrr)r>r*rssetattr)r'rWrrbs r)r*zRejecter.__init__sQdG,,, ( (D FD$ ' ' ' ' ( (r+c$|jdz|_dSr)r@r'argss r)rzRejecter.start_element_handlerskAo r+c|jdkr@|jj}|j||j|_|j|_dS|jdz |_dSNrr3)r@r?rsr~rArrBr)r'rIrs r)rzRejecter.end_element_handler s\ ;!  *]*F M ! !& ) ) ))-F &'+}F $ $ $+/DKKKr+N)rPrQrRrSr*rrrTr+r)rrsFI ( ( (&&&*****r+rceZdZdZdZdZdS)rrTcp|jj}|j||jj|ur|jdz|_dSdSr)r?rrAr@)r'rIrs r)rzSkipper.start_element_handler-sH}$ =  , *+/DKKK * *r+c|jdkr5|j|jj_|j|jj_d|_dS|jdz |_|j|dSrK)r@rAr?rsrrBrrHs r)rzSkipper.end_element_handler3s^ ;!  !9=DM ! 56:mDM ! 3 DMMM+/DK DM4 r+N)rPrQrRrSrrrTr+r)rr*s7I*** ! ! ! ! !r+rz8http://xml.python.org/entities/fragment-builder/internalz %%s ]> &fragment-builder-internal;c>eZdZdZd dZdZdZdZdZdZ d Z 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. Nc|jtkr||_||_n|j|_||_t ||dSr&)rr5originalDocumentrrrkr*)r'rrus r)r*zFragmentBuilder.__init__[sQ  } , #$+D !"DLL$+$9D !"DLdG,,,,,r+cHt|d|_dSr&)rkrtfragmentr-s r)rtzFragmentBuilder.resetds!4    r+cP||S)zRParse a document fragment from a file object, returning the fragment node.)rrr'rs r)rzFragmentBuilder.parseFilehs  ,,,r+c||_|}|jj}d}|rH|jp|}|jrd|jd|jd}n|jr d|jz}nd}|}t|||fz} | |dn#| xYw|j }| |S)zMParse a document fragment from a string, returning the fragment node.zPUBLIC "z" ""z SYSTEM "%s"T) _sourcerrRrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATErrtrT) r'rrridentrnsattrsrrTs r)rzFragmentBuilder.parseStringms  !!'/  +Ft/D/D/F/FF 9 9#,,,g.>.>.>@! 9%(88F""$$-0HH  LL4 ( ( ( (  JJLLL = s B//Ccb|jjj}d}|rt|jjD]Y}|j|}|r|dz}|d|j}|jr|d|jd|j d}L|d|j d}Zt|j jD]}|j |}|r|dz}|d|j}|jr|d|jd|j d }n'|j r|d|j d }n|d |j j d }|j r |d |j }|d z}|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. rXz z z SYSTEM "z )rrrrrlengthitemnodeNamerrr firstChildrr)r'rrXrnotationrs r)r[z FragmentBuilder._getDeclarationss ,,4   7,344 C C",11!44#F A)*H,=,=>$C11h///1B1B1BDAA./QQ0A0A0ABAA7+233   )..q11#F A'(qq&//:?@11fooov@AA_@,-AAv?AA%&QQ(9(>(>(>?A&D,-AAv/B/BCAGr+cdS)NrXrTr-s r)r\zFragmentBuilder._getNSattrssrr+c|tkr|j}|j}|j|}|j|_|j|_|j|_ ||j d||_||_d|_ n#||_||_d|_ wxYwdSt |||||S)NTr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrrrsExternalEntityParserCreaterRcreateDocumentFragmentrTrrZrkr)r'rrrr old_document old_cur_noders r)rz+FragmentBuilder.external_entity_ref_handlers ; ; 9 =Ltjd}d|_|S)z'Create a new namespace-handling parser.r[)namespace_separatorT)r rwnamespace_prefixesrs r)rxzNamespaces.createParsers"#<<<$(! r+cpt|||jjr|j|_dSdS)z.Insert the namespace-handlers onto the parser.N)rkr~rnnamespace_declarationsstart_namespace_decl_handlerStartNamespaceDeclHandlerrs r)r~zNamespaces.installsCT6*** = / 31  , , , 3 3r+c>|j||fdS)z/Push this namespace declaration on our storage.N)rqr)r'rgres r)rxz'Namespaces.start_namespace_decl_handlers# !((&#77777r+cd|vrt||\}}}}nt}|}d}t}tj||||}|j|_t|j|||_|j r|j D]y\}}|r.tj t|d|zt|d}n!tj dtdt}||_ |j|_t||z|j dd=|r||j} |j} t%dt'|dD]} || } || dz} d| vr9t|| \}}}}tj ||||}|| |<|| ||f<n2tj | t| t}|| | <|| t| f<|j|_| |_ ||_dSdS)Nr[xmlns:xmlnsrr^r3)rirrrElementrrr rrqrrYrrr _ensure_attributes_attrs_attrsNSrr` ownerElement)r'rbrrerfrgrhrrrrrr8rs r)rz Namespaces.start_element_handlers* $; ",:4,F,F )CFEE!CEI!Fuc69==!]dlD)))  $ -#8 - - < WT8f3D%E%E%4fgGGAA Wo%,l<|j}d|vrAt||\}}}}|j|kr|j|kr |j|ks Jdn/|j|ks Jd|jt ks Jd|j|_||dS)Nr[z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rrir=r>rgrdrrr)r'rbrrerfrgrhs r)rzNamespaces.end_element_handlerslGd{ D0>tT0J0J-Y,3?#-:?#Nf4??????? '4/@@?@@@+>DDCDDD"-DL  $ $W - - - - -r+N) rPrQrRrrrrxr~rxrrrTr+r)roroszAA'''  333888-&-&-&^.  . . . . .r+roceZdZdZdZdS)ExpatBuilderNSz*Document builder that supports namespaces.cbt||dSr&)rkrtrrr-s r)rtzExpatBuilderNS.reset)s.4    r+N)rPrQrRrrtrTr+r)rr&s)44r+rceZdZdZdZdZdS)FragmentBuilderNSz*Fragment builder that supports namespaces.cbt||dSr&)rPrtrrr-s r)rtzFragmentBuilderNS.reset1s.d### r+cd}|j}g}|rst|drZ|jD]@\}}||vr |||rd|z}nd}|r |d|d|d}7d|d|d}A|j}|s|S) zNReturn string of namespace attributes from this element and ancestors.rX_ns_prefix_urir|r}z z=''r[)rhasattrritemsrr)r'attrsrr rgredeclnames r)r\zFragmentBuilderNS._getNSattrs5s,  )w 011 =#*#9#?#?#A#A = =KFC{! HHV$$$+#+f#4#*=5:UUHHHccc J=/7hh <(G )  r+N)rPrQrRrrtr\rTr+r)rr.s844r+rceZdZdZdS)rzEException raised to short-circuit parsing in InternalSubsetExtractor.N)rPrQrRrrTr+r)rrSsOODr+rc@eZdZdZdZdZdZdZdZdZ dZ d Z dS) rzBXML processor which can rip out the internal document type subset.Nc|jS)z'Return the internal subset as a string.)rr-s r)rz!InternalSubsetExtractor.getSubset\s {r+c` t||dS#t$rYdSwxYwr&)rkrrrVs r)rz!InternalSubsetExtractor.parseFile`sE   " "4 . . . . .    DD   --c` t||dS#t$rYdSwxYwr&)rkrr)r'rs r)rz#InternalSubsetExtractor.parseStringfsE   $ $T6 2 2 2 2 2    DD rc6|j|_|j|_dSr&)rrrrrs r)r~zInternalSubsetExtractor.installls)-)H&%)%?"""r+c|r:|}g|_|jj|_|j|_dSt r&)rrrDefaultHandlerrrr)r'rbrrrrs r)rz2InternalSubsetExtractor.start_doctype_decl_handlerpsH  ^^%%FDK$(K$6F !+/+HF ( ( (-- r+cd|jdddd}||_t)NrXz   )joinrreplacer)r'rXs r)rz0InternalSubsetExtractor.end_doctype_decl_handlerzsF GGDK ( ( 6 6 > >tT J J mmr+ctr&)r)r'rbrs r)rz-InternalSubsetExtractor.start_element_handlers mmr+) rPrQrRrrrrrr~rrrrTr+r)rrWsLL F  @@@    r+rTc|rt}nt}t|tr>t |d5}||}dddn #1swxYwYn||}|S)z{Parse a document, returning the resulting Document node. 'file' may be either a file name or an open file object. rbN)rrk isinstancestropenr)r namespacesrWfpresults r)parsers ! ""..$) $   +&&r**F + + + + + + + + + + + + + + +""4(( MsA''A+.A+cj|rt}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrkr)rrrWs r)rrs6! ""..   v & &&r+c|rt|}nt|}t|tr>t |d5}||}dddn #1swxYwYn||}|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)rrPrrrr)rrrrWrrs r) parseFragmentrs+#G,,!'**$) $   +&&r**F + + + + + + + + + + + + + + +""4(( MsA))A-0A-cn|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. )rrPr)rrrrWs r)parseFragmentStringrs< +#G,,!'**   v & &&r+cL|jrt|St|S)z,Create a builder based on an Options object.)rrrk)rus r) makeBuilderrs)%g&&&G$$$r+)T)3rxml.domrrrrrr xml.parsersr xml.dom.minidomr r xml.dom.NodeFilterr rrr5DOMBuilderFilterrrrrgetDOMImplementationrTypeInfor6objectr rYrirkr rpr>rrrir]rProrr ExceptionrrrrrrrrTr+r)rsZ:.---------BBBBBBBBBB>>>>>>>>)))))) N ," +9 +9 )5 .?3w355! w//  }55  x00  z22  t,,  w//  x00  y11  z22   1(1(1(1(1(&1(1(1(f,,,666&|1|1|1|1|1|1|1|1B )-E7 7 7 7 7 7 7 7 t < < < < <6 < < <*****|***8!!!!!l!!!2?%*+ +r9r9r9r9r9lr9r9r9j\.\.\.\.\.\.\.\.~Z""""" O"""J     )   )))))l)))X$''''( ' ' ' '%%%%%r+