f _o=c@sdZddlTddlmZmZddlmZddlmZmZddlmZddlm Z m Z ddl Z e j dd d krxe d d[ zdd lmZWneye d dYn0eedse d dddlmZmZmZejZejZz ddlZWneyddZYn0ddlZejZ[[GdddZGdddejZGdddejejZ ddZ!e"dkrddl#Z$e!Z%e%&e$j'j(e%)e$j'*e%+ddS)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.10/xml/sax/expatreader.py_mkproxy'rc@s eZdZdS) _ClosedParserN)__name__ __module__ __qualname__rrrrr.src@s4eZdZddZddZddZddZd d Zd S) ExpatLocatorcCst||_dSr)r_refselfparserrrr__init__9rzExpatLocator.__init__cCs|j}|jdurdS|jjSr)r_parserErrorColumnNumberrrrrgetColumnNumber< zExpatLocator.getColumnNumbercCs|j}|jdurdS|jjSN)rrErrorLineNumberrrrr getLineNumberBr zExpatLocator.getLineNumbercC|j}|durdS|jSr)r_source getPublicIdrrrrr'HzExpatLocator.getPublicIdcCr%r)rr& getSystemIdrrrrr)Nr(zExpatLocator.getSystemIdN)rrrrrr$r'r)rrrrr3s rc@seZdZdCddZddZddZd d Zd d Zd dZddZ ddZ dDddZ ddZ ddZ ddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Zdd?Z d@dAZ!dBS)E ExpatParserrcCsFtj||t|_d|_||_d|_d|_g|_ d|_ d|_ dS)NFr) r IncrementalParserrZ InputSourcer&r _namespaces_lex_handler_prop_parsing _entity_stack _external_ges _interning)rZnamespaceHandlingZbufsizerrrrZs zExpatParser.__init__cCsVt|}||_z*||jt|tj ||Wn| Yn0dSr) r prepare_input_sourcer&reset _cont_handlerZsetDocumentLocatorrr r,parse _close_sourcersourcerrrr6gs zExpatParser.parsecCs |dur|j|dSr)r)rZSetBaser8rrr prepareParserws zExpatParser.prepareParsercCs tj|||jr|dSr)r r,setContentHandlerr/_reset_cont_handler)rr rrrr;}szExpatParser.setContentHandlercCsP|tkr|jS|tkr |jduS|tttfvr2dS|tkr@|jSt d|dS)NrFeature '%s' not recognized) rr-rr2rrrrr1SAXNotRecognizedExceptionrnamerrr getFeatures zExpatParser.getFeaturecCs|jrtd|tkr||_n|tkr.||_nt|tkrT|rL|jdurRi|_nVd|_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=) r/SAXNotSupportedExceptionrr-rr1rr2rrrr>)rr@Zstaterrr setFeatures: zExpatParser.setFeaturecCsd|tjkr|jS|tkr|jS|tkrT|jrLt|jdrB|jSt 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.rr2rrhasattrrDr>rBr?rrr getPropertys   zExpatParser.getPropertycCsV|tjkr ||_|jrR|n2|tkr0||_n"|tkrFtd|n t d|dS)NzProperty '%s' cannot be setrE) r rFr.r/_reset_lex_handler_proprr2rrBr>)rr@valuerrr setPropertys  zExpatParser.setPropertyFc Cs||js|d|_|jz|j||WnFtjyv}z,tt |j ||}|j |WYd}~n d}~00dS)NT) r/r4r5Z startDocumentrZParser ZerrorZSAXParseExceptionZ ErrorStringZcodeZ _err_handlerZ fatalError)rdataisFinalZeZexcrrrfeeds zExpatParser.feedcCsZ|j}z4|}|dur |W|}|durV|n|}|durT|0dSr)r&ZgetCharacterStreamcloseZ getByteStream)rr9Zfilerrrr7s  zExpatParser._close_sourcecCs|js|jdust|jtr dSz`|jddd|jd|_d|_Wd|_|jdurxt}|jj|_|jj |_ ||_| n:d|_|jdurt}|jj|_|jj |_ ||_| 0dS)NsT)rMF) r0rZ isinstancerrNr5Z endDocumentr/rr#r7rrrrrOs.         zExpatParser.closecCs|jj|j_|jj|j_dSr)r5processingInstructionrZProcessingInstructionHandler charactersZCharacterDataHandlerrrrrr<szExpatParser._reset_cont_handlercCs`|j}|j}|dur4d|_d|_d|_d|_d|_n(|j|_|j|_|j |_|j |_|j |_dSr) r.rZCommentHandlerZStartCdataSectionHandlerZEndCdataSectionHandlerZStartDoctypeDeclHandlerZEndDoctypeDeclHandlerZcommentZ startCDATAZendCDATAstart_doctype_declZendDTD)rZlexrrrrrIsz#ExpatParser._reset_lex_handler_propcCs|jr>tj|jd|jd|_d|j_|j|j_ |j |j_ n,tj|j|jd|_|j |j_ |j |j_ ||j|j_|j|j_|j|j_|j|j_d|_|jr||j|j_z|j|j_WntyYn0|jtj d|_!g|_"dS)Nz )Zinternr"F)#r-r r r&Z getEncodingr2rZnamespace_prefixesstart_element_nsZStartElementHandlerend_element_nsZEndElementHandler start_element end_elementr<unparsed_entity_declZUnparsedEntityDeclHandler notation_declZNotationDeclHandlerstart_namespace_declZStartNamespaceDeclHandlerend_namespace_declZEndNamespaceDeclHandlerZ_decl_handler_propr.rIexternal_entity_refZExternalEntityRefHandlerskipped_entity_handlerZSkippedEntityHandlerZAttributeErrorZSetParamEntityParsingZ*XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONEr/r0rRrrrr4s<           zExpatParser.resetcCs|jdurdS|jjSr)rrrRrrrr; zExpatParser.getColumnNumbercCs|jdurdS|jjSr!)rr#rRrrrr$@r^zExpatParser.getLineNumbercC |jSr)r&r'rRrrrr'ErzExpatParser.getPublicIdcCr_r)r&r)rRrrrr)HrzExpatParser.getSystemIdcCs|j|t|dSr)r5Z startElementAttributesImpl)rr@attrsrrrrVLrzExpatParser.start_elementcC|j|dSr)r5Z endElementr?rrrrWOrzExpatParser.end_elementc Cs|}t|dkrd|f}n&t|dkr<|d|df}nt|}i}i}|D]|\}}|}t|} | dkr|} d|f} n>| dkrd|d|df} |d|df} n|d} t|} ||| <| || <qT|j|dt||dS)Nr"rz%s:%si)splitlentupleZitemsr5ZstartElementNSAttributesNSImpl) rr@rapairZnewattrsZqnamesZanamerJZpartsZlengthZqnameZapairrrrrTRs0      zExpatParser.start_element_nscCsV|}t|dkrd|f}n&t|dkr<|d|df}nt|}|j|ddS)Nr"rcr)rdrerfr5Z endElementNS)rr@rhrrrrUts   zExpatParser.end_element_nscC|j||dSr)r5rP)rZtargetrLrrrprocessing_instructionrz"ExpatParser.processing_instructioncCrbr)r5rQ)rrLrrrcharacter_datarzExpatParser.character_datacCrir)r5ZstartPrefixMapping)rprefixZurirrrrZrz ExpatParser.start_namespace_declcCrbr)r5ZendPrefixMapping)rrlrrrr[rzExpatParser.end_namespace_declcCs|j|||dSr)r.ZstartDTD)rr@sysidpubidZhas_internal_subsetrrrrSrzExpatParser.start_doctype_declcCs|j||||dSr) _dtd_handlerZunparsedEntityDecl)rr@basermrnZ notation_namerrrrXrz ExpatParser.unparsed_entity_declcCs|j|||dSr)roZ notationDecl)rr@rprmrnrrrrYrzExpatParser.notation_declcCs|js dS|j||}t||jp*d}|j|j |jf|j ||_ ||_zt j ||Wn YdS|jd\|_ |_|jd=dS)Nr"Zri)r1Z _ent_handlerZ resolveEntityr r3r&r)r0ZappendrZExternalEntityParserCreater r,r6)rZcontextrprmrnr9rrrr\s" zExpatParser.external_entity_refcCs|r d|}|j|dS)Nz%)r5Z skippedEntity)rr@Zis_perrrr]sz"ExpatParser.skipped_entity_handlerN)rr+)F)"rrrrr6r:r;rArCrHrKrNr7rOr<rIr4rr$r'r)rVrWrTrUrjrkrZr[rSrXrYr\r]rrrrr*Ws>    '" r*cOst|i|Sr)r*)ZargsZkwargsrrr create_parserrrqZ__main__z:http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml),ZversionZxml.sax._exceptionsZxml.sax.handlerrrrrrrrrZsysZplatformZSAXReaderNotAvailableZ xml.parsersr Z ImportErrorrGZxml.saxr r r r`rgZ_weakrefrZweakrefZproxyrZLocatorrr,r*rqrZxml.sax.saxutilsZxmlZpr;ZsaxZ XMLGeneratorZsetErrorHandlerZ ErrorHandlerr6rrrrZsL       $_