|
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 Exception.__init__(self, msg) |
|
25 |
|
26 def getMessage(self): |
|
27 "Return a message for this exception." |
|
28 return self._msg |
|
29 |
|
30 def getException(self): |
|
31 "Return the embedded exception, or None if there was none." |
|
32 return self._exception |
|
33 |
|
34 def __str__(self): |
|
35 "Create a string representation of the exception." |
|
36 return self._msg |
|
37 |
|
38 def __getitem__(self, ix): |
|
39 """Avoids weird error messages if someone does exception[ix] by |
|
40 mistake, since Exception has __getitem__ defined.""" |
|
41 raise AttributeError("__getitem__") |
|
42 |
|
43 |
|
44 # ===== SAXPARSEEXCEPTION ===== |
|
45 |
|
46 class SAXParseException(SAXException): |
|
47 """Encapsulate an XML parse error or warning. |
|
48 |
|
49 This exception will include information for locating the error in |
|
50 the original XML document. Note that although the application will |
|
51 receive a SAXParseException as the argument to the handlers in the |
|
52 ErrorHandler interface, the application is not actually required |
|
53 to throw the exception; instead, it can simply read the |
|
54 information in it and take a different action. |
|
55 |
|
56 Since this exception is a subclass of SAXException, it inherits |
|
57 the ability to wrap another exception.""" |
|
58 |
|
59 def __init__(self, msg, exception, locator): |
|
60 "Creates the exception. The exception parameter is allowed to be None." |
|
61 SAXException.__init__(self, msg, exception) |
|
62 self._locator = locator |
|
63 |
|
64 # We need to cache this stuff at construction time. |
|
65 # If this exception is thrown, the objects through which we must |
|
66 # traverse to get this information may be deleted by the time |
|
67 # it gets caught. |
|
68 self._systemId = self._locator.getSystemId() |
|
69 self._colnum = self._locator.getColumnNumber() |
|
70 self._linenum = self._locator.getLineNumber() |
|
71 |
|
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 "Get the public identifier of the entity where the exception occurred." |
|
83 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 def __str__(self): |
|
90 "Create a string representation of the exception." |
|
91 sysid = self.getSystemId() |
|
92 if sysid is None: |
|
93 sysid = "<unknown>" |
|
94 linenum = self.getLineNumber() |
|
95 if linenum is None: |
|
96 linenum = "?" |
|
97 colnum = self.getColumnNumber() |
|
98 if colnum is None: |
|
99 colnum = "?" |
|
100 return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg) |
|
101 |
|
102 |
|
103 # ===== SAXNOTRECOGNIZEDEXCEPTION ===== |
|
104 |
|
105 class SAXNotRecognizedException(SAXException): |
|
106 """Exception class for an unrecognized identifier. |
|
107 |
|
108 An XMLReader will raise this exception when it is confronted with an |
|
109 unrecognized feature or property. SAX applications and extensions may |
|
110 use this class for similar purposes.""" |
|
111 |
|
112 |
|
113 # ===== SAXNOTSUPPORTEDEXCEPTION ===== |
|
114 |
|
115 class SAXNotSupportedException(SAXException): |
|
116 """Exception class for an unsupported operation. |
|
117 |
|
118 An XMLReader will raise this exception when a service it cannot |
|
119 perform is requested (specifically setting a state or value). SAX |
|
120 applications and extensions may use this class for similar |
|
121 purposes.""" |
|
122 |
|
123 # ===== SAXNOTSUPPORTEDEXCEPTION ===== |
|
124 |
|
125 class SAXReaderNotAvailable(SAXNotSupportedException): |
|
126 """Exception class for a missing driver. |
|
127 |
|
128 An XMLReader module (driver) should raise this exception when it |
|
129 is first imported, e.g. when a support module cannot be imported. |
|
130 It also may be raised during parsing, e.g. if executing an external |
|
131 program is not permitted.""" |