o [d @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_handlerProcessingInstructionHandlerr_rentity_decl_handlerEntityDeclHandlernotation_decl_handlerNotationDeclHandlercommentscomment_handlerCommentHandlercdata_sectionsstart_cdata_section_handlerStartCdataSectionHandlerend_cdata_section_handlerEndCdataSectionHandlercharacter_data_handler_cdataCharacterDataHandlercharacter_data_handlerexternal_entity_ref_handlerExternalEntityRefHandlerxml_decl_handlerXmlDeclHandlerelement_decl_handlerElementDeclHandlerattlist_decl_handlerAttlistDeclHandlerr$parserr&r&r'rms$  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) rnreadParserqdocumentElement _setup_subset ParseEscapererd)r$filer first_bufferbufferdocr&r&r' parseFiles(     zExpatBuilder.parseFilecCsN|}z ||d||Wn tyYnw|j}|d|_|S)zdefaultrequiredr4r&r&r'rs  z!ExpatBuilder.attlist_decl_handlercCs8||j_||j_|dkr|rd|j_dSd|j_dSdS)NrTF)rqversionencoding standalone)r$rrrr&r&r'rs  zExpatBuilder.xml_decl_handlerr#) rGrHrI__doc__r(rhrnrermrrrrurr{rrr}rrrrrrwrrcryrrrrrr&r&r&r'r]s<      r]c@seZdZdZdZddZddZddZej e j ej e j eje jeje jeje jeje jeje jeje jeje jeje jeje jej e j!i Z"d S) razoWrapper around a DOMBuilderFilter which implements the checks to make the whatToShow filter attribute work.r`cCs ||_dSr#r)r$r`r&r&r'r(s z#FilterVisibilityController.__init__cCsP|j|j}|jj|@r&|j|}|tkrt|tvr$tdt ||St S)Nz)startContainer() returned illegal value: ) _nodetype_maskrr` whatToShowrFILTER_INTERRUPTr_ALLOWED_FILTER_RETURNSrTreprr)r$rmaskvalr&r&r'rs    z)FilterVisibilityController.startContainercCs|j|j}|jj|@r>|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#)rr originalDocumentrrr]r()r$rrfr&r&r'r([s zFragmentBuilder.__init__cCst|d|_dSr#)r]refragmentr)r&r&r'reds  zFragmentBuilder.resetcCs||S)zRParse a document fragment from a file object, returning the fragment node.)rrr$rr&r&r'rhszFragmentBuilder.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) _sourcernr rr_getDeclarationsrr _getNSattrs_FRAGMENT_BUILDER_TEMPLATErrer!) r$rrridentrnsattrsrqr!r&r&r'rms0  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. r#z z%sz%s SYSTEM "%s">z %s)rrrrrlengthitemnodeNamerrr firstChildrr)r$rrMrnotationrr&r&r'r%s:      z FragmentBuilder._getDeclarationscCr)Nr#r&r)r&r&r'r&rzFragmentBuilder._getNSattrscCs|tkr<|j}|j}|j|}|j|_|j|_|j|_z||j dW||_||_d|_ dS||_||_d|_ wt |||||S)NTr) $_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDrqrrrdExternalEntityParserCreater createDocumentFragmentr!rr$r]r)r$rrrr old_document old_cur_noderr&r&r'rs&   z+FragmentBuilder.external_entity_ref_handlerr#) rGrHrIrr(rerrr%r&rr&r&r&r'rRs  $ rc@sBeZdZdZddZddZddZdd Zd d Z d dZ dS) Namespacesz7Mix-in class for builders; adds support for namespaces.cCs g|_dSr#)_ns_ordered_prefixesr)r&r&r'_initNamespacess zNamespaces._initNamespacescCstjdd}d|_|S)z'Create a new namespace-handling parser.rO)namespace_separatorT)rrgnamespace_prefixesrr&r&r'rhs zNamespaces.createParsercCs$t|||jjr|j|_dSdS)z.Insert the namespace-handlers onto the parser.N)r]rmr_namespace_declarationsstart_namespace_decl_handlerStartNamespaceDeclHandlerrr&r&r'rms zNamespaces.installcCs|j||fdS)z/Push this namespace declaration on our storage.N)r6r)r$rZrXr&r&r'r;sz'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)NrOxmlns:xmlnsrrQr.)r\rrrElementrqrr rrr6rrNrrr _ensure_attributes_attrs_attrsNSrrS ownerElement)r$rUrrXrYrZr[rrrArBrr3rr&r&r'rsZ     z Namespaces.start_element_handlerTcCs|j}d|vr$t||\}}}}|j|kr|j|kr|j|ks#Jdn|j|ks-Jd|jtks6Jd|j|_||dS)NrOz$element stack messed up! (namespace)z&element stack messed up - bad nodeNamez*element stack messed up - bad namespaceURI) rrr\r7r8rZr-rrr)r$rUrrrXrYrZr[r&r&r'rys      zNamespaces.end_element_handlerN) rGrHrIrr7rhrmr;rryr&r&r&r'r5s/ r5c@r)ExpatBuilderNSz*Document builder that supports namespaces.cCt||dSr#)r]rer7r)r&r&r're)  zExpatBuilderNS.resetN)rGrHrIrrer&r&r&r'rD&rrDc@r)FragmentBuilderNSz*Fragment builder that supports namespaces.cCrEr#)rrer7r)r&r&r're1rFzFragmentBuilderNS.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_urir=r>z%s %s='%s'z %s='%s')rhasattrrHitemsrr)r$attrsrrrZrXdeclnamer&r&r'r&5s$   zFragmentBuilderNS._getNSattrsN)rGrHrIrrer&r&r&r&r'rG.s rGc@seZdZdZdS)rzEException raised to short-circuit parsing in InternalSubsetExtractor.N)rGrHrIrr&r&r&r'rSsrc@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.)rr)r&r&r'r\sz!InternalSubsetExtractor.getSubsetcC(z t||WdStyYdSwr#)r]rrr"r&r&r'r`  z!InternalSubsetExtractor.parseFilecCrMr#)r]rr)r$rr&r&r'rfrNz#InternalSubsetExtractor.parseStringcCs|j|_|j|_dSr#)rurvrrxrr&r&r'rmls zInternalSubsetExtractor.installcCs.|r|}g|_|jj|_|j|_dStr#)rnrrDefaultHandlerrrr)r$rUrrrrr&r&r'rups   z2InternalSubsetExtractor.start_doctype_decl_handlercCs(d|jdddd}||_t)Nr#z   )joinrreplacer)r$rMr&r&r'rzsz0InternalSubsetExtractor.end_doctype_decl_handlercCstr#)r)r$rUrKr&r&r'rr:z-InternalSubsetExtractor.start_element_handler) rGrHrIrrrrrrmrurrr&r&r&r'rWs 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)rDr] isinstancestropenr)r namespacesrLfpresultr&r&r'parses     r[cCs|rt}nt}||S)zOParse a document from a string, returning the resulting Document node. )rDr]r)rrXrLr&r&r'rs 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. rTN)rGrrUrVrWr)rrrXrLrYrZr&r&r' parseFragments      r\cCs |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. )rGrr)rrrXrLr&r&r'parseFragmentStrings  r]cCs|jrt|St|S)z,Create a builder based on an Options object.)rXrDr])rfr&r&r' makeBuildersr^)T)3rxml.domrrrrrr xml.parsersrxml.dom.minidomr r xml.dom.NodeFilterr rrr DOMBuilderFilterrrrrgetDOMImplementationroTypeInfor1objectrrNr\r]rrarrrr0r'rr5rDrG Exceptionrrr[rr\r]r^r&r&r&r'sh            3 C:   u_% ,