forked from sequitur-g2p/sequitur-g2p
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlwriter.py
142 lines (120 loc) · 4.46 KB
/
xmlwriter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
__author__ = 'Maximilian Bisani'
__version__ = '$LastChangedRevision: 1667 $'
__date__ = '$LastChangedDate: 2007-06-02 16:32:35 +0200 (Sat, 02 Jun 2007) $'
__copyright__ = 'Copyright (c) 2004-2005 RWTH Aachen University'
__license__ = """
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License Version 2 (June
1991) as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, you will find it at
http://www.gnu.org/licenses/gpl.html, or write to the Free Software
Foundation, Inc., 51 Franlin Street, Fifth Floor, Boston, MA 02110,
USA.
Should a provision of no. 9 and 10 of the GNU General Public License
be invalid or become invalid, a valid provision is deemed to have been
agreed upon which comes closest to what the parties intended
commercially. In any case guarantee/warranty shall be limited to gross
negligent actions or intended actions or fraudulent concealment.
"""
import codecs, string, types
class XmlWriter:
def __init__(self, file, encoding='ISO-8859-1'):
self.path = []
self.encoding = encoding
encoder, decoder, streamReader, streamWriter = codecs.lookup(encoding)
self.file = streamWriter(file)
self.margin = 78
def write(self, data):
self.file.write(data)
def begin(self):
self.write(u'<?xml version="1.0" encoding="%s"?>\n' %
self.encoding)
def end(self):
assert len(self.path) == 0
pass
def indent_str(self):
return u' ' * len(self.path)
def escapeSpecialCharacters(self, w):
w = string.replace(w, '&', '&')
w = string.replace(w, '<', '<')
w = string.replace(w, '>', '>')
return w
def formTag(self, element, attr=[]):
result = string.join([element] + [ u'%s="%s"' % kv for kv in attr ])
return self.escapeSpecialCharacters(result)
def open(self, element, **args):
attr = filter(lambda (k, v): v is not None, args.items())
self.write(self.indent_str() + u'<' + self.formTag(element, attr) + u'>\n')
self.path.append(element)
def empty(self, element, **args):
attr = filter(lambda (k, v): v is not None, args.items())
self.write(self.indent_str() + u'<' + self.formTag(element, attr) + u'/>\n')
def close(self, element):
assert element == self.path[-1]
del self.path[-1]
self.write(self.indent_str() + u'</' + element + u'>\n')
def openComment(self):
self.write(u'<!--\n')
self.path.append('u<!--')
def closeComment(self):
assert self.path[-1] == 'u<!--'
del self.path[-1]
self.write(u'-->\n')
formatRaw = 0
formatIndent = 1
formatBreakLines = 2
formatFill = 3
def fillParagraph(self, w):
indent_str = self.indent_str()
ll = []
l = [] ; n = len(indent_str)
for a in w.split():
if n + len(a) < self.margin:
n = n + len(a) + 1
l.append(a)
else:
ll.append(indent_str + string.join(l))
l = [a] ; n = len(indent_str) + len(a)
if len(l) > 0:
ll.append(indent_str + string.join(l))
return ll
def cdata(self, w, format = formatFill):
if 'u<!--' in self.path:
w = w.replace(u'--', u'=') # comment must not contain double-hyphens
if format == self.formatRaw:
out = [ w ]
elif format == self.formatIndent:
indentStr = self.indent_str()
out = [ indentStr + line for line in w.split(u'\n') ]
elif format == self.formatBreakLines:
out = [ self.fillParagraph(line) for line in w.split(u'\n') ]
out = reduce(operator.add, out)
elif format == self.formatFill:
out = self.fillParagraph(w)
self.write(string.join(out, u'\n') + u'\n')
def formatted_cdata(self, s):
for w in string.split(s, u'\\n'):
self.cdata(w, self.formatFill)
def comment(self, comment):
comment = string.replace(comment, u'--', u'=') # comment must not contain double-hyphens
self.cdata(u'<!-- ' + comment + u' -->')
def element(self, element, cdata=None, **args):
if cdata is None:
apply(self.empty, (element,), args)
else:
attr = filter(lambda (k, v): v is not None, args.items())
s = self.indent_str() \
+ u'<' + self.formTag(element, attr) + u'>' \
+ unicode(cdata) \
+ u'</' + element + u'>'
if len(s) <= self.margin:
self.write(s + u'\n')
else:
apply(self.open, (element,), args)
self.cdata(cdata)
self.close(element)