_exceptions.py 4.7 KB

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