o =?h @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.d;d3d4Z/d;d5d6Z0d;d7d8Z1d9d:Z2dS)Install the callbacks needed to build the DOM into the parser.N)!start_doctype_decl_handlerStartDoctypeDeclHandlerfirst_element_handlerStartElementHandlerend_element_handlerEndElementHandler pi_handlerProcessingInstructionHandlerrLrentity_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_handlerZAttlistDeclHandlerrparserrrrrWs$  zExpatBuilder.installcCs|}d}z% |d}|sn||d|r!|jjr!||d}q|ddWn ty4Ynw|j}|d|_|S)zIParse a document from a file object, returning the document node.Tr"i@FN) rXreadParserZdocumentElement _setup_subset ParseEscaperRrQ)rfilerzZ first_bufferbufferdocrrr parseFiles(     zExpatBuilder.parseFilecCsN|}z ||d||Wn tyYnw|j}|d|_|S)z|j|}|tkrt|tkr0|j}|j ddD]}| |q&t S|t vr %%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)rroriginalDocumentrrrKr)rrrSrrrr[s zFragmentBuilder.__init__cCst|d|_dSr)rKrRfragmentrrrrrRds  zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)rr|rrrrrrhszFragmentBuilder.parseFilec Cs||_|}|jj}d}|r+|jp|}|jr"d|j|jf}n |jr*d|j}nd}|}t |||f}z| |dWn| |j }| |S)zMParse a document fragment from a string, returning the fragment node.zPUBLIC "%s" "%s"z SYSTEM "%s"T) _sourcerXrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATEr}rRr) rrrzrZidentrZnsattrsrZrrrrrms0  zFragmentBuilder.parseStringcCs |jjj}d}|rt|jjD])}|j|}|r|d}d||jf}|jr1d||j|j f}qd||j f}qt|j jD]C}|j |}|rM|d}d||jf}|jrad||j|j f}n|j rld||j f}nd ||j j f}|j r~d ||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. rz z%sz%s SYSTEM "%s">z %s)rrrrrZlengthitemnodeNamerrrZ firstChildrr)rrr;rZnotationr rrrrs:      z FragmentBuilder._getDeclarationscCr)NrrrrrrrrzFragmentBuilder._getNSattrscCs|tkr<|j}|j}|j|}|j|_|j|_|j|_z||j dW||_||_d|_ dS||_||_d|_ wt |||||S)NTr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrZr[rQZExternalEntityParserCreaterZcreateDocumentFragmentrr}rrKrt)rrrrrZ old_documentZ old_cur_noderzrrrrts&   z+FragmentBuilder.external_entity_ref_handlerr) r5r6r7rrrRrrrrrtrrrrrRs  $ rc@sBeZdZdZddZddZddZdd Zd d Z d dZ dS) Namespacesz7Mix-in class for builders; adds support for namespaces.cCs g|_dSr)_ns_ordered_prefixesrrrr_initNamespacess zNamespaces._initNamespacescCstjdd}d|_|S)z'Create a new namespace-handling parser.r=)Znamespace_separatorT)rrTZnamespace_prefixesryrrrrUs zNamespaces.createParsercCs$t|||jjr|j|_dSdS)z.Insert the namespace-handlers onto the parser.N)rKrWrLZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerryrrrrWs zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)rr)rrHrFrrrrsz'Namespaces.start_namespace_decl_handlercCsd|vrt||\}}}}nt}|}d}t}t||||}|j|_t|j|||_|j r`|j D](\}}|rEt t |d|t |d}nt dt dt}||_ |j|_t||q1|j dd=|r||j} |j} tdt|dD]J} || } || d} d| vrt|| \}}}}t ||||}|| |<|| ||f<nt | t| t}|| | <|| t| f<|j|_| |_ ||_qtdSdS)Nr=xmlns:xmlnsrr?r")rJrrrZElementrZrr r[rrr<rrr Z_ensure_attributes_attrs_attrsNSrrAZ ownerElement)rrCrrFrGrHrIrrrrrr'rrrrrsZ     z Namespaces.start_element_handlerTcCs|j}d|vr$t||\}}}}|j|kr|j|kr|j|ks#Jdn|j|ks-Jd|jtks6Jd|j|_||dS)Nr=z$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) r[rJr+r,rHrrrr)rrCr[rFrGrHrIrrrrbs      zNamespaces.end_element_handlerN) r5r6r7rrrUrWrrrbrrrrrs/ rc@r)ExpatBuilderNSz*Document builder that supports namespaces.cCt||dSr)rKrRrrrrrrR)  zExpatBuilderNS.resetN)r5r6r7rrRrrrrr&rrc@r)FragmentBuilderNSz*Fragment builder that supports namespaces.cCrr)rrRrrrrrrR1rzFragmentBuilderNS.resetcCsd}|j}g}|r@t|dr;|jD]'\}}||vrq|||r(d|}nd}|r4d|||f}qd||f}q|j}|s |S)zNReturn string of namespace attributes from this element and ancestors.r_ns_prefix_urirrz%s %s='%s'z %s='%s')rhasattrritemsrr)rattrsrrrHrFZdeclnamerrrr5s$   zFragmentBuilderNS._getNSattrsN)r5r6r7rrRrrrrrr.s rc@seZdZdZdS)rzEException raised to short-circuit parsing in InternalSubsetExtractor.N)r5r6r7rrrrrrSsrc@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.getSubsetcC(z t||WdStyYdSwr)rKrrrrrrr`  z!InternalSubsetExtractor.parseFilecCrr)rKrr)rrrrrrfrz#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr)r^r_rraryrrrrWls zInternalSubsetExtractor.installcCs.|r|}g|_|jj|_|j|_dStr)rXrrZDefaultHandlerrrr)rrCrrrrzrrrr^ps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs(d|jdddd}||_t)Nrz   )joinrreplacer)rr;rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCstr)r)rrCrrrrrr.z-InternalSubsetExtractor.start_element_handler) r5r6r7rrrrrrWr^rrrrrrrWs rTcCsd|rt}nt}t|tr+t|d}||}Wd|S1s$wY|S||}|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 namespacesr:fpresultrrrparses     rcCs|rt}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrKr)rrr:rrrrs rcCsh|rt|}nt|}t|tr-t|d}||}Wd|S1s&wY|S||}|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)rrrr:rrrrr 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)rrrr:rrrparseFragmentStrings  r cCs|jrt|St|S)z,Create a builder based on an Options object.)rrrK)rSrrr makeBuildersr )T)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomr r Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrYZTypeInfor%objectrr<rJrKrrNrrrrrrrrr Exceptionrrrrrr r rrrrsh            3 C:   u_% ,