2000-06-29 16:34:54 -03:00
|
|
|
"""Different kinds of SAX Exceptions"""
|
|
|
|
|
|
|
|
# ===== SAXEXCEPTION =====
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
class SAXException(Exception):
|
|
|
|
"""Encapsulate an XML error or warning. This class can contain
|
|
|
|
basic error or warning information from either the XML parser or
|
|
|
|
the application: you can subclass it to provide additional
|
|
|
|
functionality, or to add localization. Note that although you will
|
|
|
|
receive a SAXException as the argument to the handlers in the
|
2012-12-18 15:14:22 -04:00
|
|
|
ErrorHandler interface, you are not actually required to raise
|
2000-06-29 16:34:54 -03:00
|
|
|
the exception; instead, you can simply read the information in
|
|
|
|
it."""
|
2000-09-18 13:22:30 -03:00
|
|
|
|
|
|
|
def __init__(self, msg, exception=None):
|
2000-06-29 16:34:54 -03:00
|
|
|
"""Creates an exception. The message is required, but the exception
|
|
|
|
is optional."""
|
|
|
|
self._msg = msg
|
|
|
|
self._exception = exception
|
2001-01-27 04:56:24 -04:00
|
|
|
Exception.__init__(self, msg)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getMessage(self):
|
2000-07-07 18:02:00 -03:00
|
|
|
"Return a message for this exception."
|
|
|
|
return self._msg
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getException(self):
|
|
|
|
"Return the embedded exception, or None if there was none."
|
|
|
|
return self._exception
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"Create a string representation of the exception."
|
|
|
|
return self._msg
|
|
|
|
|
|
|
|
def __getitem__(self, ix):
|
|
|
|
"""Avoids weird error messages if someone does exception[ix] by
|
|
|
|
mistake, since Exception has __getitem__ defined."""
|
2000-09-21 13:32:28 -03:00
|
|
|
raise AttributeError("__getitem__")
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
# ===== SAXPARSEEXCEPTION =====
|
|
|
|
|
2000-10-23 15:09:50 -03:00
|
|
|
class SAXParseException(SAXException):
|
2000-06-29 16:34:54 -03:00
|
|
|
"""Encapsulate an XML parse error or warning.
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
This exception will include information for locating the error in
|
|
|
|
the original XML document. Note that although the application will
|
|
|
|
receive a SAXParseException as the argument to the handlers in the
|
|
|
|
ErrorHandler interface, the application is not actually required
|
2012-12-18 15:14:22 -04:00
|
|
|
to raise the exception; instead, it can simply read the
|
2000-06-29 16:34:54 -03:00
|
|
|
information in it and take a different action.
|
|
|
|
|
|
|
|
Since this exception is a subclass of SAXException, it inherits
|
|
|
|
the ability to wrap another exception."""
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def __init__(self, msg, exception, locator):
|
|
|
|
"Creates the exception. The exception parameter is allowed to be None."
|
|
|
|
SAXException.__init__(self, msg, exception)
|
|
|
|
self._locator = locator
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2001-03-22 11:34:02 -04:00
|
|
|
# We need to cache this stuff at construction time.
|
2012-12-18 15:14:22 -04:00
|
|
|
# If this exception is raised, the objects through which we must
|
2001-03-22 11:34:02 -04:00
|
|
|
# traverse to get this information may be deleted by the time
|
|
|
|
# it gets caught.
|
|
|
|
self._systemId = self._locator.getSystemId()
|
|
|
|
self._colnum = self._locator.getColumnNumber()
|
|
|
|
self._linenum = self._locator.getLineNumber()
|
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
def getColumnNumber(self):
|
2000-07-07 18:02:00 -03:00
|
|
|
"""The column number of the end of the text where the exception
|
2000-10-23 15:09:50 -03:00
|
|
|
occurred."""
|
2001-03-22 11:34:02 -04:00
|
|
|
return self._colnum
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getLineNumber(self):
|
2000-07-07 18:02:00 -03:00
|
|
|
"The line number of the end of the text where the exception occurred."
|
2001-03-22 11:34:02 -04:00
|
|
|
return self._linenum
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getPublicId(self):
|
2000-07-07 18:02:00 -03:00
|
|
|
"Get the public identifier of the entity where the exception occurred."
|
|
|
|
return self._locator.getPublicId()
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getSystemId(self):
|
2000-07-07 18:02:00 -03:00
|
|
|
"Get the system identifier of the entity where the exception occurred."
|
2001-03-22 11:34:02 -04:00
|
|
|
return self._systemId
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"Create a string representation of the exception."
|
2000-10-09 13:45:54 -03:00
|
|
|
sysid = self.getSystemId()
|
|
|
|
if sysid is None:
|
|
|
|
sysid = "<unknown>"
|
2004-03-20 04:15:30 -04:00
|
|
|
linenum = self.getLineNumber()
|
|
|
|
if linenum is None:
|
2004-07-18 03:16:08 -03:00
|
|
|
linenum = "?"
|
2004-03-20 04:15:30 -04:00
|
|
|
colnum = self.getColumnNumber()
|
|
|
|
if colnum is None:
|
2004-07-18 03:16:08 -03:00
|
|
|
colnum = "?"
|
2004-03-20 04:15:30 -04:00
|
|
|
return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
# ===== SAXNOTRECOGNIZEDEXCEPTION =====
|
|
|
|
|
|
|
|
class SAXNotRecognizedException(SAXException):
|
|
|
|
"""Exception class for an unrecognized identifier.
|
|
|
|
|
|
|
|
An XMLReader will raise this exception when it is confronted with an
|
|
|
|
unrecognized feature or property. SAX applications and extensions may
|
|
|
|
use this class for similar purposes."""
|
|
|
|
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
# ===== SAXNOTSUPPORTEDEXCEPTION =====
|
2000-09-18 13:22:30 -03:00
|
|
|
|
2000-06-29 16:34:54 -03:00
|
|
|
class SAXNotSupportedException(SAXException):
|
|
|
|
"""Exception class for an unsupported operation.
|
|
|
|
|
|
|
|
An XMLReader will raise this exception when a service it cannot
|
|
|
|
perform is requested (specifically setting a state or value). SAX
|
|
|
|
applications and extensions may use this class for similar
|
|
|
|
purposes."""
|
2000-10-06 14:41:52 -03:00
|
|
|
|
|
|
|
# ===== SAXNOTSUPPORTEDEXCEPTION =====
|
|
|
|
|
|
|
|
class SAXReaderNotAvailable(SAXNotSupportedException):
|
|
|
|
"""Exception class for a missing driver.
|
|
|
|
|
|
|
|
An XMLReader module (driver) should raise this exception when it
|
|
|
|
is first imported, e.g. when a support module cannot be imported.
|
|
|
|
It also may be raised during parsing, e.g. if executing an external
|
|
|
|
program is not permitted."""
|