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