home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / _EXCEPTIONS.PY < prev    next >
Encoding:
Python Source  |  2000-10-09  |  4.3 KB  |  118 lines

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