BMa@sdZddlmZmZmZddlmZmZmZddlm Z ddl m Z m Z ddl mZejZejZejZejjZejjZejjZejjZejZdejdd d ejdd d ejdd dejdddejdddejdddejdddejdddejddi ZGdddeZddZddZGd d!d!ZeeefZ Gd"d#d#eZ!Gd$d%d%eZ"Gd&d'd'e"Z#Gd(d)d)e"Z$d*Z%d+e%Z&Gd,d-d-eZ'Gd.d/d/Z(Gd0d1d1e(eZ)Gd2d3d3e(e'Z*Gd4d5d5e+Z,Gd6d7d7eZ-d8d9d:Z.d8d;d<Z/d8d=d>Z0d8d?d@Z1dAdBZ2dS)CzFacility 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) NodeFilterZCDATANZcdataENUMZ enumerationZENTITYentityZENTITIESentitiesIDidZIDREFZidrefZIDREFSZidrefsZNMTOKENZnmtokenZNMTOKENSZnmtokensc@seZdZdZdddZddZd d Zd d Zd dZddZ ddZ ddZ ddZ dS) ElementInfo _attr_info_modeltagNameNcCs||_g|_||_dS)N)rrr)selfrmodelr9/opt/alt/python35/lib64/python3.5/xml/dom/expatbuilder.py__init__?s  zElementInfo.__init__cCs|j|j|jfS)N)rrr)rrrr __getstate__DszElementInfo.__getstate__cCs|\|_|_|_dS)N)rrr)rstaterrr __setstate__GszElementInfo.__setstate__cCsYxO|jD]D}|d|kr |d}|ddkrBtdSt|dSq WtjS)Nr(r r )r _typeinfo_mapr_no_type)ranameinfotrrrgetAttributeTypeJs zElementInfo.getAttributeTypecCstjS)N)rr")r namespaceURI localNamerrrgetAttributeTypeNSTszElementInfo.getAttributeTypeNScCs:|jr2|jd}|tjjtjjfkSdSdS)NrF)rrrZ XML_CTYPE_ANYZXML_CTYPE_MIXED)rtyperrrisElementContentWs    zElementInfo.isElementContentcCs(|jr |jdtjjkSdSdS)NrF)rrrZXML_CTYPE_EMPTY)rrrrisEmpty_s zElementInfo.isEmptycCs6x/|jD]$}|d|kr |ddkSq WdS)NrrrFr )r)rr#r$rrrisIdeszElementInfo.isIdcCs|j||fS)N)r-)rZeuriZenameZaurir#rrrisIdNSkszElementInfo.isIdNS)rrr) __name__ __module__ __qualname__ __slots__rrrr&r)r+r,r-r.rrrrr<s       rcCs|j||S)N)_intern_setdefault)buildersrrr_internosr6cCsd|kst|jd}|j}t|dkr|\}}}|||}d||f}|||}|||}nJt|dkr|\}}t}|||}}ntd|||||||fS)N z%s:%srz4Unsupported syntax: spaces in URIs not supported: %r)AssertionErrorsplitr3lenr ValueError)r4namepartsinternuri localnameprefixqnamerrr_parse_ns_namers  rDc@sieZdZdZdddZddZddZd 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.NcCst|dkrtj}||_|jjdk rKt|jj|_nd|_t|_d|_|j dS)N) rZOptions_optionsfilterFilterVisibilityController_filterr_finish_start_element_parserreset)roptionsrrrrs      zExpatBuilder.__init__cCs tjS)zCreate a new parser object.)r ParserCreate)rrrr createParserszExpatBuilder.createParsercCse|js^|j|_|jjj|_d|j_d|j_d|j_|j|j|jS)z7Return the parser object, creating a new one if needed.T) rKrOr? setdefaultr3Z 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)rrrrrLs  zExpatBuilder.resetcCs|j|_|j|_|j|_|j|_|jj rH|j |_ |j |_ |jjrl|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_handlerProcessingInstructionHandlerrFrentity_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)rparserrrrrQs$                zExpatBuilder.installc Cs|j}d}yfxO|jd}|s.P|j|d|r]|jjr]|j|d}qW|jddWntk rYnX|j}|jd|_|S) zIParse a document from a file object, returning the document node.TirFNi@) rRreadParserTdocumentElement _setup_subset ParseEscaperLrK)rfilersZ first_bufferbufferdocrrr parseFiles$       zExpatBuilder.parseFilec Csb|j}y!|j|d|j|Wntk rAYnX|j}|jd|_|S)ztj|||j}xdD]}t||dq WdS)Nr_rfrlrhrjro)r_rfrlrhrjro)rrrKsetattr)rr4rsr=rrrrs  zRejecter.__init__cGs|jd|_dS)Nr)r)rargsrrrrszRejecter.start_element_handlercGsZ|jdkrF|jj}|jj||j|_|j|_n|jd|_dS)Nrr)rrrKrQrr[rr])rrrsrrrr\ s   zRejecter.end_element_handlerN)r/r0r1r2rrr\rrrrrs  rc@s.eZdZfZddZddZdS)rcGs?|jj}|j||jj|k r;|jd|_dS)Nr)rrUrr)rrrrrrr-s  zSkipper.start_element_handlercGs`|jdkr?|j|jj_|j|jj_d|_n|jd|_|j|dS)Nrr)rrrrKr[rr])rrrrrr\3s  zSkipper.end_element_handlerN)r/r0r1r2rr\rrrrr*s  rz8http://xml.python.org/entities/fragment-builder/internalz %%s ]> &fragment-builder-internal;c@smeZdZdZdddZddZddZd 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. NcCsM|jtkr$||_||_n|j|_||_tj||dS)N)rroriginalDocumentrrrEr)rrrMrrrr[s     zFragmentBuilder.__init__cCstj|d|_dS)N)rErLfragment)rrrrrLds zFragmentBuilder.resetcCs|j|jS)zRParse a document fragment from a file object, returning the fragment node.)rrv)rr{rrrr~hszFragmentBuilder.parseFilec Cs||_|j}|jj}d}|r}|jp?|j}|jrdd|j|jf}q|jrd|j}nd}|j}t |||f}y|j |dWn|j YnX|j }|j |S)zMParse a document fragment from a string, returning the fragment node.ruzPUBLIC "%s" "%s"z SYSTEM "%s"r) _sourcerRrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATErwrLr) rrrsrZidentrZnsattrsrTrrrrrms,         zFragmentBuilder.parseStringcCs{|jjj}d}|rwxt|jjD]s}|jj|}|rV|d}d||jf}|jrd||j|j f}q.d||j f}q.Wxt|j jD]}|j j|}|r|d}d||jf}|jrd||j|j f}n5|j r7d||j f}nd ||j j f}|j rid ||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. ruz z%sz%s SYSTEM "%s">z %s)rrrrrZlengthitemnodeNamerrrZ firstChildrr)rrr5rZnotationr rrrrs6      z FragmentBuilder._getDeclarationscCsdS)Nrur)rrrrrszFragmentBuilder._getNSattrsc Cs|tkr|j}|j}|jj|}|j|_|jj|_|j|_z|j|j dWd||_||_d|_ XdSt j |||||SdS)Nrr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrTrUrKZExternalEntityParserCreaterZcreateDocumentFragmentrrwrrErn)rrrrrZ old_documentZ old_cur_nodersrrrrns        z+FragmentBuilder.external_entity_ref_handler) r/r0r1rrrLr~rrrrnrrrrrRs     $ 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.Znamespace_separatorr7T)rrNZnamespace_prefixes)rrsrrrrOs zNamespaces.createParsercCs,tj|||jjr(|j|_dS)z.Insert the namespace-handlers onto the parser.N)rErQrFZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandler)rrsrrrrQs zNamespaces.installcCs|jj||fdS)z/Push this namespace declaration on our storage.N)rr)rrBr@rrrrsz'Namespaces.start_namespace_decl_handlercCs'd|kr*t||\}}}}nt}|}d}t}tj||||}|j|_t|j|||_|j rx|j D]t\}}|rtj t |d|t |d}ntj dt dt}||_ |j|_t||qW|j dd=|r#|j|j} |j} xtdt|dD]} || } || d} d| krt|| \}}}}tj ||||}|| |<|| ||fs\        3  B:  u_%,