Adding support for defining tag element without attributes or children as sequence element
authorlaurent
Fri, 16 Dec 2011 22:47:41 +0100
changeset 607 9c133d675b69
parent 606 d65122c61eaf
child 608 b67536e93c11
Adding support for defining tag element without attributes or children as sequence element
xmlclass/xmlclass.py
--- a/xmlclass/xmlclass.py	Fri Dec 16 22:44:47 2011 +0100
+++ b/xmlclass/xmlclass.py	Fri Dec 16 22:47:41 2011 +0100
@@ -557,16 +557,19 @@
         "check": lambda x: isinstance(x, (StringType, UnicodeType, minidom.Node))
     }
 
-def GenerateTagInfos(name):
+def GenerateTagInfos(infos):
     def ExtractTag(tree):
         if len(tree._attrs) > 0:
-            raise ValueError("\"%s\" musn't have attributes!" % name)
+            raise ValueError("\"%s\" musn't have attributes!" % infos["name"])
         if len(tree.childNodes) > 0:
-            raise ValueError("\"%s\" musn't have children!" % name)
-        return None
+            raise ValueError("\"%s\" musn't have children!" % infos["name"])
+        if infos["minOccurs"] == 0:
+            return True
+        else:
+            return None
     
     def GenerateTag(value, name=None, indent=0):
-        if name is not None:
+        if name is not None and not (infos["minOccurs"] == 0 and value is None):
             ind1, ind2 = getIndent(indent, name)
             return ind1 + "<%s/>\n" % name
         else:
@@ -577,7 +580,7 @@
         "extract": ExtractTag,
         "generate": GenerateTag,
         "initial": lambda: None,
-        "check": lambda x: x == None
+        "check": lambda x: x == None or infos["minOccurs"] == 0 and value == True
     }
 
 def FindTypeInfos(factory, infos):
@@ -645,7 +648,7 @@
                     if element_infos is not None:
                         sequence_element["elmt_type"] = element_infos
         elif choice["elmt_type"] == "tag":
-            choice["elmt_type"] = GenerateTagInfos(choice["name"])
+            choice["elmt_type"] = GenerateTagInfos(choice)
         else:
             choice_infos = factory.ExtractTypeInfos(choice["name"], name, choice["elmt_type"])
             if choice_infos is not None:
@@ -1189,7 +1192,10 @@
                 infos = GenerateAnyInfos(element)
             else:
                 elmtname = element["name"]
-                infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"])
+                if element["elmt_type"] == "tag":
+                    infos = GenerateTagInfos(element)
+                else:
+                    infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"])
             if infos is not None:
                 element["elmt_type"] = infos
             if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1: