1
|
# XML DOM tree manipulation
|
2
|
|
3
|
import cgi
|
4
|
from HTMLParser import HTMLParser
|
5
|
from xml.dom import Node
|
6
|
import xml.dom.minidom as minidom
|
7
|
|
8
|
import strings
|
9
|
import util
|
10
|
|
11
|
def escape(str_):
|
12
|
return strings.to_unicode(cgi.escape(str_, True)).encode('ascii',
|
13
|
'xmlcharrefreplace')
|
14
|
|
15
|
def unescape(str_): return HTMLParser().unescape(str_)
|
16
|
|
17
|
def get_id(node): return node.getAttribute('id')
|
18
|
|
19
|
def set_id(node, id_): node.setAttribute('id', id_)
|
20
|
|
21
|
def is_empty(node): return node.firstChild == None
|
22
|
|
23
|
def has_one_child(node):
|
24
|
return node.firstChild != None and node.firstChild.nextSibling == None
|
25
|
|
26
|
class NodeIter:
|
27
|
def __init__(self, node): self.child = node.firstChild
|
28
|
|
29
|
def __iter__(self): return self
|
30
|
|
31
|
def curr(self):
|
32
|
if self.child != None: return self.child
|
33
|
raise StopIteration
|
34
|
|
35
|
def next(self):
|
36
|
child = self.curr()
|
37
|
self.child = self.child.nextSibling
|
38
|
return child
|
39
|
|
40
|
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
|
41
|
|
42
|
class NodeElemIter:
|
43
|
def __init__(self, node): self.child = node.firstChild
|
44
|
|
45
|
def __iter__(self): return self
|
46
|
|
47
|
def curr(self):
|
48
|
while self.child != None:
|
49
|
if is_elem(self.child): return self.child
|
50
|
self.child = self.child.nextSibling
|
51
|
raise StopIteration
|
52
|
|
53
|
def next(self):
|
54
|
child = self.curr()
|
55
|
self.child = self.child.nextSibling
|
56
|
return child
|
57
|
|
58
|
def first_elem(node): return NodeElemIter(node).next()
|
59
|
|
60
|
class NodeElemReverseIter:
|
61
|
def __init__(self, node): self.child = node.lastChild
|
62
|
|
63
|
def __iter__(self): return self
|
64
|
|
65
|
def curr(self):
|
66
|
while self.child != None:
|
67
|
if is_elem(self.child): return self.child
|
68
|
self.child = self.child.previousSibling
|
69
|
raise StopIteration
|
70
|
|
71
|
def next(self):
|
72
|
child = self.curr()
|
73
|
self.child = self.child.previousSibling
|
74
|
return child
|
75
|
|
76
|
def last_elem(node): return NodeElemReverseIter(node).next()
|
77
|
|
78
|
class NodeParentIter:
|
79
|
def __init__(self, node): self.node = node
|
80
|
|
81
|
def __iter__(self): return self
|
82
|
|
83
|
def curr(self):
|
84
|
if self.node != None and is_elem(self.node): return self.node
|
85
|
raise StopIteration
|
86
|
|
87
|
def next(self):
|
88
|
node = self.curr()
|
89
|
self.node = self.node.parentNode
|
90
|
return node
|
91
|
|
92
|
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
|
93
|
|
94
|
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
|
95
|
|
96
|
def value(node):
|
97
|
if node.firstChild != None: return node.firstChild.nodeValue
|
98
|
else: return node.nodeValue
|
99
|
|
100
|
def set_value(node, value):
|
101
|
if is_elem(node): node.appendChild(node.ownerDocument.createTextNode(value))
|
102
|
else: node.nodeValue = value
|
103
|
|
104
|
class NodeTextEntryIter:
|
105
|
def __init__(self, node): self.iter_ = NodeElemIter(node)
|
106
|
|
107
|
def __iter__(self): return self
|
108
|
|
109
|
def curr(self):
|
110
|
while True:
|
111
|
child = self.iter_.curr()
|
112
|
if is_text(child): return (child.tagName, value(child))
|
113
|
self.iter_.next()
|
114
|
|
115
|
def next(self):
|
116
|
entry = self.curr()
|
117
|
self.iter_.next()
|
118
|
return entry
|
119
|
|
120
|
def set_child(node, name, value):
|
121
|
'''Note: does not remove any existing child of the same name'''
|
122
|
child = node.ownerDocument.createElement(name)
|
123
|
set_value(child, value)
|
124
|
node.appendChild(child)
|
125
|
|
126
|
def remove(node): node.parentNode.removeChild(node)
|
127
|
|
128
|
def replace(old_node, new_node):
|
129
|
old_node.parentNode.replaceChild(new_node, old_node) # note order reversed
|
130
|
|
131
|
def replace_with_text(node, str_):
|
132
|
replace(node, node.ownerDocument.createTextNode(str_))
|
133
|
|
134
|
def by_tag_name(node, name, last_only=False):
|
135
|
'''last_only optimization returns last matching node'''
|
136
|
children = []
|
137
|
for child in NodeElemReverseIter(node):
|
138
|
if child.tagName == name:
|
139
|
children.append(child)
|
140
|
if last_only: break
|
141
|
return children
|
142
|
|
143
|
def create_doc(root='_'):
|
144
|
return minidom.getDOMImplementation().createDocument(None, root, None)
|
145
|
|
146
|
prettyxml_config = dict(addindent=' ', newl='\n')
|
147
|
toprettyxml_config = prettyxml_config.copy()
|
148
|
util.rename_key(toprettyxml_config, 'addindent', 'indent')
|
149
|
|
150
|
# minidom modifications
|
151
|
|
152
|
minidom._write_data = lambda writer, data: writer.write(escape(data))
|
153
|
|
154
|
minidom.Node.__iter__ = lambda self: NodeIter(self)
|
155
|
|
156
|
minidom.Node.__str__ = lambda self: self.toprettyxml(**toprettyxml_config)
|
157
|
|
158
|
minidom.Node.pop = lambda self: self.removeChild(self.lastChild)
|
159
|
|
160
|
def __Node_clear(self):
|
161
|
while not is_empty(self): self.pop()
|
162
|
minidom.Node.clear = __Node_clear
|
163
|
|
164
|
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
|
165
|
writer.write(indent+'<'+self.tagName)
|
166
|
for attr_idx in xrange(self.attributes.length):
|
167
|
attr = self.attributes.item(attr_idx)
|
168
|
writer.write(' '+attr.name+'='+escape(attr.value))
|
169
|
writer.write('>'+newl)
|
170
|
minidom.Element.write_opening = __Element_write_opening
|
171
|
|
172
|
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
|
173
|
writer.write('</'+self.tagName+'>'+newl)
|
174
|
minidom.Element.write_closing = __Element_write_closing
|
175
|
|
176
|
_writexml_orig = minidom.Element.writexml
|
177
|
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
|
178
|
if isinstance(indent, int): indent = addindent*indent
|
179
|
if is_text(self):
|
180
|
self.write_opening(writer, indent, addindent, '') # no newline
|
181
|
writer.write(escape(value(self)))
|
182
|
self.write_closing(writer, indent, addindent, newl)
|
183
|
else: _writexml_orig(self, writer, indent, addindent, newl)
|
184
|
minidom.Element.writexml = __Element_writexml
|
185
|
|
186
|
def __Document_write_opening(self, writer, indent='', addindent='', newl='',
|
187
|
encoding=None):
|
188
|
xmlDecl = '<?xml version="1.0" '
|
189
|
if encoding != None: xmlDecl += 'encoding="'+escape(encoding)+'"'
|
190
|
xmlDecl += '?>'+newl
|
191
|
writer.write(xmlDecl)
|
192
|
assert has_one_child(self)
|
193
|
assert is_elem(self.firstChild)
|
194
|
self.firstChild.write_opening(writer, indent, addindent, newl)
|
195
|
minidom.Document.write_opening = __Document_write_opening
|
196
|
|
197
|
def __Document_write_closing(self, writer, indent='', addindent='', newl=''):
|
198
|
self.firstChild.write_closing(writer, indent, addindent, newl)
|
199
|
minidom.Document.write_closing = __Document_write_closing
|