13257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel"""An XML Reader is the SAX 2 name for an XML parser. XML Parsers 23257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielshould be based on this code. """ 33257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 43257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport handler 53257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 63257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielfrom _exceptions import SAXNotSupportedException, SAXNotRecognizedException 73257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 83257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 93257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# ===== XMLREADER ===== 103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass XMLReader: 123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Interface for reading an XML document using callbacks. 133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel XMLReader is the interface that an XML parser's SAX2 driver must 153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel implement. This interface allows an application to set and query 163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel features and properties in the parser, to register event handlers 173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel for document processing, and to initiate a document parse. 183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel All SAX interfaces are assumed to be synchronous: the parse 203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel methods must not return until parsing is complete, and readers 213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel must wait for an event-handler callback to return before reporting 223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel the next event.""" 233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self): 253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._cont_handler = handler.ContentHandler() 263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._dtd_handler = handler.DTDHandler() 273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._ent_handler = handler.EntityResolver() 283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._err_handler = handler.ErrorHandler() 293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def parse(self, source): 313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Parse an XML document from a system identifier or an InputSource." 323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise NotImplementedError("This method must be implemented!") 333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getContentHandler(self): 353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Returns the current ContentHandler." 363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._cont_handler 373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setContentHandler(self, handler): 393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Registers a new object to receive document content events." 403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._cont_handler = handler 413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getDTDHandler(self): 433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Returns the current DTD handler." 443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._dtd_handler 453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setDTDHandler(self, handler): 473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Register an object to receive basic DTD-related events." 483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._dtd_handler = handler 493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getEntityResolver(self): 513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Returns the current EntityResolver." 523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._ent_handler 533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setEntityResolver(self, resolver): 553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Register an object to resolve external entities." 563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._ent_handler = resolver 573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getErrorHandler(self): 593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Returns the current ErrorHandler." 603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._err_handler 613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setErrorHandler(self, handler): 633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Register an object to receive error-message events." 643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._err_handler = handler 653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setLocale(self, locale): 673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Allow an application to set the locale for errors and warnings. 683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel SAX parsers are not required to provide localization for errors 703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel and warnings; if they cannot support the requested locale, 713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel however, they must raise a SAX exception. Applications may 723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel request a locale change in the middle of a parse.""" 733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise SAXNotSupportedException("Locale support not implemented") 743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getFeature(self, name): 763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Looks up and returns the state of a SAX2 feature." 773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise SAXNotRecognizedException("Feature '%s' not recognized" % name) 783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setFeature(self, name, state): 803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Sets the state of a SAX2 feature." 813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise SAXNotRecognizedException("Feature '%s' not recognized" % name) 823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getProperty(self, name): 843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Looks up and returns the value of a SAX2 property." 853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise SAXNotRecognizedException("Property '%s' not recognized" % name) 863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setProperty(self, name, value): 883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Sets the value of a SAX2 property." 893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise SAXNotRecognizedException("Property '%s' not recognized" % name) 903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass IncrementalParser(XMLReader): 923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """This interface adds three extra methods to the XMLReader 933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel interface that allow XML parsers to support incremental 943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel parsing. Support for this interface is optional, since not all 953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel underlying XML parsers support this functionality. 963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel When the parser is instantiated it is ready to begin accepting 983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel data from the feed method immediately. After parsing has been 993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel finished with a call to close the reset method must be called to 1003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel make the parser ready to accept new data, either from feed or 1013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel using the parse method. 1023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Note that these methods must _not_ be called during parsing, that 1043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel is, after parse has been called and before it returns. 1053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel By default, the class also implements the parse method of the XMLReader 1073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel interface using the feed, close and reset methods of the 1083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel IncrementalParser interface as a convenience to SAX 2.0 driver 1093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel writers.""" 1103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self, bufsize=2**16): 1123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._bufsize = bufsize 1133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel XMLReader.__init__(self) 1143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def parse(self, source): 1163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel import saxutils 1173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel source = saxutils.prepare_input_source(source) 1183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.prepareParser(source) 1203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel file = source.getByteStream() 1213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel buffer = file.read(self._bufsize) 1223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel while buffer != "": 1233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.feed(buffer) 1243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel buffer = file.read(self._bufsize) 1253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.close() 1263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def feed(self, data): 1283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """This method gives the raw XML data in the data parameter to 1293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel the parser and makes it parse the data, emitting the 1303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel corresponding events. It is allowed for XML constructs to be 1313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel split across several calls to feed. 1323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel feed may raise SAXException.""" 1343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise NotImplementedError("This method must be implemented!") 1353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def prepareParser(self, source): 1373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """This method is called by the parse implementation to allow 1383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel the SAX 2.0 driver to prepare itself for parsing.""" 1393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise NotImplementedError("prepareParser must be overridden!") 1403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def close(self): 1423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """This method is called when the entire XML document has been 1433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel passed to the parser through the feed method, to notify the 1443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel parser that there are no more data. This allows the parser to 1453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel do the final checks on the document and empty the internal 1463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel data buffer. 1473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel The parser will not be ready to parse another document until 1493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel the reset method has been called. 1503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel close may raise SAXException.""" 1523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise NotImplementedError("This method must be implemented!") 1533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def reset(self): 1553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """This method is called after close has been called to reset 1563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel the parser so that it is ready to parse new documents. The 1573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel results of calling parse or feed after close without calling 1583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel reset are undefined.""" 1593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise NotImplementedError("This method must be implemented!") 1603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# ===== LOCATOR ===== 1623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass Locator: 1643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Interface for associating a SAX event with a document 1653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel location. A locator object will return valid results only during 1663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel calls to DocumentHandler methods; at any other time, the 1673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel results are unpredictable.""" 1683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getColumnNumber(self): 1703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Return the column number where the current event ends." 1713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return -1 1723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getLineNumber(self): 1743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Return the line number where the current event ends." 1753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return -1 1763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getPublicId(self): 1783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Return the public identifier for the current event." 1793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return None 1803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getSystemId(self): 1823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Return the system identifier for the current event." 1833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return None 1843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# ===== INPUTSOURCE ===== 1863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass InputSource: 1883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Encapsulation of the information needed by the XMLReader to 1893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel read entities. 1903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel This class may include information about the public identifier, 1923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel system identifier, byte stream (possibly with character encoding 1933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel information) and/or the character stream of an entity. 1943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Applications will create objects of this class for use in the 1963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel XMLReader.parse method and for returning from 1973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel EntityResolver.resolveEntity. 1983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel An InputSource belongs to the application, the XMLReader is not 2003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel allowed to modify InputSource objects passed to it from the 2013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel application, although it may make copies and modify those.""" 2023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self, system_id = None): 2043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__system_id = system_id 2053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__public_id = None 2063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__encoding = None 2073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__bytefile = None 2083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__charfile = None 2093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setPublicId(self, public_id): 2113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Sets the public identifier of this InputSource." 2123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__public_id = public_id 2133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getPublicId(self): 2153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Returns the public identifier of this InputSource." 2163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__public_id 2173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setSystemId(self, system_id): 2193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Sets the system identifier of this InputSource." 2203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__system_id = system_id 2213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getSystemId(self): 2233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Returns the system identifier of this InputSource." 2243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__system_id 2253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setEncoding(self, encoding): 2273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Sets the character encoding of this InputSource. 2283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel The encoding must be a string acceptable for an XML encoding 2303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel declaration (see section 4.3.3 of the XML recommendation). 2313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel The encoding attribute of the InputSource is ignored if the 2333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel InputSource also contains a character stream.""" 2343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__encoding = encoding 2353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getEncoding(self): 2373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Get the character encoding of this InputSource." 2383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__encoding 2393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setByteStream(self, bytefile): 2413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Set the byte stream (a Python file-like object which does 2423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel not perform byte-to-character conversion) for this input 2433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel source. 2443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel The SAX parser will ignore this if there is also a character 2463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel stream specified, but it will use a byte stream in preference 2473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel to opening a URI connection itself. 2483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel If the application knows the character encoding of the byte 2503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel stream, it should set it with the setEncoding method.""" 2513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__bytefile = bytefile 2523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getByteStream(self): 2543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Get the byte stream for this input source. 2553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel The getEncoding method will return the character encoding for 2573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel this byte stream, or None if unknown.""" 2583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__bytefile 2593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def setCharacterStream(self, charfile): 2613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Set the character stream for this input source. (The stream 2623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel must be a Python 2.0 Unicode-wrapped file-like that performs 2633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel conversion to Unicode strings.) 2643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel If there is a character stream specified, the SAX parser will 2663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel ignore any byte stream and will not attempt to open a URI 2673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel connection to the system identifier.""" 2683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.__charfile = charfile 2693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getCharacterStream(self): 2713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Get the character stream for this input source." 2723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__charfile 2733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# ===== ATTRIBUTESIMPL ===== 2753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass AttributesImpl: 2773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self, attrs): 2793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Non-NS-aware implementation. 2803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel attrs should be of the form {name : value}.""" 2823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._attrs = attrs 2833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getLength(self): 2853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return len(self._attrs) 2863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getType(self, name): 2883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return "CDATA" 2893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getValue(self, name): 2913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs[name] 2923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getValueByQName(self, name): 2943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs[name] 2953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getNameByQName(self, name): 2973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not name in self._attrs: 2983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise KeyError, name 2993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return name 3003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getQNameByName(self, name): 3023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not name in self._attrs: 3033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise KeyError, name 3043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return name 3053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getNames(self): 3073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs.keys() 3083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getQNames(self): 3103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs.keys() 3113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __len__(self): 3133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return len(self._attrs) 3143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __getitem__(self, name): 3163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs[name] 3173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def keys(self): 3193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs.keys() 3203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def has_key(self, name): 3223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return name in self._attrs 3233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __contains__(self, name): 3253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return name in self._attrs 3263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def get(self, name, alternative=None): 3283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs.get(name, alternative) 3293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def copy(self): 3313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__class__(self._attrs) 3323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def items(self): 3343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs.items() 3353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def values(self): 3373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs.values() 3383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# ===== ATTRIBUTESNSIMPL ===== 3403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass AttributesNSImpl(AttributesImpl): 3423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self, attrs, qnames): 3443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """NS-aware implementation. 3453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel attrs should be of the form {(ns_uri, lname): value, ...}. 3473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel qnames of the form {(ns_uri, lname): qname, ...}.""" 3483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._attrs = attrs 3493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._qnames = qnames 3503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getValueByQName(self, name): 3523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel for (nsname, qname) in self._qnames.items(): 3533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if qname == name: 3543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._attrs[nsname] 3553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise KeyError, name 3573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getNameByQName(self, name): 3593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel for (nsname, qname) in self._qnames.items(): 3603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if qname == name: 3613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return nsname 3623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise KeyError, name 3643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getQNameByName(self, name): 3663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._qnames[name] 3673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def getQNames(self): 3693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._qnames.values() 3703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def copy(self): 3723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.__class__(self._attrs, self._qnames) 3733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef _test(): 3763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel XMLReader() 3773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel IncrementalParser() 3783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Locator() 3793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielif __name__ == "__main__": 3813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel _test() 382