3 \X=@sdZdZddlTddlmZmZddlmZddlmZmZddlm Z ddlm Z m Z dd l Z e j d d d kr|ed d [ ydd lmZWnek redd YnXeedsedd ddlmZmZmZejZejZy dd lZWnek r ddZYnXdd lZejZ[[GdddZGdddejZGdddej ejZ!ddZ"e#dkrdd l$Z%e"Z&e&j'e%j(jj)e&j*e%j(j+e&j,dd S)z] SAX driver for the pyexpat C module. This driver works with pyexpat.__version__ == '2.22'. z0.20)*)feature_validationfeature_namespaces)feature_namespace_prefixes)feature_external_gesfeature_external_pes)feature_string_interning)property_xml_stringproperty_interning_dictNjavazexpat not available in Java)expatzexpat not supported ParserCreate) xmlreadersaxutilshandlercCs|S)N)orr+/usr/lib64/python3.6/xml/sax/expatreader.py_mkproxy'src@s eZdZdS) _ClosedParserN)__name__ __module__ __qualname__rrrrr.src@s8eZdZdZddZddZddZdd Zd d Zd S) ExpatLocatorzLocator for use with the ExpatParser class. This uses a weak reference to the parser object to avoid creating a circular reference between the parser and the content handler. cCst||_dS)N)r_ref)selfparserrrr__init__9szExpatLocator.__init__cCs|j}|jdkrdS|jjS)N)r_parserErrorColumnNumber)rrrrrgetColumnNumber<s zExpatLocator.getColumnNumbercCs|j}|jdkrdS|jjS)N)rrErrorLineNumber)rrrrr getLineNumberBs zExpatLocator.getLineNumbercCs|j}|dkrdS|jjS)N)r_source getPublicId)rrrrrr&HszExpatLocator.getPublicIdcCs|j}|dkrdS|jjS)N)rr% getSystemId)rrrrrr'NszExpatLocator.getSystemIdN) rrr__doc__rr!r$r&r'rrrrr3s rc@s eZdZdZdGddZdd Zd d Zd d ZddZddZ ddZ ddZ dHddZ ddZ ddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Zdd?Z d@dAZ!dBdCZ"dDS)I ExpatParserz$SAX driver for the pyexpat C module.rcCsFtjj||tj|_d|_||_d|_d|_g|_ d|_ d|_ dS)Nr) rIncrementalParserrZ InputSourcer%r _namespaces_lex_handler_prop_parsing _entity_stack _external_ges _interning)rZnamespaceHandlingbufsizerrrrZs zExpatParser.__init__c CsVtj|}||_y*|j|jjt|tjj ||Wn|j YnXdS)z3Parse an XML document from a URL or an InputSource.N) rprepare_input_sourcer%reset _cont_handlerZsetDocumentLocatorrrr-parse _close_source)rsourcerrrr8gs zExpatParser.parsecCs |jdk r|jj|jdS)N)r'rZSetBase)rr:rrr prepareParserws zExpatParser.prepareParsercCs tjj|||jr|jdS)N)rr-setContentHandlerr0_reset_cont_handler)rrrrrr<}szExpatParser.setContentHandlercCsP|tkr|jS|tkr |jdk S|tttfkr2dS|tkr@|jSt d|dS)NrzFeature '%s' not recognized) rr.rr3rrrrr2SAXNotRecognizedException)rnamerrr getFeatures zExpatParser.getFeaturecCs|jrtd|tkr||_n|tkr.||_nt|tkrT|rL|jdkrRi|_qd|_nN|tkrj|rtdn8|t kr|rtdn"|t kr|rtdn t d|dS)Nz!Cannot set features while parsingz!expat does not support validationz/expat does not read external parameter entitiesz(expat does not report namespace prefixeszFeature '%s' not recognized) r0SAXNotSupportedExceptionrr.rr2rr3rrrr>)rr?staterrr setFeatures2 zExpatParser.setFeaturecCsd|tjkr|jS|tkr|jS|tkrT|jrLt|jdrB|jjSt dnt dt d|dS)NGetInputContextz=This version of expat does not support getting the XML stringz.XML string cannot be returned when not parsingzProperty '%s' not recognized) rproperty_lexical_handlerr/r r3r rhasattrrDr>rA)rr?rrr getPropertys   zExpatParser.getPropertycCsV|tjkr ||_|jrR|jn2|tkr0||_n"|tkrFtd|n t d|dS)NzProperty '%s' cannot be setzProperty '%s' not recognized) rrEr/r0_reset_lex_handler_propr r3r rAr>)rr?valuerrr setPropertys   zExpatParser.setPropertycCsz|js|jd|_|jjy|jj||WnDtjk rt}z&ttj |j ||}|j j |WYdd}~XnXdS)Nr") r0r6r7Z startDocumentrZParser errorZSAXParseExceptionZ ErrorStringcodeZ _err_handlerZ fatalError)rdataisFinaleexcrrrfeeds zExpatParser.feedc CsB|j}z|j}|dk r |jWd|j}|dk r<|jXdS)N)r%ZgetCharacterStreamcloseZ getByteStream)rr:filerrrr9s zExpatParser._close_sourcec Cs|js|jdkst|jtr dSz(|jddd|jjd|_d|_Wdd|_|jdk rzt}|jj|_|jj |_ ||_|j XdS)Nr")rNr) r1r isinstancerrQr7Z endDocumentr0r r#r9)rrrrrrRs      zExpatParser.closecCs|jj|j_|jj|j_dS)N)r7processingInstructionrZProcessingInstructionHandler charactersZCharacterDataHandler)rrrrr=s zExpatParser._reset_cont_handlercCs`|j}|j}|dkr4d|_d|_d|_d|_d|_n(|j|_|j|_|j |_|j |_|j |_dS)N) r/rZCommentHandlerZStartCdataSectionHandlerZEndCdataSectionHandlerZStartDoctypeDeclHandlerZEndDoctypeDeclHandlerZcommentZ startCDATAZendCDATAstart_doctype_declZendDTD)rZlexrrrrrHsz#ExpatParser._reset_lex_handler_propc Cs|jr>tj|jjd|jd|_d|j_|j|j_ |j |j_ n,tj|jj|jd|_|j |j_ |j |j_ |j|j|j_|j|j_|j|j_|j|j_d|_|jr|j|j|j_y|j|j_Wntk rYnX|jjtj d|_!g|_"dS)N )internr"r)#r.r rr%Z getEncodingr3rZnamespace_prefixesstart_element_nsZStartElementHandlerend_element_nsZEndElementHandler start_element end_elementr=unparsed_entity_declZUnparsedEntityDeclHandler notation_declZNotationDeclHandlerstart_namespace_declZStartNamespaceDeclHandlerend_namespace_declZEndNamespaceDeclHandlerZ_decl_handler_propr/rHexternal_entity_refZExternalEntityRefHandlerskipped_entity_handlerZSkippedEntityHandlerAttributeErrorZSetParamEntityParsingZ*XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONEr0r1)rrrrr6s6            zExpatParser.resetcCs|jdkrdS|jjS)N)rr )rrrrr!;s zExpatParser.getColumnNumbercCs|jdkrdS|jjS)Nr")rr#)rrrrr$@s zExpatParser.getLineNumbercCs |jjS)N)r%r&)rrrrr&EszExpatParser.getPublicIdcCs |jjS)N)r%r')rrrrr'HszExpatParser.getSystemIdcCs|jj|t|dS)N)r7Z startElementAttributesImpl)rr?attrsrrrr]LszExpatParser.start_elementcCs|jj|dS)N)r7Z endElement)rr?rrrr^OszExpatParser.end_elementc Cs|j}t|dkrd|f}n&t|dkr<|d|df}nt|}i}i}x|jD]|\}}|j}t|} | dkr|} d|f} n>| dkrd|d|df} |d|df} n|d} t|} ||| <| || <qVW|jj|dt||dS)Nr"rz%s:%sr*)splitlentupleitemsr7ZstartElementNSAttributesNSImpl) rr?rgpairZnewattrsZqnamesZanamerIpartsZlengthZqnameZapairrrrr[Rs.      zExpatParser.start_element_nscCsV|j}t|dkrd|f}n&t|dkr<|d|df}nt|}|jj|ddS)Nr"rhr)rirjrkr7Z endElementNS)rr?rnrrrr\ts   zExpatParser.end_element_nscCs|jj||dS)N)r7rV)rtargetrMrrrprocessing_instructionsz"ExpatParser.processing_instructioncCs|jj|dS)N)r7rW)rrMrrrcharacter_dataszExpatParser.character_datacCs|jj||dS)N)r7ZstartPrefixMapping)rprefixZurirrrrasz ExpatParser.start_namespace_declcCs|jj|dS)N)r7ZendPrefixMapping)rrsrrrrbszExpatParser.end_namespace_declcCs|jj|||dS)N)r/ZstartDTD)rr?sysidpubidZhas_internal_subsetrrrrXszExpatParser.start_doctype_declcCs|jj||||dS)N) _dtd_handlerZunparsedEntityDecl)rr?basertruZ notation_namerrrr_sz ExpatParser.unparsed_entity_declcCs|jj|||dS)N)rvZ notationDecl)rr?rwrtrurrrr`szExpatParser.notation_declc Cs|js dS|jj||}tj||jjp*d}|jj|j |jf|j j ||_ ||_yt j j ||Wn dS|jd\|_ |_|jd=dS)Nr"rTrrx)r2Z _ent_handlerZ resolveEntityrr5r%r'r1appendrZExternalEntityParserCreaterr-r8)rcontextrwrtrur:rrrrcs  zExpatParser.external_entity_refcCs|r d|}|jj|dS)N%)r7Z skippedEntity)rr?Zis_perrrrdsz"ExpatParser.skipped_entity_handlerNi)rr|)r)#rrrr(rr8r;r<r@rCrGrJrQr9rRr=rHr6r!r$r&r'r]r^r[r\rqrrrarbrXr_r`rcrdrrrrr)Ws@    '" r)cOs t||S)N)r))argskwargsrrr create_parsersr__main__z:http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml)-r(versionZxml.sax._exceptionsZxml.sax.handlerrrrrrrr r sysplatformZSAXReaderNotAvailableZ xml.parsersr ImportErrorrFZxml.saxrrrrfrm_weakrefrweakrefproxyrZLocatorrr-r)rrZxml.sax.saxutilsZxmlpr<ZsaxZ XMLGeneratorZsetErrorHandlerZ ErrorHandlerr8rrrrsN      $_