1 |
21
|
aaronmk
|
# XML DOM tree manipulation
|
2 |
|
|
|
3 |
73
|
aaronmk
|
import cgi
|
4 |
|
|
from HTMLParser import HTMLParser
|
5 |
21
|
aaronmk
|
from xml.dom import Node
|
6 |
28
|
aaronmk
|
import xml.dom.minidom
|
7 |
21
|
aaronmk
|
|
8 |
73
|
aaronmk
|
import strings
|
9 |
|
|
|
10 |
|
|
def escape(str_):
|
11 |
|
|
return strings.to_unicode(cgi.escape(str_, True)).encode('ascii',
|
12 |
|
|
'xmlcharrefreplace')
|
13 |
|
|
|
14 |
|
|
def unescape(str_): return HTMLParser().unescape(str_)
|
15 |
|
|
|
16 |
21
|
aaronmk
|
def name_of(node): return node.tagName.lower()
|
17 |
|
|
|
18 |
|
|
def get_id(node): return node.getAttribute('id')
|
19 |
|
|
|
20 |
|
|
def set_id(node, id_): node.setAttribute('id', id_)
|
21 |
|
|
|
22 |
|
|
class NodeElemIter:
|
23 |
|
|
def __init__(self, node): self.child = node.firstChild
|
24 |
|
|
|
25 |
|
|
def __iter__(self): return self
|
26 |
|
|
|
27 |
|
|
def curr(self):
|
28 |
|
|
while self.child != None:
|
29 |
|
|
if self.child.nodeType == Node.ELEMENT_NODE: return self.child
|
30 |
|
|
self.child = self.child.nextSibling
|
31 |
|
|
raise StopIteration
|
32 |
|
|
|
33 |
|
|
def next(self):
|
34 |
|
|
child = self.curr()
|
35 |
|
|
self.child = self.child.nextSibling
|
36 |
|
|
return child
|
37 |
|
|
|
38 |
|
|
def first_elem(node): return NodeElemIter(node).next()
|
39 |
|
|
|
40 |
|
|
class NodeElemReverseIter:
|
41 |
|
|
def __init__(self, node): self.child = node.lastChild
|
42 |
|
|
|
43 |
|
|
def __iter__(self): return self
|
44 |
|
|
|
45 |
|
|
def curr(self):
|
46 |
|
|
while self.child != None:
|
47 |
|
|
if self.child.nodeType == Node.ELEMENT_NODE: return self.child
|
48 |
|
|
self.child = self.child.previousSibling
|
49 |
|
|
raise StopIteration
|
50 |
|
|
|
51 |
|
|
def next(self):
|
52 |
|
|
child = self.curr()
|
53 |
|
|
self.child = self.child.previousSibling
|
54 |
|
|
return child
|
55 |
|
|
|
56 |
|
|
def last_elem(node): return NodeElemReverseIter(node).next()
|
57 |
|
|
|
58 |
|
|
class NodeParentIter:
|
59 |
|
|
def __init__(self, node): self.node = node
|
60 |
|
|
|
61 |
|
|
def __iter__(self): return self
|
62 |
|
|
|
63 |
|
|
def curr(self):
|
64 |
|
|
if self.node != None and self.node.nodeType == Node.ELEMENT_NODE:
|
65 |
|
|
return self.node
|
66 |
|
|
raise StopIteration
|
67 |
|
|
|
68 |
|
|
def next(self):
|
69 |
|
|
node = self.curr()
|
70 |
|
|
self.node = self.node.parentNode
|
71 |
|
|
return node
|
72 |
|
|
|
73 |
|
|
def is_text(node):
|
74 |
|
|
for child in NodeElemIter(node): return False # has an element node
|
75 |
|
|
return True
|
76 |
|
|
|
77 |
|
|
def value(node):
|
78 |
29
|
aaronmk
|
if node.firstChild != None: return node.firstChild.nodeValue
|
79 |
21
|
aaronmk
|
else: return node.nodeValue
|
80 |
|
|
|
81 |
22
|
aaronmk
|
def set_value(doc, node, value):
|
82 |
|
|
if node.nodeType == Node.ELEMENT_NODE:
|
83 |
|
|
node.appendChild(doc.createTextNode(value))
|
84 |
|
|
else: node.nodeValue = value
|
85 |
|
|
|
86 |
86
|
aaronmk
|
class NodeTextEntryIter:
|
87 |
|
|
def __init__(self, node): self.iter_ = NodeElemIter(node)
|
88 |
|
|
|
89 |
|
|
def __iter__(self): return self
|
90 |
|
|
|
91 |
|
|
def curr(self):
|
92 |
|
|
while True:
|
93 |
|
|
child = self.iter_.curr()
|
94 |
|
|
if is_text(child): return (name_of(child), value(child))
|
95 |
|
|
self.iter_.next()
|
96 |
|
|
|
97 |
|
|
def next(self):
|
98 |
|
|
entry = self.curr()
|
99 |
|
|
self.iter_.next()
|
100 |
|
|
return entry
|
101 |
|
|
|
102 |
|
|
def replace(old_node, new_node):
|
103 |
|
|
old_node.parentNode.replaceChild(new_node, old_node) # note order reversed
|
104 |
|
|
|
105 |
|
|
def replace_with_text(doc, node, str_): replace(node, doc.createTextNode(str_))
|
106 |
|
|
|
107 |
22
|
aaronmk
|
def by_tag_name(node, name, last_only=False):
|
108 |
23
|
aaronmk
|
# last_only optimization returns last (most recently inserted) matching node
|
109 |
22
|
aaronmk
|
children = []
|
110 |
21
|
aaronmk
|
for child in NodeElemReverseIter(node):
|
111 |
22
|
aaronmk
|
if child.tagName == name:
|
112 |
|
|
children.append(child)
|
113 |
|
|
if last_only: break
|
114 |
|
|
return children
|
115 |
28
|
aaronmk
|
|
116 |
133
|
aaronmk
|
def create_doc(root='_'):
|
117 |
|
|
return xml.dom.minidom.getDOMImplementation().createDocument(None, root,
|
118 |
|
|
None)
|
119 |
|
|
|
120 |
73
|
aaronmk
|
# xml.dom.minidom modifications
|
121 |
|
|
|
122 |
|
|
def _write_data(writer, data): writer.write(escape(data))
|
123 |
|
|
|
124 |
|
|
xml.dom.minidom._write_data = _write_data
|
125 |
|
|
|
126 |
28
|
aaronmk
|
_writexml_orig = xml.dom.minidom.Element.writexml
|
127 |
|
|
|
128 |
|
|
def _writexml(self, writer, indent="", addindent="", newl=""):
|
129 |
36
|
aaronmk
|
if self.firstChild != None and self.firstChild.nextSibling == None\
|
130 |
|
|
and self.firstChild.nodeType == Node.TEXT_NODE: # a single text node
|
131 |
28
|
aaronmk
|
writer.write(indent+'<'+self.tagName)
|
132 |
70
|
aaronmk
|
for attr_idx in xrange(self.attributes.length):
|
133 |
28
|
aaronmk
|
attr = self.attributes.item(attr_idx)
|
134 |
73
|
aaronmk
|
writer.write(' '+attr.name+'='+escape(attr.value))
|
135 |
|
|
writer.write('>'+escape(value(self))+'</'+self.tagName+'>'+newl)
|
136 |
28
|
aaronmk
|
else: _writexml_orig(self, writer, indent, addindent, newl)
|
137 |
|
|
|
138 |
|
|
xml.dom.minidom.Element.writexml = _writexml
|