1cc671dd3caac1d0cbf3f6999ab063c9ff1f297b2Dake Gu"""Different kinds of SAX Exceptions"""
26193c12a1897723c87b41f4e304a8cd04deef2dcDake Guimport sys
36193c12a1897723c87b41f4e304a8cd04deef2dcDake Guif sys.platform[:4] == "java":
461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    from java.lang import Exception
561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gudel sys
661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu# ===== SAXEXCEPTION =====
861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
961905b0b52c50018dcaebcd79699c39b8f28d622Dake Guclass SAXException(Exception):
1061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    """Encapsulate an XML error or warning. This class can contain
1161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    basic error or warning information from either the XML parser or
1261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    the application: you can subclass it to provide additional
1361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    functionality, or to add localization. Note that although you will
1461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    receive a SAXException as the argument to the handlers in the
1561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    ErrorHandler interface, you are not actually required to raise
1661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    the exception; instead, you can simply read the information in
1761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    it."""
1861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
1961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    def __init__(self, msg, exception=None):
2061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        """Creates an exception. The message is required, but the exception
2161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        is optional."""
2261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        self._msg = msg
232f5ebf3f6f7bb6a24856f389e369b247118ba119susnata        self._exception = exception
240246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu        Exception.__init__(self, msg)
252f5ebf3f6f7bb6a24856f389e369b247118ba119susnata
262f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    def getMessage(self):
272f5ebf3f6f7bb6a24856f389e369b247118ba119susnata        "Return a message for this exception."
280246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu        return self._msg
2961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
3061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    def getException(self):
3161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        "Return the embedded exception, or None if there was none."
3261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return self._exception
3361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
3461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    def __str__(self):
3561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        "Create a string representation of the exception."
3661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return self._msg
37fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak
3861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    def __getitem__(self, ix):
3999ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        """Avoids weird error messages if someone does exception[ix] by
4061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        mistake, since Exception has __getitem__ defined."""
4150cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        raise AttributeError("__getitem__")
4299ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas
43902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu
44bb0a680c10b84b83833a59634373140f8bd0750csusnata# ===== SAXPARSEEXCEPTION =====
4561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
4642752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Guclass SAXParseException(SAXException):
4761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    """Encapsulate an XML parse error or warning.
480246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu
490246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    This exception will include information for locating the error in
500246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    the original XML document. Note that although the application will
510246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    receive a SAXParseException as the argument to the handlers in the
520246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    ErrorHandler interface, the application is not actually required
530d841b3454f896da58deb506ca22730bfd04f34fDake Gu    to raise the exception; instead, it can simply read the
540246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    information in it and take a different action.
550246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu
560246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    Since this exception is a subclass of SAXException, it inherits
5761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    the ability to wrap another exception."""
580246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu
590246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    def __init__(self, msg, exception, locator):
6061905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        "Creates the exception. The exception parameter is allowed to be None."
6161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        SAXException.__init__(self, msg, exception)
6261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        self._locator = locator
6361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
6461905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        # We need to cache this stuff at construction time.
6561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        # If this exception is raised, the objects through which we must
6661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        # traverse to get this information may be deleted by the time
67902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        # it gets caught.
68902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        self._systemId = self._locator.getSystemId()
69902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        self._colnum = self._locator.getColumnNumber()
70902e68c114f86e8002516ff3f0248b722b6c5711Dake Gu        self._linenum = self._locator.getLineNumber()
7161905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
7261905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    def getColumnNumber(self):
7361905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        """The column number of the end of the text where the exception
7442752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu        occurred."""
7561905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return self._colnum
7661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
7761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    def getLineNumber(self):
7861905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        "The line number of the end of the text where the exception occurred."
7961905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu        return self._linenum
80fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak
81fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak    def getPublicId(self):
82fb11ded6cfa3965883e68625e0c7e14b4b4fe0b3Susnata Basak        "Get the public identifier of the entity where the exception occurred."
8350cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        return self._locator.getPublicId()
84bb0a680c10b84b83833a59634373140f8bd0750csusnata
85bb0a680c10b84b83833a59634373140f8bd0750csusnata    def getSystemId(self):
86bb0a680c10b84b83833a59634373140f8bd0750csusnata        "Get the system identifier of the entity where the exception occurred."
87bb0a680c10b84b83833a59634373140f8bd0750csusnata        return self._systemId
88bb0a680c10b84b83833a59634373140f8bd0750csusnata
89bb0a680c10b84b83833a59634373140f8bd0750csusnata    def __str__(self):
90bb0a680c10b84b83833a59634373140f8bd0750csusnata        "Create a string representation of the exception."
91bb0a680c10b84b83833a59634373140f8bd0750csusnata        sysid = self.getSystemId()
92bb0a680c10b84b83833a59634373140f8bd0750csusnata        if sysid is None:
93bb0a680c10b84b83833a59634373140f8bd0750csusnata            sysid = "<unknown>"
9499ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        linenum = self.getLineNumber()
9599ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas        if linenum is None:
9699ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas            linenum = "?"
9715375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas        colnum = self.getColumnNumber()
98bb0a680c10b84b83833a59634373140f8bd0750csusnata        if colnum is None:
99bb0a680c10b84b83833a59634373140f8bd0750csusnata            colnum = "?"
100bb0a680c10b84b83833a59634373140f8bd0750csusnata        return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg)
101bb0a680c10b84b83833a59634373140f8bd0750csusnata
10215375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas
103bb0a680c10b84b83833a59634373140f8bd0750csusnata# ===== SAXNOTRECOGNIZEDEXCEPTION =====
104bb0a680c10b84b83833a59634373140f8bd0750csusnata
105bb0a680c10b84b83833a59634373140f8bd0750csusnataclass SAXNotRecognizedException(SAXException):
106bb0a680c10b84b83833a59634373140f8bd0750csusnata    """Exception class for an unrecognized identifier.
107bb0a680c10b84b83833a59634373140f8bd0750csusnata
108bb0a680c10b84b83833a59634373140f8bd0750csusnata    An XMLReader will raise this exception when it is confronted with an
109bb0a680c10b84b83833a59634373140f8bd0750csusnata    unrecognized feature or property. SAX applications and extensions may
110bb0a680c10b84b83833a59634373140f8bd0750csusnata    use this class for similar purposes."""
111bb0a680c10b84b83833a59634373140f8bd0750csusnata
112bb0a680c10b84b83833a59634373140f8bd0750csusnata
113bb0a680c10b84b83833a59634373140f8bd0750csusnata# ===== SAXNOTSUPPORTEDEXCEPTION =====
114bb0a680c10b84b83833a59634373140f8bd0750csusnata
11561905b0b52c50018dcaebcd79699c39b8f28d622Dake Guclass SAXNotSupportedException(SAXException):
11661905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu    """Exception class for an unsupported operation.
11761905b0b52c50018dcaebcd79699c39b8f28d622Dake Gu
118bb0a680c10b84b83833a59634373140f8bd0750csusnata    An XMLReader will raise this exception when a service it cannot
119bb0a680c10b84b83833a59634373140f8bd0750csusnata    perform is requested (specifically setting a state or value). SAX
120bb0a680c10b84b83833a59634373140f8bd0750csusnata    applications and extensions may use this class for similar
121bb0a680c10b84b83833a59634373140f8bd0750csusnata    purposes."""
122bb0a680c10b84b83833a59634373140f8bd0750csusnata
123bb0a680c10b84b83833a59634373140f8bd0750csusnata# ===== SAXNOTSUPPORTEDEXCEPTION =====
124bb0a680c10b84b83833a59634373140f8bd0750csusnata
125bb0a680c10b84b83833a59634373140f8bd0750csusnataclass SAXReaderNotAvailable(SAXNotSupportedException):
126bb0a680c10b84b83833a59634373140f8bd0750csusnata    """Exception class for a missing driver.
127bb0a680c10b84b83833a59634373140f8bd0750csusnata
128bb0a680c10b84b83833a59634373140f8bd0750csusnata    An XMLReader module (driver) should raise this exception when it
129bb0a680c10b84b83833a59634373140f8bd0750csusnata    is first imported, e.g. when a support module cannot be imported.
130bb0a680c10b84b83833a59634373140f8bd0750csusnata    It also may be raised during parsing, e.g. if executing an external
131bb0a680c10b84b83833a59634373140f8bd0750csusnata    program is not permitted."""
132bb0a680c10b84b83833a59634373140f8bd0750csusnata