_exceptions.py 4.6 KB

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