7 # Validate RSPEC hiearchy, values, types, and names using supplied xsd.
9 # Faiyaz Ahmed <faiyaza at cs dot princeton dot edu>
11 # Copyright 2009 Princeton University
12 # http://www.planet-lab.org
17 from xml.dom import minidom
20 def getText(nodelist):
23 if node.nodeType == node.TEXT_NODE:
27 # complexType: a supernode comprised of element nodes below it.
28 def traverseComplexType(cmpTypeNode):
30 if cmpTypeNode.hasChildNodes():
31 for n in cmpTypeNode.getElementsByTagName("xsd:attribute"):
32 _elements[n.getAttribute("name")] = {'type': n.getAttribute("type")}
35 # Element. {name, value, default}
36 def Element(elementDom):
37 node = {} #parsed dict
38 for attr in elementDom.attributes.keys():
39 node[attr] = elementDom.attributes.get(attr).value
40 # set the name to the name of the element. otherwise, node name.
41 if elementDom.attributes.has_key("name"):
42 element = {(elementDom.localName, elementDom.attributes.get("name").value) : node}
44 element = {elementDom.localName: node}
49 # Sequence is a list of dicts. Each dict is an element type with Type fields
50 def Sequence(sequenceNode):
53 def buildDict(document, docdict = {}):
54 if document.hasChildNodes():
55 for i in document.childNodes:
56 if i.attributes: docdict.update({ i.localName: buildDict(i, docdict)})
57 if document.attributes: docdict.update(Element(document))
61 pp = pprint.PrettyPrinter(indent=4)
62 pp.pprint(buildDict(minidom.parse(fname)))
64 if __name__ == '__main__':
65 main(fname="planetlab.xsd")