Class: SOAP::Mapping::RubytypeFactory

Inherits:
Factory show all
Defined in:
lib/soap/mapping/rubytypeFactory.rb

Constant Summary collapse

TYPE_STRING =
XSD::QName.new(RubyTypeNamespace, 'String')
TYPE_TIME =
XSD::QName.new(RubyTypeNamespace, 'Time')
TYPE_ARRAY =
XSD::QName.new(RubyTypeNamespace, 'Array')
TYPE_REGEXP =
XSD::QName.new(RubyTypeNamespace, 'Regexp')
TYPE_RANGE =
XSD::QName.new(RubyTypeNamespace, 'Range')
TYPE_CLASS =
XSD::QName.new(RubyTypeNamespace, 'Class')
TYPE_MODULE =
XSD::QName.new(RubyTypeNamespace, 'Module')
TYPE_SYMBOL =
XSD::QName.new(RubyTypeNamespace, 'Symbol')
TYPE_STRUCT =
XSD::QName.new(RubyTypeNamespace, 'Struct')
TYPE_HASH =
XSD::QName.new(RubyTypeNamespace, 'Map')

Instance Method Summary collapse

Methods inherited from Factory

#setiv2obj, #setiv2soap

Methods included from TraverseSupport

#mark_marshalled_obj, #mark_unmarshalled_obj

Constructor Details

#initialize(config = {}) ⇒ RubytypeFactory

Returns a new instance of RubytypeFactory.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/soap/mapping/rubytypeFactory.rb', line 25

def initialize(config = {})
  @config = config
  @allow_untyped_struct = @config.key?(:allow_untyped_struct) ?
    @config[:allow_untyped_struct] : true
  @allow_original_mapping = @config.key?(:allow_original_mapping) ?
    @config[:allow_original_mapping] : false
  @string_factory = StringFactory_.new(true)
  @basetype_factory = BasetypeFactory_.new(true)
  @datetime_factory = DateTimeFactory_.new(true)
  @array_factory = ArrayFactory_.new(true)
  @hash_factory = HashFactory_.new(true)
end

Instance Method Details

#obj2soap(soap_class, obj, info, map) ⇒ Object



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

def obj2soap(soap_class, obj, info, map)
  param = nil
  case obj
  when ::String
    unless @allow_original_mapping
      return nil
    end
    param = @string_factory.obj2soap(SOAPString, obj, info, map)
    if obj.class != String
      param.extraattr[RubyTypeName] = obj.class.name
    end
    addiv2soapattr(param, obj, map)
  when ::Time
    unless @allow_original_mapping
      return nil
    end
    param = @datetime_factory.obj2soap(SOAPDateTime, obj, info, map)
    if obj.class != Time
      param.extraattr[RubyTypeName] = obj.class.name
    end
    addiv2soapattr(param, obj, map)
  when ::Array
    unless @allow_original_mapping
      return nil
    end
    param = @array_factory.obj2soap(nil, obj, info, map)
    if obj.class != Array
      param.extraattr[RubyTypeName] = obj.class.name
    end
    addiv2soapattr(param, obj, map)
  when ::NilClass
    unless @allow_original_mapping
      return nil
    end
    param = @basetype_factory.obj2soap(SOAPNil, obj, info, map)
    addiv2soapattr(param, obj, map)
  when ::FalseClass, ::TrueClass
    unless @allow_original_mapping
      return nil
    end
    param = @basetype_factory.obj2soap(SOAPBoolean, obj, info, map)
    addiv2soapattr(param, obj, map)
  when ::Integer
    unless @allow_original_mapping
      return nil
    end
    param = @basetype_factory.obj2soap(SOAPInt, obj, info, map)
    param ||= @basetype_factory.obj2soap(SOAPInteger, obj, info, map)
    param ||= @basetype_factory.obj2soap(SOAPDecimal, obj, info, map)
    addiv2soapattr(param, obj, map)
  when ::Float
    unless @allow_original_mapping
      return nil
    end
    param = @basetype_factory.obj2soap(SOAPDouble, obj, info, map)
    if obj.class != Float
      param.extraattr[RubyTypeName] = obj.class.name
    end
    addiv2soapattr(param, obj, map)
  when ::Hash
    unless @allow_original_mapping
      return nil
    end
    if obj.respond_to?(:default_proc) && obj.default_proc
      raise TypeError.new("cannot dump hash with default proc")
    end
    param = SOAPStruct.new(TYPE_HASH)
    mark_marshalled_obj(obj, param)
    if obj.class != Hash
      param.extraattr[RubyTypeName] = obj.class.name
    end
    obj.each do |key, value|
      elem = SOAPStruct.new # Undefined type.
      elem.add("key", Mapping._obj2soap(key, map))
      elem.add("value", Mapping._obj2soap(value, map))
      param.add("item", elem)
    end
    param.add('default', Mapping._obj2soap(obj.default, map))
    addiv2soapattr(param, obj, map)
  when ::Regexp
    unless @allow_original_mapping
      return nil
    end
    param = SOAPStruct.new(TYPE_REGEXP)
    mark_marshalled_obj(obj, param)
    if obj.class != Regexp
      param.extraattr[RubyTypeName] = obj.class.name
    end
    param.add('source', SOAPBase64.new(obj.source))
    if obj.respond_to?('options')
      # Regexp#options is from Ruby/1.7
      options = obj.options
    else
      options = 0
      obj.inspect.sub(/^.*\//, '').each_byte do |c|
        options += case c
          when ?i
            1
          when ?x
            2
          when ?m
            4
          when ?n
            16
          when ?e
            32
          when ?s
            48
          when ?u
            64
          end
      end
    end
    param.add('options', SOAPInt.new(options))
    addiv2soapattr(param, obj, map)
  when ::Range
    unless @allow_original_mapping
      return nil
    end
    param = SOAPStruct.new(TYPE_RANGE)
    mark_marshalled_obj(obj, param)
    if obj.class != Range
      param.extraattr[RubyTypeName] = obj.class.name
    end
    param.add('begin', Mapping._obj2soap(obj.begin, map))
    param.add('end', Mapping._obj2soap(obj.end, map))
    param.add('exclude_end', SOAP::SOAPBoolean.new(obj.exclude_end?))
    addiv2soapattr(param, obj, map)
  when ::Class
    unless @allow_original_mapping
      return nil
    end
    if obj.to_s[0] == ?#
      raise TypeError.new("can't dump anonymous class #{obj}")
    end
    param = SOAPStruct.new(TYPE_CLASS)
    mark_marshalled_obj(obj, param)
    param.add('name', SOAPString.new(obj.name))
    addiv2soapattr(param, obj, map)
  when ::Module
    unless @allow_original_mapping
      return nil
    end
    if obj.to_s[0] == ?#
      raise TypeError.new("can't dump anonymous module #{obj}")
    end
    param = SOAPStruct.new(TYPE_MODULE)
    mark_marshalled_obj(obj, param)
    param.add('name', SOAPString.new(obj.name))
    addiv2soapattr(param, obj, map)
  when ::Symbol
    unless @allow_original_mapping
      return nil
    end
    param = SOAPStruct.new(TYPE_SYMBOL)
    mark_marshalled_obj(obj, param)
    param.add('id', SOAPString.new(obj.id2name))
    addiv2soapattr(param, obj, map)
  when ::Struct
    unless @allow_original_mapping
      # treat it as an user defined class. [ruby-talk:104980]
      #param = unknownobj2soap(soap_class, obj, info, map)
      param = SOAPStruct.new(XSD::AnyTypeName)
      mark_marshalled_obj(obj, param)
      obj.members.each do |member|
        param.add(Mapping.name2elename(member),
          Mapping._obj2soap(obj[member], map))
      end
    else
      param = SOAPStruct.new(TYPE_STRUCT)
      mark_marshalled_obj(obj, param)
      param.add('type', ele_type = SOAPString.new(obj.class.to_s))
      ele_member = SOAPStruct.new
      obj.members.each do |member|
        ele_member.add(Mapping.name2elename(member),
          Mapping._obj2soap(obj[member], map))
      end
      param.add('member', ele_member)
      addiv2soapattr(param, obj, map)
    end
  when ::IO, ::Binding, ::Continuation, ::Data, ::Dir, ::File::Stat,
      ::MatchData, Method, ::Proc, ::Thread, ::ThreadGroup
      # from 1.8: Process::Status, UnboundMethod
    return nil
  when ::SOAP::Mapping::Object
    param = SOAPStruct.new(XSD::AnyTypeName)
    mark_marshalled_obj(obj, param)
    obj.__xmlele.each do |key, value|
      param.add(key.name, Mapping._obj2soap(value, map))
    end
    obj.__xmlattr.each do |key, value|
      param.extraattr[key] = value
    end
  when ::Exception
    typestr = Mapping.name2elename(obj.class.to_s)
    param = SOAPStruct.new(XSD::QName.new(RubyTypeNamespace, typestr))
    mark_marshalled_obj(obj, param)
    param.add('message', Mapping._obj2soap(obj.message, map))
    param.add('backtrace', Mapping._obj2soap(obj.backtrace, map))
    addiv2soapattr(param, obj, map)
  else
    param = unknownobj2soap(soap_class, obj, info, map)
  end
  param
end

#soap2obj(obj_class, node, info, map) ⇒ Object



244
245
246
247
248
249
250
251
252
253
# File 'lib/soap/mapping/rubytypeFactory.rb', line 244

def soap2obj(obj_class, node, info, map)
  rubytype = node.extraattr[RubyTypeName]
  if rubytype or node.type.namespace == RubyTypeNamespace
    rubytype2obj(node, info, map, rubytype)
  elsif node.type == XSD::AnyTypeName or node.type == XSD::AnySimpleTypeName
    anytype2obj(node, info, map)
  else
    unknowntype2obj(node, info, map)
  end
end