1 |
# Copyright (c) 2003 by Intevation GmbH |
# Copyright (c) 2003, 2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> |
4 |
# |
# |
11 |
|
|
12 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
13 |
|
|
14 |
import os, string |
import os |
15 |
from types import UnicodeType |
from types import UnicodeType |
16 |
|
|
17 |
|
from Thuban import unicode_from_internal |
18 |
|
|
19 |
# |
# |
20 |
# one level of indention |
# one level of indention |
21 |
# |
# |
24 |
def escape(data): |
def escape(data): |
25 |
"""Escape &, \", ', <, and > in a string of data. |
"""Escape &, \", ', <, and > in a string of data. |
26 |
""" |
""" |
27 |
data = string.replace(data, "&", "&") |
data = data.replace("&", "&") |
28 |
data = string.replace(data, "<", "<") |
data = data.replace("<", "<") |
29 |
data = string.replace(data, ">", ">") |
data = data.replace(">", ">") |
30 |
data = string.replace(data, '"', """) |
data = data.replace('"', """) |
31 |
data = string.replace(data, "'", "'") |
data = data.replace("'", "'") |
32 |
return data |
return data |
33 |
|
|
34 |
class XMLWriter: |
class XMLWriter: |
121 |
self.file.write(' %s="%s"' % (self.encode(name), |
self.file.write(' %s="%s"' % (self.encode(name), |
122 |
self.encode(value))) |
self.encode(value))) |
123 |
|
|
124 |
def encode(self, str): |
def encode(self, s): |
125 |
"""Return an XML-escaped and UTF-8 encoded copy of the string str.""" |
"""Return an XML-escaped and UTF-8 encoded copy of the string s. |
|
|
|
|
esc = escape(str) |
|
126 |
|
|
127 |
if isinstance(esc, UnicodeType): |
The parameter must be a string in Thuban's internal string |
128 |
return esc.encode("utf8") |
representation or a unicode object. |
129 |
else: |
""" |
130 |
return unicode(escape(str),'latin1').encode("utf8") |
if not isinstance(s, unicode): |
131 |
|
s = unicode_from_internal(s) |
132 |
|
return escape(s).encode("utf8") |