-
Notifications
You must be signed in to change notification settings - Fork 466
/
Copy pathxmltools.py
executable file
·164 lines (143 loc) · 4.95 KB
/
xmltools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
import sys
from xml.dom.minidom import parse, parseString
from xml.dom import Node
##
## This script does any post processing required when moving an
## example from it's home in /examples to ${build.dir}/dist/examples.
##
def readFile(filename):
"read a file into a string"
FH=open(filename, 'r')
fileString = FH.read()
FH.close()
return fileString
def writeFile(filename, content):
"read a file into a string"
FH=open(filename, 'w')
FH.write(content)
FH.close()
def pruneTextNodes(aNode):
""
toRemove = []
for child in aNode.childNodes:
if child.nodeType == Node.TEXT_NODE:
toRemove.append(child)
else:
pruneTextNodes(child)
for child in toRemove:
aNode.removeChild(child)
def getElementId(node):
assert node.nodeType == Node.ELEMENT_NODE
if node.hasAttribute("name"):
return node.tagName + "-" + node.getAttribute("name")
elif node.hasAttribute("id"):
return node.tagName + "-" + node.getAttribute("id")
else:
return node.tagName
def findNodesWithId(node, id):
retval = []
if (id == None) or (getElementId(node) == id):
retval.append(node)
for i in range(node.childNodes.length):
child = node.childNodes.item(i)
if child.nodeType == Node.ELEMENT_NODE:
retval += findNodesWithId(child, id);
return retval
def addBeforeNodesWithId(doc, root, id, text, comment):
toadd = findNodesWithId(root, id)
for node in toadd:
if text != None:
text = doc.createTextNode(text)
root.insertBefore(text, node)
if comment != None:
comment = doc.createComment(comment)
root.insertBefore(comment, node)
def addWhitespace(doc, project):
toadd = []
for i in range(project.childNodes.length):
child = project.childNodes.item(i)
if child.nodeType == Node.ELEMENT_NODE:
if child.tagName == "property":
continue
ws = doc.createTextNode("\n")
toadd.append((ws,child))
for pair in toadd:
project.insertBefore(pair[0], pair[1])
def orderAttributes__(attributes, attributeOrder):
ordered = []
if attributeOrder == None:
attributeOrder = ["name"]
for attrName in attributeOrder:
for i in range(attributes.length):
attr = attributes.item(i)
if attr.name == attrName:
ordered.append(attr)
for i in range(attributes.length):
attr = attributes.item(i)
found = False
for attr2 in ordered:
if attr2 == attr:
found = True
if not found:
ordered.append(attr)
return ordered
def prettyXml__(node, indent, attributeOrder):
if attributeOrder == None:
attributeOrder = {}
output = indent
if node.nodeType == Node.ELEMENT_NODE:
# start tag
output = output + "<" + node.tagName
# print attributes
order = attributeOrder.get(node.tagName)
attrs = orderAttributes__(node.attributes, order)
for attr in attrs:
output += " " + attr.name + "=\""
output += node.getAttribute(attr.name)
output += "\""
if node.hasChildNodes():
output += ">\n"
# print childen
for child in node.childNodes:
output += prettyXml__(child, " " + indent, attributeOrder)
# close tag
output += indent + "</" + node.tagName + ">\n"
else:
# end tag
output += "/>\n"
elif node.nodeType == Node.TEXT_NODE:
output += node.data
elif node.nodeType == Node.CDATA_SECTION_NODE:
print "CDATA_SECTION_NODE"
elif node.nodeType == Node.ENTITY_NODE:
print "ENTITY_NODE"
elif node.nodeType == Node.PROCESSING_INSTRUCTION_NODE:
print "PROCESSING_INSTRUCTION_NODE"
elif node.nodeType == Node.COMMENT_NODE:
output += "<!-- " + node.data + " -->\n"
elif node.nodeType == Node.DOCUMENT_NODE:
# print childen
for child in node.childNodes:
output += prettyXml__(child, "", attributeOrder)
elif node.nodeType == Node.DOCUMENT_TYPE_NODE:
print "DOCUMENT_TYPE_NODE"
elif node.nodeType == Node.NOTATION_NODE:
print "NOTATION_NODE"
return output
def prettyXml(document, attributeOrder):
output = "<?xml version=\"1.0\" ?>\n"
output += prettyXml__(document, "", attributeOrder)
return output
def cloneNode(doc, aNode):
e = doc.createElement(aNode.tagName)
if aNode.hasAttributes():
for i in range(aNode.attributes.length):
attr = aNode.attributes.item(i)
e.setAttribute(attr.name, aNode.getAttribute(attr.name))
if aNode.hasChildNodes():
for cnode in aNode.childNodes:
if cnode.nodeType == Node.ELEMENT_NODE:
cnodeClone = cloneNode(doc, cnode)
e.appendChild(cnodeClone)
return e