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, FixnumFactory_, 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
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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/soap/mapping/mapping.rb', line 127

def self._obj2soap(obj, registry, type = nil)
  if obj.respond_to?(:to_xmlpart)
    SOAPRawData.new(obj)
  elsif defined?(::REXML) && defined?(::REXML::Element) && 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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/soap/mapping/mapping.rb', line 142

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



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

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



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

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



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

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



239
240
241
242
243
# File 'lib/soap/mapping/mapping.rb', line 239

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



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/soap/mapping/mapping.rb', line 204

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

.const_from_name(name, lenient = false) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/soap/mapping/mapping.rb', line 180

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

.const_from_name_nonlenient(name) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/soap/mapping/mapping.rb', line 230

def self.const_from_name_nonlenient(name)
  if Thread.current[:SOAPMapping]
    Thread.current[:SOAPMapping][:ConstFromName][name] ||=
      const_from_name(name)
  else
    const_from_name(name)
  end
end

.create_empty_object(klass) ⇒ Object



158
159
160
# File 'lib/soap/mapping/mapping.rb', line 158

def self.create_empty_object(klass)
  klass.allocate
end

.create_schema_definition(klass, definition) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/soap/mapping/mapping.rb', line 441

def self.create_schema_definition(klass, definition)
  schema_ns = definition[:schema_ns]
  schema_name = definition[:schema_name]
  schema_type = definition[:schema_type]
  is_anonymous = definition[:is_anonymous]
  schema_basetype = definition[:schema_basetype]
  schema_qualified = definition[:schema_qualified]
  schema_element = definition[:schema_element]
  schema_attributes = definition[:schema_attribute]
  definition = SchemaDefinition.new(klass, schema_name, schema_type, is_anonymous, schema_qualified)
  definition.basetype = schema_basetype
  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 = schema_ns
      default_ns ||= schema_name.namespace if schema_name
      default_ns ||= schema_type.namespace if schema_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



275
276
277
278
279
280
# File 'lib/soap/mapping/mapping.rb', line 275

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

.elename2name(name) ⇒ Object



174
175
176
177
178
# File 'lib/soap/mapping/mapping.rb', line 174

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

.external_cesObject



388
389
390
# File 'lib/soap/mapping/mapping.rb', line 388

def self.external_ces
  Thread.current[:SOAPMapping][:ExternalCES]
end

.fault2exception(fault, registry = nil) ⇒ Object



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

def self.fault2exception(fault, registry = nil)
  registry ||= Mapping::DefaultRegistry
  detail = ""
  if fault.detail
    begin
      fault.detail.type ||= XSD::QName::EMPTY
      detail = soap2obj(fault.detail, registry) || ""
    rescue MappingError
      detail = fault.detail
    end
  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



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

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 = Mapping.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



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

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



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

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



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/soap/mapping/mapping.rb', line 217

def self.module_from_name(name, lenient = false)
  unless lenient
    const = const_from_name_nonlenient(name)
  else
    const = const_from_name(name, true)
  end
  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


168
169
170
171
172
# File 'lib/soap/mapping/mapping.rb', line 168

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

.obj2element(obj) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/soap/mapping/mapping.rb', line 252

def self.obj2element(obj)
  name = namespace = nil
  if obj.instance_variable_defined?('@schema_type')
    name = obj.instance_variable_get('@schema_type')
  end
  if obj.instance_variable_defined?('@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



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

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



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

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

returns SchemaComplexTypeDefinition



471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/soap/mapping/mapping.rb', line 471

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



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/soap/mapping/mapping.rb', line 487

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
      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



527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/soap/mapping/mapping.rb', line 527

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

.reset_root_type_hintObject



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

def self.reset_root_type_hint
  Thread.current[:SOAPMapping][:RootTypeHint] = false
end

.root_type_hintObject



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

def self.root_type_hint
  Thread.current[:SOAPMapping][:RootTypeHint]
end

.safeconstname(name) ⇒ Object



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

def self.safeconstname(name)
  Thread.current[:SOAPMapping][:SafeConstName][name] ||=
    XSD::CodeGen::GenSupport.safeconstname(name)
end

.safemethodname(name) ⇒ Object



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

def self.safemethodname(name)
  Thread.current[:SOAPMapping][:SafeMethodName][name] ||=
    XSD::CodeGen::GenSupport.safemethodname(name)
end

.safevarname(name) ⇒ Object



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

def self.safevarname(name)
  Thread.current[:SOAPMapping][:SafeVarName][name] ||=
    XSD::CodeGen::GenSupport.safevarname(name)
end

.schema_attribute_definition(klass) ⇒ Object



412
413
414
# File 'lib/soap/mapping/mapping.rb', line 412

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

.schema_definition_classdef(klass) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/soap/mapping/mapping.rb', line 416

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

.schema_element_definition(klass) ⇒ Object



408
409
410
# File 'lib/soap/mapping/mapping.rb', line 408

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

.schema_name_definition(klass) ⇒ Object



396
397
398
# File 'lib/soap/mapping/mapping.rb', line 396

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

.schema_ns_definition(klass) ⇒ Object



392
393
394
# File 'lib/soap/mapping/mapping.rb', line 392

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

.schema_qualified_definition(klass) ⇒ Object



404
405
406
# File 'lib/soap/mapping/mapping.rb', line 404

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

.schema_type_definition(klass) ⇒ Object



400
401
402
# File 'lib/soap/mapping/mapping.rb', line 400

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

.set_attributes(obj, values) ⇒ Object



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

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 = Mapping.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



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

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

.to_qname(obj, ns = nil) ⇒ Object



267
268
269
270
271
272
273
# File 'lib/soap/mapping/mapping.rb', line 267

def self.to_qname(obj, ns = nil)
  if obj.is_a?(XSD::QName)
    obj
  else
    XSD::QName.new(ns, obj)
  end
end