3 \1@sdZddlmZddlmZmZGdddZGdddeZGdd d ZGd d d Z Gd d d Z Gddde Z ddZ e dkre dS)z]An XML Reader is the SAX 2 name for an XML parser. XML Parsers should be based on this code. )handler)SAXNotSupportedExceptionSAXNotRecognizedExceptionc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd S)! XMLReadera%Interface for reading an XML document using callbacks. XMLReader is the interface that an XML parser's SAX2 driver must implement. This interface allows an application to set and query features and properties in the parser, to register event handlers for document processing, and to initiate a document parse. All SAX interfaces are assumed to be synchronous: the parse methods must not return until parsing is complete, and readers must wait for an event-handler callback to return before reporting the next event.cCs,tj|_tj|_tj|_tj|_dS)N) rZContentHandler _cont_handlerZ DTDHandler _dtd_handlerZEntityResolver _ent_handlerZ ErrorHandler _err_handler)selfr )/usr/lib64/python3.6/xml/sax/xmlreader.py__init__s   zXMLReader.__init__cCs tddS)zAParse an XML document from a system identifier or an InputSource.z This method must be implemented!N)NotImplementedError)r sourcer r r parseszXMLReader.parsecCs|jS)z#Returns the current ContentHandler.)r)r r r r getContentHandler"szXMLReader.getContentHandlercCs ||_dS)z:Registers a new object to receive document content events.N)r)r rr r r setContentHandler&szXMLReader.setContentHandlercCs|jS)z Returns the current DTD handler.)r)r r r r getDTDHandler*szXMLReader.getDTDHandlercCs ||_dS)z7Register an object to receive basic DTD-related events.N)r)r rr r r setDTDHandler.szXMLReader.setDTDHandlercCs|jS)z#Returns the current EntityResolver.)r)r r r r getEntityResolver2szXMLReader.getEntityResolvercCs ||_dS)z0Register an object to resolve external entities.N)r)r Zresolverr r r setEntityResolver6szXMLReader.setEntityResolvercCs|jS)z!Returns the current ErrorHandler.)r )r r r r getErrorHandler:szXMLReader.getErrorHandlercCs ||_dS)z3Register an object to receive error-message events.N)r )r rr r r setErrorHandler>szXMLReader.setErrorHandlercCs tddS)aHAllow an application to set the locale for errors and warnings. SAX parsers are not required to provide localization for errors and warnings; if they cannot support the requested locale, however, they must raise a SAX exception. Applications may request a locale change in the middle of a parse.zLocale support not implementedN)r)r Zlocaler r r setLocaleBszXMLReader.setLocalecCstd|dS)z1Looks up and returns the state of a SAX2 feature.zFeature '%s' not recognizedN)r)r namer r r getFeatureKszXMLReader.getFeaturecCstd|dS)z!Sets the state of a SAX2 feature.zFeature '%s' not recognizedN)r)r rstater r r setFeatureOszXMLReader.setFeaturecCstd|dS)z2Looks up and returns the value of a SAX2 property.zProperty '%s' not recognizedN)r)r rr r r getPropertySszXMLReader.getPropertycCstd|dS)z"Sets the value of a SAX2 property.zProperty '%s' not recognizedN)r)r rvaluer r r setPropertyWszXMLReader.setPropertyN)__name__ __module__ __qualname____doc__r rrrrrrrrrrrrrr r r r r r s   rc@sBeZdZdZdddZddZdd Zd d Zd d ZddZ dS)IncrementalParserasThis interface adds three extra methods to the XMLReader interface that allow XML parsers to support incremental parsing. Support for this interface is optional, since not all underlying XML parsers support this functionality. When the parser is instantiated it is ready to begin accepting data from the feed method immediately. After parsing has been finished with a call to close the reset method must be called to make the parser ready to accept new data, either from feed or using the parse method. Note that these methods must _not_ be called during parsing, that is, after parse has been called and before it returns. By default, the class also implements the parse method of the XMLReader interface using the feed, close and reset methods of the IncrementalParser interface as a convenience to SAX 2.0 driver writers.cCs||_tj|dS)N)_bufsizerr )r bufsizer r r r oszIncrementalParser.__init__cCspddlm}|j|}|j||j}|dkr8|j}|j|j}x|rb|j||j|j}qFW|j dS)Nr)saxutils) r*Zprepare_input_source prepareParsergetCharacterStream getByteStreamreadr(feedclose)r rr*filebufferr r r rss     zIncrementalParser.parsecCs tddS)aThis method gives the raw XML data in the data parameter to the parser and makes it parse the data, emitting the corresponding events. It is allowed for XML constructs to be split across several calls to feed. feed may raise SAXException.z This method must be implemented!N)r)r datar r r r0szIncrementalParser.feedcCs tddS)ztThis method is called by the parse implementation to allow the SAX 2.0 driver to prepare itself for parsing.z!prepareParser must be overridden!N)r)r rr r r r,szIncrementalParser.prepareParsercCs tddS)aThis method is called when the entire XML document has been passed to the parser through the feed method, to notify the parser that there are no more data. This allows the parser to do the final checks on the document and empty the internal data buffer. The parser will not be ready to parse another document until the reset method has been called. close may raise SAXException.z This method must be implemented!N)r)r r r r r1s zIncrementalParser.closecCs tddS)zThis method is called after close has been called to reset the parser so that it is ready to parse new documents. The results of calling parse or feed after close without calling reset are undefined.z This method must be implemented!N)r)r r r r resetszIncrementalParser.resetN)r6) r!r"r#r$r rr0r,r1r5r r r r r%[s   r%c@s0eZdZdZddZddZddZdd Zd S) LocatorzInterface for associating a SAX event with a document location. A locator object will return valid results only during calls to DocumentHandler methods; at any other time, the results are unpredictable.cCsdS)z6Return the column number where the current event ends.rr )r r r r getColumnNumberszLocator.getColumnNumbercCsdS)z4Return the line number where the current event ends.rr8r )r r r r getLineNumberszLocator.getLineNumbercCsdS)z3Return the public identifier for the current event.Nr )r r r r getPublicIdszLocator.getPublicIdcCsdS)z3Return the system identifier for the current event.Nr )r r r r getSystemIdszLocator.getSystemIdN)r!r"r#r$r9r:r;r<r r r r r7s r7c@sjeZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdS) InputSourceanEncapsulation of the information needed by the XMLReader to read entities. This class may include information about the public identifier, system identifier, byte stream (possibly with character encoding information) and/or the character stream of an entity. Applications will create objects of this class for use in the XMLReader.parse method and for returning from EntityResolver.resolveEntity. An InputSource belongs to the application, the XMLReader is not allowed to modify InputSource objects passed to it from the application, although it may make copies and modify those.NcCs"||_d|_d|_d|_d|_dS)N)_InputSource__system_id_InputSource__public_id_InputSource__encoding_InputSource__bytefile_InputSource__charfile)r system_idr r r r s zInputSource.__init__cCs ||_dS)z/Sets the public identifier of this InputSource.N)r?)r Z public_idr r r setPublicIdszInputSource.setPublicIdcCs|jS)z2Returns the public identifier of this InputSource.)r?)r r r r r;szInputSource.getPublicIdcCs ||_dS)z/Sets the system identifier of this InputSource.N)r>)r rCr r r setSystemIdszInputSource.setSystemIdcCs|jS)z2Returns the system identifier of this InputSource.)r>)r r r r r<szInputSource.getSystemIdcCs ||_dS)a4Sets the character encoding of this InputSource. The encoding must be a string acceptable for an XML encoding declaration (see section 4.3.3 of the XML recommendation). The encoding attribute of the InputSource is ignored if the InputSource also contains a character stream.N)r@)r encodingr r r setEncodingszInputSource.setEncodingcCs|jS)z/Get the character encoding of this InputSource.)r@)r r r r getEncodingszInputSource.getEncodingcCs ||_dS)aSet the byte stream (a Python file-like object which does not perform byte-to-character conversion) for this input source. The SAX parser will ignore this if there is also a character stream specified, but it will use a byte stream in preference to opening a URI connection itself. If the application knows the character encoding of the byte stream, it should set it with the setEncoding method.N)rA)r Zbytefiler r r setByteStreams zInputSource.setByteStreamcCs|jS)zGet the byte stream for this input source. The getEncoding method will return the character encoding for this byte stream, or None if unknown.)rA)r r r r r.szInputSource.getByteStreamcCs ||_dS)a^Set the character stream for this input source. (The stream must be a Python 2.0 Unicode-wrapped file-like that performs conversion to Unicode strings.) If there is a character stream specified, the SAX parser will ignore any byte stream and will not attempt to open a URI connection to the system identifier.N)rB)r Zcharfiler r r setCharacterStreamszInputSource.setCharacterStreamcCs|jS)z/Get the character stream for this input source.)rB)r r r r r-szInputSource.getCharacterStream)N)r!r"r#r$r rDr;rEr<rGrHrIr.rJr-r r r r r=s    r=c@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZd$ddZddZd d!Zd"d#ZdS)%AttributesImplcCs ||_dS)zQNon-NS-aware implementation. attrs should be of the form {name : value}.N)_attrs)r attrsr r r r szAttributesImpl.__init__cCs t|jS)N)lenrL)r r r r getLengthszAttributesImpl.getLengthcCsdS)NZCDATAr )r rr r r getType!szAttributesImpl.getTypecCs |j|S)N)rL)r rr r r getValue$szAttributesImpl.getValuecCs |j|S)N)rL)r rr r r getValueByQName'szAttributesImpl.getValueByQNamecCs||jkrt||S)N)rLKeyError)r rr r r getNameByQName*s zAttributesImpl.getNameByQNamecCs||jkrt||S)N)rLrS)r rr r r getQNameByName/s zAttributesImpl.getQNameByNamecCst|jjS)N)listrLkeys)r r r r getNames4szAttributesImpl.getNamescCst|jjS)N)rVrLrW)r r r r getQNames7szAttributesImpl.getQNamescCs t|jS)N)rNrL)r r r r __len__:szAttributesImpl.__len__cCs |j|S)N)rL)r rr r r __getitem__=szAttributesImpl.__getitem__cCst|jjS)N)rVrLrW)r r r r rW@szAttributesImpl.keyscCs ||jkS)N)rL)r rr r r __contains__CszAttributesImpl.__contains__NcCs|jj||S)N)rLget)r r alternativer r r r]FszAttributesImpl.getcCs |j|jS)N) __class__rL)r r r r copyIszAttributesImpl.copycCst|jjS)N)rVrLitems)r r r r raLszAttributesImpl.itemscCst|jjS)N)rVrLvalues)r r r r rbOszAttributesImpl.values)N)r!r"r#r rOrPrQrRrTrUrXrYrZr[rWr\r]r`rarbr r r r rKs" rKc@s<eZdZddZddZddZddZd d Zd d Zd S)AttributesNSImplcCs||_||_dS)zNS-aware implementation. attrs should be of the form {(ns_uri, lname): value, ...}. qnames of the form {(ns_uri, lname): qname, ...}.N)rL_qnames)r rMZqnamesr r r r VszAttributesNSImpl.__init__cCs6x(|jjD]\}}||kr |j|Sq Wt|dS)N)rdrarLrS)r rnsnameqnamer r r rR^sz AttributesNSImpl.getValueByQNamecCs0x"|jjD]\}}||kr |Sq Wt|dS)N)rdrarS)r rrerfr r r rTeszAttributesNSImpl.getNameByQNamecCs |j|S)N)rd)r rr r r rUlszAttributesNSImpl.getQNameByNamecCst|jjS)N)rVrdrb)r r r r rYoszAttributesNSImpl.getQNamescCs|j|j|jS)N)r_rLrd)r r r r r`rszAttributesNSImpl.copyN) r!r"r#r rRrTrUrYr`r r r r rcTs rccCstttdS)N)rr%r7r r r r _testvsrg__main__N)r$r+r _exceptionsrrrr%r7r=rKrcrgr!r r r r s PJY>"