1"""Different kinds of SAX Exceptions""" 2 3# ===== SAXEXCEPTION ===== 4 5class SAXException(Exception): 6 """Encapsulate an XML error or warning. This class can contain 7 basic error or warning information from either the XML parser or 8 the application: you can subclass it to provide additional 9 functionality, or to add localization. Note that although you will 10 receive a SAXException as the argument to the handlers in the 11 ErrorHandler interface, you are not actually required to raise 12 the exception; instead, you can simply read the information in 13 it.""" 14 15 def __init__(self, msg, exception=None): 16 """Creates an exception. The message is required, but the exception 17 is optional.""" 18 self._msg = msg 19 self._exception = exception 20 Exception.__init__(self, msg) 21 22 def getMessage(self): 23 "Return a message for this exception." 24 return self._msg 25 26 def getException(self): 27 "Return the embedded exception, or None if there was none." 28 return self._exception 29 30 def __str__(self): 31 "Create a string representation of the exception." 32 return self._msg 33 34 def __getitem__(self, ix): 35 """Avoids weird error messages if someone does exception[ix] by 36 mistake, since Exception has __getitem__ defined.""" 37 raise AttributeError("__getitem__") 38 39 40# ===== SAXPARSEEXCEPTION ===== 41 42class SAXParseException(SAXException): 43 """Encapsulate an XML parse error or warning. 44 45 This exception will include information for locating the error in 46 the original XML document. Note that although the application will 47 receive a SAXParseException as the argument to the handlers in the 48 ErrorHandler interface, the application is not actually required 49 to raise the exception; instead, it can simply read the 50 information in it and take a different action. 51 52 Since this exception is a subclass of SAXException, it inherits 53 the ability to wrap another exception.""" 54 55 def __init__(self, msg, exception, locator): 56 "Creates the exception. The exception parameter is allowed to be None." 57 SAXException.__init__(self, msg, exception) 58 self._locator = locator 59 60 # We need to cache this stuff at construction time. 61 # If this exception is raised, the objects through which we must 62 # traverse to get this information may be deleted by the time 63 # it gets caught. 64 self._systemId = self._locator.getSystemId() 65 self._colnum = self._locator.getColumnNumber() 66 self._linenum = self._locator.getLineNumber() 67 68 def getColumnNumber(self): 69 """The column number of the end of the text where the exception 70 occurred.""" 71 return self._colnum 72 73 def getLineNumber(self): 74 "The line number of the end of the text where the exception occurred." 75 return self._linenum 76 77 def getPublicId(self): 78 "Get the public identifier of the entity where the exception occurred." 79 return self._locator.getPublicId() 80 81 def getSystemId(self): 82 "Get the system identifier of the entity where the exception occurred." 83 return self._systemId 84 85 def __str__(self): 86 "Create a string representation of the exception." 87 sysid = self.getSystemId() 88 if sysid is None: 89 sysid = "<unknown>" 90 linenum = self.getLineNumber() 91 if linenum is None: 92 linenum = "?" 93 colnum = self.getColumnNumber() 94 if colnum is None: 95 colnum = "?" 96 return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg) 97 98 99# ===== SAXNOTRECOGNIZEDEXCEPTION ===== 100 101class SAXNotRecognizedException(SAXException): 102 """Exception class for an unrecognized identifier. 103 104 An XMLReader will raise this exception when it is confronted with an 105 unrecognized feature or property. SAX applications and extensions may 106 use this class for similar purposes.""" 107 108 109# ===== SAXNOTSUPPORTEDEXCEPTION ===== 110 111class SAXNotSupportedException(SAXException): 112 """Exception class for an unsupported operation. 113 114 An XMLReader will raise this exception when a service it cannot 115 perform is requested (specifically setting a state or value). SAX 116 applications and extensions may use this class for similar 117 purposes.""" 118 119# ===== SAXNOTSUPPORTEDEXCEPTION ===== 120 121class SAXReaderNotAvailable(SAXNotSupportedException): 122 """Exception class for a missing driver. 123 124 An XMLReader module (driver) should raise this exception when it 125 is first imported, e.g. when a support module cannot be imported. 126 It also may be raised during parsing, e.g. if executing an external 127 program is not permitted.""" 128