U e5dX=@sdZdZddlTddlmZmZddlmZddlmZmZddlm Z ddlm Z m Z dd l Z e j d d d kr|ed d [ zdd lmZWnek redd YnXeedsedd ddlmZmZmZejZejZz dd lZWnek r ddZYnXdd lZejZ[[GdddZGdddejZGdddej ejZ!ddZ"e#dkrdd l$Z%e"Z&e&'e%j(j)e&*e%j(+e&,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|SN)orr+/usr/lib64/python3.8/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||_dSr)r_refselfparserrrr__init__9szExpatLocator.__init__cCs|j}|jdkrdS|jjSr)r_parserErrorColumnNumberrrrrgetColumnNumber<s zExpatLocator.getColumnNumbercCs|j}|jdkrdS|jjSN)rr!ErrorLineNumberrrrr getLineNumberBs zExpatLocator.getLineNumbercCs|j}|dkrdS|jSr)r_source getPublicIdrrrrr)HszExpatLocator.getPublicIdcCs|j}|dkrdS|jSr)rr( getSystemIdrrrrr*NszExpatLocator.getSystemIdN) rrr__doc__r r#r'r)r*rrrrr3s rc@s eZdZdZdCddZddZdd Zd d Zd d ZddZ ddZ ddZ dDddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Zdd?Z!d@dAZ"dBS)E ExpatParserz$SAX driver for the pyexpat C module.rcCsFtj||t|_d|_||_d|_d|_g|_ d|_ d|_ dS)Nr) rIncrementalParserr Z InputSourcer(r! _namespaces_lex_handler_prop_parsing _entity_stack _external_ges _interning)rZnamespaceHandlingbufsizerrrr Zs zExpatParser.__init__cCsVt|}||_z*||jt|tj ||Wn| YnXdS)z3Parse an XML document from a URL or an InputSource.N) rprepare_input_sourcer(reset _cont_handlerZsetDocumentLocatorrrr.parse _close_sourcersourcerrrr9gs zExpatParser.parsecCs |dk r|j|dSr)r*r!ZSetBaser;rrr prepareParserws zExpatParser.prepareParsercCs tj|||jr|dSr)rr.setContentHandlerr1_reset_cont_handler)rrrrrr>}szExpatParser.setContentHandlercCsP|tkr|jS|tkr |jdk S|tttfkr2dS|tkr@|jSt d|dS)NrFeature '%s' not recognized) rr/rr4rrrrr3SAXNotRecognizedExceptionrnamerrr 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@) r1SAXNotSupportedExceptionrr/rr3rr4rrrrA)rrCstaterrr 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) rproperty_lexical_handlerr0r r4r r!hasattrrHrArErBrrr getPropertys   zExpatParser.getPropertycCsV|tjkr ||_|jrR|n2|tkr0||_n"|tkrFtd|n t d|dS)NzProperty '%s' cannot be setrI) rrJr0r1_reset_lex_handler_propr r4r rErA)rrCvaluerrr setPropertys  zExpatParser.setPropertyc Csz|js|d|_|jz|j||WnDtjk rt}z$tt |j ||}|j |W5d}~XYnXdSr$) r1r7r8Z startDocumentr!ZParser errorZSAXParseExceptionZ ErrorStringcodeZ _err_handlerZ fatalError)rdataisFinaleexcrrrfeeds zExpatParser.feedcCsB|j}z|}|dk r |W5|}|dk r<|XdSr)r(Z getByteStreamcloseZgetCharacterStream)rr<filerrrr:s zExpatParser._close_sourcecCs|js|jdkst|jtr dSz(|jddd|j d|_d|_W5d|_|jdk rzt}|jj|_|jj|_||_|XdS)Nrr%)rS) r2r! isinstancerr1r"r&r:rVr8Z endDocumentrrrrrWs       zExpatParser.closecCs|jj|j_|jj|j_dSr)r8processingInstructionr!ZProcessingInstructionHandler charactersZCharacterDataHandlerrrrrr?szExpatParser._reset_cont_handlercCs`|j}|j}|dkr4d|_d|_d|_d|_d|_n(|j|_|j|_|j |_|j |_|j |_dSr) r0r!ZCommentHandlerZStartCdataSectionHandlerZEndCdataSectionHandlerZStartDoctypeDeclHandlerZEndDoctypeDeclHandlerZcommentZ startCDATAZendCDATAstart_doctype_declZendDTD)rZlexrrrrrMsz#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_Wntk rYnX|jtj d|_!g|_"dS)N )internr%r)#r/r rr(Z getEncodingr4r!Znamespace_prefixesstart_element_nsZStartElementHandlerend_element_nsZEndElementHandler start_element end_elementr?unparsed_entity_declZUnparsedEntityDeclHandler notation_declZNotationDeclHandlerstart_namespace_declZStartNamespaceDeclHandlerend_namespace_declZEndNamespaceDeclHandlerZ_decl_handler_propr0rMexternal_entity_refZExternalEntityRefHandlerskipped_entity_handlerZSkippedEntityHandlerAttributeErrorZSetParamEntityParsingZ*XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONEr1r2r]rrrr7s<          zExpatParser.resetcCs|jdkrdS|jjSr)r!r"r]rrrr#;s zExpatParser.getColumnNumbercCs|jdkrdS|jjSr$)r!r&r]rrrr'@s zExpatParser.getLineNumbercCs |jSr)r(r)r]rrrr)EszExpatParser.getPublicIdcCs |jSr)r(r*r]rrrr*HszExpatParser.getSystemIdcCs|j|t|dSr)r8Z startElementAttributesImpl)rrCattrsrrrrcLszExpatParser.start_elementcCs|j|dSr)r8Z endElementrBrrrrdOszExpatParser.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:%s)splitlentupleitemsr8ZstartElementNSAttributesNSImpl) rrCrmpairZnewattrsZqnamesZanamerNpartsZlengthZqnameZapairrrrraRs0      zExpatParser.start_element_nscCsV|}t|dkrd|f}n&t|dkr<|d|df}nt|}|j|ddS)Nr%rnr)rprqrrr8Z endElementNS)rrCrurrrrbts   zExpatParser.end_element_nscCs|j||dSr)r8r[)rtargetrRrrrprocessing_instructionsz"ExpatParser.processing_instructioncCs|j|dSr)r8r\)rrRrrrcharacter_dataszExpatParser.character_datacCs|j||dSr)r8ZstartPrefixMapping)rprefixZurirrrrgsz ExpatParser.start_namespace_declcCs|j|dSr)r8ZendPrefixMapping)rrzrrrrhszExpatParser.end_namespace_declcCs|j|||dSr)r0ZstartDTD)rrCsysidpubidZhas_internal_subsetrrrr^szExpatParser.start_doctype_declcCs|j||||dSr) _dtd_handlerZunparsedEntityDecl)rrCbaser{r|Z notation_namerrrresz ExpatParser.unparsed_entity_declcCs|j|||dSr)r}Z notationDecl)rrCr~r{r|rrrrfszExpatParser.notation_declcCs|js dS|j||}t||jp*d}|j|j |jf|j ||_ ||_zt j ||WnYdSX|jd\|_ |_|jd=dS)Nr%rYr)r3Z _ent_handlerZ resolveEntityrr6r(r*r2appendr!ZExternalEntityParserCreaterr.r9)rcontextr~r{r|r<rrrris" zExpatParser.external_entity_refcCs|r d|}|j|dS)N%)r8Z skippedEntity)rrCZis_perrrrjsz"ExpatParser.skipped_entity_handlerN)rr-)r)#rrrr+r r9r=r>rDrGrLrOrVr:rWr?rMr7r#r'r)r*rcrdrarbrxryrgrhr^rerfrirjrrrrr,Ws@    '" r,cOs t||Sr)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 ImportErrorrKZxml.saxrrrrlrt_weakrefrweakrefproxyrZLocatorrr.r,rrZxml.sax.saxutilsZxmlpr>ZsaxZ XMLGeneratorZsetErrorHandlerZ ErrorHandlerr9rrrrsN      $_