Project

General

Profile

1 21 aaronmk
# XML DOM tree manipulation
2
3 73 aaronmk
import cgi
4
from HTMLParser import HTMLParser
5 3632 aaronmk
import itertools
6 1809 aaronmk
import re
7 21 aaronmk
from xml.dom import Node
8 299 aaronmk
import xml.dom.minidom as minidom
9 21 aaronmk
10 73 aaronmk
import strings
11 331 aaronmk
import util
12 73 aaronmk
13 840 aaronmk
##### Escaping input
14
15 73 aaronmk
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 1814 aaronmk
##### Names
22
23
def strip_namespace(name):
24
    namespace, sep, base = name.partition(':')
25
    if sep != '': return base
26
    else: return name
27
28 2431 aaronmk
##### Nodes
29
30
def is_node(value): return isinstance(value, Node)
31
32 2008 aaronmk
##### Replacing a node
33
34
def remove(node): node.parentNode.removeChild(node)
35
36
def replace(old, new):
37
    '''@param new Node|None'''
38 3329 aaronmk
    assert old.parentNode != None # not removed from parent tree
39
40 2008 aaronmk
    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 3225 aaronmk
    '''
45
    @return The *new* node
46
    '''
47 2008 aaronmk
    if util.is_str(new): new = node.ownerDocument.createTextNode(new)
48
    replace(node, new)
49 3225 aaronmk
    return new
50 2008 aaronmk
51 840 aaronmk
##### Element node contents
52 455 aaronmk
53 840 aaronmk
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
54
55 453 aaronmk
def is_completely_empty(node): return node.firstChild == None
56 135 aaronmk
57 301 aaronmk
def has_one_child(node):
58
    return node.firstChild != None and node.firstChild.nextSibling == None
59
60 840 aaronmk
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 305 aaronmk
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 840 aaronmk
##### Comments
80 455 aaronmk
81 453 aaronmk
def is_comment(node): return node.nodeType == Node.COMMENT_NODE
82
83
def is_empty(node):
84
    for child in NodeIter(node):
85 4038 aaronmk
        if not (is_whitespace(child) or is_comment(child)): return False
86 453 aaronmk
    return True
87
88 1809 aaronmk
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 840 aaronmk
##### Child nodes that are elements
95 455 aaronmk
96 21 aaronmk
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 298 aaronmk
            if is_elem(self.child): return self.child
104 21 aaronmk
            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 450 aaronmk
def has_elems(node):
115
    try: first_elem(node); return True
116
    except StopIteration: return False
117
118 21 aaronmk
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 298 aaronmk
            if is_elem(self.child): return self.child
126 21 aaronmk
            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 840 aaronmk
##### Parent nodes
137
138 1017 aaronmk
def parent(node):
139 999 aaronmk
    '''Does not treat the document object as the root node's parent, since it's
140
    not a true element node'''
141 1017 aaronmk
    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 21 aaronmk
    def __init__(self, node): self.node = node
149
150
    def __iter__(self): return self
151
152
    def curr(self):
153 1017 aaronmk
        if self.node != None: return self.node
154
        else: raise StopIteration
155 21 aaronmk
156
    def next(self):
157
        node = self.curr()
158 1017 aaronmk
        self.node = parent(self.node)
159 21 aaronmk
        return node
160
161 840 aaronmk
##### Element nodes containing text
162 661 aaronmk
163 298 aaronmk
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
164
165 301 aaronmk
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
166 21 aaronmk
167 2008 aaronmk
def value_node(node):
168 1832 aaronmk
    if is_elem(node):
169
        iter_ = NodeIter(node)
170
        util.skip(iter_, is_comment)
171 2008 aaronmk
        try: return iter_.next()
172 1832 aaronmk
        except StopIteration: return None
173 2008 aaronmk
    else: return node
174 21 aaronmk
175 2008 aaronmk
def value(node):
176 4029 aaronmk
    value_ = util.do_ignore_none(lambda n: n.nodeValue, value_node(node))
177 4034 aaronmk
    if value_ == strings.isspace_none_str: value_ = None # None equiv
178 4029 aaronmk
    return value_
179 2008 aaronmk
180 4031 aaronmk
def is_whitespace(node):
181
    return is_text_node(node) and (node.nodeValue == ''
182
        or node.nodeValue.isspace())
183 1720 aaronmk
184 143 aaronmk
def set_value(node, value):
185 2008 aaronmk
    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 2009 aaronmk
    elif value_node_ != None:
193 4029 aaronmk
        if is_elem(node): remove(value_node_)
194
        else:
195 4033 aaronmk
            if is_text_node(node): value = strings.isspace_none_str # None equiv
196 4029 aaronmk
            node.nodeValue = value
197 22 aaronmk
198 86 aaronmk
class NodeTextEntryIter:
199
    def __init__(self, node): self.iter_ = NodeElemIter(node)
200
201
    def __iter__(self): return self
202
203 1176 aaronmk
    def next(self):
204
        entry = self.iter_.next()
205 3632 aaronmk
        if is_empty(entry): value_ = None
206
        elif is_text(entry): value_ = value(entry)
207 839 aaronmk
        else:
208
            assert has_one_child(entry) # TODO: convert to an exception
209
            value_ = entry.firstChild
210
        return (entry.tagName, value_)
211 86 aaronmk
212 792 aaronmk
def is_text_node_entry(val): return util.is_str(val[1])
213
214 3632 aaronmk
def non_empty(iterable):
215
    return itertools.ifilter(lambda i: i[1] != None, iterable)
216
217 792 aaronmk
class TextEntryOnlyIter(util.CheckedIter):
218
    def __init__(self, iterable):
219 3632 aaronmk
        util.CheckedIter.__init__(self, is_text_node_entry, non_empty(iterable))
220 792 aaronmk
221 1836 aaronmk
##### 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 2008 aaronmk
##### Child nodes
234 455 aaronmk
235 135 aaronmk
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 143 aaronmk
    set_value(child, value)
239 135 aaronmk
    node.appendChild(child)
240
241 1814 aaronmk
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 22 aaronmk
    children = []
248 888 aaronmk
    if last_only: iter_ = NodeElemReverseIter(node)
249
    else: iter_ = NodeElemIter(node)
250
    for child in iter_:
251 1814 aaronmk
        if filter_name(child.tagName) == name:
252 22 aaronmk
            children.append(child)
253
            if last_only: break
254
    return children
255 28 aaronmk
256 3226 aaronmk
def merge(from_, into):
257 3331 aaronmk
    '''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 3226 aaronmk
    for child in NodeIter(from_): into.appendChild(child)
265
    remove(from_)
266
267 3331 aaronmk
    # Recurse
268
    merge(from_first, from_first.previousSibling) # = into.lastChild
269 3226 aaronmk
270 3331 aaronmk
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 840 aaronmk
##### XML documents
277 455 aaronmk
278 133 aaronmk
def create_doc(root='_'):
279 303 aaronmk
    return minidom.getDOMImplementation().createDocument(None, root, None)
280 133 aaronmk
281 840 aaronmk
##### Printing XML
282 455 aaronmk
283 304 aaronmk
prettyxml_config = dict(addindent='    ', newl='\n')
284 331 aaronmk
toprettyxml_config = prettyxml_config.copy()
285
util.rename_key(toprettyxml_config, 'addindent', 'indent')
286 304 aaronmk
287 840 aaronmk
##### minidom modifications
288 455 aaronmk
289 1720 aaronmk
#### Module
290
291 299 aaronmk
minidom._write_data = lambda writer, data: writer.write(escape(data))
292 73 aaronmk
293 305 aaronmk
minidom.Node.__iter__ = lambda self: NodeIter(self)
294
295 857 aaronmk
def __Node_str(self):
296 860 aaronmk
    return strings.remove_extra_newl(self.toprettyxml(**toprettyxml_config))
297 796 aaronmk
minidom.Node.__str__ = __Node_str
298
minidom.Node.__repr__ = __Node_str
299
minidom.Element.__repr__ = __Node_str
300 301 aaronmk
301 1720 aaronmk
#### Node
302 888 aaronmk
303 315 aaronmk
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 1720 aaronmk
#### 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 301 aaronmk
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
326 1563 aaronmk
    writer.write(indent+'<'+escape(self.tagName))
327 298 aaronmk
    for attr_idx in xrange(self.attributes.length):
328 891 aaronmk
        writer.write(' '+str(self.attributes.item(attr_idx)))
329 298 aaronmk
    writer.write('>'+newl)
330 301 aaronmk
minidom.Element.write_opening = __Element_write_opening
331 73 aaronmk
332 301 aaronmk
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
333 1563 aaronmk
    writer.write('</'+escape(self.tagName)+'>'+newl)
334 301 aaronmk
minidom.Element.write_closing = __Element_write_closing
335 298 aaronmk
336 1720 aaronmk
__Element_writexml_orig = minidom.Element.writexml
337 301 aaronmk
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
338 306 aaronmk
    if isinstance(indent, int): indent = addindent*indent
339 774 aaronmk
    if is_simple(self):
340
        writer.write(indent)
341 1720 aaronmk
        __Element_writexml_orig(self, writer)
342 774 aaronmk
        writer.write(newl)
343 1720 aaronmk
    else: __Element_writexml_orig(self, writer, indent, addindent, newl)
344 301 aaronmk
minidom.Element.writexml = __Element_writexml
345 28 aaronmk
346 1720 aaronmk
#### Document
347
348 301 aaronmk
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