3 ^c @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) NodeFilterNZcdataZ enumerationentityentitiesidZidrefZidrefsZnmtokenZnmtokens) ZCDATAENUMZENTITYZENTITIESIDZIDREFZIDREFSZNMTOKENZNMTOKENSc@sZeZdZdZdddZddZd d Zd d Zd dZddZ ddZ ddZ ddZ dS) ElementInfo _attr_info_modeltagNameNcCs||_g|_||_dSN)rrr)selfrmodelr,/usr/lib64/python3.6/xml/dom/expatbuilder.py__init__?szElementInfo.__init__cCs|j|j|jfSrrrrrrrr __getstate__DzElementInfo.__getstate__cCs|\|_|_|_dSrr)rZstaterrr __setstate__GrzElementInfo.__setstate__cCsJxB|jD]8}|d|kr|d}|ddkr4tdSt|dSqWtjS)Nrz(rr!)r _typeinfo_mapr_no_type)ranameinfoZtrrrgetAttributeTypeJs   zElementInfo.getAttributeTypecCstjSr)rr#)r namespaceURI localNamerrrgetAttributeTypeNSTrzElementInfo.getAttributeTypeNScCs,|jr$|jd}|tjjtjjfkSdSdSNrF)rrrZ XML_CTYPE_ANYZXML_CTYPE_MIXED)rtyperrrisElementContentWs   zElementInfo.isElementContentcCs |jr|jdtjjkSdSdSr*)rrrZXML_CTYPE_EMPTYrrrrisEmpty_szElementInfo.isEmptycCs,x&|jD]}|d|kr|ddkSqWdS)Nrr rFr!)r)rr$r%rrrisIdes  zElementInfo.isIdcCs|j||fSr)r.)rZeuriZenameZaurir$rrrisIdNSkzElementInfo.isIdNSrr) __name__ __module__ __qualname__ __slots__rrrr&r)r,r-r.r/rrrrr<s  rcCs |j||Sr)_intern_setdefault)buildersrrr_internorr8cCsd|ks t|jd}|j}t|dkr^|\}}}|||}d||f}|||}|||}n4t|dkr|\}}t}|||}}n td|||||||fS)N iz%s:%sr z4Unsupported syntax: spaces in URIs not supported: %r)AssertionErrorZsplitr5lenr ValueError)r6nameZpartsinternuri localnameprefixqnamerrr_parse_ns_namers          rCc@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 dSr) rZOptions_optionsfilterFilterVisibilityController_filterr _finish_start_element_parserreset)roptionsrrrrs zExpatBuilder.__init__cCstjS)zCreate a new parser object.)r ParserCreaterrrr createParserr0zExpatBuilder.createParsercCsF|js@|j|_|jjj|_d|j_d|j_d|j_|j|j|jS)z7Return the parser object, creating a new one if needed.T) rJrNr>Z setdefaultr5Z buffer_textZordered_attributesZspecified_attributesinstallrrrr getParsers   zExpatBuilder.getParsercCs,tjtdd|_|j|_|jj|_d|_dS)z6Free all data structures used during DOM construction.NF)theDOMImplementationZcreateDocumentrdocumentcurNode _elem_info_cdatarrrrrKs   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_handlerProcessingInstructionHandlerrEr 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_handlerZAttlistDeclHandlerrparserrrrrOs$ 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.TiirFNi@) rPreadParserRdocumentElement _setup_subset ParseEscaperKrJ)rfilerrZ first_bufferbufferdocrrr parseFiles$    zExpatBuilder.parseFilec CsP|j}y|j|d|j|Wntk r6YnX|j}|jd|_|S)z|jj|}t|j||jr:|jj|tkr:|jj|dSr)rRZ createCommentrrSrHrrr)rrrrrrrcIs  zExpatBuilder.comment_handlercCsd|_d|_dS)NTFrUrrrrrreOz(ExpatBuilder.start_cdata_section_handlercCsd|_d|_dS)NFrrrrrrgSrz&ExpatBuilder.end_cdata_section_handlercCdSNrr)rcontextrrrrrrrlWrz(ExpatBuilder.external_entity_ref_handlercCs4|jdkr|j rt|_|j|j_|j||dSr)rHrTr rstart_element_handlerrPrY)rr= attributesrrrrXZs z"ExpatBuilder.first_element_handlercCs|jj|}t|j|||_|rtxPtdt|dD]<}tj||tdt }||d}||_ |j|_ t ||q4W||jj k r|j|dS)Nrr r)rRZ createElementrrSranger;rAttrrrrrr rvrI)rr=rriarrrrr`s     z"ExpatBuilder.start_element_handlercCsj|jrf||jjkrdS|jj|}|tkr4t|n|tkrFt|ndS|j|_ |jj ||j dSr) rHrRrvstartContainerrRejecter FILTER_SKIPSkipper parentNoderSrunlink)rrZfiltrrrrIqs     z"ExpatBuilder._finish_start_elementcCs|j}|j|_|j|dSr)rSrr)rr=rSrrrrZsz ExpatBuilder.end_element_handlercCs\|jj|j}|r|j|||jrX||jjkr4dS|jj|tkrX|j j ||j dSr) rTgetr_handle_white_text_nodesrHrRrvrrrSrr)rrSr%rrrrs   z ExpatBuilder._finish_end_elementcCsf|jjs|j rdSg}x.|jD]$}|jtkr"|jj r"|j|q"Wx|D]}|j |qPWdSr) rEZwhitespace_in_element_contentr,rrrrZstriprr)rrr%Lrrrrrs   z%ExpatBuilder._handle_white_text_nodescCs>|jj|}|dkr&t|||j|<n|jdks4t||_dSr)rTrrrr:)rr=rr%rrrros  z!ExpatBuilder.element_decl_handlerc CsF|jj|}|dkr&t|}||j|<|jjd|dd|d||gdSNr)rTrrrr)rZelemr=r+ZdefaultZrequiredr%rrrrps   z!ExpatBuilder.attlist_decl_handlercCs2||j_||j_|dkr.|r&d|j_nd|j_dS)NrTF)rRversionencoding standalone)rrrrrrrrns  zExpatBuilder.xml_decl_handlerr) r1r2r3__doc__rrNrPrKrOr|r~rwrVrr\rirkr^r`rcrergrlrXrrIrZrrrorprnrrrrrDs:     rDc@seZdZdZd ZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) rGzoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.rFcCs ||_dSrrF)rrFrrrrrz#FilterVisibilityController.__init__cCsT|j|j}|jj|@rL|jj|}|tkr0t|tkrHtdt ||St SdS)Nz)startContainer() returned illegal value: ) _nodetype_maskrrF whatToShowrFILTER_INTERRUPTrx_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: )rrrFrrrrxrrrZ appendChildrrr<rr)rrrrZparentrrrrrs   z%FilterVisibilityController.acceptNodeNr)#r1r2r3rr4rrrrZ 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_NOTATIONrrrrrrGs" rGc@seZdZdZddZdS) FilterCrutch_builder_level _old_start_old_endcCs6d|_||_|j}|j|_|j|_|j|_|j|_dSr) rrrJrYrr[rrrZ)rr6rrrrrrszFilterCrutch.__init__N)rrrr)r1r2r3r4rrrrrrrc@s(eZdZfZddZddZddZdS)rcCs0tj|||j}xdD]}t||dqWdS)Nr]rdrjrfrhrm)r]rdrjrfrhrm)rrrJZsetattr)rr6rrr=rrrrs zRejecter.__init__cGs|jd|_dSr)rrargsrrrrrzRejecter.start_element_handlercGs@|jdkr0|jj}|jj||j|_|j|_n |jd|_dSNrr)rrrJrOrrYrr[)rrrrrrrrZ s    zRejecter.end_element_handlerN)r1r2r3r4rrrZrrrrrs rc@s eZdZfZddZddZdS)rcGs.|jj}|j||jj|k r*|jd|_dSr)rrSrr)rrrrrrr-s  zSkipper.start_element_handlercGsD|jdkr*|j|jj_|j|jj_d|_n|jd|_|j|dSr)rrrrJrYrr[rrrrrZ3s     zSkipper.end_element_handlerN)r1r2r3r4rrZrrrrr*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||dSr)rroriginalDocumentrrrDr)rrrLrrrr[s  zFragmentBuilder.__init__cCstj|d|_dSr)rDrKfragmentrrrrrKd zFragmentBuilder.resetcCs|j|jS)zRParse a document fragment from a file object, returning the fragment node.)r~rtrryrrrr|hzFragmentBuilder.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.rszPUBLIC "%s" "%s"z SYSTEM "%s"r) _sourcerPrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATErurKr) rr}rrrZidentrZnsattrsrRrrrrr~ms, 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. rsz z%sz%s SYSTEM "%s">z %s)rrrrrZlengthZitemnodeNamerrr Z firstChildrr)rrr7rZnotationr rrrrs6    z FragmentBuilder._getDeclarationscCr)NrsrrrrrrrzFragmentBuilder._getNSattrsc Cs~|tkrh|j}|j}|jj|}|j|_|jj|_|j|_z|j|j dWd||_||_d|_ XdSt j |||||SdSr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrRrSrJZExternalEntityParserCreaterZcreateDocumentFragmentrrurrDrl)rrrrrZ old_documentZ old_cur_noderrrrrrls  z+FragmentBuilder.external_entity_ref_handlerr) r1r2r3rrrKr|r~rrrlrrrrrRs $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.r9)Znamespace_separatorT)rrMZnamespace_prefixesrqrrrrNs zNamespaces.createParsercCs tj|||jjr|j|_dS)z.Insert the namespace-handlers onto the parser.N)rDrOrEZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerrqrrrrOs zNamespaces.installcCs|jj||fdS)z/Push this namespace declaration on our storage.N)rr)rrAr?rrrrr0z'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)Nr9xmlns:xmlnsrr r)rCrrrZElementrRrrrSrrr8rrr Z_ensure_attributes_attrs_attrsNSrr;Z ownerElement)rr=rr?r@rArBrrrrrr$rrrrrsP      z Namespaces.start_element_handlercCs|j}d|krHt||\}}}}|j|kr>|j|kr>|j|ksltdn$|j|ksZtd|jtksltd|j|_|j |dS)Nr9z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rSrCr'r(rAr:rrrr)rr=rSr?r@rArBrrrrZs     zNamespaces.end_element_handlerN) r1r2r3rrrNrOrrrZrrrrrs5rc@seZdZdZddZdS)ExpatBuilderNSz*Document builder that supports namespaces.cCtj||jdSr)rDrKrrrrrrK)rzExpatBuilderNS.resetN)r1r2r3rrKrrrrr&rrc@s eZdZdZddZddZdS)FragmentBuilderNSz*Fragment builder that supports namespaces.cCrr)rrKrrrrrrK1rzFragmentBuilderNS.resetcCsd}|j}g}xv|rt|dr|x\|jjD]N\}}||krt|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)rrD isinstancestropenr|)ry namespacesr6fpresultrrrparses   rcCs|r t}nt}|j|S)zOParse a document from a string, returning the resulting Document node. )rrDr~)r}rr6rrrr~sr~c 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|)ryrrr6rrrrr 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~)r}rrr6rrrparseFragmentStrings rcCs|jrt|St|SdS)z,Create a builder based on an Options object.N)rrrD)rLrrr makeBuildersrTrrr)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomrr Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrQZTypeInfor"Zobjectrr8rCrDrrGrrrrrrrrrZ Exceptionrxrrr~rrrrrrrZs^          3C :  u_%,