Project

General

Profile

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