_exceptions.py 4.67 KB
Newer Older
1 2 3 4
"""Different kinds of SAX Exceptions"""
import sys
if sys.platform[:4] == "java":
    from java.lang import Exception
5
del sys
6 7

# ===== SAXEXCEPTION =====
8

9 10 11 12 13 14 15 16 17
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
    ErrorHandler interface, you are not actually required to throw
    the exception; instead, you can simply read the information in
    it."""
18 19

    def __init__(self, msg, exception=None):
20 21 22 23
        """Creates an exception. The message is required, but the exception
        is optional."""
        self._msg = msg
        self._exception = exception
24
        Exception.__init__(self, msg)
25 26

    def getMessage(self):
27 28
        "Return a message for this exception."
        return self._msg
29 30 31 32 33 34 35 36 37 38 39 40

    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."""
41
        raise AttributeError("__getitem__")
42

43

44 45
# ===== SAXPARSEEXCEPTION =====

46
class SAXParseException(SAXException):
47
    """Encapsulate an XML parse error or warning.
48

49 50 51 52 53 54 55 56 57
    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
    to throw the exception; instead, it can simply read the
    information in it and take a different action.

    Since this exception is a subclass of SAXException, it inherits
    the ability to wrap another exception."""
58

59 60 61 62
    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
63

64 65 66 67 68 69 70 71
        # We need to cache this stuff at construction time.
        # If this exception is thrown, the objects through which we must
        # 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()

72
    def getColumnNumber(self):
73
        """The column number of the end of the text where the exception
74
        occurred."""
75
        return self._colnum
76 77

    def getLineNumber(self):
78
        "The line number of the end of the text where the exception occurred."
79
        return self._linenum
80 81

    def getPublicId(self):
82 83
        "Get the public identifier of the entity where the exception occurred."
        return self._locator.getPublicId()
84 85

    def getSystemId(self):
86
        "Get the system identifier of the entity where the exception occurred."
87
        return self._systemId
88 89 90

    def __str__(self):
        "Create a string representation of the exception."
91 92 93
        sysid = self.getSystemId()
        if sysid is None:
            sysid = "<unknown>"
94 95
        linenum = self.getLineNumber()
        if linenum is None:
96
            linenum = "?"
97 98
        colnum = self.getColumnNumber()
        if colnum is None:
99
            colnum = "?"
100
        return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg)
101

102

103 104 105 106 107 108 109 110 111
# ===== 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."""

112

113
# ===== SAXNOTSUPPORTEDEXCEPTION =====
114

115 116 117 118 119 120 121
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."""
122 123 124 125 126 127 128 129 130 131

# ===== 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."""