Hi Everyone,

In a couple of our XML Schema, we have defined default values for some
attributes.  These values are considered to be "hardcoded" constants
for our application so they must be accessible from the application
code.  generateDS allowed me to create Python objects from the XML
using the schema information, but the default values were incorrect.

Attached is a patch to set the default values in the __init__ method's
parameter list from the schema attributes.

Regards,

Arne Grimstrup
[EMAIL PROTECTED]
--- generateDS-1.11b/generateDS.py      2007-11-19 16:44:23.000000000 -0700
+++ generateDS-1.11c/generateDS.py      2008-04-24 13:56:54.000000000 -0600
@@ -763,10 +763,11 @@
 # end class XschemaAttributeGroup
 
 class XschemaAttribute:
-    def __init__(self, name, data_type='xs:string', use='optional'):
+    def __init__(self, name, data_type='xs:string', use='optional', 
default=None):
         self.name = name
         self.data_type = data_type
         self.use = use
+        self.default = default
         # Enumeration values for the attribute.
         self.values = list()
     def setName(self, name): self.name = name
@@ -775,6 +776,8 @@
     def getData_type(self): return self.data_type
     def setUse(self, use): self.use = use
     def getUse(self): return self.use
+    def setDefault(self, default): self.default = default
+    def getDefault(self): return self.default
 # end class XschemaAttribute
 
 
@@ -876,13 +879,17 @@
                 use = attrs['use']
             else:
                 use = 'optional'
+            if 'default' in attrs.keys():
+                default = attrs['default']
+            else:
+                default = None
             if self.stack[-1].attributeGroup:
                 # Add this attribute to a current attributeGroup.
-                attribute = XschemaAttribute(name, data_type, use)
+                attribute = XschemaAttribute(name, data_type, use, default)
                 self.stack[-1].attributeGroup.add(name, attribute)
             else:
                 # Add this attribute to the element/complexType.
-                attribute = XschemaAttribute(name, data_type, use)
+                attribute = XschemaAttribute(name, data_type, use, default)
                 self.stack[-1].attributeDefs[name] = attribute
             self.lastAttribute = attribute
         elif name == AttributeGroupType:
@@ -2157,6 +2164,7 @@
     for key in attrDefs:
         attrDef = attrDefs[key]
         name = attrDef.getName()
+        default = attrDef.getDefault()
         if name in addedArgs:
             continue
         addedArgs[name] = 1
@@ -2170,21 +2178,45 @@
             atype == TokenType or \
             atype == DateTimeType or \
             atype == DateType:
-            add(', %s=\'\'' % mappedName)
+            if default is None:
+                add(', %s=\'\'' % mappedName)
+            else:
+                add(', %s="%s"' % (mappedName, default))
         elif atype in IntegerType:
-            add(', %s=-1' % mappedName)
+            if default is None:
+                add(', %s=-1' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         elif atype == PositiveIntegerType:
-            add(', %s=1' % mappedName)
+            if default is None:
+                add(', %s=1' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         elif atype == NonPositiveIntegerType:
-            add(', %s=0' % mappedName)
+            if default is None:
+                add(', %s=0' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         elif atype == NegativeIntegerType:
-            add(', %s=-1' % mappedName)
+            if default is None:
+                add(', %s=-1' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         elif atype == NonNegativeIntegerType:
-            add(', %s=0' % mappedName)
+            if default is None:
+                add(', %s=0' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         elif atype == BooleanType:
-            add(', %s=0' % mappedName)
+            if default is None:
+                add(', %s=0' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         elif atype == FloatType or atype == DoubleType or atype == DecimalType:
-            add(', %s=0.0' % mappedName)
+            if default is None:
+                add(', %s=0.0' % mappedName)
+            else:
+                add(', %s=%s' % (mappedName, default))
         else:
             add(', %s=None' % mappedName)
     nestedElements = 0
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to