in RecordSpec.dict2dom, no need to always hardcode add the root_tag, it may already...
[sfa.git] / geni / util / rspec.py
index 0831d91..393d8df 100644 (file)
@@ -79,19 +79,26 @@ class Rspec():
         if elementName and not elementName.startswith("#"):
             # attributes have tags and values.  get {tag: value}, else {type: value}
             node[elementName] = self._attributeDict(nodeDom)
-            #node.update(self._attributeDict(nodeDom))
             # resolve the child nodes.
             if nodeDom.hasChildNodes():
                 for child in nodeDom.childNodes:
                     childName = self._getName(child)
+                    # skip null children 
                     if not childName:
                         continue
+                    # initialize the possible array of children        
                     if not node[elementName].has_key(childName):
-                        node[elementName][childName] = []       
-                        #node[childName] = []
-                    childdict = self.toDict(child)
-                    for value in childdict.values():
-                        node[elementName][childName].append(value)
+                        node[elementName][childName] = []
+                    # if child node has text child nodes
+                    # append the children to the array as strings
+                    if child.hasChildNodes() and isinstance(child.childNodes[0], minidom.Text):
+                        for nextchild in child.childNodes:
+                            node[elementName][childName].append(nextchild.data)
+                    # convert element child node to dict
+                    else:       
+                        childdict = self.toDict(child)
+                        for value in childdict.values():
+                            node[elementName][childName].append(value)
                     #node[childName].append(self.toDict(child))
         return node
 
@@ -185,8 +192,15 @@ class Rspec():
                     element.appendChild(child)
                 elif isinstance(rd[key], list):
                     for item in rd[key]:
-                        child = elementNode(key, {key:item})
-                        element.appendChild(child)
+                        if isinstance(item, dict):
+                            child = elementNode(key, item)
+                            element.appendChild(child)
+                        elif isinstance(item, StringTypes) or isinstance(item, int):
+                            child = minidom.Element(key)
+                            text = minidom.Text()
+                            text.data = item
+                            child.appendChild(text)
+                            element.appendChild(child) 
             return element
         
         # Minidom does not allow documents to have more then one
@@ -277,6 +291,7 @@ class Rspec():
 
 class RecordSpec(Rspec):
 
+    root_tag = 'record'
     def parseDict(self, rdict, include_doc = False):
         """
         Convert a dictionary into a dom object and store it.
@@ -284,7 +299,10 @@ class RecordSpec(Rspec):
         self.rootNode = self.dict2dom(rdict, include_doc)
 
     def dict2dom(self, rdict, include_doc = False):
-        record_dict = {'record': rdict}
+        record_dict = rdict
+        if not len(rdict.keys()) == 1:
+            record_dict = {self.root_tag : rdict}
         return Rspec.dict2dom(self, record_dict, include_doc)
         
 # vim:ts=4:expandtab
+