1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# WSDL4R - Creating driver code from WSDL.
# Copyright (C) 2002, 2003, 2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.
require 'wsdl/info'
require 'wsdl/soap/classDefCreatorSupport'
require 'soap/rpc/element'
module WSDL
module SOAP
class MethodDefCreator
include ClassDefCreatorSupport
attr_reader :definitions
def initialize(definitions)
@definitions = definitions
@simpletypes = @definitions.collect_simpletypes
@complextypes = @definitions.collect_complextypes
@elements = @definitions.collect_elements
@types = []
end
def dump(porttype)
@types.clear
result = ""
operations = @definitions.porttype(porttype).operations
binding = @definitions.porttype_binding(porttype)
operations.each do |operation|
op_bind = binding.operations[operation.name]
next unless op_bind # no binding is defined
next unless op_bind.soapoperation # not a SOAP operation binding
result << ",\n" unless result.empty?
result << dump_method(operation, op_bind).chomp
end
return result, @types
end
def collect_rpcparameter(operation)
result = operation.inputparts.collect { |part|
collect_type(part.type)
param_set(::SOAP::RPC::SOAPMethod::IN, part.name, rpcdefinedtype(part))
}
outparts = operation.outputparts
if outparts.size > 0
retval = outparts[0]
collect_type(retval.type)
result << param_set(::SOAP::RPC::SOAPMethod::RETVAL, retval.name,
rpcdefinedtype(retval))
cdr(outparts).each { |part|
collect_type(part.type)
result << param_set(::SOAP::RPC::SOAPMethod::OUT, part.name,
rpcdefinedtype(part))
}
end
result
end
def collect_documentparameter(operation)
param = []
operation.inputparts.each do |input|
param << param_set(::SOAP::RPC::SOAPMethod::IN, input.name,
documentdefinedtype(input), elementqualified(input))
end
operation.outputparts.each do |output|
param << param_set(::SOAP::RPC::SOAPMethod::OUT, output.name,
documentdefinedtype(output), elementqualified(output))
end
param
end
private
def dump_method(operation, binding)
name = safemethodname(operation.name.name)
name_as = operation.name.name
style = binding.soapoperation_style
inputuse = binding.input.soapbody_use
outputuse = binding.output.soapbody_use
namespace = binding.input.soapbody.namespace
if style == :rpc
qname = XSD::QName.new(namespace, name_as)
paramstr = param2str(collect_rpcparameter(operation))
else
qname = nil
paramstr = param2str(collect_documentparameter(operation))
end
if paramstr.empty?
paramstr = '[]'
else
paramstr = "[ " << paramstr.split(/\r?\n/).join("\n ") << " ]"
end
definitions = <<__EOD__
#{ndq(binding.soapaction)},
#{dq(name)},
#{paramstr},
{ :request_style => #{sym(style.id2name)}, :request_use => #{sym(inputuse.id2name)},
:response_style => #{sym(style.id2name)}, :response_use => #{sym(outputuse.id2name)} }
__EOD__
if style == :rpc
return <<__EOD__
[ #{qname.dump},
#{definitions}]
__EOD__
else
return <<__EOD__
[ #{definitions}]
__EOD__
end
end
def rpcdefinedtype(part)
if mapped = basetype_mapped_class(part.type)
['::' + mapped.name]
elsif definedtype = @simpletypes[part.type]
['::' + basetype_mapped_class(definedtype.base).name]
elsif definedtype = @elements[part.element]
#['::SOAP::SOAPStruct', part.element.namespace, part.element.name]
['nil', part.element.namespace, part.element.name]
elsif definedtype = @complextypes[part.type]
case definedtype.compoundtype
when :TYPE_STRUCT, :TYPE_EMPTY # ToDo: empty should be treated as void.
type = create_class_name(part.type)
[type, part.type.namespace, part.type.name]
when :TYPE_MAP
[Hash.name, part.type.namespace, part.type.name]
when :TYPE_ARRAY
arytype = definedtype.find_arytype || XSD::AnyTypeName
ns = arytype.namespace
name = arytype.name.sub(/\[(?:,)*\]$/, '')
type = create_class_name(XSD::QName.new(ns, name))
[type + '[]', ns, name]
else
raise NotImplementedError.new("must not reach here")
end
else
raise RuntimeError.new("part: #{part.name} cannot be resolved")
end
end
def documentdefinedtype(part)
if mapped = basetype_mapped_class(part.type)
['::' + mapped.name, nil, part.name]
elsif definedtype = @simpletypes[part.type]
['::' + basetype_mapped_class(definedtype.base).name, nil, part.name]
elsif definedtype = @elements[part.element]
['::SOAP::SOAPElement', part.element.namespace, part.element.name]
elsif definedtype = @complextypes[part.type]
['::SOAP::SOAPElement', part.type.namespace, part.type.name]
else
raise RuntimeError.new("part: #{part.name} cannot be resolved")
end
end
def elementqualified(part)
if mapped = basetype_mapped_class(part.type)
false
elsif definedtype = @simpletypes[part.type]
false
elsif definedtype = @elements[part.element]
definedtype.elementform == 'qualified'
elsif definedtype = @complextypes[part.type]
false
else
raise RuntimeError.new("part: #{part.name} cannot be resolved")
end
end
def param_set(io_type, name, type, ele = nil)
[io_type, name, type, ele]
end
def collect_type(type)
# ignore inline type definition.
return if type.nil?
return if @types.include?(type)
@types << type
return unless @complextypes[type]
@complextypes[type].each_element do |element|
collect_type(element.type)
end
end
def param2str(params)
params.collect { |param|
io, name, type, ele = param
unless ele.nil?
"[#{dq(io)}, #{dq(name)}, #{type2str(type)}, #{ele2str(ele)}]"
else
"[#{dq(io)}, #{dq(name)}, #{type2str(type)}]"
end
}.join(",\n")
end
def type2str(type)
if type.size == 1
"[#{dq(type[0])}]"
else
"[#{dq(type[0])}, #{ndq(type[1])}, #{dq(type[2])}]"
end
end
def ele2str(ele)
qualified = ele
if qualified
"true"
else
"false"
end
end
def cdr(ary)
result = ary.dup
result.shift
result
end
end
end
end
|