Project

General

Profile

1
# XML DOM tree manipulation
2

    
3
import cgi
4
from HTMLParser import HTMLParser
5
import re
6
from xml.dom import Node
7
import xml.dom.minidom as minidom
8

    
9
import strings
10
import util
11

    
12
##### Escaping input
13

    
14
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
##### Names
21

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

    
27
##### Nodes
28

    
29
def is_node(value): return isinstance(value, Node)
30

    
31
##### 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
##### Element node contents
45

    
46
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
47

    
48
def is_completely_empty(node): return node.firstChild == None
49

    
50
def has_one_child(node):
51
    return node.firstChild != None and node.firstChild.nextSibling == None
52

    
53
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
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
##### Comments
73

    
74
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
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
##### Child nodes that are elements
88

    
89
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
            if is_elem(self.child): return self.child
97
            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
def has_elems(node):
108
    try: first_elem(node); return True
109
    except StopIteration: return False
110

    
111
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
            if is_elem(self.child): return self.child
119
            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
##### Parent nodes
130

    
131
def parent(node):
132
    '''Does not treat the document object as the root node's parent, since it's
133
    not a true element node'''
134
    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
    def __init__(self, node): self.node = node
142
    
143
    def __iter__(self): return self
144
    
145
    def curr(self):
146
        if self.node != None: return self.node
147
        else: raise StopIteration
148
    
149
    def next(self):
150
        node = self.curr()
151
        self.node = parent(self.node)
152
        return node
153

    
154
##### Element nodes containing text
155

    
156
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
157

    
158
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
159

    
160
def value_node(node):
161
    if is_elem(node):
162
        iter_ = NodeIter(node)
163
        util.skip(iter_, is_comment)
164
        try: return iter_.next()
165
        except StopIteration: return None
166
    else: return node
167

    
168
def value(node):
169
    return util.do_ignore_none(lambda n: n.nodeValue, value_node(node))
170

    
171
def is_whitespace(node): return is_text_node(node) and value(node).isspace()
172

    
173
def set_value(node, value):
174
    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
    elif value_node_ != None:
182
        if is_elem(node): remove(value_node_)
183
        else: node.nodeValue = value
184

    
185
class NodeTextEntryIter:
186
    def __init__(self, node): self.iter_ = NodeElemIter(node)
187
    
188
    def __iter__(self): return self
189
    
190
    def next(self):
191
        util.skip(self.iter_, is_empty)
192
        entry = self.iter_.next()
193
        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

    
199
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
##### 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
##### Child nodes
218

    
219
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
    set_value(child, value)
223
    node.appendChild(child)
224

    
225
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
    children = []
232
    if last_only: iter_ = NodeElemReverseIter(node)
233
    else: iter_ = NodeElemIter(node)
234
    for child in iter_:
235
        if filter_name(child.tagName) == name:
236
            children.append(child)
237
            if last_only: break
238
    return children
239

    
240
##### XML documents
241

    
242
def create_doc(root='_'):
243
    return minidom.getDOMImplementation().createDocument(None, root, None)
244

    
245
##### Printing XML
246

    
247
prettyxml_config = dict(addindent='    ', newl='\n')
248
toprettyxml_config = prettyxml_config.copy()
249
util.rename_key(toprettyxml_config, 'addindent', 'indent')
250

    
251
##### minidom modifications
252

    
253
#### Module
254

    
255
minidom._write_data = lambda writer, data: writer.write(escape(data))
256

    
257
minidom.Node.__iter__ = lambda self: NodeIter(self)
258

    
259
def __Node_str(self):
260
    return strings.remove_extra_newl(self.toprettyxml(**toprettyxml_config))
261
minidom.Node.__str__ = __Node_str
262
minidom.Node.__repr__ = __Node_str
263
minidom.Element.__repr__ = __Node_str
264

    
265
#### Node
266

    
267
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
#### 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
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
290
    writer.write(indent+'<'+escape(self.tagName))
291
    for attr_idx in xrange(self.attributes.length):
292
        writer.write(' '+str(self.attributes.item(attr_idx)))
293
    writer.write('>'+newl)
294
minidom.Element.write_opening = __Element_write_opening
295

    
296
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
297
    writer.write('</'+escape(self.tagName)+'>'+newl)
298
minidom.Element.write_closing = __Element_write_closing
299

    
300
__Element_writexml_orig = minidom.Element.writexml
301
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
302
    if isinstance(indent, int): indent = addindent*indent
303
    if is_simple(self):
304
        writer.write(indent)
305
        __Element_writexml_orig(self, writer)
306
        writer.write(newl)
307
    else: __Element_writexml_orig(self, writer, indent, addindent, newl)
308
minidom.Element.writexml = __Element_writexml
309

    
310
#### Document
311

    
312
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
(33-33/37)