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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
# SOAP4R - Ruby type mapping utility.
# Copyright (C) 2000, 2001, 2003-2005 NAKAMURA Hiroshi <[email protected]>.
# 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 'xsd/codegen/gensupport'
module SOAP
module Mapping
RubyTypeNamespace = 'http://www.ruby-lang.org/xmlns/ruby/type/1.6'
RubyTypeInstanceNamespace =
'http://www.ruby-lang.org/xmlns/ruby/type-instance'
RubyCustomTypeNamespace = 'http://www.ruby-lang.org/xmlns/ruby/type/custom'
ApacheSOAPTypeNamespace = 'http://xml.apache.org/xml-soap'
# TraverseSupport breaks Thread.current[:SOAPMarshalDataKey].
module TraverseSupport
def mark_marshalled_obj(obj, soap_obj)
raise if obj.nil?
Thread.current[:SOAPMarshalDataKey][obj.__id__] = soap_obj
end
def mark_unmarshalled_obj(node, obj)
return if obj.nil?
# node.id is not Object#id but SOAPReference#id
Thread.current[:SOAPMarshalDataKey][node.id] = obj
end
end
def self.obj2soap(obj, registry = nil, type = nil)
registry ||= Mapping::DefaultRegistry
Thread.current[:SOAPMarshalDataKey] = {}
soap_obj = _obj2soap(obj, registry, type)
Thread.current[:SOAPMarshalDataKey] = nil
soap_obj
end
def self.soap2obj(node, registry = nil, klass = nil)
registry ||= Mapping::DefaultRegistry
Thread.current[:SOAPMarshalDataKey] = {}
obj = _soap2obj(node, registry, klass)
Thread.current[:SOAPMarshalDataKey] = nil
obj
end
def self.ary2soap(ary, type_ns = XSD::Namespace, typename = XSD::AnyTypeLiteral, registry = nil)
registry ||= Mapping::DefaultRegistry
type = XSD::QName.new(type_ns, typename)
soap_ary = SOAPArray.new(ValueArrayName, 1, type)
Thread.current[:SOAPMarshalDataKey] = {}
ary.each do |ele|
soap_ary.add(_obj2soap(ele, registry, type))
end
Thread.current[:SOAPMarshalDataKey] = nil
soap_ary
end
def self.ary2md(ary, rank, type_ns = XSD::Namespace, typename = XSD::AnyTypeLiteral, registry = nil)
registry ||= Mapping::DefaultRegistry
type = XSD::QName.new(type_ns, typename)
md_ary = SOAPArray.new(ValueArrayName, rank, type)
Thread.current[:SOAPMarshalDataKey] = {}
add_md_ary(md_ary, ary, [], registry)
Thread.current[:SOAPMarshalDataKey] = nil
md_ary
end
def self.fault2exception(fault, registry = nil)
registry ||= Mapping::DefaultRegistry
detail = if fault.detail
soap2obj(fault.detail, registry) || ""
else
""
end
if detail.is_a?(Mapping::SOAPException)
begin
e = detail.to_e
remote_backtrace = e.backtrace
e.set_backtrace(nil)
raise e # ruby sets current caller as local backtrace of e => e2.
rescue Exception => e
e.set_backtrace(remote_backtrace + e.backtrace[1..-1])
raise
end
else
fault.detail = detail
fault.set_backtrace(
if detail.is_a?(Array)
detail
else
[detail.to_s]
end
)
raise
end
end
def self._obj2soap(obj, registry, type = nil)
if referent = Thread.current[:SOAPMarshalDataKey][obj.__id__]
SOAPReference.new(referent)
elsif registry
registry.obj2soap(obj, type)
else
raise MappingError.new("no mapping registry given")
end
end
def self._soap2obj(node, registry, klass = nil)
if node.is_a?(SOAPReference)
target = node.__getobj__
# target.id is not Object#id but SOAPReference#id
if referent = Thread.current[:SOAPMarshalDataKey][target.id]
return referent
else
return _soap2obj(target, registry, klass)
end
end
return registry.soap2obj(node, klass)
end
if Object.respond_to?(:allocate)
# ruby/1.7 or later.
def self.create_empty_object(klass)
klass.allocate
end
else
MARSHAL_TAG = {
String => ['"', 1],
Regexp => ['/', 2],
Array => ['[', 1],
Hash => ['{', 1]
}
def self.create_empty_object(klass)
if klass <= Struct
name = klass.name
return ::Marshal.load(sprintf("\004\006S:%c%s\000", name.length + 5, name))
end
if MARSHAL_TAG.has_key?(klass)
tag, terminate = MARSHAL_TAG[klass]
return ::Marshal.load(sprintf("\004\006%s%s", tag, "\000" * terminate))
end
MARSHAL_TAG.each do |k, v|
if klass < k
name = klass.name
tag, terminate = v
return ::Marshal.load(sprintf("\004\006C:%c%s%s%s", name.length + 5, name, tag, "\000" * terminate))
end
end
name = klass.name
::Marshal.load(sprintf("\004\006o:%c%s\000", name.length + 5, name))
end
end
# Allow only (Letter | '_') (Letter | Digit | '-' | '_')* here.
# Caution: '.' is not allowed here.
# To follow XML spec., it should be NCName.
# (denied chars) => .[0-F][0-F]
# ex. a.b => a.2eb
#
def self.name2elename(name)
name.gsub(/([^a-zA-Z0-9:_\-]+)/n) {
'.' << $1.unpack('H2' * $1.size).join('.')
}.gsub(/::/n, '..')
end
def self.elename2name(name)
name.gsub(/\.\./n, '::').gsub(/((?:\.[0-9a-fA-F]{2})+)/n) {
[$1.delete('.')].pack('H*')
}
end
def self.const_from_name(name, lenient = false)
const = ::Object
name.sub(/\A::/, '').split('::').each do |const_str|
if XSD::CodeGen::GenSupport.safeconstname?(const_str)
if const.const_defined?(const_str)
const = const.const_get(const_str)
next
end
elsif lenient
const_str = XSD::CodeGen::GenSupport.safeconstname(const_str)
if const.const_defined?(const_str)
const = const.const_get(const_str)
next
end
end
return nil
end
const
end
def self.class_from_name(name, lenient = false)
const = const_from_name(name, lenient)
if const.is_a?(::Class)
const
else
nil
end
end
def self.module_from_name(name, lenient = false)
const = const_from_name(name, lenient)
if const.is_a?(::Module)
const
else
nil
end
end
def self.class2qname(klass)
name = if klass.class_variables.include?('@@schema_type')
klass.class_eval('@@schema_type')
else
nil
end
namespace = if klass.class_variables.include?('@@schema_ns')
klass.class_eval('@@schema_ns')
else
nil
end
XSD::QName.new(namespace, name)
end
def self.class2element(klass)
type = Mapping.class2qname(klass)
type.name ||= Mapping.name2elename(klass.name)
type.namespace ||= RubyCustomTypeNamespace
type
end
def self.obj2element(obj)
name = namespace = nil
ivars = obj.instance_variables
if ivars.include?('@schema_type')
name = obj.instance_variable_get('@schema_type')
end
if ivars.include?('@schema_ns')
namespace = obj.instance_variable_get('@schema_ns')
end
if !name or !namespace
class2qname(obj.class)
else
XSD::QName.new(namespace, name)
end
end
def self.define_singleton_method(obj, name, &block)
sclass = (class << obj; self; end)
sclass.__send__(:define_method, name, &block)
end
def self.get_attribute(obj, attr_name)
if obj.is_a?(::Hash)
obj[attr_name] || obj[attr_name.intern]
else
name = XSD::CodeGen::GenSupport.safevarname(attr_name)
if obj.instance_variables.include?('@' + name)
obj.instance_variable_get('@' + name)
elsif ((obj.is_a?(::Struct) or obj.is_a?(Marshallable)) and
obj.respond_to?(name))
obj.__send__(name)
end
end
end
def self.set_attributes(obj, values)
if obj.is_a?(::SOAP::Mapping::Object)
values.each do |attr_name, value|
obj.__add_xmlele_value(attr_name, value)
end
else
values.each do |attr_name, value|
name = XSD::CodeGen::GenSupport.safevarname(attr_name)
setter = name + "="
if obj.respond_to?(setter)
obj.__send__(setter, value)
else
obj.instance_variable_set('@' + name, value)
begin
define_attr_accessor(obj, name,
proc { instance_variable_get('@' + name) },
proc { |value| instance_variable_set('@' + name, value) })
rescue TypeError
# singleton class may not exist (e.g. Float)
end
end
end
end
end
def self.define_attr_accessor(obj, name, getterproc, setterproc = nil)
define_singleton_method(obj, name, &getterproc)
define_singleton_method(obj, name + '=', &setterproc) if setterproc
end
def self.schema_element_definition(klass)
return nil unless klass.class_variables.include?('@@schema_element')
elements = {}
as_array = []
klass.class_eval('@@schema_element').each do |varname, definition|
class_name, name = definition
if /\[\]$/ =~ class_name
class_name = class_name.sub(/\[\]$/, '')
as_array << class_name
end
elements[name ? name.name : varname] = class_name
end
[elements, as_array]
end
def self.schema_attribute_definition(klass)
return nil unless klass.class_variables.include?('@@schema_attribute')
klass.class_eval('@@schema_attribute')
end
class << Mapping
private
def add_md_ary(md_ary, ary, indices, registry)
for idx in 0..(ary.size - 1)
if ary[idx].is_a?(Array)
add_md_ary(md_ary, ary[idx], indices + [idx], registry)
else
md_ary[*(indices + [idx])] = _obj2soap(ary[idx], registry)
end
end
end
end
end
end
|