6 """ |
6 """ |
7 Module implementing a base class for all of eric4s XML handlers. |
7 Module implementing a base class for all of eric4s XML handlers. |
8 """ |
8 """ |
9 |
9 |
10 import sys |
10 import sys |
11 from types import UnicodeType |
11 ##from types import UnicodeType |
12 try: |
12 import pickle as pickle |
13 import cPickle as pickle |
|
14 except ImportError: |
|
15 import pickle |
|
16 |
13 |
17 from xml.sax.handler import ContentHandler |
14 from xml.sax.handler import ContentHandler |
18 |
15 |
19 class XMLHandlerBase(ContentHandler): |
16 class XMLHandlerBase(ContentHandler): |
20 """ |
17 """ |
43 |
40 |
44 self.buffer = "" |
41 self.buffer = "" |
45 self.stack = [] |
42 self.stack = [] |
46 self._marker = '__MARKER__' |
43 self._marker = '__MARKER__' |
47 |
44 |
48 self.NEWPARA = unichr(0x2029) |
45 self.NEWPARA = chr(0x2029) |
49 self.NEWLINE = unichr(0x2028) |
46 self.NEWLINE = chr(0x2028) |
50 |
47 |
51 def utf8_to_code(self, text): |
48 def utf8_to_code(self, text): |
52 """ |
49 """ |
53 Public method to convert a string to unicode and encode it for XML. |
50 Public method to convert a string to unicode and encode it for XML. |
54 |
51 |
55 @param text the text to encode (string) |
52 @param text the text to encode (string) |
56 """ |
53 """ |
57 if type(text) is not UnicodeType: |
54 # TODO: convert calls to this method to not use it anymore |
58 text = unicode(text, "utf-8") |
55 ## if not isinstance(text, UnicodeType): |
|
56 ## text = str(text, "utf-8") |
59 return text |
57 return text |
60 |
58 |
61 def unescape(self, text, attribute = False): |
59 def unescape(self, text, attribute = False): |
62 """ |
60 """ |
63 Public method used to unescape certain characters. |
61 Public method used to unescape certain characters. |
191 |
189 |
192 def endUnicode(self): |
190 def endUnicode(self): |
193 """ |
191 """ |
194 Handler method for the "unicode" end tag. |
192 Handler method for the "unicode" end tag. |
195 """ |
193 """ |
196 u = unicode(self.utf8_to_code(self.unescape(self.buffer))) |
194 u = str(self.utf8_to_code(self.unescape(self.buffer))) |
197 self.stack.append(u) |
195 self.stack.append(u) |
198 |
196 |
199 def startList(self, attrs): |
197 def startList(self, attrs): |
200 """ |
198 """ |
201 Handler method for the "list" start tag. |
199 Handler method for the "list" start tag. |