rubysl-soap 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/.travis.yml +8 -0
  4. data/README.md +2 -2
  5. data/Rakefile +0 -1
  6. data/lib/rubysl/soap.rb +1 -0
  7. data/lib/rubysl/soap/version.rb +5 -0
  8. data/lib/soap/attachment.rb +107 -0
  9. data/lib/soap/baseData.rb +942 -0
  10. data/lib/soap/element.rb +258 -0
  11. data/lib/soap/encodingstyle/aspDotNetHandler.rb +213 -0
  12. data/lib/soap/encodingstyle/handler.rb +100 -0
  13. data/lib/soap/encodingstyle/literalHandler.rb +226 -0
  14. data/lib/soap/encodingstyle/soapHandler.rb +582 -0
  15. data/lib/soap/generator.rb +268 -0
  16. data/lib/soap/header/handler.rb +57 -0
  17. data/lib/soap/header/handlerset.rb +70 -0
  18. data/lib/soap/header/simplehandler.rb +44 -0
  19. data/lib/soap/httpconfigloader.rb +119 -0
  20. data/lib/soap/mapping.rb +10 -0
  21. data/lib/soap/mapping/factory.rb +355 -0
  22. data/lib/soap/mapping/mapping.rb +381 -0
  23. data/lib/soap/mapping/registry.rb +541 -0
  24. data/lib/soap/mapping/rubytypeFactory.rb +475 -0
  25. data/lib/soap/mapping/typeMap.rb +50 -0
  26. data/lib/soap/mapping/wsdlencodedregistry.rb +280 -0
  27. data/lib/soap/mapping/wsdlliteralregistry.rb +418 -0
  28. data/lib/soap/marshal.rb +59 -0
  29. data/lib/soap/mimemessage.rb +240 -0
  30. data/lib/soap/netHttpClient.rb +190 -0
  31. data/lib/soap/parser.rb +251 -0
  32. data/lib/soap/processor.rb +66 -0
  33. data/lib/soap/property.rb +333 -0
  34. data/lib/soap/rpc/cgistub.rb +206 -0
  35. data/lib/soap/rpc/driver.rb +254 -0
  36. data/lib/soap/rpc/element.rb +325 -0
  37. data/lib/soap/rpc/httpserver.rb +129 -0
  38. data/lib/soap/rpc/proxy.rb +497 -0
  39. data/lib/soap/rpc/router.rb +594 -0
  40. data/lib/soap/rpc/rpc.rb +25 -0
  41. data/lib/soap/rpc/soaplet.rb +162 -0
  42. data/lib/soap/rpc/standaloneServer.rb +43 -0
  43. data/lib/soap/soap.rb +140 -0
  44. data/lib/soap/streamHandler.rb +229 -0
  45. data/lib/soap/wsdlDriver.rb +575 -0
  46. data/rubysl-soap.gemspec +19 -18
  47. metadata +115 -86
  48. data/lib/rubysl-soap.rb +0 -7
  49. data/lib/rubysl-soap/version.rb +0 -5
@@ -0,0 +1,50 @@
1
+ # SOAP4R - Base type mapping definition
2
+ # Copyright (C) 2000, 2001, 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
+
4
+ # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
5
+ # redistribute it and/or modify it under the same terms of Ruby's license;
6
+ # either the dual license version in 2003, or any later version.
7
+
8
+
9
+ module SOAP
10
+
11
+
12
+ TypeMap = {
13
+ XSD::XSDAnySimpleType::Type => SOAPAnySimpleType,
14
+ XSD::XSDString::Type => SOAPString,
15
+ XSD::XSDBoolean::Type => SOAPBoolean,
16
+ XSD::XSDDecimal::Type => SOAPDecimal,
17
+ XSD::XSDFloat::Type => SOAPFloat,
18
+ XSD::XSDDouble::Type => SOAPDouble,
19
+ XSD::XSDDuration::Type => SOAPDuration,
20
+ XSD::XSDDateTime::Type => SOAPDateTime,
21
+ XSD::XSDTime::Type => SOAPTime,
22
+ XSD::XSDDate::Type => SOAPDate,
23
+ XSD::XSDGYearMonth::Type => SOAPGYearMonth,
24
+ XSD::XSDGYear::Type => SOAPGYear,
25
+ XSD::XSDGMonthDay::Type => SOAPGMonthDay,
26
+ XSD::XSDGDay::Type => SOAPGDay,
27
+ XSD::XSDGMonth::Type => SOAPGMonth,
28
+ XSD::XSDHexBinary::Type => SOAPHexBinary,
29
+ XSD::XSDBase64Binary::Type => SOAPBase64,
30
+ XSD::XSDAnyURI::Type => SOAPAnyURI,
31
+ XSD::XSDQName::Type => SOAPQName,
32
+ XSD::XSDInteger::Type => SOAPInteger,
33
+ XSD::XSDNonPositiveInteger::Type => SOAPNonPositiveInteger,
34
+ XSD::XSDNegativeInteger::Type => SOAPNegativeInteger,
35
+ XSD::XSDLong::Type => SOAPLong,
36
+ XSD::XSDInt::Type => SOAPInt,
37
+ XSD::XSDShort::Type => SOAPShort,
38
+ XSD::XSDByte::Type => SOAPByte,
39
+ XSD::XSDNonNegativeInteger::Type => SOAPNonNegativeInteger,
40
+ XSD::XSDUnsignedLong::Type => SOAPUnsignedLong,
41
+ XSD::XSDUnsignedInt::Type => SOAPUnsignedInt,
42
+ XSD::XSDUnsignedShort::Type => SOAPUnsignedShort,
43
+ XSD::XSDUnsignedByte::Type => SOAPUnsignedByte,
44
+ XSD::XSDPositiveInteger::Type => SOAPPositiveInteger,
45
+
46
+ SOAP::SOAPBase64::Type => SOAPBase64,
47
+ }
48
+
49
+
50
+ end
@@ -0,0 +1,280 @@
1
+ # SOAP4R - WSDL encoded mapping registry.
2
+ # Copyright (C) 2000-2003, 2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
+
4
+ # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
5
+ # redistribute it and/or modify it under the same terms of Ruby's license;
6
+ # either the dual license version in 2003, or any later version.
7
+
8
+
9
+ require 'xsd/qname'
10
+ require 'xsd/namedelements'
11
+ require 'soap/baseData'
12
+ require 'soap/mapping/mapping'
13
+ require 'soap/mapping/typeMap'
14
+
15
+
16
+ module SOAP
17
+ module Mapping
18
+
19
+
20
+ class WSDLEncodedRegistry < Registry
21
+ include TraverseSupport
22
+
23
+ attr_reader :definedelements
24
+ attr_reader :definedtypes
25
+ attr_accessor :excn_handler_obj2soap
26
+ attr_accessor :excn_handler_soap2obj
27
+
28
+ def initialize(definedtypes = XSD::NamedElements::Empty)
29
+ @definedtypes = definedtypes
30
+ # @definedelements = definedelements needed?
31
+ @excn_handler_obj2soap = nil
32
+ @excn_handler_soap2obj = nil
33
+ # For mapping AnyType element.
34
+ @rubytype_factory = RubytypeFactory.new(
35
+ :allow_untyped_struct => true,
36
+ :allow_original_mapping => true
37
+ )
38
+ @schema_element_cache = {}
39
+ end
40
+
41
+ def obj2soap(obj, qname = nil)
42
+ soap_obj = nil
43
+ if type = @definedtypes[qname]
44
+ soap_obj = obj2typesoap(obj, type)
45
+ else
46
+ soap_obj = any2soap(obj, qname)
47
+ end
48
+ return soap_obj if soap_obj
49
+ if @excn_handler_obj2soap
50
+ soap_obj = @excn_handler_obj2soap.call(obj) { |yield_obj|
51
+ Mapping._obj2soap(yield_obj, self)
52
+ }
53
+ return soap_obj if soap_obj
54
+ end
55
+ if qname
56
+ raise MappingError.new("cannot map #{obj.class.name} as #{qname}")
57
+ else
58
+ raise MappingError.new("cannot map #{obj.class.name} to SOAP/OM")
59
+ end
60
+ end
61
+
62
+ # map anything for now: must refer WSDL while mapping. [ToDo]
63
+ def soap2obj(node, obj_class = nil)
64
+ begin
65
+ return any2obj(node, obj_class)
66
+ rescue MappingError
67
+ end
68
+ if @excn_handler_soap2obj
69
+ begin
70
+ return @excn_handler_soap2obj.call(node) { |yield_node|
71
+ Mapping._soap2obj(yield_node, self)
72
+ }
73
+ rescue Exception
74
+ end
75
+ end
76
+ raise MappingError.new("cannot map #{node.type.name} to Ruby object")
77
+ end
78
+
79
+ private
80
+
81
+ def any2soap(obj, qname)
82
+ if obj.nil?
83
+ SOAPNil.new
84
+ elsif qname.nil? or qname == XSD::AnyTypeName
85
+ @rubytype_factory.obj2soap(nil, obj, nil, self)
86
+ elsif obj.is_a?(XSD::NSDBase)
87
+ soap2soap(obj, qname)
88
+ elsif (type = TypeMap[qname])
89
+ base2soap(obj, type)
90
+ else
91
+ nil
92
+ end
93
+ end
94
+
95
+ def soap2soap(obj, type_qname)
96
+ if obj.is_a?(SOAPBasetype)
97
+ obj
98
+ elsif obj.is_a?(SOAPStruct) && (type = @definedtypes[type_qname])
99
+ soap_obj = obj
100
+ mark_marshalled_obj(obj, soap_obj)
101
+ elements2soap(obj, soap_obj, type.content.elements)
102
+ soap_obj
103
+ elsif obj.is_a?(SOAPArray) && (type = @definedtypes[type_qname])
104
+ soap_obj = obj
105
+ contenttype = type.child_type
106
+ mark_marshalled_obj(obj, soap_obj)
107
+ obj.replace do |ele|
108
+ Mapping._obj2soap(ele, self, contenttype)
109
+ end
110
+ soap_obj
111
+ else
112
+ nil
113
+ end
114
+ end
115
+
116
+ def obj2typesoap(obj, type)
117
+ if type.is_a?(::WSDL::XMLSchema::SimpleType)
118
+ simpleobj2soap(obj, type)
119
+ else
120
+ complexobj2soap(obj, type)
121
+ end
122
+ end
123
+
124
+ def simpleobj2soap(obj, type)
125
+ type.check_lexical_format(obj)
126
+ return SOAPNil.new if obj.nil? # ToDo: check nillable.
127
+ o = base2soap(obj, TypeMap[type.base])
128
+ o
129
+ end
130
+
131
+ def complexobj2soap(obj, type)
132
+ case type.compoundtype
133
+ when :TYPE_STRUCT
134
+ struct2soap(obj, type.name, type)
135
+ when :TYPE_ARRAY
136
+ array2soap(obj, type.name, type)
137
+ when :TYPE_MAP
138
+ map2soap(obj, type.name, type)
139
+ when :TYPE_SIMPLE
140
+ simpleobj2soap(obj, type.simplecontent)
141
+ when :TYPE_EMPTY
142
+ raise MappingError.new("should be empty") unless obj.nil?
143
+ SOAPNil.new
144
+ else
145
+ raise MappingError.new("unknown compound type: #{type.compoundtype}")
146
+ end
147
+ end
148
+
149
+ def base2soap(obj, type)
150
+ soap_obj = nil
151
+ if type <= XSD::XSDString
152
+ str = XSD::Charset.encoding_conv(obj.to_s,
153
+ Thread.current[:SOAPExternalCES], XSD::Charset.encoding)
154
+ soap_obj = type.new(str)
155
+ mark_marshalled_obj(obj, soap_obj)
156
+ else
157
+ soap_obj = type.new(obj)
158
+ end
159
+ soap_obj
160
+ end
161
+
162
+ def struct2soap(obj, type_qname, type)
163
+ return SOAPNil.new if obj.nil? # ToDo: check nillable.
164
+ soap_obj = SOAPStruct.new(type_qname)
165
+ unless obj.nil?
166
+ mark_marshalled_obj(obj, soap_obj)
167
+ elements2soap(obj, soap_obj, type.content.elements)
168
+ end
169
+ soap_obj
170
+ end
171
+
172
+ def array2soap(obj, type_qname, type)
173
+ return SOAPNil.new if obj.nil? # ToDo: check nillable.
174
+ arytype = type.child_type
175
+ soap_obj = SOAPArray.new(ValueArrayName, 1, arytype)
176
+ unless obj.nil?
177
+ mark_marshalled_obj(obj, soap_obj)
178
+ obj.each do |item|
179
+ soap_obj.add(Mapping._obj2soap(item, self, arytype))
180
+ end
181
+ end
182
+ soap_obj
183
+ end
184
+
185
+ MapKeyName = XSD::QName.new(nil, "key")
186
+ MapValueName = XSD::QName.new(nil, "value")
187
+ def map2soap(obj, type_qname, type)
188
+ return SOAPNil.new if obj.nil? # ToDo: check nillable.
189
+ keytype = type.child_type(MapKeyName) || XSD::AnyTypeName
190
+ valuetype = type.child_type(MapValueName) || XSD::AnyTypeName
191
+ soap_obj = SOAPStruct.new(MapQName)
192
+ unless obj.nil?
193
+ mark_marshalled_obj(obj, soap_obj)
194
+ obj.each do |key, value|
195
+ elem = SOAPStruct.new
196
+ elem.add("key", Mapping._obj2soap(key, self, keytype))
197
+ elem.add("value", Mapping._obj2soap(value, self, valuetype))
198
+ # ApacheAxis allows only 'item' here.
199
+ soap_obj.add("item", elem)
200
+ end
201
+ end
202
+ soap_obj
203
+ end
204
+
205
+ def elements2soap(obj, soap_obj, elements)
206
+ elements.each do |element|
207
+ name = element.name.name
208
+ child_obj = Mapping.get_attribute(obj, name)
209
+ soap_obj.add(name,
210
+ Mapping._obj2soap(child_obj, self, element.type || element.name))
211
+ end
212
+ end
213
+
214
+ def any2obj(node, obj_class)
215
+ unless obj_class
216
+ typestr = XSD::CodeGen::GenSupport.safeconstname(node.elename.name)
217
+ obj_class = Mapping.class_from_name(typestr)
218
+ end
219
+ if obj_class and obj_class.class_variables.include?('@@schema_element')
220
+ soap2stubobj(node, obj_class)
221
+ else
222
+ Mapping._soap2obj(node, Mapping::DefaultRegistry, obj_class)
223
+ end
224
+ end
225
+
226
+ def soap2stubobj(node, obj_class)
227
+ obj = Mapping.create_empty_object(obj_class)
228
+ unless node.is_a?(SOAPNil)
229
+ add_elements2stubobj(node, obj)
230
+ end
231
+ obj
232
+ end
233
+
234
+ def add_elements2stubobj(node, obj)
235
+ elements, as_array = schema_element_definition(obj.class)
236
+ vars = {}
237
+ node.each do |name, value|
238
+ item = elements.find { |k, v| k.name == name }
239
+ if item
240
+ elename, class_name = item
241
+ if klass = Mapping.class_from_name(class_name)
242
+ # klass must be a SOAPBasetype or a class
243
+ if klass.ancestors.include?(::SOAP::SOAPBasetype)
244
+ if value.respond_to?(:data)
245
+ child = klass.new(value.data).data
246
+ else
247
+ child = klass.new(nil).data
248
+ end
249
+ else
250
+ child = Mapping._soap2obj(value, self, klass)
251
+ end
252
+ elsif klass = Mapping.module_from_name(class_name)
253
+ # simpletype
254
+ if value.respond_to?(:data)
255
+ child = value.data
256
+ else
257
+ raise MappingError.new(
258
+ "cannot map to a module value: #{class_name}")
259
+ end
260
+ else
261
+ raise MappingError.new("unknown class: #{class_name}")
262
+ end
263
+ else # untyped element is treated as anyType.
264
+ child = Mapping._soap2obj(value, self)
265
+ end
266
+ vars[name] = child
267
+ end
268
+ Mapping.set_attributes(obj, vars)
269
+ end
270
+
271
+ # it caches @@schema_element. this means that @@schema_element must not be
272
+ # changed while a lifetime of a WSDLLiteralRegistry.
273
+ def schema_element_definition(klass)
274
+ @schema_element_cache[klass] ||= Mapping.schema_element_definition(klass)
275
+ end
276
+ end
277
+
278
+
279
+ end
280
+ end
@@ -0,0 +1,418 @@
1
+ # SOAP4R - WSDL literal mapping registry.
2
+ # Copyright (C) 2004, 2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
+
4
+ # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
5
+ # redistribute it and/or modify it under the same terms of Ruby's license;
6
+ # either the dual license version in 2003, or any later version.
7
+
8
+
9
+ require 'soap/baseData'
10
+ require 'soap/mapping/mapping'
11
+ require 'soap/mapping/typeMap'
12
+ require 'xsd/codegen/gensupport'
13
+ require 'xsd/namedelements'
14
+
15
+
16
+ module SOAP
17
+ module Mapping
18
+
19
+
20
+ class WSDLLiteralRegistry < Registry
21
+ attr_reader :definedelements
22
+ attr_reader :definedtypes
23
+ attr_accessor :excn_handler_obj2soap
24
+ attr_accessor :excn_handler_soap2obj
25
+
26
+ def initialize(definedtypes = XSD::NamedElements::Empty,
27
+ definedelements = XSD::NamedElements::Empty)
28
+ @definedtypes = definedtypes
29
+ @definedelements = definedelements
30
+ @excn_handler_obj2soap = nil
31
+ @excn_handler_soap2obj = nil
32
+ @schema_element_cache = {}
33
+ @schema_attribute_cache = {}
34
+ end
35
+
36
+ def obj2soap(obj, qname)
37
+ soap_obj = nil
38
+ if ele = @definedelements[qname]
39
+ soap_obj = obj2elesoap(obj, ele)
40
+ elsif type = @definedtypes[qname]
41
+ soap_obj = obj2typesoap(obj, type, true)
42
+ else
43
+ soap_obj = any2soap(obj, qname)
44
+ end
45
+ return soap_obj if soap_obj
46
+ if @excn_handler_obj2soap
47
+ soap_obj = @excn_handler_obj2soap.call(obj) { |yield_obj|
48
+ Mapping.obj2soap(yield_obj, nil, nil, MAPPING_OPT)
49
+ }
50
+ return soap_obj if soap_obj
51
+ end
52
+ raise MappingError.new("cannot map #{obj.class.name} as #{qname}")
53
+ end
54
+
55
+ # node should be a SOAPElement
56
+ def soap2obj(node, obj_class = nil)
57
+ # obj_class is given when rpc/literal service. but ignored for now.
58
+ begin
59
+ return any2obj(node)
60
+ rescue MappingError
61
+ end
62
+ if @excn_handler_soap2obj
63
+ begin
64
+ return @excn_handler_soap2obj.call(node) { |yield_node|
65
+ Mapping.soap2obj(yield_node, nil, nil, MAPPING_OPT)
66
+ }
67
+ rescue Exception
68
+ end
69
+ end
70
+ if node.respond_to?(:type)
71
+ raise MappingError.new("cannot map #{node.type.name} to Ruby object")
72
+ else
73
+ raise MappingError.new("cannot map #{node.elename.name} to Ruby object")
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ MAPPING_OPT = { :no_reference => true }
80
+
81
+ def obj2elesoap(obj, ele)
82
+ o = nil
83
+ qualified = (ele.elementform == 'qualified')
84
+ if ele.type
85
+ if type = @definedtypes[ele.type]
86
+ o = obj2typesoap(obj, type, qualified)
87
+ elsif type = TypeMap[ele.type]
88
+ o = base2soap(obj, type)
89
+ else
90
+ raise MappingError.new("cannot find type #{ele.type}")
91
+ end
92
+ elsif ele.local_complextype
93
+ o = obj2typesoap(obj, ele.local_complextype, qualified)
94
+ add_attributes2soap(obj, o)
95
+ elsif ele.local_simpletype
96
+ o = obj2typesoap(obj, ele.local_simpletype, qualified)
97
+ else
98
+ raise MappingError.new('illegal schema?')
99
+ end
100
+ o.elename = ele.name
101
+ o
102
+ end
103
+
104
+ def obj2typesoap(obj, type, qualified)
105
+ if type.is_a?(::WSDL::XMLSchema::SimpleType)
106
+ simpleobj2soap(obj, type)
107
+ else
108
+ complexobj2soap(obj, type, qualified)
109
+ end
110
+ end
111
+
112
+ def simpleobj2soap(obj, type)
113
+ type.check_lexical_format(obj)
114
+ return SOAPNil.new if obj.nil? # ToDo: check nillable.
115
+ o = base2soap(obj, TypeMap[type.base])
116
+ o
117
+ end
118
+
119
+ def complexobj2soap(obj, type, qualified)
120
+ o = SOAPElement.new(type.name)
121
+ o.qualified = qualified
122
+ type.each_element do |child_ele|
123
+ child = Mapping.get_attribute(obj, child_ele.name.name)
124
+ if child.nil?
125
+ if child_ele.nillable
126
+ # ToDo: test
127
+ # add empty element
128
+ child_soap = obj2elesoap(nil, child_ele)
129
+ o.add(child_soap)
130
+ elsif Integer(child_ele.minoccurs) == 0
131
+ # nothing to do
132
+ else
133
+ raise MappingError.new("nil not allowed: #{child_ele.name.name}")
134
+ end
135
+ elsif child_ele.map_as_array?
136
+ child.each do |item|
137
+ child_soap = obj2elesoap(item, child_ele)
138
+ o.add(child_soap)
139
+ end
140
+ else
141
+ child_soap = obj2elesoap(child, child_ele)
142
+ o.add(child_soap)
143
+ end
144
+ end
145
+ o
146
+ end
147
+
148
+ def any2soap(obj, qname)
149
+ if obj.is_a?(SOAPElement)
150
+ obj
151
+ elsif obj.class.class_variables.include?('@@schema_element')
152
+ stubobj2soap(obj, qname)
153
+ elsif obj.is_a?(SOAP::Mapping::Object)
154
+ mappingobj2soap(obj, qname)
155
+ elsif obj.is_a?(Hash)
156
+ ele = SOAPElement.from_obj(obj)
157
+ ele.elename = qname
158
+ ele
159
+ else
160
+ # expected to be a basetype or an anyType.
161
+ # SOAPStruct, etc. is used instead of SOAPElement.
162
+ begin
163
+ ele = Mapping.obj2soap(obj, nil, nil, MAPPING_OPT)
164
+ ele.elename = qname
165
+ ele
166
+ rescue MappingError
167
+ ele = SOAPElement.new(qname, obj.to_s)
168
+ end
169
+ if obj.respond_to?(:__xmlattr)
170
+ obj.__xmlattr.each do |key, value|
171
+ ele.extraattr[key] = value
172
+ end
173
+ end
174
+ ele
175
+ end
176
+ end
177
+
178
+ def stubobj2soap(obj, qname)
179
+ ele = SOAPElement.new(qname)
180
+ ele.qualified =
181
+ (obj.class.class_variables.include?('@@schema_qualified') and
182
+ obj.class.class_eval('@@schema_qualified'))
183
+ add_elements2soap(obj, ele)
184
+ add_attributes2soap(obj, ele)
185
+ ele
186
+ end
187
+
188
+ def mappingobj2soap(obj, qname)
189
+ ele = SOAPElement.new(qname)
190
+ obj.__xmlele.each do |key, value|
191
+ if value.is_a?(::Array)
192
+ value.each do |item|
193
+ ele.add(obj2soap(item, key))
194
+ end
195
+ else
196
+ ele.add(obj2soap(value, key))
197
+ end
198
+ end
199
+ obj.__xmlattr.each do |key, value|
200
+ ele.extraattr[key] = value
201
+ end
202
+ ele
203
+ end
204
+
205
+ def add_elements2soap(obj, ele)
206
+ elements, as_array = schema_element_definition(obj.class)
207
+ if elements
208
+ elements.each do |elename, type|
209
+ if child = Mapping.get_attribute(obj, elename.name)
210
+ if as_array.include?(elename.name)
211
+ child.each do |item|
212
+ ele.add(obj2soap(item, elename))
213
+ end
214
+ else
215
+ ele.add(obj2soap(child, elename))
216
+ end
217
+ elsif obj.is_a?(::Array) and as_array.include?(elename.name)
218
+ obj.each do |item|
219
+ ele.add(obj2soap(item, elename))
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
225
+
226
+ def add_attributes2soap(obj, ele)
227
+ attributes = schema_attribute_definition(obj.class)
228
+ if attributes
229
+ attributes.each do |qname, param|
230
+ attr = obj.__send__('xmlattr_' +
231
+ XSD::CodeGen::GenSupport.safevarname(qname.name))
232
+ ele.extraattr[qname] = attr
233
+ end
234
+ end
235
+ end
236
+
237
+ def base2soap(obj, type)
238
+ soap_obj = nil
239
+ if type <= XSD::XSDString
240
+ str = XSD::Charset.encoding_conv(obj.to_s,
241
+ Thread.current[:SOAPExternalCES], XSD::Charset.encoding)
242
+ soap_obj = type.new(str)
243
+ else
244
+ soap_obj = type.new(obj)
245
+ end
246
+ soap_obj
247
+ end
248
+
249
+ def anytype2obj(node)
250
+ if node.is_a?(::SOAP::SOAPBasetype)
251
+ return node.data
252
+ end
253
+ klass = ::SOAP::Mapping::Object
254
+ obj = klass.new
255
+ obj
256
+ end
257
+
258
+ def any2obj(node, obj_class = nil)
259
+ unless obj_class
260
+ typestr = XSD::CodeGen::GenSupport.safeconstname(node.elename.name)
261
+ obj_class = Mapping.class_from_name(typestr)
262
+ end
263
+ if obj_class and obj_class.class_variables.include?('@@schema_element')
264
+ soapele2stubobj(node, obj_class)
265
+ elsif node.is_a?(SOAPElement) or node.is_a?(SOAPStruct)
266
+ # SOAPArray for literal?
267
+ soapele2plainobj(node)
268
+ else
269
+ obj = Mapping.soap2obj(node, nil, obj_class, MAPPING_OPT)
270
+ add_attributes2plainobj(node, obj)
271
+ obj
272
+ end
273
+ end
274
+
275
+ def soapele2stubobj(node, obj_class)
276
+ obj = Mapping.create_empty_object(obj_class)
277
+ add_elements2stubobj(node, obj)
278
+ add_attributes2stubobj(node, obj)
279
+ obj
280
+ end
281
+
282
+ def soapele2plainobj(node)
283
+ obj = anytype2obj(node)
284
+ add_elements2plainobj(node, obj)
285
+ add_attributes2plainobj(node, obj)
286
+ obj
287
+ end
288
+
289
+ def add_elements2stubobj(node, obj)
290
+ elements, as_array = schema_element_definition(obj.class)
291
+ vars = {}
292
+ node.each do |name, value|
293
+ item = elements.find { |k, v| k.name == name }
294
+ if item
295
+ elename, class_name = item
296
+ if klass = Mapping.class_from_name(class_name)
297
+ # klass must be a SOAPBasetype or a class
298
+ if klass.ancestors.include?(::SOAP::SOAPBasetype)
299
+ if value.respond_to?(:data)
300
+ child = klass.new(value.data).data
301
+ else
302
+ child = klass.new(nil).data
303
+ end
304
+ else
305
+ child = any2obj(value, klass)
306
+ end
307
+ elsif klass = Mapping.module_from_name(class_name)
308
+ # simpletype
309
+ if value.respond_to?(:data)
310
+ child = value.data
311
+ else
312
+ raise MappingError.new(
313
+ "cannot map to a module value: #{class_name}")
314
+ end
315
+ else
316
+ raise MappingError.new("unknown class/module: #{class_name}")
317
+ end
318
+ else # untyped element is treated as anyType.
319
+ child = any2obj(value)
320
+ end
321
+ if as_array.include?(elename.name)
322
+ (vars[name] ||= []) << child
323
+ else
324
+ vars[name] = child
325
+ end
326
+ end
327
+ Mapping.set_attributes(obj, vars)
328
+ end
329
+
330
+ def add_attributes2stubobj(node, obj)
331
+ if attributes = schema_attribute_definition(obj.class)
332
+ define_xmlattr(obj)
333
+ attributes.each do |qname, class_name|
334
+ attr = node.extraattr[qname]
335
+ next if attr.nil? or attr.empty?
336
+ klass = Mapping.class_from_name(class_name)
337
+ if klass.ancestors.include?(::SOAP::SOAPBasetype)
338
+ child = klass.new(attr).data
339
+ else
340
+ child = attr
341
+ end
342
+ obj.__xmlattr[qname] = child
343
+ define_xmlattr_accessor(obj, qname)
344
+ end
345
+ end
346
+ end
347
+
348
+ def add_elements2plainobj(node, obj)
349
+ node.each do |name, value|
350
+ obj.__add_xmlele_value(value.elename, any2obj(value))
351
+ end
352
+ end
353
+
354
+ def add_attributes2plainobj(node, obj)
355
+ return if node.extraattr.empty?
356
+ define_xmlattr(obj)
357
+ node.extraattr.each do |qname, value|
358
+ obj.__xmlattr[qname] = value
359
+ define_xmlattr_accessor(obj, qname)
360
+ end
361
+ end
362
+
363
+ if RUBY_VERSION > "1.7.0"
364
+ def define_xmlattr_accessor(obj, qname)
365
+ name = XSD::CodeGen::GenSupport.safemethodname(qname.name)
366
+ Mapping.define_attr_accessor(obj, 'xmlattr_' + name,
367
+ proc { @__xmlattr[qname] },
368
+ proc { |value| @__xmlattr[qname] = value })
369
+ end
370
+ else
371
+ def define_xmlattr_accessor(obj, qname)
372
+ name = XSD::CodeGen::GenSupport.safemethodname(qname.name)
373
+ obj.instance_eval <<-EOS
374
+ def #{name}
375
+ @__xmlattr[#{qname.dump}]
376
+ end
377
+
378
+ def #{name}=(value)
379
+ @__xmlattr[#{qname.dump}] = value
380
+ end
381
+ EOS
382
+ end
383
+ end
384
+
385
+ if RUBY_VERSION > "1.7.0"
386
+ def define_xmlattr(obj)
387
+ obj.instance_variable_set('@__xmlattr', {})
388
+ unless obj.respond_to?(:__xmlattr)
389
+ Mapping.define_attr_accessor(obj, :__xmlattr, proc { @__xmlattr })
390
+ end
391
+ end
392
+ else
393
+ def define_xmlattr(obj)
394
+ obj.instance_variable_set('@__xmlattr', {})
395
+ unless obj.respond_to?(:__xmlattr)
396
+ obj.instance_eval <<-EOS
397
+ def __xmlattr
398
+ @__xmlattr
399
+ end
400
+ EOS
401
+ end
402
+ end
403
+ end
404
+
405
+ # it caches @@schema_element. this means that @@schema_element must not be
406
+ # changed while a lifetime of a WSDLLiteralRegistry.
407
+ def schema_element_definition(klass)
408
+ @schema_element_cache[klass] ||= Mapping.schema_element_definition(klass)
409
+ end
410
+
411
+ def schema_attribute_definition(klass)
412
+ @schema_attribute_cache[klass] ||= Mapping.schema_attribute_definition(klass)
413
+ end
414
+ end
415
+
416
+
417
+ end
418
+ end