Hi,
i have a first version ready which supports Namespaces. If a
targetNamespace is given in the Schema, the generated export
functions will put the according namespace out.
Diff against version 1.12.a is attached. I hope its useful.
--- generateDS.py 2008-05-07 23:43:36.000000000 +0200
+++ generateDS_namespaces.py 2008-05-20 14:08:45.687500000 +0200
@@ -104,6 +104,9 @@
SaxElementDict = {}
Force = 0
+NamespacesDict = {}
+Targetnamespace = ""
+
##NameTable = {
## 'class': 'klass',
## 'import': 'emport',
@@ -822,6 +825,7 @@
sys.exit(-1)
def startElement(self, name, attrs):
+ global Targetnamespace, NamespacesDict
logging.debug("Start element: %s %s" % (name, repr(attrs.items())))
if name == SchemaType:
@@ -834,10 +838,14 @@
# "http://www.w3.org/2001/XMLSchema", then remember and
# use that namespace prefix.
for name, value in attrs.items():
- if name[:6] == 'xmlns:' and \
- value == 'http://www.w3.org/2001/XMLSchema':
+ if name[:6] == 'xmlns:':
nameSpace = name[6:] + ':'
- set_type_constants(nameSpace)
+ if value == 'http://www.w3.org/2001/XMLSchema':
+ set_type_constants(nameSpace)
+ NamespacesDict[value] = nameSpace
+ elif name == 'targetNamespace':
+ Targetnamespace = value
+
elif name == ElementType or ((name == ComplexTypeType) and
(len(self.stack) == 1)):
self.inElement = 1
self.inNonanonymousComplexType = 1
@@ -1082,7 +1090,7 @@
# Code generation
#
-def generateExportFn_1(outfile, child, name, fill):
+def generateExportFn_1(outfile, child, name, namespace, fill):
cleanName = cleanupName(name)
mappedName = mapName(cleanName)
if child.getType() in StringType or \
@@ -1091,8 +1099,8 @@
child.getType() == DateType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%s</%s>\\n' %%
quote_xml(self.get%s()))\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%s</%s%s>\\n' %%
quote_xml(self.get%s()))\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
elif child.getType() in IntegerType or \
child.getType() == BooleanType or \
@@ -1102,21 +1110,21 @@
child.getType() == NonNegativeIntegerType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%d</%s>\\n' %% self.get%s())\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%d</%s%s>\\n' %%
self.get%s())\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
elif child.getType() == FloatType or \
child.getType() == DecimalType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%f</%s>\\n' %% self.get%s())\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%f</%s%s>\\n' %%
self.get%s())\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
elif child.getType() == DoubleType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%e</%s>\\n' %% self.get%s())\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%e</%s%s>\\n' %%
self.get%s())\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
else:
s1 = "%s if self.%s:\n" % (fill, mappedName)
@@ -1125,12 +1133,12 @@
s1 = "%s self.%s.export(outfile, level)\n" % \
(fill, mappedName)
else:
- s1 = "%s self.%s.export(outfile, level, name_='%s')\n"
% \
- (fill, mappedName, name)
+ s1 = "%s self.%s.export(outfile, level, name_='%s',
namespace_='%s')\n" % \
+ (fill, mappedName, name, namespace)
outfile.write(s1)
-def generateExportFn_2(outfile, child, name, fill):
+def generateExportFn_2(outfile, child, name, namespace, fill):
cleanName = cleanupName(name)
mappedName = mapName(cleanName)
s1 = "%s for %s_ in self.get%s():\n" % (fill, cleanName,
make_gs_name(cleanName))
@@ -1141,8 +1149,8 @@
child.getType() == DateType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%s</%s>\\n' %% quote_xml(%s_))\n"
% \
- (fill, name, name, cleanName,)
+ s1 = "%s outfile.write('<%s%s>%%s</%s%s>\\n' %%
quote_xml(%s_))\n" % \
+ (fill, namespace, name, namespace, name, cleanName,)
outfile.write(s1)
elif child.getType() in IntegerType or \
child.getType() == BooleanType or \
@@ -1152,32 +1160,32 @@
child.getType() == NonNegativeIntegerType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%d</%s>\\n' %% %s_)\n" % \
- (fill, name, name, cleanName, )
+ s1 = "%s outfile.write('<%s%s>%%d</%s%s>\\n' %% %s_)\n" % \
+ (fill, namespace, name, namespace, name, cleanName, )
outfile.write(s1)
elif child.getType() == FloatType or \
child.getType() == DecimalType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%f</%s>\\n' %% %s_)\n" % \
- (fill, name, name, cleanName, )
+ s1 = "%s outfile.write('<%s%s>%%f</%s%s>\\n' %% %s_)\n" % \
+ (fill, namespace, name, namespace, name, cleanName, )
outfile.write(s1)
elif child.getType() == DoubleType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%e</%s>\\n' %% %s_)\n" % \
- (fill, name, name, cleanName)
+ s1 = "%s outfile.write('<%s%s>%%e</%s%s>\\n' %% %s_)\n" % \
+ (fill, namespace, name, namespace, name, cleanName)
outfile.write(s1)
else:
if name == child.getType():
s1 = "%s %s_.export(outfile, level)\n" % (fill, mappedName)
else:
- s1 = "%s %s_.export(outfile, level, name_='%s')\n" % \
- (fill, mappedName, cleanName, )
+ s1 = "%s %s_.export(outfile, level, name_='%s',
namespace_='%s')\n" % \
+ (fill, mappedName, cleanName, namespace)
outfile.write(s1)
-def generateExportFn_3(outfile, child, name, fill):
+def generateExportFn_3(outfile, child, name, namespace, fill):
cleanName = cleanupName(name)
mappedName = mapName(cleanName)
s1 = "%s if self.get%s() != None :\n" % (
@@ -1192,8 +1200,8 @@
outfile.write(s1)
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%s</%s>\\n' %%
quote_xml(self.get%s()))\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%s</%s%s>\\n' %%
quote_xml(self.get%s()))\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
elif child.getType() in IntegerType or \
child.getType() == BooleanType or \
@@ -1203,21 +1211,21 @@
child.getType() == NonNegativeIntegerType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%d</%s>\\n' %%
self.get%s())\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%d</%s%s>\\n' %%
self.get%s())\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
elif child.getType() == FloatType or \
child.getType() == DecimalType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%f</%s>\\n' %%
self.get%s())\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%f</%s%s>\\n' %%
self.get%s())\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
elif child.getType() == DoubleType:
s1 = '%s showIndent(outfile, level)\n' % fill
outfile.write(s1)
- s1 = "%s outfile.write('<%s>%%e</%s>\\n' %%
self.get%s())\n" % \
- (fill, name, name, make_gs_name(cleanName))
+ s1 = "%s outfile.write('<%s%s>%%e</%s%s>\\n' %%
self.get%s())\n" % \
+ (fill, namespace, name, namespace, name, make_gs_name(cleanName))
outfile.write(s1)
else:
s1 = "%s if self.%s:\n" % (fill, mappedName)
@@ -1226,8 +1234,8 @@
s1 = "%s self.%s.export(outfile, level)\n" % \
(fill, mappedName)
else:
- s1 = "%s self.%s.export(outfile, level,
name_='%s')\n" % \
- (fill, mappedName, name)
+ s1 = "%s self.%s.export(outfile, level, name_='%s',
namespace_='%s')\n" % \
+ (fill, mappedName, name, namespace)
outfile.write(s1)
@@ -1258,24 +1266,24 @@
return hasAttributes
-def generateExportChildren(outfile, element, hasChildren):
+def generateExportChildren(outfile, element, hasChildren, namespace):
if len(element.getChildren()) > 0:
hasChildren += 1
if element.isMixed():
s1 = " for item_ in self.content_:\n"
outfile.write(s1)
- s1 = " item_.export(outfile, level, name_)\n"
+ s1 = " item_.export(outfile, level, name_,
namespace_)\n"
outfile.write(s1)
else:
for child in element.getChildren():
name = child.getName()
if child.getMaxOccurs() > 1:
- generateExportFn_2(outfile, child, name, ' ')
+ generateExportFn_2(outfile, child, name, namespace, ' ')
else:
if (child.getOptional()):
- generateExportFn_3(outfile, child, name, '')
+ generateExportFn_3(outfile, child, name, namespace, '')
else:
- generateExportFn_1(outfile, child, name, '')
+ generateExportFn_1(outfile, child, name, namespace, '')
## base = element.getBase()
## if base and base in ElementDict:
## parent = ElementDict[base]
@@ -1292,20 +1300,20 @@
return count
-def generateExportFn(outfile, prefix, element):
+def generateExportFn(outfile, prefix, element, namespace):
childCount = countChildren(element, 0)
base = element.getBase()
- s1 = " def export(self, outfile, level, name_='%s'):\n" % \
- element.getName()
+ s1 = " def export(self, outfile, level, name_='%s',
namespace_='%s'):\n" % \
+ (element.getName(), namespace)
outfile.write(s1)
s1 = ' showIndent(outfile, level)\n'
outfile.write(s1)
if len(element.getAttributeDefs()) > 0 or element.getAnyAttribute():
- s1 = " outfile.write('<%s' % (name_, ))\n"
+ s1 = " outfile.write('<%s%s' % (namespace_, name_))\n"
outfile.write(s1)
- s1 = " self.exportAttributes(outfile, level, name_='%s')\n" % \
- element.getName()
+ s1 = " self.exportAttributes(outfile, level, name_='%s',
namespace_='%s')\n" % \
+ (element.getName(), namespace)
outfile.write(s1)
if element.isMixed() or childCount == 0:
s1 = " outfile.write('>')\n"
@@ -1314,30 +1322,30 @@
outfile.write(s1)
else:
if element.isMixed() or childCount == 0:
- s1 = " outfile.write('<%s>' % name_)\n"
+ s1 = " outfile.write('<%s%s>' % (namespace_, name_))\n"
else:
- s1 = " outfile.write('<%s>\\n' % name_)\n"
+ s1 = " outfile.write('<%s%s>\\n' % (namespace_, name_))\n"
outfile.write(s1)
- s1 = " self.exportChildren(outfile, level + 1, name_)\n"
+ s1 = " self.exportChildren(outfile, level + 1, name_, namespace_)\n"
outfile.write(s1)
if element.isMixed() or childCount == 0:
- s1 = " outfile.write('</%s>\\n' % name_)\n"
+ s1 = " outfile.write('</%s%s>\\n' % (namespace_, name_))\n"
outfile.write(s1)
else:
s1 = ' showIndent(outfile, level)\n'
outfile.write(s1)
- s1 = " outfile.write('</%s>\\n' % name_)\n"
+ s1 = " outfile.write('</%s%s>\\n' % (namespace_, name_))\n"
outfile.write(s1)
- s1 = " def exportAttributes(self, outfile, level, name_='%s'):\n" % \
- element.getName()
+ s1 = " def exportAttributes(self, outfile, level, name_='%s',
namespace_='%s'):\n" % \
+ (element.getName(), namespace)
outfile.write(s1)
hasAttributes = 0
hasAttributes = generateExportAttributes(outfile, element, hasAttributes)
if base and base not in SimpleTypeDict:
hasAttributes += 1
- s1 = " %s.exportAttributes(self, outfile, level, name_='%s')\n"
% \
- (base, element.getName(), )
+ s1 = " %s.exportAttributes(self, outfile, level, name_='%s',
namespace_='%s')\n" % \
+ (base, element.getName(), namespace)
outfile.write(s1)
if hasAttributes == 0:
s1 = " pass\n"
@@ -1345,14 +1353,14 @@
## if len(element.getChildren()) > 0 and not element.isMixed():
## s1 = ' showIndent(outfile, level)\n'
## outfile.write(s1)
- s1 = " def exportChildren(self, outfile, level, name_='%s'):\n" % \
- element.getName()
+ s1 = " def exportChildren(self, outfile, level, name_='%s',
namespace_='%s'):\n" % \
+ (element.getName(), namespace)
outfile.write(s1)
hasChildren = 0
- hasChildren = generateExportChildren(outfile, element, hasChildren)
+ hasChildren = generateExportChildren(outfile, element, hasChildren,
namespace)
if base and base not in SimpleTypeDict:
hasChildren += 1
- s1 = " %s.exportChildren(self, outfile, level, name_)\n" %
(base, )
+ s1 = " %s.exportChildren(self, outfile, level, name_,
namespace_)\n" % (base, )
outfile.write(s1)
if childCount == 0:
s1 = " outfile.write(self.valueOf_)\n"
@@ -2448,7 +2456,9 @@
# a validator method.
typeName = None
if child.getSimpleType():
- typeName = child.getSimpleType()
+ #typeName = child.getSimpleType()
+ typeName = cleanupName(child.getName())
+
elif (childType in ElementDict and
ElementDict[childType].getSimpleType()):
typeName = ElementDict[childType].getType()
@@ -2598,7 +2608,11 @@
wrt(' return %s%s(*args_, **kwargs_)\n' % (prefix, name))
wrt(' factory = staticmethod(factory)\n')
generateGettersAndSetters(outfile, element)
- generateExportFn(outfile, prefix, element)
+ if Targetnamespace in NamespacesDict:
+ namespace = NamespacesDict[Targetnamespace]
+ else:
+ namespace = ''
+ generateExportFn(outfile, prefix, element, namespace)
generateExportLiteralFn(outfile, prefix, element)
generateBuildFn(outfile, prefix, element, delayed)
generateUserMethods(outfile, element)
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________ generateds-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/generateds-users
