Project

General

Profile

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 lists
11
import strings
12
import util
13

    
14
##### Escaping input
15

    
16
def escape(str_):
17
    return strings.to_unicode(cgi.escape(str_, True)).encode('ascii',
18
        'xmlcharrefreplace')
19

    
20
def unescape(str_): return HTMLParser().unescape(str_)
21

    
22
##### Names
23

    
24
def strip_namespace(name):
25
    namespace, sep, base = name.partition(':')
26
    if sep != '': return base
27
    else: return name
28

    
29
##### Nodes
30

    
31
def is_node(value): return isinstance(value, Node)
32

    
33
##### Replacing a node
34

    
35
def remove(node): node.parentNode.removeChild(node)
36

    
37
def replace(old, new):
38
    '''@param new Node|None'''
39
    assert old.parentNode != None # not removed from parent tree
40
    if new == None: new = []
41
    else: new = lists.mk_seq(new)
42
    
43
    olds_parent = old.parentNode
44
    if not new: olds_parent.removeChild(old)
45
    else: # at least one node
46
        last = new.pop(-1)
47
        olds_parent.replaceChild(last, old) # note arg order reversed
48
        for node in reversed(new):
49
            olds_parent.insertBefore(node, last) # there is no insertAfter()
50

    
51
def bool2str(val):
52
    '''For use with replace_with_text()'''
53
    if val: return '1'
54
    else: return None # remove node
55

    
56
def replace_with_text(node, new):
57
    '''
58
    @return The *new* node
59
    '''
60
    if isinstance(new, bool): new = bool2str(new)
61
    if util.is_str(new): new = node.ownerDocument.createTextNode(new)
62
    replace(node, new)
63
    return new
64

    
65
##### Element node contents
66

    
67
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
68

    
69
def is_completely_empty(node): return node.firstChild == None
70

    
71
def has_one_child(node):
72
    return node.firstChild != None and node.firstChild.nextSibling == None
73

    
74
def only_child(node):
75
    if not has_one_child(node):
76
        raise Exception('Must contain only one child:\n'+strings.ustr(node))
77
    return node.firstChild
78

    
79
def is_simple(node):
80
    '''Whether every child recursively has no more than one child'''
81
    return (not is_elem(node) or is_completely_empty(node)
82
        or (has_one_child(node) and is_simple(node.firstChild)))
83

    
84
class NodeIter:
85
    def __init__(self, node): self.child = node.firstChild
86
    
87
    def __iter__(self): return self
88
    
89
    def curr(self):
90
        if self.child != None: return self.child
91
        raise StopIteration
92
    
93
    def next(self):
94
        child = self.curr()
95
        self.child = self.child.nextSibling
96
        return child
97

    
98
##### Comments
99

    
100
def is_comment(node): return node.nodeType == Node.COMMENT_NODE
101

    
102
def is_empty(node):
103
    for child in NodeIter(node):
104
        if not (is_whitespace(child) or is_comment(child)): return False
105
    return True
106

    
107
def clean_comment(str_):
108
    '''Sanitizes comment node contents. Strips invalid strings.'''
109
    return re.sub(r'-{2,}', r'-', str_) # comments can't contain '--'
110

    
111
def mk_comment(doc, str_): return doc.createComment(clean_comment(str_))
112

    
113
##### Child nodes that are elements
114

    
115
class NodeElemIter:
116
    def __init__(self, node): self.child = node.firstChild
117
    
118
    def __iter__(self): return self
119
    
120
    def curr(self):
121
        while self.child != None:
122
            if is_elem(self.child): return self.child
123
            self.child = self.child.nextSibling
124
        raise StopIteration
125
    
126
    def next(self):
127
        child = self.curr()
128
        self.child = self.child.nextSibling
129
        return child
130

    
131
def first_elem(node): return NodeElemIter(node).next()
132

    
133
def has_elems(node):
134
    try: first_elem(node); return True
135
    except StopIteration: return False
136

    
137
class NodeElemReverseIter:
138
    def __init__(self, node): self.child = node.lastChild
139
    
140
    def __iter__(self): return self
141
    
142
    def curr(self):
143
        while self.child != None:
144
            if is_elem(self.child): return self.child
145
            self.child = self.child.previousSibling
146
        raise StopIteration
147
    
148
    def next(self):
149
        child = self.curr()
150
        self.child = self.child.previousSibling
151
        return child
152

    
153
def last_elem(node): return NodeElemReverseIter(node).next()
154

    
155
class NodeEntryIter:
156
    def __init__(self, node): self.iter_ = NodeElemIter(node)
157
    
158
    def __iter__(self): return self
159
    
160
    def next(self):
161
        entry = self.iter_.next()
162
        values = list(NodeIter(entry))
163
        if not values: values = None
164
        elif len(values) == 1: values = values[0]
165
        return (entry.tagName, values)
166

    
167
##### Parent nodes
168

    
169
def parent(node):
170
    '''Does not treat the document object as the root node's parent, since it's
171
    not a true element node'''
172
    parent_ = node.parentNode
173
    if parent_ != None and is_elem(parent_): return parent_
174
    else: return None
175

    
176
class NodeParentIter:
177
    '''See parent() for special treatment of root node.
178
    Note that the first element returned is the current node, not its parent.'''
179
    def __init__(self, node): self.node = node
180
    
181
    def __iter__(self): return self
182
    
183
    def curr(self):
184
        if self.node != None: return self.node
185
        else: raise StopIteration
186
    
187
    def next(self):
188
        node = self.curr()
189
        self.node = parent(self.node)
190
        return node
191

    
192
##### Element nodes containing text
193

    
194
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
195

    
196
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
197

    
198
def value_node(node):
199
    if is_elem(node):
200
        iter_ = NodeIter(node)
201
        util.skip(iter_, is_comment)
202
        try: return iter_.next()
203
        except StopIteration: return None
204
    else: return node
205

    
206
def value(node):
207
    value_ = util.do_ignore_none(lambda n: n.nodeValue, value_node(node))
208
    if value_ == strings.isspace_none_str: value_ = None # None equiv
209
    return value_
210

    
211
def is_whitespace(node):
212
    return is_text_node(node) and (node.nodeValue == ''
213
        or node.nodeValue.isspace())
214

    
215
def set_value(node, value):
216
    value_node_ = value_node(node)
217
    if value != None:
218
        if value_node_ != None:
219
            value_node_.nodeValue = value
220
        else:
221
            assert is_elem(node)
222
            node.appendChild(node.ownerDocument.createTextNode(value))
223
    elif value_node_ != None:
224
        if is_elem(node): remove(value_node_)
225
        else:
226
            if is_text_node(node): value = strings.isspace_none_str # None equiv
227
            node.nodeValue = value
228

    
229
class NodeTextEntryIter:
230
    def __init__(self, node): self.iter_ = NodeElemIter(node)
231
    
232
    def __iter__(self): return self
233
    
234
    def next(self):
235
        entry = self.iter_.next()
236
        if is_empty(entry): value_ = None
237
        elif is_text(entry): value_ = value(entry)
238
        else: value_ = only_child(entry)
239
        return (entry.tagName, value_)
240

    
241
def is_text_node_entry(val): return util.is_str(val[1])
242

    
243
def non_empty(iterable):
244
    return itertools.ifilter(lambda i: i[1] != None, iterable)
245

    
246
class TextEntryOnlyIter(util.CheckedIter):
247
    def __init__(self, iterable):
248
        util.CheckedIter.__init__(self, is_text_node_entry, non_empty(iterable))
249

    
250
##### IDs
251

    
252
def get_id(node):
253
    '''If the node doesn't have an ID, assumes the node itself is the ID.
254
    @return None if the node doesn't have an ID or a value
255
    '''
256
    id_ = node.getAttribute('id')
257
    if id_ != '': return id_
258
    else: return value(node) # assume the node itself is the ID
259

    
260
def set_id(node, id_): node.setAttribute('id', id_)
261

    
262
##### Child nodes
263

    
264
def prune_empty(node):
265
    '''Removes node if it's empty'''
266
    if is_empty(node): remove(node)
267

    
268
def prune_children(node):
269
    '''Removes empty children'''
270
    for child in NodeElemIter(node): prune_empty(child)
271

    
272
def prune(node):
273
    '''Removes empty children and then node if it's empty'''
274
    prune_children(node)
275
    prune_empty(node)
276

    
277
def set_child(node, name, value):
278
    '''Note: does not remove any existing child of the same name'''
279
    child = node.ownerDocument.createElement(name)
280
    set_value(child, value)
281
    node.appendChild(child)
282

    
283
def by_tag_name(node, name, last_only=False, ignore_namespace=False):
284
    '''last_only optimization returns only the last matching node'''
285
    if ignore_namespace: filter_name = strip_namespace
286
    else: filter_name = lambda name: name
287
    name = filter_name(name)
288
    
289
    children = []
290
    if last_only: iter_ = NodeElemReverseIter(node)
291
    else: iter_ = NodeElemIter(node)
292
    for child in iter_:
293
        if filter_name(child.tagName) == name:
294
            children.append(child)
295
            if last_only: break
296
    return children
297

    
298
def merge(from_, into):
299
    '''Merges two nodes of the same tag name and their newly-adjacent children.
300
    @post The into node is saved; the from_ node is deleted.
301
    '''
302
    if from_ == None or into == None: return # base case
303
    if from_.tagName != into.tagName: return # not mergeable
304
    
305
    from_first = from_.firstChild # save before merge
306
    for child in NodeIter(from_): into.appendChild(child)
307
    remove(from_)
308
    
309
    # Recurse
310
    merge(from_first, from_first.previousSibling) # = into.lastChild
311

    
312
def merge_by_name(root, name):
313
    '''Merges siblings in root with the given name'''
314
    children = by_tag_name(root, name)
315
    child0 = children.pop(0)
316
    for child in children: merge(child, child0)
317

    
318
##### XML documents
319

    
320
def create_doc(root='_'):
321
    return minidom.getDOMImplementation().createDocument(None, root, None)
322

    
323
##### Printing XML
324

    
325
prettyxml_config = dict(addindent='    ', newl='\n')
326
toprettyxml_config = prettyxml_config.copy()
327
util.rename_key(toprettyxml_config, 'addindent', 'indent')
328

    
329
##### minidom modifications
330

    
331
#### Module
332

    
333
minidom._write_data = lambda writer, data: writer.write(escape(data))
334

    
335
minidom.Node.__iter__ = lambda self: NodeIter(self)
336

    
337
def __Node_str(self):
338
    return strings.remove_extra_newl(self.toprettyxml(**toprettyxml_config))
339
minidom.Node.__str__ = __Node_str
340
minidom.Node.__repr__ = __Node_str
341
minidom.Element.__repr__ = __Node_str
342

    
343
#### Node
344

    
345
minidom.Node.pop = lambda self: self.removeChild(self.lastChild)
346

    
347
def __Node_clear(self):
348
    while not is_empty(self): self.pop()
349
minidom.Node.clear = __Node_clear
350

    
351
#### Text
352

    
353
__Text_writexml_orig = minidom.Text.writexml
354
def __Text_writexml(self, *args, **kw_args):
355
    if is_whitespace(self): pass # we add our own whitespace
356
    else: __Text_writexml_orig(self, *args, **kw_args)
357
minidom.Text.writexml = __Text_writexml
358

    
359
#### Attr
360

    
361
def __Attr_str(self): return escape(self.name)+'="'+escape(self.value)+'"'
362
minidom.Attr.__str__ = __Attr_str
363
minidom.Attr.__repr__ = __Attr_str
364

    
365
#### Element
366

    
367
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
368
    writer.write(indent+'<'+escape(self.tagName))
369
    for attr_idx in xrange(self.attributes.length):
370
        writer.write(' '+strings.ustr(self.attributes.item(attr_idx)))
371
    writer.write('>'+newl)
372
minidom.Element.write_opening = __Element_write_opening
373

    
374
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
375
    writer.write('</'+escape(self.tagName)+'>'+newl)
376
minidom.Element.write_closing = __Element_write_closing
377

    
378
__Element_writexml_orig = minidom.Element.writexml
379
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
380
    if isinstance(indent, int): indent = addindent*indent
381
    if is_simple(self):
382
        writer.write(indent)
383
        __Element_writexml_orig(self, writer)
384
        writer.write(newl)
385
    else: __Element_writexml_orig(self, writer, indent, addindent, newl)
386
minidom.Element.writexml = __Element_writexml
387

    
388
#### Document
389

    
390
def __Document_write_opening(self, writer, indent='', addindent='', newl='',
391
    encoding=None):
392
    xmlDecl = '<?xml version="1.0" '
393
    if encoding != None: xmlDecl += 'encoding="'+escape(encoding)+'"'
394
    xmlDecl += '?>'+newl
395
    writer.write(xmlDecl)
396
    assert has_one_child(self)
397
    assert is_elem(self.firstChild)
398
    self.firstChild.write_opening(writer, indent, addindent, newl)
399
minidom.Document.write_opening = __Document_write_opening
400

    
401
def __Document_write_closing(self, writer, indent='', addindent='', newl=''):
402
    self.firstChild.write_closing(writer, indent, addindent, newl)
403
minidom.Document.write_closing = __Document_write_closing
(36-36/40)