14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao"""Different kinds of SAX Exceptions""" 24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport sys 34adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoif sys.platform[:4] == "java": 44adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao from java.lang import Exception 54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodel sys 64adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 74adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# ===== SAXEXCEPTION ===== 84adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 94adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass SAXException(Exception): 104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Encapsulate an XML error or warning. This class can contain 114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao basic error or warning information from either the XML parser or 124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao the application: you can subclass it to provide additional 134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao functionality, or to add localization. Note that although you will 144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao receive a SAXException as the argument to the handlers in the 154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao ErrorHandler interface, you are not actually required to raise 164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao the exception; instead, you can simply read the information in 174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao it.""" 184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def __init__(self, msg, exception=None): 204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Creates an exception. The message is required, but the exception 214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao is optional.""" 224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self._msg = msg 234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self._exception = exception 244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao Exception.__init__(self, msg) 254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def getMessage(self): 274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Return a message for this exception." 284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._msg 294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def getException(self): 314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Return the embedded exception, or None if there was none." 324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._exception 334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def __str__(self): 354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Create a string representation of the exception." 364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._msg 374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def __getitem__(self, ix): 394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Avoids weird error messages if someone does exception[ix] by 404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao mistake, since Exception has __getitem__ defined.""" 414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao raise AttributeError("__getitem__") 424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# ===== SAXPARSEEXCEPTION ===== 454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass SAXParseException(SAXException): 474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Encapsulate an XML parse error or warning. 484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao This exception will include information for locating the error in 504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao the original XML document. Note that although the application will 514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao receive a SAXParseException as the argument to the handlers in the 524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao ErrorHandler interface, the application is not actually required 534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao to raise the exception; instead, it can simply read the 544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao information in it and take a different action. 554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao Since this exception is a subclass of SAXException, it inherits 574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao the ability to wrap another exception.""" 584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def __init__(self, msg, exception, locator): 604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Creates the exception. The exception parameter is allowed to be None." 614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao SAXException.__init__(self, msg, exception) 624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self._locator = locator 634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao # We need to cache this stuff at construction time. 654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao # If this exception is raised, the objects through which we must 664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao # traverse to get this information may be deleted by the time 674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao # it gets caught. 684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self._systemId = self._locator.getSystemId() 694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self._colnum = self._locator.getColumnNumber() 704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao self._linenum = self._locator.getLineNumber() 714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def getColumnNumber(self): 734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """The column number of the end of the text where the exception 744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao occurred.""" 754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._colnum 764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def getLineNumber(self): 784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "The line number of the end of the text where the exception occurred." 794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._linenum 804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def getPublicId(self): 824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Get the public identifier of the entity where the exception occurred." 834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._locator.getPublicId() 844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def getSystemId(self): 864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Get the system identifier of the entity where the exception occurred." 874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return self._systemId 884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao def __str__(self): 904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao "Create a string representation of the exception." 914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao sysid = self.getSystemId() 924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao if sysid is None: 934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao sysid = "<unknown>" 944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao linenum = self.getLineNumber() 954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao if linenum is None: 964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao linenum = "?" 974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao colnum = self.getColumnNumber() 984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao if colnum is None: 994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao colnum = "?" 1004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg) 1014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# ===== SAXNOTRECOGNIZEDEXCEPTION ===== 1044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass SAXNotRecognizedException(SAXException): 1064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Exception class for an unrecognized identifier. 1074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao An XMLReader will raise this exception when it is confronted with an 1094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao unrecognized feature or property. SAX applications and extensions may 1104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao use this class for similar purposes.""" 1114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# ===== SAXNOTSUPPORTEDEXCEPTION ===== 1144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass SAXNotSupportedException(SAXException): 1164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Exception class for an unsupported operation. 1174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao An XMLReader will raise this exception when a service it cannot 1194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao perform is requested (specifically setting a state or value). SAX 1204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao applications and extensions may use this class for similar 1214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao purposes.""" 1224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# ===== SAXNOTSUPPORTEDEXCEPTION ===== 1244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass SAXReaderNotAvailable(SAXNotSupportedException): 1264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao """Exception class for a missing driver. 1274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao 1284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao An XMLReader module (driver) should raise this exception when it 1294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao is first imported, e.g. when a support module cannot be imported. 1304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao It also may be raised during parsing, e.g. if executing an external 1314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao program is not permitted.""" 132