Module: SOAP::Mapping

Defined in:
lib/soap/attachment.rb,
lib/soap/mapping/factory.rb,
lib/soap/mapping/mapping.rb,
lib/soap/mapping/registry.rb,
lib/soap/mapping/encodedregistry.rb,
lib/soap/mapping/literalregistry.rb,
lib/soap/mapping/rubytypeFactory.rb,
lib/soap/mapping/schemadefinition.rb,
lib/soap/mapping/wsdlencodedregistry.rb,
lib/soap/mapping/wsdlliteralregistry.rb

Defined Under Namespace

Modules: MappedException, RegistrySupport, SchemaComplexTypeDefinition, TraverseSupport Classes: ArrayFactory_, AttachmentFactory, Base64Factory_, BasetypeFactory_, DateTimeFactory_, EncodedRegistry, Factory, HashFactory_, LiteralRegistry, MappingError, Object, RubytypeFactory, SOAPException, SchemaChoiceDefinition, SchemaDefinition, SchemaElementDefinition, SchemaEmptyDefinition, SchemaSequenceDefinition, StringFactory_, TypedArrayFactory_, TypedStructFactory_, URIFactory_, WSDLEncodedRegistry, WSDLLiteralRegistry

Constant Summary collapse

MapQName =
XSD::QName.new(ApacheSOAPTypeNamespace, 'Map')
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'
EMPTY_OPT =
{}.freeze
MARSHAL_TAG =
{
  String => ['"', 1],
  Regexp => ['/', 2],
  Array => ['[', 1],
  Hash => ['{', 1]
}
EMPTY_ATTRIBUTES =
{}.freeze
RubyTypeName =
XSD::QName.new(RubyTypeInstanceNamespace, 'rubyType')
RubyExtendName =
XSD::QName.new(RubyTypeInstanceNamespace, 'extends')
RubyIVarName =
XSD::QName.new(RubyTypeInstanceNamespace, 'ivars')
Registry =
EncodedRegistry
DefaultRegistry =
EncodedRegistry.new
RubyOriginalRegistry =
EncodedRegistry.new(:allow_original_mapping => true)

Class Method Summary collapse

Class Method Details

._obj2soap(obj, registry, type = nil) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/soap/mapping/mapping.rb', line 122

def self._obj2soap(obj, registry, type = nil)
  if obj.respond_to?(:to_xmlpart)
    SOAPRawData.new(obj)
  elsif defined?(::REXML) and obj.is_a?(::REXML::Element)
    SOAPRawData.new(SOAPREXMLElementWrap.new(obj))
  elsif referent = Thread.current[:SOAPMapping][:MarshalKey][obj.__id__] and
      !Thread.current[:SOAPMapping][:NoReference]
    SOAPReference.new(referent)
  elsif registry
    registry.obj2soap(obj, type)
  else
    raise MappingError.new("no mapping registry given")
  end
end

._soap2obj(node, registry, klass = nil) ⇒ Object


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/soap/mapping/mapping.rb', line 137

def self._soap2obj(node, registry, klass = nil)
  if node.nil?
    return nil
  elsif node.is_a?(SOAPReference)
    target = node.__getobj__
    # target.id is not Object#id but SOAPReference#id
    if referent = Thread.current[:SOAPMapping][:MarshalKey][target.id] and
        !Thread.current[:SOAPMapping][:NoReference]
      return referent
    else
      return _soap2obj(target, registry, klass)
    end
  end
  return registry.soap2obj(node, klass)
end

.ary2md(ary, rank, type_ns = XSD::Namespace, typename = XSD::AnyTypeLiteral, registry = nil, opt = EMPTY_OPT) ⇒ Object


82
83
84
85
86
87
88
89
90
# File 'lib/soap/mapping/mapping.rb', line 82

def self.ary2md(ary, rank, type_ns = XSD::Namespace, typename = XSD::AnyTypeLiteral, registry = nil, opt = EMPTY_OPT)
  registry ||= Mapping::DefaultRegistry
  type = XSD::QName.new(type_ns, typename)
  md_ary = SOAPArray.new(ValueArrayName, rank, type)
  protect_mapping(opt) do
    add_md_ary(md_ary, ary, [], registry)
  end
  md_ary
end

.ary2soap(ary, type_ns = XSD::Namespace, typename = XSD::AnyTypeLiteral, registry = nil, opt = EMPTY_OPT) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/soap/mapping/mapping.rb', line 70

def self.ary2soap(ary, type_ns = XSD::Namespace, typename = XSD::AnyTypeLiteral, registry = nil, opt = EMPTY_OPT)
  registry ||= Mapping::DefaultRegistry
  type = XSD::QName.new(type_ns, typename)
  soap_ary = SOAPArray.new(ValueArrayName, 1, type)
  protect_mapping(opt) do
    ary.each do |ele|
      soap_ary.add(_obj2soap(ele, registry, type))
    end
  end
  soap_ary
end

.class2element(klass) ⇒ Object


250
251
252
253
254
255
# File 'lib/soap/mapping/mapping.rb', line 250

def self.class2element(klass)
  name = schema_type_definition(klass) ||
    Mapping.name2elename(klass.name)
  namespace = schema_ns_definition(klass) || RubyCustomTypeNamespace
  XSD::QName.new(namespace, name)
end

.class2qname(klass) ⇒ Object


244
245
246
247
248
# File 'lib/soap/mapping/mapping.rb', line 244

def self.class2qname(klass)
  name = schema_type_definition(klass)
  namespace = schema_ns_definition(klass)
  XSD::QName.new(namespace, name)
end

.class_from_name(name, lenient = false) ⇒ Object


226
227
228
229
230
231
232
233
# File 'lib/soap/mapping/mapping.rb', line 226

def self.class_from_name(name, lenient = false)
  const = const_from_name(name, lenient)
  if const.is_a?(::Class)
    const
  else
    nil
  end
end

.const_from_name(name, lenient = false) ⇒ Object


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/soap/mapping/mapping.rb', line 204

def self.const_from_name(name, lenient = false)
  const = ::Object
  name.sub(/\A::/, '').split('::').each do |const_str|
    begin
      if const.const_defined?(const_str)
        const = const.const_get(const_str)
        next
      end
    rescue NameError
      if lenient
        const_str = XSD::CodeGen::GenSupport.safeconstname(const_str)
        if const.const_defined?(const_str)
          const = const.const_get(const_str)
          next
        end
      end
    end
    return nil
  end
  const
end

.create_empty_object(klass) ⇒ Object

ruby/1.7 or later.


155
156
157
# File 'lib/soap/mapping/mapping.rb', line 155

def self.create_empty_object(klass)
  klass.allocate
end

.create_schema_definition(klass, definition) ⇒ Object


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/soap/mapping/mapping.rb', line 410

def self.create_schema_definition(klass, definition)
  schema_ns = definition[:schema_ns]
  schema_name = definition[:schema_name]
  schema_type = definition[:schema_type]
  schema_qualified = definition[:schema_qualified]
  schema_element = definition[:schema_element]
  schema_attributes = definition[:schema_attribute]
  elename = schema_name ? XSD::QName.new(schema_ns, schema_name) : nil
  type = schema_type ? XSD::QName.new(schema_ns, schema_type) : nil
  definition = SchemaDefinition.new(klass, elename, type, schema_qualified)
  definition.attributes = schema_attributes
  if schema_element
    if schema_element.respond_to?(:is_concrete_definition) and
        schema_element.is_concrete_definition
      definition.elements = schema_element
    else
      default_ns = elename.namespace if elename
      default_ns ||= type.namespace if type
      definition.elements = parse_schema_definition(schema_element, default_ns)
      if klass < ::Array
        definition.elements.set_array
      end
    end
  end
  definition
end

.define_singleton_method(obj, name, &block) ⇒ Object


273
274
275
276
277
278
# File 'lib/soap/mapping/mapping.rb', line 273

def self.define_singleton_method(obj, name, &block)
  sclass = (class << obj; self; end)
  sclass.class_eval {
    define_method(name, &block)
  }
end

.elename2name(name) ⇒ Object


198
199
200
201
202
# File 'lib/soap/mapping/mapping.rb', line 198

def self.elename2name(name)
  name.gsub(/\.\./n, '::').gsub(/((?:\.[0-9a-fA-F]{2})+)/n) {
    [$1.delete('.')].pack('H*')
  }
end

.fault2exception(fault, registry = nil) ⇒ Object


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
# File 'lib/soap/mapping/mapping.rb', line 92

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

.get_attribute(obj, attr_name) ⇒ Object


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/soap/mapping/mapping.rb', line 301

def self.get_attribute(obj, attr_name)
  case obj
  when ::SOAP::Mapping::Object
    return obj[attr_name]
  when ::Hash
    return obj[attr_name] || obj[attr_name.intern]
  else
    if obj.respond_to?(attr_name)
      return obj.__send__(attr_name)
    end
    iv = obj.instance_variables
    name = XSD::CodeGen::GenSupport.safevarname(attr_name)
    if iv.include?("@#{name}")
      return obj.instance_variable_get("@#{name}")
    elsif iv.include?("@#{attr_name}")
      return obj.instance_variable_get("@#{attr_name}")
    end
    if obj.respond_to?(name)
      return obj.__send__(name)
    end
    nil
  end
end

.get_attributes(obj) ⇒ Object


280
281
282
283
284
285
286
287
288
289
290
# File 'lib/soap/mapping/mapping.rb', line 280

def self.get_attributes(obj)
  if obj.is_a?(::Hash)
    obj
  else
    rs = {}
    obj.instance_variables.each do |ele|
      rs[ele.sub(/^@/, '')] = obj.instance_variable_get(ele)
    end
    rs
  end
end

.get_attributes_for_any(obj) ⇒ Object


293
294
295
296
297
298
299
# File 'lib/soap/mapping/mapping.rb', line 293

def self.get_attributes_for_any(obj)
  if obj.respond_to?(:__xmlele_any)
    obj.__xmlele_any || EMPTY_ATTRIBUTES
  else
    get_attributes(obj)
  end
end

.module_from_name(name, lenient = false) ⇒ Object


235
236
237
238
239
240
241
242
# File 'lib/soap/mapping/mapping.rb', line 235

def self.module_from_name(name, lenient = false)
  const = const_from_name(name, lenient)
  if const.is_a?(::Module)
    const
  else
    nil
  end
end

.name2elename(name) ⇒ Object

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

192
193
194
195
196
# File 'lib/soap/mapping/mapping.rb', line 192

def self.name2elename(name)
  name.gsub(/([^a-zA-Z0-9:_\-]+)/n) {
    '.' << $1.unpack('H2' * $1.size).join('.')
  }.gsub(/::/n, '..')
end

.obj2element(obj) ⇒ Object


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/soap/mapping/mapping.rb', line 257

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

.obj2soap(obj, registry = nil, type = nil, opt = EMPTY_OPT) ⇒ Object


39
40
41
42
43
44
45
46
# File 'lib/soap/mapping/mapping.rb', line 39

def self.obj2soap(obj, registry = nil, type = nil, opt = EMPTY_OPT)
  registry ||= Mapping::DefaultRegistry
  soap_obj = nil
  protect_mapping(opt) do
    soap_obj = _obj2soap(obj, registry, type)
  end
  soap_obj
end

.objs2soap(objs, registry = nil, types = nil, opt = EMPTY_OPT) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/soap/mapping/mapping.rb', line 48

def self.objs2soap(objs, registry = nil, types = nil, opt = EMPTY_OPT)
  registry ||= Mapping::DefaultRegistry
  ary = []
  protect_mapping(opt) do
    0.upto(objs.length - 1) do |idx|
      type = types ? types[idx] : nil
      soap = _obj2soap(objs[idx], registry, type)
      ary << soap
    end
  end
  ary
end

.parse_schema_definition(schema_element, default_ns) ⇒ Object

for backward compatibility returns SchemaComplexTypeDefinition


439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/soap/mapping/mapping.rb', line 439

def self.parse_schema_definition(schema_element, default_ns)
  definition = nil
  if schema_element[0] == :choice
    schema_element.shift
    definition = SchemaChoiceDefinition.new
  else
    definition = SchemaSequenceDefinition.new
  end
  schema_element.each do |ele|
    element_definition = parse_schema_element_definition(ele, default_ns)
    definition << element_definition
  end
  definition
end

.parse_schema_element_definition(schema_element, default_ns) ⇒ Object

returns SchemaElementDefinition


455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/soap/mapping/mapping.rb', line 455

def self.parse_schema_element_definition(schema_element, default_ns)
  if schema_element[0] == :choice
    parse_schema_definition(schema_element, default_ns)
  elsif schema_element[0].is_a?(Array)
    parse_schema_definition(schema_element, default_ns)
  else
    varname, info, occurrence = schema_element
    mapped_class_str, elename = info
    if occurrence
      minoccurs, maxoccurs = occurrence
    else
      # for backward compatibility
      minoccurs, maxoccurs = 1, 1
    end
    as_any = as_array = false
    if /\[\]$/ =~ mapped_class_str
      mapped_class_str = mapped_class_str.sub(/\[\]$/, '')
      if mapped_class_str.empty?
        mapped_class_str = nil
      end
      as_array = true
    end
    if mapped_class_str
      mapped_class = Mapping.class_from_name(mapped_class_str)
      if mapped_class.nil?
        warn("cannot find mapped class: #{mapped_class_str}")
      end
    end
    if elename == XSD::AnyTypeName
      as_any = true
    elsif elename.nil?
      elename = XSD::QName.new(default_ns, varname)
    end
    SchemaElementDefinition.new(
      varname, mapped_class, elename, minoccurs, maxoccurs, as_any, as_array)
  end
end

.protect_threadvars(*symbols) ⇒ Object


496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/soap/mapping/mapping.rb', line 496

def protect_threadvars(*symbols)
  backup = {}
  begin
    symbols.each do |sym|
      backup[sym] = Thread.current[sym]
    end
    yield
  ensure
    symbols.each do |sym|
      Thread.current[sym] = backup[sym]
    end
  end
end

.schema_attribute_definition(klass) ⇒ Object


383
384
385
# File 'lib/soap/mapping/mapping.rb', line 383

def self.schema_attribute_definition(klass)
  class_schema_variable(:schema_attribute, klass)
end

.schema_definition_classdef(klass) ⇒ Object


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/soap/mapping/mapping.rb', line 387

def self.schema_definition_classdef(klass)
  if Thread.current[:SOAPMapping][:SchemaDefinition].key?(klass)
    return Thread.current[:SOAPMapping][:SchemaDefinition][klass]
  end
  ns = schema_ns_definition(klass)
  name = schema_name_definition(klass)
  type = schema_type_definition(klass)
  qualified = schema_qualified_definition(klass)
  elements = schema_element_definition(klass)
  attributes = schema_attribute_definition(klass)
  return nil if ns.nil? and name.nil? and type.nil? and elements.nil? and attributes.nil?
  definition = create_schema_definition(klass,
    :schema_ns => ns,
    :schema_name => name,
    :schema_type => type,
    :schema_qualified => qualified,
    :schema_element => elements,
    :schema_attribute => attributes
  )
  Thread.current[:SOAPMapping][:SchemaDefinition][klass] = definition
  definition
end

.schema_element_definition(klass) ⇒ Object


379
380
381
# File 'lib/soap/mapping/mapping.rb', line 379

def self.schema_element_definition(klass)
  class_schema_variable(:schema_element, klass)
end

.schema_name_definition(klass) ⇒ Object


367
368
369
# File 'lib/soap/mapping/mapping.rb', line 367

def self.schema_name_definition(klass)
  class_schema_variable(:schema_name, klass)
end

.schema_ns_definition(klass) ⇒ Object


363
364
365
# File 'lib/soap/mapping/mapping.rb', line 363

def self.schema_ns_definition(klass)
  class_schema_variable(:schema_ns, klass)
end

.schema_qualified_definition(klass) ⇒ Object


375
376
377
# File 'lib/soap/mapping/mapping.rb', line 375

def self.schema_qualified_definition(klass)
  class_schema_variable(:schema_qualified, klass)
end

.schema_type_definition(klass) ⇒ Object


371
372
373
# File 'lib/soap/mapping/mapping.rb', line 371

def self.schema_type_definition(klass)
  class_schema_variable(:schema_type, klass)
end

.set_attributes(obj, values) ⇒ Object


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/soap/mapping/mapping.rb', line 325

def self.set_attributes(obj, values)
  case obj
  when ::SOAP::Mapping::Object
    values.each do |attr_name, value|
      obj.__add_xmlele_value(attr_name, value)
    end
  else
    values.each do |attr_name, value|
      # untaint depends GenSupport.safevarname
      name = XSD::CodeGen::GenSupport.safevarname(attr_name).untaint
      setter = name + "="
      if obj.respond_to?(setter)
        obj.__send__(setter, value)
      else
        obj.instance_variable_set('@' + name, value)
        begin
          unless obj.respond_to?(name)
            obj.instance_eval <<-EOS
              def #{name}
                @#{name}
              end
            EOS
          end
          unless self.respond_to?(name + "=")
            obj.instance_eval <<-EOS
              def #{name}=(value)
                @#{name} = value
              end
            EOS
          end
        rescue TypeError
          # singleton class may not exist (e.g. Float)
        end
      end
    end
  end
end

.soap2obj(node, registry = nil, klass = nil, opt = EMPTY_OPT) ⇒ Object


61
62
63
64
65
66
67
68
# File 'lib/soap/mapping/mapping.rb', line 61

def self.soap2obj(node, registry = nil, klass = nil, opt = EMPTY_OPT)
  registry ||= Mapping::DefaultRegistry
  obj = nil
  protect_mapping(opt) do
    obj = _soap2obj(node, registry, klass)
  end
  obj
end