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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# -*- coding: utf-8 -*-
#
# XML factory - parser and composer
#
# Copyright 2016-2018 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim.common.enumeration import *
from awlsim.common.util import *
import xml.etree.ElementTree
import xml.sax.saxutils as saxutils
__all__ = [
"XmlFactory",
]
class _XmlFactoryBuilder(object):
def __init__(self, xmlFactory):
self.__factoryList = []
self.__tags = []
self.pushFactory(xmlFactory)
def start(self, tagName, attrs):
try:
xmlFactory = self.__factoryList[-1]
except IndexError:
raise XmlFactory.Error("Starting tag, but "
"no factory object is available.")
tag = xmlFactory.Tag(name=tagName,
attrs=attrs)
self.__tags.append(tag)
xmlFactory.parser_beginTag(tag)
def end(self, tagName):
try:
xmlFactory = self.__factoryList[-1]
except IndexError:
raise XmlFactory.Error("Closing tag, but "
"no factory object is available.")
tag = None
while tag is None or tag.name != tagName:
try:
tag = self.__tags.pop()
except IndexError:
raise XmlFactory.Error("Closing tag <%s>, "
"which is not open." % tagName)
xmlFactory.parser_endTag(tag)
def data(self, data):
try:
xmlFactory = self.__factoryList[-1]
except IndexError:
raise XmlFactory.Error("Receiving tag data, but "
"no factory object is available.")
xmlFactory.parser_data(data)
def close(self):
while self.__factoryList:
self.popFactory(self.__factoryList[-1])
self.__tags = []
return None
def pushFactory(self, xmlFactory):
self.__factoryList.append(xmlFactory)
xmlFactory.builder = self
xmlFactory.parser_open(self.__tags[-1] if self.__tags else None)
def popFactory(self, xmlFactory):
try:
oldFactory = self.__factoryList.pop()
oldFactory.parser_close()
assert(oldFactory is xmlFactory)
except IndexError:
pass
class _XmlFactoryError(Exception):
pass
class XmlFactory(object):
"""XML parser and factory."""
XML_VERSION = "1.0"
XML_ENCODING = "UTF-8"
Error = _XmlFactoryError
class Tag(object):
"""An XML tag as it is being used for the
XmlFactory parser and composer interfaces.
"""
__slots__ = (
"name",
"attrs",
"tags",
"data",
"comment",
"flags",
)
class NoDefault: pass
EnumGen.start
FLAG_ATTR_LINE_BREAK = EnumGen.bitmask
FLAG_EMIT_EMPTY_ATTRS = EnumGen.bitmask
FLAG_EMIT_EMPTY_TAG = EnumGen.bitmask
FLAG_USE_CDATA = EnumGen.bitmask
EnumGen.end
def __init__(self,
name, # Tag name
attrs=None, # Tag attributes
tags=None, # Child tags
data=None, # Tag data
comment=None, # Comment data
attrLineBreak=False, # Use line breaks between attrs?
emitEmptyAttrs=False, # Emit attributes with empty data?
emitEmptyTag=False, # Emit tag, if it is completely empty?
useCDATA=False): # Use CDATA for tag data?
self.name = name or ""
self.attrs = attrs or {}
self.tags = tags or []
self.data = data or ""
self.comment = comment or ""
self.flags = self.FLAG_ATTR_LINE_BREAK if attrLineBreak else 0
self.flags |= self.FLAG_EMIT_EMPTY_ATTRS if emitEmptyAttrs else 0
self.flags |= self.FLAG_EMIT_EMPTY_TAG if emitEmptyTag else 0
self.flags |= self.FLAG_USE_CDATA if useCDATA else 0
@property
def attrLineBreak(self):
return bool(self.flags & self.FLAG_ATTR_LINE_BREAK)
@property
def emitEmptyAttrs(self):
return bool(self.flags & self.FLAG_EMIT_EMPTY_ATTRS)
@property
def emitEmptyTag(self):
return bool(self.flags & self.FLAG_EMIT_EMPTY_TAG)
@property
def useCDATA(self):
return bool(self.flags & self.FLAG_USE_CDATA)
def hasAttr(self, name):
return name in self.attrs
def getAttr(self, name, default=NoDefault):
try:
return self.attrs[name]
except KeyError:
if default is self.NoDefault:
raise XmlFactory.Error("Tag <%s> attribute "
"'%s' does not exist." % (
self.name, name))
return default
def getAttrInt(self, name, default=NoDefault):
try:
return int(self.attrs[name])
except KeyError:
if default is self.NoDefault:
raise XmlFactory.Error("Tag <%s> attribute "
"'%s' does not exist." % (
self.name, name))
except ValueError:
if default is self.NoDefault:
raise XmlFactory.Error("Tag <%s> attribute "
"'%s' is not an integer." % (
self.name, name))
return default
def getAttrBool(self, name, default=NoDefault):
return bool(self.getAttrInt(name,
default if default is self.NoDefault else int(bool(default))))
def getAttrFloat(self, name, default=NoDefault):
try:
return float(self.attrs[name])
except KeyError:
if default is self.NoDefault:
raise XmlFactory.Error("Tag <%s> attribute "
"'%s' does not exist." % (
self.name, name))
except ValueError:
if default is self.NoDefault:
raise XmlFactory.Error("Tag <%s> attribute "
"'%s' is not an floating point value." % (
self.name, name))
return default
class Comment(Tag):
"""An XML comment.
"""
def __init__(self, text):
super(XmlFactory.Comment, self).__init__(name=None,
comment=text)
def __init__(self, **kwargs):
self.builder = None
self.__genXmlHeader = True
self.__baseIndent = 0
self.__lineBreakStr = "\n"
self.__globalAttrLineBreak = False
for kwarg, kwval in dictItems(kwargs):
setattr(self, kwarg, kwval)
def parser_open(self, tag=None):
pass
def parser_close(self):
pass
def parser_beginTag(self, tag): #@nocov
printWarning("[XML-parser - %s] Unhandled tag: <%s>" % (
type(self).__name__, tag.name))
def parser_endTag(self, tag): #@nocov
printWarning("[XML-parser - %s] Unhandled tag: </%s>" % (
type(self).__name__, tag.name))
def parser_data(self, data):
data = data.strip()
if data: #@nocov
printWarning("[XML-parser - %s] Unhandled data: %s" % (
type(self).__name__, data))
def composer_getTags(self):
raise NotImplementedError
def __tags2text(self, tags, indent=0):
ret = []
for tagIndex, tag in enumerate(tags):
ind = "\t" * indent
# Force convert attrs to str and remove empty attrs
attrs = { str(aName) : str(aVal)
for aName, aVal in dictItems(tag.attrs)
if tag.emitEmptyAttrs or str(aVal)
}
# Convert attrs to XML
if self.__globalAttrLineBreak or tag.attrLineBreak:
attrSpacer = self.__lineBreakStr + ind +\
(" " * (1 + len(tag.name) + 1))
else:
attrSpacer = " "
attrText = (" " + attrSpacer.join(
"%s=%s" % (aName, saxutils.quoteattr(aVal))
for aName, aVal in sorted(dictItems(attrs),
key=lambda a: a[0])
)).rstrip()
# Convert the child tags to XML
if tag.tags:
childTags = self.__tags2text(tag.tags, indent + 1)
else:
childTags = []
# Add comment, if any.
def addComment(comment):
if comment.startswith("\n"):
prefix = "" if (tagIndex == 0) else self.__lineBreakStr
comment = comment[1:]
else:
prefix = ""
ret.append("%s%s<!-- %s -->" % (
prefix, ind,
comment.replace("-->", " ->")))
# Convert tags to XML
data = tag.data
if data or childTags:
if tag.comment:
addComment(tag.comment)
if tag.name:
if data:
if tag.useCDATA:
# Escape CDATA-end
data = data.replace("]]>", "]]]]><![CDATA[>")
# Create CDATA section
data = "<![CDATA[%s]]>" % data
else:
data = saxutils.escape(tag.data)
startStr = "%s<%s%s>%s" % (
ind, tag.name,
attrText, data)
if childTags:
ret.append(startStr)
ret.extend(childTags)
ret.append("%s</%s>" % (
ind, tag.name))
else:
ret.append("%s</%s>" % (
startStr, tag.name))
elif attrs or tag.emitEmptyTag:
if tag.comment:
addComment(tag.comment)
if tag.name:
ret.append(
"%s<%s%s />" % (
ind,
tag.name,
attrText)
)
return ret
def compose(self, genXmlHeader=True, baseIndent=0, lineBreakStr="\n", attrLineBreak=False, stripWs=False):
self.builder = None
self.__genXmlHeader = genXmlHeader
self.__baseIndent = baseIndent
self.__lineBreakStr = lineBreakStr
self.__globalAttrLineBreak = attrLineBreak
lines = []
if self.__genXmlHeader:
lines.append('<?xml version="%s" encoding="%s" standalone="yes"?>' % (
self.XML_VERSION, self.XML_ENCODING))
lines.extend(self.__tags2text(self.composer_getTags(),
self.__baseIndent))
xmlStr = lineBreakStr.join(lines) + lineBreakStr
if stripWs:
xmlStr = xmlStr.strip()
return xmlStr.encode(self.XML_ENCODING)
def parser_switchTo(self, otherFactory):
self.builder.pushFactory(otherFactory)
def parser_finish(self):
self.builder.popFactory(self)
def parse(self, xmlText):
if not xmlText.decode(self.XML_ENCODING).strip():
return False #@nocov
try:
builder = _XmlFactoryBuilder(self)
parser = xml.etree.ElementTree.XMLParser(target=builder)
self.parser_open(None)
parser.feed(xmlText)
parser.close()
except xml.etree.ElementTree.ParseError as e:
raise XmlFactory.Error("Failed to parse "
"XML data: %s" % str(e))
return True
|