|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an error handler class. |
|
8 """ |
|
9 |
|
10 from xml.sax.handler import ErrorHandler |
|
11 from xml.sax import SAXParseException |
|
12 |
|
13 from .XMLMessageDialog import XMLMessageDialog |
|
14 |
|
15 class XMLParseError(Exception): |
|
16 """ |
|
17 Class implementing an exception for recoverable parse errors. |
|
18 """ |
|
19 pass |
|
20 |
|
21 class XMLFatalParseError(XMLParseError): |
|
22 """ |
|
23 Class implementing an exception for recoverable parse errors. |
|
24 """ |
|
25 pass |
|
26 |
|
27 class XMLErrorHandler(ErrorHandler): |
|
28 """ |
|
29 Class implementing an error handler class. |
|
30 """ |
|
31 def __init__(self): |
|
32 """ |
|
33 Constructor |
|
34 """ |
|
35 self.errors = 0 |
|
36 self.fatals = 0 |
|
37 self.warnings = 0 |
|
38 self.totals = 0 |
|
39 |
|
40 # list of tuples of (message type, system id, line number, |
|
41 # column number, message) |
|
42 self.msgs = [] |
|
43 |
|
44 def error(self, exception): |
|
45 """ |
|
46 Public method to handle a recoverable error. |
|
47 |
|
48 @param exception Exception object describing the error (SAXParseException) |
|
49 """ |
|
50 self.errors += 1 |
|
51 self.totals += 1 |
|
52 self.msgs.append((\ |
|
53 "E", |
|
54 exception.getSystemId(), |
|
55 exception.getLineNumber(), |
|
56 exception.getColumnNumber(), |
|
57 exception.getMessage() |
|
58 )) |
|
59 |
|
60 def fatalError(self, exception): |
|
61 """ |
|
62 Public method to handle a non-recoverable error. |
|
63 |
|
64 @param exception Exception object describing the error (SAXParseException) |
|
65 @exception XMLFatalParseError a fatal parse error has occured |
|
66 """ |
|
67 self.fatals += 1 |
|
68 self.totals += 1 |
|
69 self.msgs.append((\ |
|
70 "F", |
|
71 exception.getSystemId(), |
|
72 exception.getLineNumber(), |
|
73 exception.getColumnNumber(), |
|
74 exception.getMessage() |
|
75 )) |
|
76 raise XMLFatalParseError |
|
77 |
|
78 def warning(self, exception): |
|
79 """ |
|
80 Public method to handle a warning. |
|
81 |
|
82 @param exception Exception object describing the error (SAXParseException) |
|
83 """ |
|
84 self.warnings += 1 |
|
85 self.totals += 1 |
|
86 self.msgs.append((\ |
|
87 "W", |
|
88 exception.getSystemId(), |
|
89 exception.getLineNumber(), |
|
90 exception.getColumnNumber(), |
|
91 exception.getMessage() |
|
92 )) |
|
93 |
|
94 def getParseMessages(self): |
|
95 """ |
|
96 Public method to retrieve all messages. |
|
97 |
|
98 @return list of tuples of (message type, system id, line no, column no, |
|
99 message) |
|
100 """ |
|
101 return self.msgs |
|
102 |
|
103 def showParseMessages(self): |
|
104 """ |
|
105 Public method to show the parse messages (if any) in a dialog. |
|
106 """ |
|
107 if self.totals: |
|
108 dlg = XMLMessageDialog(self.msgs, None) |
|
109 dlg.exec_() |