m &UIc@sdZdklZlZlZdklZlZlZdkl Z dk l Z l Z dk lZdkTeiZeiZeiZeiiZeiiZeiiZeiiZeiZhdeied<d eied <d eied <d eied<deied<deied<deied<deied<deieds       cCs|i||S(N(tbuildert_intern_setdefaultts(R:R<((Rt_internqscCs|id}|i}t|djoP|\}}}|||}d||f}|||}|||}n&|\}}t }|||}}||||||fS(Nt is%s:%s( tnametsplittpartsR:R;tinterntlenturit localnametprefixtqnamet EMPTY_PREFIX(R:R?RFRDRGRBRARE((Rt_parse_ns_namets  t ExpatBuildercBs tZdZedZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZRS(sPDocument builder that uses Expat to build a ParsedXML.DOM document instance.cCs{|djoti}n||_|iidj ot|ii|_nd|_t |_ d|_ |i dS(N( toptionsR9t xmlbuildertOptionsRt_optionstfiltertFilterVisibilityControllert_filterR t_finish_start_elementt_parsertreset(RRK((RRs     cCs tiS(sCreate a new parser object.N(R*t ParserCreate(R((Rt createParserscCsj|ipY|i|_|iii|_t|i_t|i_t|i_ |i |in|iS(s7Return the parser object, creating a new one if needed.N( RRSRVRBt setdefaultR;tTruet buffer_texttordered_attributestspecified_attributestinstall(R((Rt getParsers    cCs@titdd|_|i|_|ii|_t|_ dS(s6Free all data structures used during DOM construction.N( ttheDOMImplementationtcreateDocumenttEMPTY_NAMESPACER9RtdocumenttcurNodet _elem_infoR-t_cdata(R((RRTs  cCs|i|_|i|_|i|_|i|_ |i i o|i |_ n|i|_|i io|i|_n|i io(|i|_|i|_|i|_n |i|_|i|_|i|_|i|_ |i!|_"dS(s>Install the callbacks needed to build the DOM into the parser.N(#Rtstart_doctype_decl_handlertparsertStartDoctypeDeclHandlertfirst_element_handlertStartElementHandlertend_element_handlertEndElementHandlert pi_handlertProcessingInstructionHandlerRNRtentity_decl_handlertEntityDeclHandlertnotation_decl_handlertNotationDeclHandlertcommentstcomment_handlertCommentHandlertcdata_sectionststart_cdata_section_handlertStartCdataSectionHandlertend_cdata_section_handlertEndCdataSectionHandlertcharacter_data_handler_cdatatCharacterDataHandlertcharacter_data_handlertexternal_entity_ref_handlertExternalEntityRefHandlertxml_decl_handlertXmlDeclHandlertelement_decl_handlertElementDeclHandlertattlist_decl_handlertAttlistDeclHandler(RRf((RR\s&              cCs|i}t}yvx_|idd}|pPn|i|d|o|i i o|i |nt }qW|idtWnt j onX|i }|id|_|S(sIParse a document from a file object, returning the document node.iiiitN(RR]RfRXt first_buffertfiletreadtbuffertParseRatdocumentElementt _setup_subsetR-t ParseEscapetdocRTR9RS(RRRRRfR((Rt parseFiles(     cCsc|i}y!|i|t|i|Wntj onX|i}|i d|_ |S(s<Parse a document from a string, returning the document node.N( RR]RfRtstringRXRRRaRRTR9RS(RRRRf((Rt parseStrings    cCsF|iio5t}|i||i}||ii_ ndS(s/Load the internal subset if there might be one.N( RRatdoctypetInternalSubsetExtractort extractorRRt getSubsettsubsettinternalSubset(RRRR((RRs     cCs|iii|||}|i|_|ii i |||i_|i oT|i i |t jo;d|i_|ii d=d}d|i_d|i_n|oT|dj og|i_g|i_nd|i_d|i_|i|i_ndS(Ni(RRatimplementationtcreateDocumentTypet doctypeNametpublicIdtsystemIdRt ownerDocumentt childNodestappendRQt acceptNodet FILTER_REJECTR9RSRoRqthas_internal_subsetRt_seqt notationsRtRmtend_doctype_decl_handlertEndDoctypeDeclHandler(RRRRRR((RRes"  #       cCsT|iio|i|i_n|i|i_|ip|i p t |_ ndS(N( RRNRrRsRSRtRlRmRcRQR t_finish_end_element(R((RRs  cCs`|ii||}t|i||io-|ii |t jo|ii |ndS(N( RRatcreateProcessingInstructionttargettdatatnodet _append_childRbRQRRt removeChild(RRRR((RRl s#cCs |ii}|ioV|io-|ditjo|di|dSn|i i |}t |_n|oN|dit jo:|d}|i|}|i}||d<|dti|||i}xdD]}t||dq WdS(NRmRtR{RwRyR~(sProcessingInstructionHandlersCommentHandlersCharacterDataHandlersStartCdataSectionHandlersEndCdataSectionHandlersExternalEntityRefHandler( RRRR:RSRfR?tsetattrR9(RR:R?Rf((RRs  cGs|id|_dS(Ni(RR(Rtargs((RR$scGs\|idjo8|ii}|ii||i|_|i|_ n|id|_dS(Nii( RRRRSRfR\RRiRRk(RRRf((RRj's   (R6R7R8RRRj(((RRs RcBs tZfZdZdZRS(NcGsD|ii}|i||ii|j o|id|_ndS(Ni(RRRbRRRR(RRR((RR4s  cGsb|idjo1|i|ii_|i|ii_d|_n|id|_|i|dS(Nii( RRRRRSRiRRkR9R(RR((RRj:s  (R6R7R8RRj(((RR1s s8http://xml.python.org/entities/fragment-builder/internals %%s ]> &fragment-builder-internal;tFragmentBuildercBsPtZdZedZdZdZdZdZdZ dZ RS(sBuilder 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. cCsO|itjo||_||_n|i|_||_ti||dS(N( RRRRtoriginalDocumentRRJRRK(RRRK((RRbs     cCsti|d|_dS(N(RJRTRR9tfragment(R((RRTks cCs|i|iS(sRParse a document fragment from a file object, returning the fragment node.N(RRRR(RR((RRosc Cs||_|i}|ii}d}|oY|ip |i }|i od|i |i f}q|i od|i }qnd}|i }t|||f}y|i|dWn|inX|i}|i|S(sMParse a document fragment from a string, returning the fragment node.RsPUBLIC "%s" "%s"s SYSTEM "%s"iN(RRt_sourceR]RfR RtidentRt_getDeclarationsRRRt _getNSattrstnsattrst_FRAGMENT_BUILDER_TEMPLATERaRRTR ( RRRR R RfRRRa((RRts,         cCs|iii}d}|ouxt|iiD]z}|ii |}|o|d}nd||i f}|i od||i |i f}q/d||i f}q/Wxt|iiD]}|ii |}|o|d}nd||i f}|i od||i |i f}n8|i od||i f}nd ||iif}|iod ||if}n|d }qWn|S( sRe-create the internal subset from the DocumentType node. This is only needed if we don't already have the internalSubset as a string. Rs s%ss%s SYSTEM "%s">s %sN(RRRRR<RRtlengthRtitemtnotationtnodeNameRRRRt firstChildRR(RRRRR<R((RR s8    cCsdS(NR((R((RRscCs|tjo|i}|i}|ii|}|i |_|ii |_ |i |_z|i|idWd||_||_d|_XdSnti|||||SdS(Nii(Rt$_FRAGMENT_BUILDER_INTERNAL_SYSTEM_IDRRat old_documentRbt old_cur_nodeRStExternalEntityParserCreateRRfR tcreateDocumentFragmentR RR R9RJR}RR(RRRRRRfRR((RR}s        ( R6R7RR9RRTRRR RR}(((RRYs     $ t NamespacescBs;tZdZdZdZdZdZdZRS(s7Mix-in class for builders; adds support for namespaces.cCs g|_dS(N(Rt_ns_ordered_prefixes(R((Rt_initNamespacesscCstidd}t|_|S(s'Create a new namespace-handling parser.tnamespace_separatorR>N(R*RURfRXtnamespace_prefixes(RRf((RRVs cCs1ti|||iio|i|_ndS(s.Insert the namespace-handlers onto the parser.N(RJR\RRfRNtnamespace_declarationststart_namespace_decl_handlertStartNamespaceDeclHandler(RRf((RR\s cCs|ii||fdS(s/Push this namespace declaration on our storage.N(RRRRFRD(RRFRD((RR"scCsd|jot||\} }}}nt} |}d}t }t i || ||}|i |_t|i|||_|iox|iD]\}} |o)t it|d|t|d}nt idtdt }|idi} | | d<| d<|i} | | d<| d<|i | dsxmlns:txmlnsiRRRRiit ownerElement("R?RIRRDRERFRGR`R9RHR#tElementRRaRRRbRRR=tXMLNS_NAMESPACERRRRRRt_attrst_attrsNSRRCRR R(RR?RRGRFRER(RRR RRDRRR)((RRs^                    (R6R7RRRVR\R"R(((RRs     tExpatBuilderNScBstZdZdZRS(s*Document builder that supports namespaces.cCsti||idS(N(RJRTRR(R((RRT5s (R6R7RRT(((RR*2s tFragmentBuilderNScBs tZdZdZdZRS(s*Fragment builder that supports namespaces.cCsti||idS(N(RRTRR(R((RRT=s cCsd}|i}g}x|ot|dox|iiD]v\}}||joq?n|i ||od|}nd}|od|||f}q?d||f}q?Wn|i }qW|S(sNReturn string of namespace attributes from this element and ancestors.Rt_ns_prefix_urisxmlns:R$s%s %s='%s's %s='%s'N( tattrsRRRthasattrR,titemsRFRDRtdeclnameR(RR0RRDRFR-R((RRAs(    (R6R7RRTR(((RR+:s  RcBstZdZRS(sEException raised to short-circuit parsing in InternalSubsetExtractor.(R6R7R(((RR_s RcBsStZdZeZdZdZdZdZdZ dZ dZ RS(sBXML processor which can rip out the internal document type subset.cCs|iS(s'Return the internal subset as a string.N(RR(R((RRhscCs.yti||Wntj onXdS(N(RJRRRR(RR((RRlscCs.yti||Wntj onXdS(N(RJRRRR(RR((RRrscCs|i|_|i|_dS(N(RReRfRgRRi(RRf((RR\xs cCsH|o4|i}g|_|ii|_|i|_n t dS(N( RRR]RfRRtDefaultHandlerRRR(RR?RRRRf((RRe|s   cCs@di|iiddidd}||_tdS(NRs s s (tjoinRRtreplaceR<R(RR<((RRs* cCs tdS(N(R(RR?R-((RRs( R6R7RR9RRRRR\ReRR(((RRcs      icCst|o t}n t}t|to4t|d}z|i |}Wd|i Xn|i |}|S(s{Parse a document, returning the resulting Document node. 'file' may be either a file name or an open file object. trbN( t namespacesR*R:RJt isinstanceRt StringTypestopentfpRtresulttclose(RR5R9R:R:((Rtparses  cCs*|o t}n t}|i|S(sOParse a document from a string, returning the resulting Document node. N(R5R*R:RJRR(RR5R:((RRs   cCsz|ot|}n t|}t|to4t|d}z|i |}Wd|i Xn|i |}|S(sParse 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. R4N( R5R+RR:RR6RR7R8R9RR:R;(RRR5R9R:R:((Rt parseFragments cCs0|ot|}n t|}|i|S(sParse 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. N(R5R+RR:RRR(RRR5R:((RtparseFragmentStrings  cCs&|iot|Sn t|SdS(s,Create a builder based on an Options object.N(RKR5R*RJ(RK((Rt makeBuilders N(5Rtxml.domRLR#RR`RHR't xml.parsersR*txml.dom.minidomRRtxml.dom.NodeFilterRtxml.dom.minicompatRRRtDOMBuilderFilterRRRRtgetDOMImplementationR^tTypeInfoR9R"tNewStyleRR=RIRJRRPRRRRRRRR*R+t ExceptionRRR<RR=R>R?()RRRIR*RPR?R<R`R=R+RRRR'RRRRLRRRR^RRRRRRRR*RJRR>R#RR"RRHRR=R((Rt?sL           3  I:  ud%,