stil broken.
authorFaiyaz Ahmed <faiyaza@cs.princeton.edu>
Thu, 15 Jan 2009 20:57:29 +0000 (20:57 +0000)
committerFaiyaz Ahmed <faiyaza@cs.princeton.edu>
Thu, 15 Jan 2009 20:57:29 +0000 (20:57 +0000)
rspec/rspecvalidate.py

index ec594bd..17f208f 100644 (file)
@@ -13,6 +13,7 @@
 #
 
 import sys
+import pprint
 from xml.dom import minidom
 
 
@@ -25,30 +26,40 @@ def getText(nodelist):
 
 # complexType: a supernode comprised of element nodes below it.
 def traverseComplexType(cmpTypeNode):
-       _elements = {}
-       if cmpTypeNode.hasChildNodes():
-               for n in cmpTypeNode.getElementsByTagName("xsd:attribute"):
-                       _elements[n.getAttribute("name")] = {'type': n.getAttribute("type")}
+    _elements = {}
+    if cmpTypeNode.hasChildNodes():
+        for n in cmpTypeNode.getElementsByTagName("xsd:attribute"):
+            _elements[n.getAttribute("name")] = {'type': n.getAttribute("type")}
 
 
 # Element.  {name, value, default}
 def Element(elementDom):
-       node = {} #parsed dict
-       for attr in elementDom.attributes.keys():
-               node[attr] = elementDom.attributes.get(attr).value
-       return node
+    node = {} #parsed dict
+    for attr in elementDom.attributes.keys():
+        node[attr] = elementDom.attributes.get(attr).value
+    # set the name to the name of the element.  otherwise, node name.
+    if elementDom.attributes.has_key("name"): 
+        element = {(elementDom.localName, elementDom.attributes.get("name").value) : node}
+    else:
+        element = {elementDom.localName: node}
+    # print repr(element)
+    # print
+    return element
 
 # Sequence is a list of dicts.  Each dict is an element type with Type fields
 def Sequence(sequenceNode):
-       pass
+    pass
 
-def buildDict(document):
-       if document.hasChildNodes():
-               for i in document.childNodes: buildDict(i)
-       print document.localName
+def buildDict(document, docdict = {}):
+    if document.hasChildNodes():
+        for i in document.childNodes: 
+            if i.attributes: docdict.update({ i.localName: buildDict(i, docdict)})
+    if document.attributes: docdict.update(Element(document))
+    return docdict
 
 def main(fname):
-       buildDict(minidom.parse(fname))
+    pp = pprint.PrettyPrinter(indent=4)
+    pp.pprint(buildDict(minidom.parse(fname)))
 
 if __name__ == '__main__':  
-       main(fname="planetlab.xsd")
+    main(fname="planetlab.xsd")