3 ^ @sdZddlmZmZmZddlmZmZmZddlm Z ddl m Z m Z ddl mZejZejZejZejjZejjZejjZejjZejZejddejdd ejdd ejdd ejdd ejdd ejddejddejddd 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@sZeZdZdZdddZddZd d Zd d Zd dZddZ ddZ ddZ ddZ dS) ElementInfo _attr_info_modeltagNameNcCs||_g|_||_dS)N)rrr)selfrmodelr,/usr/lib64/python3.6/xml/dom/expatbuilder.py__init__?szElementInfo.__init__cCs|j|j|jfS)N)rrr)rrrr __getstate__DszElementInfo.__getstate__cCs|\|_|_|_dS)N)rrr)rstaterrr __setstate__GszElementInfo.__setstate__cCsJxB|jD]8}|d|kr|d}|ddkr4tdSt|dSqWtjS)Nr(rr!)r _typeinfo_mapr_no_type)ranameinfotrrrgetAttributeTypeJs   zElementInfo.getAttributeTypecCstjS)N)rr#)r namespaceURI localNamerrrgetAttributeTypeNSTszElementInfo.getAttributeTypeNScCs,|jr$|jd}|tjjtjjfkSdSdS)NrF)rrrZ XML_CTYPE_ANYZXML_CTYPE_MIXED)rtyperrrisElementContentWs   zElementInfo.isElementContentcCs |jr|jdtjjkSdSdS)NrF)rrrZXML_CTYPE_EMPTY)rrrrisEmpty_szElementInfo.isEmptycCs,x&|jD]}|d|kr|ddkSqWdS)NrrrFr!)r)rr$r%rrrisIdes  zElementInfo.isIdcCs|j||fS)N)r.)rZeuriZenameZaurir$rrrisIdNSkszElementInfo.isIdNS)rrr)N) __name__ __module__ __qualname__ __slots__rrrr'r*r,r-r.r/rrrrr<s  rcCs |j||S)N)_intern_setdefault)buildersrrr_internosr7cCsd|ks t|jd}|j}t|dkr^|\}}}|||}d||f}|||}|||}n4t|dkr|\}}t}|||}}n td|||||||fS)N z%s:%srz4Unsupported syntax: spaces in URIs not supported: %r)AssertionErrorsplitr4lenr ValueError)r5namepartsinternuri localnameprefixqnamerrr_parse_ns_namers          rEc@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|dkrtj}||_|jjdk r2t|jj|_n d|_t|_d|_|j dS)N) rZOptions_optionsfilterFilterVisibilityController_filterr_finish_start_element_parserreset)roptionsrrrrs zExpatBuilder.__init__cCstjS)zCreate a new parser object.)r ParserCreate)rrrr createParserszExpatBuilder.createParsercCsF|js@|j|_|jjj|_d|j_d|j_d|j_|j|j|jS)z7Return the parser object, creating a new one if needed.T) rLrPr@ setdefaultr4Z buffer_textZordered_attributesZspecified_attributesinstall)rrrr getParsers   zExpatBuilder.getParsercCs,tjtdd|_|j|_|jj|_d|_dS)z6Free all data structures used during DOM construction.NF)theDOMImplementationZcreateDocumentrdocumentcurNode _elem_info_cdata)rrrrrMs   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_handlerProcessingInstructionHandlerrGrentity_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_handlerZAttlistDeclHandler)rparserrrrrRs$ zExpatBuilder.installc Cs|j}d}yLx:|jd}|s P|j|d|rB|jjrB|j|d}qW|jddWntk rlYnX|j}|jd|_|S) zIParse a document from a file object, returning the document node.TirFNi@) rSreadParserUdocumentElement _setup_subset ParseEscaperMrL)rfilertZ first_bufferbufferdocrrr parseFiles$    zExpatBuilder.parseFilec CsP|j}y|j|d|j|Wntk r6YnX|j}|jd|_|S)z|jj|}t|j||jr:|jj|tkr:|jj|dS)N)rUZ createCommentr rVrJrrr)rrrrrrrfIs  zExpatBuilder.comment_handlercCsd|_d|_dS)NTF)rXr)rrrrrhOsz(ExpatBuilder.start_cdata_section_handlercCsd|_d|_dS)NF)rXr)rrrrrjSsz&ExpatBuilder.end_cdata_section_handlercCsdS)Nrr)rcontextrrrrrrroWsz(ExpatBuilder.external_entity_ref_handlercCs4|jdkr|j rt|_|j|j_|j||dS)N)rJrWrrstart_element_handlerrSr\)rr> attributesrrrr[Zs z"ExpatBuilder.first_element_handlercCs|jj|}t|j|||_|rtxPtdt|dD]<}tj||tdt }||d}||_ |j|_ t ||q4W||jj k r|j|dS)Nrrr)rUZ createElementr rVranger<rAttrrrrrr ryrK)rr>rriarrrrr`s     z"ExpatBuilder.start_element_handlercCsj|jrf||jjkrdS|jj|}|tkr4t|n|tkrFt|ndS|j|_ |jj ||j dS)N) rJrUrystartContainerrRejecter FILTER_SKIPSkipper parentNoderVrunlink)rrZfiltrrrrKqs     z"ExpatBuilder._finish_start_elementcCs|j}|j|_|j|dS)N)rVrr)rr>rVrrrr]sz ExpatBuilder.end_element_handlercCs\|jj|j}|r|j|||jrX||jjkr4dS|jj|tkrX|j j ||j dS)N) rWgetr_handle_white_text_nodesrJrUryrrrVrr)rrVr%rrrrs   z ExpatBuilder._finish_end_elementcCsf|jjs|j rdSg}x.|jD]$}|jtkr"|jj r"|j|q"Wx|D]}|j |qPWdS)N) rGZwhitespace_in_element_contentr,rrrrstriprr)rrr%Lrrrrrs   z%ExpatBuilder._handle_white_text_nodescCs>|jj|}|dkr&t|||j|<n|jdks4t||_dS)N)rWrrrr:)rr>rr%rrrrrs  z!ExpatBuilder.element_decl_handlerc CsF|jj|}|dkr&t|}||j|<|jjd|dd|d||gdS)Nr)rWrrrr)relemr>r+defaultZrequiredr%rrrrss   z!ExpatBuilder.attlist_decl_handlercCs2||j_||j_|dkr.|r&d|j_nd|j_dS)NrTF)rUversionencoding standalone)rrrrrrrrqs  zExpatBuilder.xml_decl_handler)N) r0r1r2__doc__rrPrSrMrRrrrzrYrr_rlrnrarcrfrhrjror[rrKr]rrrrrsrqrrrrrFs:     rFc@seZdZdZd ZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) rIzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.rHcCs ||_dS)N)rH)rrHrrrrsz#FilterVisibilityController.__init__cCsT|j|j}|jj|@rL|jj|}|tkr0t|tkrHtdt ||St SdS)Nz)startContainer() returned illegal value: ) _nodetype_maskrrH whatToShowrFILTER_INTERRUPTr{_ALLOWED_FILTER_RETURNSr=reprr)rrmaskvalrrrrs   z)FilterVisibilityController.startContainercCs|j|j}|jj|@r|jj|}|tkr0t|tkrd|j}x |j ddD]}|j |qNWt S|t kr|t dt||StSdS)Nz%acceptNode() returned illegal value: )rrrHrrrr{rrrZ appendChildrrr=rr)rrrrparentrrrrrs   z%FilterVisibilityController.acceptNodeN)rH)#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_NOTATIONrrrrrrIs" rIc@seZdZdZddZdS) FilterCrutch_builder_level _old_start_old_endcCs6d|_||_|j}|j|_|j|_|j|_|j|_dS)Nr) rrrLr\rr^rrr])rr5rtrrrrszFilterCrutch.__init__N)rrrr)r0r1r2r3rrrrrrsrc@s(eZdZfZddZddZddZdS)rcCs0tj|||j}xdD]}t||dqWdS)Nr`rgrmrirkrp)r`rgrmrirkrp)rrrLsetattr)rr5rtr>rrrrs zRejecter.__init__cGs|jd|_dS)Nr)r)rargsrrrrszRejecter.start_element_handlercGs@|jdkr0|jj}|jj||j|_|j|_n |jd|_dS)Nrr)rrrLrRrr\rr^)rrrtrrrr] s    zRejecter.end_element_handlerN)r0r1r2r3rrr]rrrrrs rc@s eZdZfZddZddZdS)rcGs.|jj}|j||jj|k r*|jd|_dS)Nr)rrVrr)rrrrrrr-s  zSkipper.start_element_handlercGsD|jdkr*|j|jj_|j|jj_d|_n|jd|_|j|dS)Nrr)rrrrLr\rr^)rrrrrr]3s     zSkipper.end_element_handlerN)r0r1r2r3rr]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|_||_tj||dS)N)rroriginalDocumentrrrFr)rrrNrrrr[s  zFragmentBuilder.__init__cCstj|d|_dS)N)rFrMfragment)rrrrrMds zFragmentBuilder.resetcCs|j|jS)zRParse a document fragment from a file object, returning the fragment node.)rrw)rr|rrrrhszFragmentBuilder.parseFilec Cs||_|j}|jj}d}|rV|jp*|j}|jrDd|j|jf}qZ|jrZd|j}nd}|j}t |||f}y|j |dWn|j YnX|j }|j |S)zMParse a document fragment from a string, returning the fragment node.rvzPUBLIC "%s" "%s"z SYSTEM "%s"r) _sourcerSrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATErxrMr) rrrtrZidentrZnsattrsrUrrrrrms, zFragmentBuilder.parseStringcCs|jjj}d}|rxbt|jjD]R}|jj|}|r>|d}d||jf}|jrfd||j|j f}q"d||j f}q"Wxt|j jD]}|j j|}|r|d}d||jf}|jrd||j|j f}n&|j rd||j f}nd ||j j f}|j rd ||j f}|d }qW|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. rvz z%sz%s SYSTEM "%s">z %s)rrrrrZlengthitemnodeNamerrrZ firstChildrr)rrr6rZnotationr rrrrs6    z FragmentBuilder._getDeclarationscCsdS)Nrvr)rrrrrszFragmentBuilder._getNSattrsc Cs~|tkrh|j}|j}|jj|}|j|_|jj|_|j|_z|j|j dWd||_||_d|_ XdSt j |||||SdS)Nrr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrUrVrLZExternalEntityParserCreaterZcreateDocumentFragmentrrxrrFro)rrrrrZ old_documentZ old_cur_nodertrrrros  z+FragmentBuilder.external_entity_ref_handler)N) r0r1r2rrrMrrrrrorrrrrRs $rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) Namespacesz7Mix-in class for builders; adds support for namespaces.cCs g|_dS)N)_ns_ordered_prefixes)rrrr_initNamespacesszNamespaces._initNamespacescCstjdd}d|_|S)z'Create a new namespace-handling parser.r8)Znamespace_separatorT)rrOZnamespace_prefixes)rrtrrrrPs zNamespaces.createParsercCs tj|||jjr|j|_dS)z.Insert the namespace-handlers onto the parser.N)rFrRrGZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandler)rrtrrrrRs zNamespaces.installcCs|jj||fdS)z/Push this namespace declaration on our storage.N)rr)rrCrArrrrsz'Namespaces.start_namespace_decl_handlercCsd|krt||\}}}}nt}|}d}t}tj||||}|j|_t|j|||_|j rxZ|j D]P\}}|rtj t |d|t |d}ntj dt dt}||_ |j|_t||qdW|j dd=|r|j|j} |j} xtdt|dD]} || } || d} d| krJt|| \}}}}tj ||||}|| |<|| ||f<n$tj | t| t}|| | <|| t| f<|j|_| |_ ||_qWdS)Nr8zxmlns:xmlnsrrr)rErrrZElementrUrr rVrrr7rrr Z_ensure_attributes_attrs_attrsNSrr<Z ownerElement)rr>rrArBrCrDrrrrrr$rrrrrsP      z Namespaces.start_element_handlercCs|j}d|krHt||\}}}}|j|kr>|j|kr>|j|ksltdn$|j|ksZtd|jtksltd|j|_|j |dS)Nr8z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rVrEr(r)rCr:rrrr)rr>rVrArBrCrDrrrr]s     zNamespaces.end_element_handlerN) r0r1r2rrrPrRrrr]rrrrrs5rc@seZdZdZddZdS)ExpatBuilderNSz*Document builder that supports namespaces.cCstj||jdS)N)rFrMr)rrrrrM)s zExpatBuilderNS.resetN)r0r1r2rrMrrrrr&src@s eZdZdZddZddZdS)FragmentBuilderNSz*Fragment builder that supports namespaces.cCstj||jdS)N)rrMr)rrrrrM1s zFragmentBuilderNS.resetcCsd}|j}g}xv|rt|dr|x\|jjD]N\}}||krrrrrtrrrrYps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs,dj|jjddjdd}||_tdS)Nrvz   )joinrreplacer{)rr6rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCs tdS)N)r{)rr>rrrrrsz-InternalSubsetExtractor.start_element_handler) r0r1r2rrrrrrRrYrrrrrrrWs rTc CsL|r t}nt}t|tr>t|d}|j|}WdQRXn |j|}|S)z{Parse a document, returning the resulting Document node. 'file' may be either a file name or an open file object. rbN)rrF isinstancestropenr)r| namespacesr5fpresultrrrparses   rcCs|r t}nt}|j|S)zOParse a document from a string, returning the resulting Document node. )rrFr)rrr5rrrrsrc CsP|rt|}nt|}t|trBt|d}|j|}WdQRXn |j|}|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)r|rrr5rrrrr parseFragments    rcCs |rt|}nt|}|j|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)rrrr5rrrparseFragmentStrings rcCs|jrt|St|SdS)z,Create a builder based on an Options object.N)rrrF)rNrrr makeBuildersr)T)T)T)T)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomr r Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrTZTypeInfor"objectrr7rErFrrIrrrrrrrrr Exceptionr{rrrrrrrrrrs^          3C :  u_%,