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