o ?Og @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_handlerProcessingInstructionHandlerrJrentity_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_handlerZAttlistDeclHandlerrparserrrrrUs$  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) rVreadParserXdocumentElement _setup_subset ParseEscaperPrO)rfilerxZ 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)rroriginalDocumentrrrIr)rrrQrrrr[s zFragmentBuilder.__init__cCst|d|_dSr)rIrPfragmentrrrrrPds  zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)rrzrrrrrrhszFragmentBuilder.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) _sourcerVrrr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATEr{rPr) rrrxrZidentrZnsattrsrXrrrrrms0  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)rrrrrZlengthitemZnodeNamerrrZ firstChildrr)rrr9rZnotationr 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_IDrXrYrOZExternalEntityParserCreaterZcreateDocumentFragmentrr{rrIrr)rrrrrZ old_documentZ old_cur_noderxrrrrrs&   z+FragmentBuilder.external_entity_ref_handlerr) r3r4r5rrrPrrrrrrrrrrrRs  $ rc@s8eZdZdZddZddZddZdd Zd d ZdS) 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)rrRZnamespace_prefixesrwrrrrSs zNamespaces.createParsercCs$t|||jjr|j|_dSdS)z.Insert the namespace-handlers onto the parser.N)rIrUrJZnamespace_declarationsstart_namespace_decl_handlerZStartNamespaceDeclHandlerrwrrrrUs zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)rr)rrFrDrrrrsz'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")rHrrrZElementrXrr rYrrr:rrr Z_ensure_attributes_attrs_attrsNSrr?Z ownerElement)rrArrDrErFrGrrrrrr'rrrrrsZ     z Namespaces.start_element_handlerFcCs6|j}d|vrt||\}}}}|j|_||dS)Nr;)rYrHrr)rrArYrDrErFrGrrrr`s  zNamespaces.end_element_handlerN) r3r4r5rrrSrUrrr`rrrrrs/rc@r)ExpatBuilderNSz*Document builder that supports namespaces.cCt||dSr)rIrPrrrrrrP)  zExpatBuilderNS.resetN)r3r4r5rrPrrrrr&rrc@r)FragmentBuilderNSz*Fragment builder that supports namespaces.cCrr)rrPrrrrrrP1rzFragmentBuilderNS.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)rattrsrrrFrDZdeclnamerrrr5s$   zFragmentBuilderNS._getNSattrsN)r3r4r5rrPrrrrrr.s rc@seZdZdZdS)r~zEException raised to short-circuit parsing in InternalSubsetExtractor.N)r3r4r5rrrrrr~Ssr~c@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)rIrr~rrrrr`  z!InternalSubsetExtractor.parseFilecCrr)rIrr~)rrrrrrfrz#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr)r\r]rr_rwrrrrUls zInternalSubsetExtractor.installcCs.|r|}g|_|jj|_|j|_dStr)rVrrZDefaultHandlerrrr~)rrArrrrxrrrr\ps   z2InternalSubsetExtractor.start_doctype_decl_handlercCs(d|jdddd}||_t)Nrz   )joinrreplacer~)rr9rrrrzsz0InternalSubsetExtractor.end_doctype_decl_handlercCstr)r~)rrArrrrrr,z-InternalSubsetExtractor.start_element_handler) r3r4r5rrrrrrUr\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)rrI isinstancestropenr)r namespacesr8fpresultrrrparses     rcCs|rt}nt}||S)zOParse a document from a string, returning the resulting Document node. )rrIr)rrr8rrrrs 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)rrrr8rrrrr 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)rrrr8rrrparseFragmentStrings  rcCs|jrt|St|S)z,Create a builder based on an Options object.)rrrI)rQrrr makeBuildersr)T)3rZxml.domrrrrrrZ xml.parsersrZxml.dom.minidomr r Zxml.dom.NodeFilterr rrrZDOMBuilderFilterrrrrZgetDOMImplementationrWZTypeInfor%objectrr:rHrIrrLrrrrrrrrr Exceptionr~rrrrrrrrrrsh            3 C:   u_% ,