3 ^X=c@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)Z*)feature_validationfeature_namespaces)feature_namespace_prefixes)feature_external_gesfeature_external_pes)feature_string_interning)property_xml_stringproperty_interning_dictNiZjavazexpat not available in Java)expatzexpat not supported ParserCreate) xmlreadersaxutilshandlercCs|SN)Zorr+/usr/lib64/python3.6/xml/sax/expatreader.py_mkproxy'rc@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||_dSr)r_refselfparserrrr__init__9rzExpatLocator.__init__cCs|j}|jdkrdS|jjSr)r_parserErrorColumnNumberrrrrgetColumnNumber< zExpatLocator.getColumnNumbercCs|j}|jdkrdS|jjSN)rrErrorLineNumberrrrr getLineNumberBr zExpatLocator.getLineNumbercC|j}|dkrdS|jjSr)r_source getPublicIdrrrrr'HzExpatLocator.getPublicIdcCr%r)rr& getSystemIdrrrrr)Nr(zExpatLocator.getSystemIdN) rrr__doc__rrr$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.riicCsFtjj||tj|_d|_||_d|_d|_g|_ d|_ d|_ dS)Nr) r IncrementalParserrZ InputSourcer&r _namespaces_lex_handler_prop_parsing _entity_stack _external_ges _interning)rZnamespaceHandlingZbufsizerrrrZs zExpatParser.__init__c CsVtj|}||_y*|j|jjt|tjj ||Wn|j YnXdS)z3Parse an XML document from a URL or an InputSource.N) r prepare_input_sourcer&reset _cont_handlerZsetDocumentLocatorrr r-parse _close_sourcersourcerrrr7gs zExpatParser.parsecCs |jdk r|jj|jdSr)r)rZSetBaser9rrr prepareParserws zExpatParser.prepareParsercCs tjj|||jr|jdSr)r r-setContentHandlerr0_reset_cont_handler)rr rrrr<}szExpatParser.setContentHandlercCsP|tkr|jS|tkr |jdk S|tttfkr2dS|tkr@|jSt d|dS)NrFeature '%s' not recognized) rr.rr3rrrrr2SAXNotRecognizedExceptionrnamerrr 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 prefixesr>) r0SAXNotSupportedExceptionrr.rr2rr3rrrr?)rrAZstaterrr 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 parsingProperty '%s' not recognized) r property_lexical_handlerr/rr3rrhasattrrEr?rCr@rrr getPropertys   zExpatParser.getPropertycCsV|tjkr ||_|jrR|jn2|tkr0||_n"|tkrFtd|n t d|dS)NzProperty '%s' cannot be setrF) r rGr/r0_reset_lex_handler_proprr3rrCr?)rrAvaluerrr setPropertys   zExpatParser.setPropertycCsz|js|jd|_|jjy|jj||WnDtjk rt}z&ttj |j ||}|j j |WYdd}~XnXdSr!) r0r5r6Z startDocumentrZParser ZerrorZSAXParseExceptionZ ErrorStringZcodeZ _err_handlerZ fatalError)rdataisFinalZeZexcrrrfeeds zExpatParser.feedc CsB|j}z|j}|dk r |jWd|j}|dk r<|jXdSr)r&ZgetCharacterStreamcloseZ getByteStream)rr:Zfilerrrr8s zExpatParser._close_sourcec Cs|js|jdkst|jtr dSz(|jddd|jjd|_d|_Wdd|_|jdk rzt}|jj|_|jj |_ ||_|j XdS)Nr")rNr) r1rZ isinstancerrOr6Z endDocumentr0rr#r8rrrrrPs      zExpatParser.closecCs|jj|j_|jj|j_dSr)r6processingInstructionrZProcessingInstructionHandler charactersZCharacterDataHandlerrrrrr=s zExpatParser._reset_cont_handlercCs`|j}|j}|dkr4d|_d|_d|_d|_d|_n(|j|_|j|_|j |_|j |_|j |_dSr) r/rZCommentHandlerZStartCdataSectionHandlerZEndCdataSectionHandlerZStartDoctypeDeclHandlerZEndDoctypeDeclHandlerZcommentZ startCDATAZendCDATAstart_doctype_declZendDTD)rZlexrrrrrJsz#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)Nz )Zinternr"r)#r.r r r&Z getEncodingr3rZnamespace_prefixesstart_element_nsZStartElementHandlerend_element_nsZEndElementHandler start_element end_elementr=unparsed_entity_declZUnparsedEntityDeclHandler notation_declZNotationDeclHandlerstart_namespace_declZStartNamespaceDeclHandlerend_namespace_declZEndNamespaceDeclHandlerZ_decl_handler_propr/rJexternal_entity_refZExternalEntityRefHandlerskipped_entity_handlerZSkippedEntityHandlerZAttributeErrorZSetParamEntityParsingZ*XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONEr0r1rTrrrr5s6            zExpatParser.resetcCs|jdkrdS|jjSr)rrrTrrrr; zExpatParser.getColumnNumbercCs|jdkrdS|jjSr!)rr#rTrrrr$@r`zExpatParser.getLineNumbercC |jjSr)r&r'rTrrrr'ErzExpatParser.getPublicIdcCrar)r&r)rTrrrr)HrzExpatParser.getSystemIdcCs|jj|t|dSr)r6Z startElementAttributesImpl)rrAattrsrrrrXLrzExpatParser.start_elementcC|jj|dSr)r6Z endElementr@rrrrYOrzExpatParser.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,)splitlentupleZitemsr6ZstartElementNSAttributesNSImpl) rrArcpairZnewattrsZqnamesZanamerKZpartsZlengthZqnameZapairrrrrVRs.      zExpatParser.start_element_nscCsV|j}t|dkrd|f}n&t|dkr<|d|df}nt|}|jj|ddS)Nr"rer)rfrgrhr6Z endElementNS)rrArjrrrrWts   zExpatParser.end_element_nscC|jj||dSr)r6rR)rZtargetrMrrrprocessing_instructionrz"ExpatParser.processing_instructioncCrdr)r6rS)rrMrrrcharacter_datarzExpatParser.character_datacCrkr)r6ZstartPrefixMapping)rprefixZurirrrr\rz ExpatParser.start_namespace_declcCrdr)r6ZendPrefixMapping)rrnrrrr]rzExpatParser.end_namespace_declcCs|jj|||dSr)r/ZstartDTD)rrAsysidpubidZhas_internal_subsetrrrrUrzExpatParser.start_doctype_declcCs|jj||||dSr) _dtd_handlerZunparsedEntityDecl)rrAbaserorpZ notation_namerrrrZrz ExpatParser.unparsed_entity_declcCs|jj|||dSr)rqZ notationDecl)rrArrrorprrrr[rzExpatParser.notation_declc Cs|js dS|jj||}tj||jjp*d}|jj|j |jf|j j ||_ ||_yt j j ||Wn dS|jd\|_ |_|jd=dS)Nr"rQrrs)r2Z _ent_handlerZ resolveEntityr r4r&r)r1ZappendrZExternalEntityParserCreater r-r7)rZcontextrrrorpr:rrrr^s  zExpatParser.external_entity_refcCs|r d|}|jj|dS)Nz%)r6Z skippedEntity)rrAZis_perrrr_sz"ExpatParser.skipped_entity_handlerNi)rrt)r)#rrrr*rr7r;r<rBrDrIrLrOr8rPr=rJr5rr$r'r)rXrYrVrWrlrmr\r]rUrZr[r^r_rrrrr+Ws@    '" r+cOs t||Sr)r+)ZargsZkwargsrrr create_parserrruZ__main__z:http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml)-r*ZversionZxml.sax._exceptionsZxml.sax.handlerrrrrrrrrZsysZplatformZSAXReaderNotAvailableZ xml.parsersr Z ImportErrorrHZxml.saxr r r rbriZ_weakrefrZweakrefZproxyrZLocatorrr-r+rurZxml.sax.saxutilsZxmlZpr<ZsaxZ XMLGeneratorZsetErrorHandlerZ ErrorHandlerr7rrrrZsN      $_