Skip to content

Commit 01acb91

Browse files
author
Jamie Kirkpatrick
committed
Refactor code so that a namespace lookup object is passed to all methods that need to qualify XML tags / attributes.
This change allows valid serialization of objects in isolation of the main framework in a manner that will produce fully valid XML.
1 parent b684fa0 commit 01acb91

15 files changed

+334
-370
lines changed

TODO.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Code to sort out after namespace map addition:
2+
3+
Message.to_xml()
4+
ClassSerializerMeta.namespace
5+
6+
create_xml_element - pass nsmap
7+
_add_messages_for_methods / _add_bindings_for_methods : create_xml_element (no nsmap)

soaplib/serializers/binary.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import base64
22
import cStringIO
3-
from soaplib.xml import *
3+
from soaplib.xml import ns, create_xml_element
44

55
class Attachment(object):
66

@@ -36,7 +36,7 @@ def load_from_file(self):
3636
f.close()
3737

3838
@classmethod
39-
def to_xml(cls,value,name='retval'):
39+
def to_xml(cls,value,name='retval',nsmap=ns):
4040
'''This class method takes the data from the attachment and
4141
base64 encodes it as the text of an Element. An attachment can
4242
specify a filename and if no data is given, it will read the data
@@ -45,7 +45,7 @@ def to_xml(cls,value,name='retval'):
4545
if value.__class__ is not Attachment:
4646
raise Exception("Do not know how to serialize class %s"%type(value))
4747

48-
element = create_xml_element(name)
48+
element = create_xml_element(name, nsmap)
4949
if value.data:
5050
# the data has already been loaded, just encode
5151
# and return the element
@@ -79,14 +79,18 @@ def from_xml(cls,element):
7979
return a
8080

8181
@classmethod
82-
def get_datatype(cls,withNamespace=False):
82+
def get_datatype(cls,nsmap=None):
8383
'''Returns the datatype base64Binary'''
84-
if withNamespace:
85-
return 'xs:base64Binary'
84+
if nsmap is not None:
85+
return nsmap.get(cls.get_namespace_id()) + 'base64Binary'
8686
return 'base64Binary'
87+
88+
@classmethod
89+
def get_namespace_id(cls):
90+
return 'xs'
8791

8892
@classmethod
89-
def add_to_schema(cls,added_params):
93+
def add_to_schema(cls,added_params,nsmap):
9094
'''
9195
Nothing needs to happen here as base64Binary is a standard
9296
schema element

soaplib/serializers/clazz.py

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import inspect
2-
from soaplib.xml import *
2+
from soaplib.xml import ns, create_xml_element, create_xml_subelement, qualify
33

44
from primitive import Null
55

@@ -43,8 +43,9 @@ def __init__(self):
4343
setattr(self,k,None)
4444

4545
@classmethod
46-
def to_xml(cls,value,name='retval'):
47-
element = create_xml_element(qualify(name, cls.namespace))
46+
def to_xml(cls,value,name='retval', nsmap=ns):
47+
element = create_xml_element(
48+
nsmap.get(cls.get_namespace_id()) + name, nsmap)
4849

4950
for k,v in cls.soap_members.items():
5051
member_value = getattr(value,k,None)
@@ -53,7 +54,7 @@ def to_xml(cls,value,name='retval'):
5354
if subvalue is None:
5455
v = Null
5556

56-
subelements = v.to_xml(subvalue,name=k)
57+
subelements = v.to_xml(subvalue,name=k,nsmap=nsmap)
5758
if type(subelements) != list:
5859
subelements = [subelements]
5960
for s in subelements:
@@ -79,38 +80,40 @@ def from_xml(cls, element):
7980
return obj
8081

8182
@classmethod
82-
def get_datatype(cls,withNamespace=False):
83-
if withNamespace:
84-
return 'tns:%s'%(cls.__name__)
83+
def get_datatype(cls,nsmap=None):
84+
if nsmap is not None:
85+
return nsmap.get(cls.get_namespace_id()) + cls.__name__
8586
return cls.__name__
86-
87+
88+
@classmethod
89+
def get_namespace_id(cls):
90+
return 'tns'
91+
8792
@classmethod
88-
def add_to_schema(cls, schemaDict):
93+
def add_to_schema(cls, schemaDict, nsmap):
8994

90-
if not schemaDict.has_key(cls.get_datatype(True)):
95+
if not schemaDict.has_key(cls.get_datatype(nsmap)):
9196
for k,v in cls.soap_members.items():
92-
v.add_to_schema(schemaDict)
97+
v.add_to_schema(schemaDict, nsmap)
9398

94-
tag = qualify("complexType", ns["xs"])
95-
schema_node = create_xml_element(tag)
99+
schema_node = create_xml_element(
100+
nsmap.get("xs") + "complexType", nsmap)
96101
schema_node.set('name',cls.__name__)
97102

98103
sequence_node = create_xml_subelement(
99-
schema_node,
100-
qualify('sequence', ns["xs"])
101-
)
104+
schema_node, nsmap.get('xs') + 'sequence')
102105
for k,v in cls.soap_members.items():
103106
member_node = create_xml_subelement(
104-
sequence_node,
105-
qualify('element', ns["xs"])
106-
)
107+
sequence_node, nsmap.get('xs') + 'element')
107108
member_node.set('name',k)
108109
member_node.set('minOccurs','0')
109-
member_node.set('type',v.get_datatype(True))
110+
member_node.set('type',
111+
"%s:%s" % (v.get_namespace_id(), v.get_datatype()))
110112

111-
tag = qualify("element", ns["xs"])
112-
typeElement = create_xml_element(tag)
113+
typeElement = create_xml_element(
114+
nsmap.get('xs') + 'element', nsmap)
113115
typeElement.set('name',cls.__name__)
114-
typeElement.set('type','tns:'+cls.__name__)
115-
schemaDict[cls.get_datatype(True)+'Complex'] = schema_node
116-
schemaDict[cls.get_datatype(True)] = typeElement
116+
typeElement.set('type',
117+
"%s:%s" % (cls.get_namespace_id(),cls.__name__))
118+
schemaDict[cls.get_datatype(nsmap)+'Complex'] = schema_node
119+
schemaDict[cls.get_datatype(nsmap)] = typeElement

0 commit comments

Comments
 (0)