1
|
# XML DOM tree manipulation
|
2
|
|
3
|
import cgi
|
4
|
from HTMLParser import HTMLParser
|
5
|
import itertools
|
6
|
import re
|
7
|
from xml.dom import Node
|
8
|
import xml.dom.minidom as minidom
|
9
|
|
10
|
import strings
|
11
|
import util
|
12
|
|
13
|
##### Escaping input
|
14
|
|
15
|
def escape(str_):
|
16
|
return strings.to_unicode(cgi.escape(str_, True)).encode('ascii',
|
17
|
'xmlcharrefreplace')
|
18
|
|
19
|
def unescape(str_): return HTMLParser().unescape(str_)
|
20
|
|
21
|
##### Names
|
22
|
|
23
|
def strip_namespace(name):
|
24
|
namespace, sep, base = name.partition(':')
|
25
|
if sep != '': return base
|
26
|
else: return name
|
27
|
|
28
|
##### Nodes
|
29
|
|
30
|
def is_node(value): return isinstance(value, Node)
|
31
|
|
32
|
##### Replacing a node
|
33
|
|
34
|
def remove(node): node.parentNode.removeChild(node)
|
35
|
|
36
|
def replace(old, new):
|
37
|
'''@param new Node|None'''
|
38
|
assert old.parentNode != None # not removed from parent tree
|
39
|
|
40
|
if new == None: old.parentNode.removeChild(old)
|
41
|
else: old.parentNode.replaceChild(new, old) # note order reversed
|
42
|
|
43
|
def replace_with_text(node, new):
|
44
|
'''
|
45
|
@return The *new* node
|
46
|
'''
|
47
|
if util.is_str(new): new = node.ownerDocument.createTextNode(new)
|
48
|
replace(node, new)
|
49
|
return new
|
50
|
|
51
|
##### Element node contents
|
52
|
|
53
|
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
|
54
|
|
55
|
def is_completely_empty(node): return node.firstChild == None
|
56
|
|
57
|
def has_one_child(node):
|
58
|
return node.firstChild != None and node.firstChild.nextSibling == None
|
59
|
|
60
|
def is_simple(node):
|
61
|
'''Whether every child recursively has no more than one child'''
|
62
|
return (not is_elem(node) or is_completely_empty(node)
|
63
|
or (has_one_child(node) and is_simple(node.firstChild)))
|
64
|
|
65
|
class NodeIter:
|
66
|
def __init__(self, node): self.child = node.firstChild
|
67
|
|
68
|
def __iter__(self): return self
|
69
|
|
70
|
def curr(self):
|
71
|
if self.child != None: return self.child
|
72
|
raise StopIteration
|
73
|
|
74
|
def next(self):
|
75
|
child = self.curr()
|
76
|
self.child = self.child.nextSibling
|
77
|
return child
|
78
|
|
79
|
##### Comments
|
80
|
|
81
|
def is_comment(node): return node.nodeType == Node.COMMENT_NODE
|
82
|
|
83
|
def is_empty(node):
|
84
|
for child in NodeIter(node):
|
85
|
if not is_comment(child): return False
|
86
|
return True
|
87
|
|
88
|
def clean_comment(str_):
|
89
|
'''Sanitizes comment node contents. Strips invalid strings.'''
|
90
|
return re.sub(r'-{2,}', r'-', str_) # comments can't contain '--'
|
91
|
|
92
|
def mk_comment(doc, str_): return doc.createComment(clean_comment(str_))
|
93
|
|
94
|
##### Child nodes that are elements
|
95
|
|
96
|
class NodeElemIter:
|
97
|
def __init__(self, node): self.child = node.firstChild
|
98
|
|
99
|
def __iter__(self): return self
|
100
|
|
101
|
def curr(self):
|
102
|
while self.child != None:
|
103
|
if is_elem(self.child): return self.child
|
104
|
self.child = self.child.nextSibling
|
105
|
raise StopIteration
|
106
|
|
107
|
def next(self):
|
108
|
child = self.curr()
|
109
|
self.child = self.child.nextSibling
|
110
|
return child
|
111
|
|
112
|
def first_elem(node): return NodeElemIter(node).next()
|
113
|
|
114
|
def has_elems(node):
|
115
|
try: first_elem(node); return True
|
116
|
except StopIteration: return False
|
117
|
|
118
|
class NodeElemReverseIter:
|
119
|
def __init__(self, node): self.child = node.lastChild
|
120
|
|
121
|
def __iter__(self): return self
|
122
|
|
123
|
def curr(self):
|
124
|
while self.child != None:
|
125
|
if is_elem(self.child): return self.child
|
126
|
self.child = self.child.previousSibling
|
127
|
raise StopIteration
|
128
|
|
129
|
def next(self):
|
130
|
child = self.curr()
|
131
|
self.child = self.child.previousSibling
|
132
|
return child
|
133
|
|
134
|
def last_elem(node): return NodeElemReverseIter(node).next()
|
135
|
|
136
|
##### Parent nodes
|
137
|
|
138
|
def parent(node):
|
139
|
'''Does not treat the document object as the root node's parent, since it's
|
140
|
not a true element node'''
|
141
|
parent_ = node.parentNode
|
142
|
if parent_ != None and is_elem(parent_): return parent_
|
143
|
else: return None
|
144
|
|
145
|
class NodeParentIter:
|
146
|
'''See parent() for special treatment of root node.
|
147
|
Note that the first element returned is the current node, not its parent.'''
|
148
|
def __init__(self, node): self.node = node
|
149
|
|
150
|
def __iter__(self): return self
|
151
|
|
152
|
def curr(self):
|
153
|
if self.node != None: return self.node
|
154
|
else: raise StopIteration
|
155
|
|
156
|
def next(self):
|
157
|
node = self.curr()
|
158
|
self.node = parent(self.node)
|
159
|
return node
|
160
|
|
161
|
##### Element nodes containing text
|
162
|
|
163
|
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
|
164
|
|
165
|
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
|
166
|
|
167
|
def value_node(node):
|
168
|
if is_elem(node):
|
169
|
iter_ = NodeIter(node)
|
170
|
util.skip(iter_, is_comment)
|
171
|
try: return iter_.next()
|
172
|
except StopIteration: return None
|
173
|
else: return node
|
174
|
|
175
|
def value(node):
|
176
|
value_ = util.do_ignore_none(lambda n: n.nodeValue, value_node(node))
|
177
|
if value_ is strings.none_str: value_ = None # text node's None
|
178
|
return value_
|
179
|
|
180
|
def is_whitespace(node):
|
181
|
return is_text_node(node) and (node.nodeValue == ''
|
182
|
or node.nodeValue.isspace())
|
183
|
|
184
|
def set_value(node, value):
|
185
|
value_node_ = value_node(node)
|
186
|
if value != None:
|
187
|
if value_node_ != None:
|
188
|
value_node_.nodeValue = value
|
189
|
else:
|
190
|
assert is_elem(node)
|
191
|
node.appendChild(node.ownerDocument.createTextNode(value))
|
192
|
elif value_node_ != None:
|
193
|
if is_elem(node): remove(value_node_)
|
194
|
else:
|
195
|
if is_text_node(node): value = strings.none_str # text node's None
|
196
|
node.nodeValue = value
|
197
|
|
198
|
class NodeTextEntryIter:
|
199
|
def __init__(self, node): self.iter_ = NodeElemIter(node)
|
200
|
|
201
|
def __iter__(self): return self
|
202
|
|
203
|
def next(self):
|
204
|
entry = self.iter_.next()
|
205
|
if is_empty(entry): value_ = None
|
206
|
elif is_text(entry): value_ = value(entry)
|
207
|
else:
|
208
|
assert has_one_child(entry) # TODO: convert to an exception
|
209
|
value_ = entry.firstChild
|
210
|
return (entry.tagName, value_)
|
211
|
|
212
|
def is_text_node_entry(val): return util.is_str(val[1])
|
213
|
|
214
|
def non_empty(iterable):
|
215
|
return itertools.ifilter(lambda i: i[1] != None, iterable)
|
216
|
|
217
|
class TextEntryOnlyIter(util.CheckedIter):
|
218
|
def __init__(self, iterable):
|
219
|
util.CheckedIter.__init__(self, is_text_node_entry, non_empty(iterable))
|
220
|
|
221
|
##### IDs
|
222
|
|
223
|
def get_id(node):
|
224
|
'''If the node doesn't have an ID, assumes the node itself is the ID.
|
225
|
@return None if the node doesn't have an ID or a value
|
226
|
'''
|
227
|
id_ = node.getAttribute('id')
|
228
|
if id_ != '': return id_
|
229
|
else: return value(node) # assume the node itself is the ID
|
230
|
|
231
|
def set_id(node, id_): node.setAttribute('id', id_)
|
232
|
|
233
|
##### Child nodes
|
234
|
|
235
|
def set_child(node, name, value):
|
236
|
'''Note: does not remove any existing child of the same name'''
|
237
|
child = node.ownerDocument.createElement(name)
|
238
|
set_value(child, value)
|
239
|
node.appendChild(child)
|
240
|
|
241
|
def by_tag_name(node, name, last_only=False, ignore_namespace=False):
|
242
|
'''last_only optimization returns only the last matching node'''
|
243
|
if ignore_namespace: filter_name = strip_namespace
|
244
|
else: filter_name = lambda name: name
|
245
|
name = filter_name(name)
|
246
|
|
247
|
children = []
|
248
|
if last_only: iter_ = NodeElemReverseIter(node)
|
249
|
else: iter_ = NodeElemIter(node)
|
250
|
for child in iter_:
|
251
|
if filter_name(child.tagName) == name:
|
252
|
children.append(child)
|
253
|
if last_only: break
|
254
|
return children
|
255
|
|
256
|
def merge(from_, into):
|
257
|
'''Merges two nodes of the same tag name and their newly-adjacent children.
|
258
|
@post The into node is saved; the from_ node is deleted.
|
259
|
'''
|
260
|
if from_ == None or into == None: return # base case
|
261
|
if from_.tagName != into.tagName: return # not mergeable
|
262
|
|
263
|
from_first = from_.firstChild # save before merge
|
264
|
for child in NodeIter(from_): into.appendChild(child)
|
265
|
remove(from_)
|
266
|
|
267
|
# Recurse
|
268
|
merge(from_first, from_first.previousSibling) # = into.lastChild
|
269
|
|
270
|
def merge_by_name(root, name):
|
271
|
'''Merges siblings in root with the given name'''
|
272
|
children = by_tag_name(root, name)
|
273
|
child0 = children.pop(0)
|
274
|
for child in children: merge(child, child0)
|
275
|
|
276
|
##### XML documents
|
277
|
|
278
|
def create_doc(root='_'):
|
279
|
return minidom.getDOMImplementation().createDocument(None, root, None)
|
280
|
|
281
|
##### Printing XML
|
282
|
|
283
|
prettyxml_config = dict(addindent=' ', newl='\n')
|
284
|
toprettyxml_config = prettyxml_config.copy()
|
285
|
util.rename_key(toprettyxml_config, 'addindent', 'indent')
|
286
|
|
287
|
##### minidom modifications
|
288
|
|
289
|
#### Module
|
290
|
|
291
|
minidom._write_data = lambda writer, data: writer.write(escape(data))
|
292
|
|
293
|
minidom.Node.__iter__ = lambda self: NodeIter(self)
|
294
|
|
295
|
def __Node_str(self):
|
296
|
return strings.remove_extra_newl(self.toprettyxml(**toprettyxml_config))
|
297
|
minidom.Node.__str__ = __Node_str
|
298
|
minidom.Node.__repr__ = __Node_str
|
299
|
minidom.Element.__repr__ = __Node_str
|
300
|
|
301
|
#### Node
|
302
|
|
303
|
minidom.Node.pop = lambda self: self.removeChild(self.lastChild)
|
304
|
|
305
|
def __Node_clear(self):
|
306
|
while not is_empty(self): self.pop()
|
307
|
minidom.Node.clear = __Node_clear
|
308
|
|
309
|
#### Text
|
310
|
|
311
|
__Text_writexml_orig = minidom.Text.writexml
|
312
|
def __Text_writexml(self, *args, **kw_args):
|
313
|
if is_whitespace(self): pass # we add our own whitespace
|
314
|
else: __Text_writexml_orig(self, *args, **kw_args)
|
315
|
minidom.Text.writexml = __Text_writexml
|
316
|
|
317
|
#### Attr
|
318
|
|
319
|
def __Attr_str(self): return escape(self.name)+'="'+escape(self.value)+'"'
|
320
|
minidom.Attr.__str__ = __Attr_str
|
321
|
minidom.Attr.__repr__ = __Attr_str
|
322
|
|
323
|
#### Element
|
324
|
|
325
|
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
|
326
|
writer.write(indent+'<'+escape(self.tagName))
|
327
|
for attr_idx in xrange(self.attributes.length):
|
328
|
writer.write(' '+str(self.attributes.item(attr_idx)))
|
329
|
writer.write('>'+newl)
|
330
|
minidom.Element.write_opening = __Element_write_opening
|
331
|
|
332
|
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
|
333
|
writer.write('</'+escape(self.tagName)+'>'+newl)
|
334
|
minidom.Element.write_closing = __Element_write_closing
|
335
|
|
336
|
__Element_writexml_orig = minidom.Element.writexml
|
337
|
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
|
338
|
if isinstance(indent, int): indent = addindent*indent
|
339
|
if is_simple(self):
|
340
|
writer.write(indent)
|
341
|
__Element_writexml_orig(self, writer)
|
342
|
writer.write(newl)
|
343
|
else: __Element_writexml_orig(self, writer, indent, addindent, newl)
|
344
|
minidom.Element.writexml = __Element_writexml
|
345
|
|
346
|
#### Document
|
347
|
|
348
|
def __Document_write_opening(self, writer, indent='', addindent='', newl='',
|
349
|
encoding=None):
|
350
|
xmlDecl = '<?xml version="1.0" '
|
351
|
if encoding != None: xmlDecl += 'encoding="'+escape(encoding)+'"'
|
352
|
xmlDecl += '?>'+newl
|
353
|
writer.write(xmlDecl)
|
354
|
assert has_one_child(self)
|
355
|
assert is_elem(self.firstChild)
|
356
|
self.firstChild.write_opening(writer, indent, addindent, newl)
|
357
|
minidom.Document.write_opening = __Document_write_opening
|
358
|
|
359
|
def __Document_write_closing(self, writer, indent='', addindent='', newl=''):
|
360
|
self.firstChild.write_closing(writer, indent, addindent, newl)
|
361
|
minidom.Document.write_closing = __Document_write_closing
|