1
# SOAP4R - SOAP EncodingStyle handler library
2
# Copyright (C) 2001, 2003, 2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
4
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
5
# redistribute it and/or modify it under the same terms of Ruby's license;
6
# either the dual license version in 2003, or any later version.
9
require 'soap/encodingstyle/handler'
16
class SOAPHandler < Handler
17
Namespace = SOAP::EncodingNamespace
20
def initialize(charset = nil)
25
@is_first_top_ele = true
32
def encode_data(generator, ns, data, parent)
33
attrs = encode_attrs(generator, ns, data, parent)
34
if parent && parent.is_a?(SOAPArray) && parent.position
35
attrs[ns.name(AttrPositionName)] = "[#{parent.position.join(',')}]"
37
name = generator.encode_name(ns, data, attrs)
40
attrs['href'] = data.refidstr
41
generator.encode_tag(name, attrs)
42
when SOAPExternalReference
44
attrs['href'] = data.refidstr
45
generator.encode_tag(name, attrs)
47
generator.encode_tag(name, attrs)
48
generator.encode_rawstring(data.to_s)
50
generator.encode_tag(name, attrs)
51
generator.encode_string(@charset ?
52
XSD::Charset.encoding_to_xml(data.to_s, @charset) : data.to_s)
53
when XSD::XSDAnySimpleType
54
generator.encode_tag(name, attrs)
55
generator.encode_string(data.to_s)
57
generator.encode_tag(name, attrs)
58
data.each do |key, value|
59
generator.encode_child(ns, value, data)
62
generator.encode_tag(name, attrs)
63
data.traverse do |child, *rank|
64
data.position = data.sparse ? rank : nil
65
generator.encode_child(ns, child, data)
68
raise EncodingStyleError.new(
69
"unknown object:#{data} in this encodingStyle")
73
def encode_data_end(generator, ns, data, parent)
74
name = generator.encode_name_end(ns, data)
75
cr = data.is_a?(SOAPCompoundtype)
76
generator.encode_tag_end(name, cr)
83
class SOAPTemporalObject
85
attr_accessor :position
97
class SOAPUnknown < SOAPTemporalObject
99
attr_accessor :definedtype
100
attr_reader :extraattr
102
def initialize(handler, elename, type, extraattr)
107
@extraattr = extraattr
112
o = SOAPStruct.decode(@elename, @type)
116
o.position = @position
117
o.extraattr.update(@extraattr)
118
@handler.decode_parent(@parent, o)
123
o = SOAPString.decode(@elename)
127
o.position = @position
128
o.extraattr.update(@extraattr)
129
@handler.decode_parent(@parent, o)
134
o = SOAPNil.decode(@elename)
138
o.position = @position
139
o.extraattr.update(@extraattr)
140
@handler.decode_parent(@parent, o)
145
def decode_tag(ns, elename, attrs, parent)
147
is_nil, type, arytype, root, offset, position, href, id, extraattr =
148
decode_attrs(ns, attrs)
151
o = SOAPNil.decode(elename)
153
o = SOAPReference.decode(elename, href)
155
elsif @decode_typemap
156
o = decode_tag_by_wsdl(ns, elename, type, parent.node, arytype, extraattr)
158
o = decode_tag_by_type(ns, elename, type, parent.node, arytype, extraattr)
161
if o.is_a?(SOAPArray)
163
o.offset = decode_arypos(offset)
173
o.position = position
175
unless o.is_a?(SOAPTemporalObject)
177
decode_parent(parent, o)
182
def decode_tag_end(ns, node)
184
if o.is_a?(SOAPUnknown)
185
newnode = if /\A\s*\z/ =~ @textbuf
193
node.replace_node(newnode)
201
def decode_text(ns, text)
208
@is_first_top_ele = true
215
def decode_parent(parent, node)
216
return unless parent.node
219
newparent = parent.node.as_struct
220
node.parent = newparent
224
parent.replace_node(newparent)
225
decode_parent(parent, node)
227
parent.node.add(node.elename.name, node)
228
node.parent = parent.node
231
parent.node[*(decode_arypos(node.position))] = node
232
parent.node.sparse = true
234
parent.node.add(node)
236
node.parent = parent.node
238
raise EncodingStyleError.new("illegal parent: #{parent.node}")
244
def content_ranksize(typename)
245
typename.scan(/\[[\d,]*\]$/)[0]
248
def content_typename(typename)
249
typename.sub(/\[,*\]$/, '')
252
def create_arytype(ns, data)
253
XSD::QName.new(data.arytype.namespace,
254
content_typename(data.arytype.name) + "[#{data.size.join(',')}]")
257
def encode_attrs(generator, ns, data, parent)
259
return attrs if data.is_a?(SOAPReference)
261
if !parent || parent.encodingstyle != EncodingNamespace
262
if @generate_explicit_type
263
SOAPGenerator.assign_ns(attrs, ns, EnvelopeNamespace)
264
attrs[ns.name(AttrEncodingStyleName)] = EncodingNamespace
266
data.encodingstyle = EncodingNamespace
269
if data.is_a?(SOAPNil)
270
attrs[ns.name(XSD::AttrNilName)] = XSD::NilValue
271
elsif @generate_explicit_type
272
if data.type.namespace
273
SOAPGenerator.assign_ns(attrs, ns, data.type.namespace)
275
if data.is_a?(SOAPArray)
276
if data.arytype.namespace
277
SOAPGenerator.assign_ns(attrs, ns, data.arytype.namespace)
279
SOAPGenerator.assign_ns(attrs, ns, EncodingNamespace)
280
attrs[ns.name(AttrArrayTypeName)] = ns.name(create_arytype(ns, data))
282
attrs[ns.name(XSD::AttrTypeName)] = ns.name(data.type)
284
elsif parent && parent.is_a?(SOAPArray) && (parent.arytype == data.type)
286
elsif !data.type.namespace
289
attrs[ns.name(XSD::AttrTypeName)] = ns.name(data.type)
293
data.extraattr.each do |key, value|
294
SOAPGenerator.assign_ns(attrs, ns, key.namespace)
295
attrs[ns.name(key)] = encode_attr_value(generator, ns, key, value)
298
attrs['id'] = data.id
303
def encode_attr_value(generator, ns, qname, value)
304
if value.is_a?(SOAPType)
305
ref = SOAPReference.new(value)
306
generator.add_reftarget(qname.name, value)
313
def decode_tag_by_wsdl(ns, elename, typestr, parent, arytypestr, extraattr)
315
if parent.class == SOAPBody
316
# root element: should branch by root attribute?
318
# Unqualified name is allowed here.
319
@is_first_top_ele = false
320
type = @decode_typemap[elename] ||
321
@decode_typemap.find_name(elename.name)
323
o = SOAPStruct.new(elename)
330
typename = ns.parse(typestr)
331
typedef = @decode_typemap[typename]
333
return decode_definedtype(elename, typename, typedef, arytypestr)
336
return decode_tag_by_type(ns, elename, typestr, parent, arytypestr,
340
if parent.type == XSD::AnyTypeName
341
return decode_tag_by_type(ns, elename, typestr, parent, arytypestr,
345
# parent.definedtype == nil means the parent is SOAPUnknown. SOAPUnknown
346
# is generated by decode_tag_by_type when its type is anyType.
347
parenttype = parent.definedtype || @decode_typemap[parent.type]
349
return decode_tag_by_type(ns, elename, typestr, parent, arytypestr,
353
definedtype_name = parenttype.child_type(elename)
354
if definedtype_name and (klass = TypeMap[definedtype_name])
355
return decode_basetype(klass, elename)
356
elsif definedtype_name == XSD::AnyTypeName
357
return decode_tag_by_type(ns, elename, typestr, parent, arytypestr,
362
typedef = @decode_typemap[definedtype_name]
364
typedef = parenttype.child_defined_complextype(elename)
366
decode_definedtype(elename, definedtype_name, typedef, arytypestr)
369
def decode_definedtype(elename, typename, typedef, arytypestr)
371
raise EncodingStyleError.new("unknown type '#{typename}'")
373
if typedef.is_a?(::WSDL::XMLSchema::SimpleType)
374
decode_defined_simpletype(elename, typename, typedef, arytypestr)
376
decode_defined_complextype(elename, typename, typedef, arytypestr)
380
def decode_basetype(klass, elename)
381
klass.decode(elename)
384
def decode_defined_simpletype(elename, typename, typedef, arytypestr)
385
o = decode_basetype(TypeMap[typedef.base], elename)
386
o.definedtype = typedef
390
def decode_defined_complextype(elename, typename, typedef, arytypestr)
391
case typedef.compoundtype
392
when :TYPE_STRUCT, :TYPE_MAP
393
o = SOAPStruct.decode(elename, typename)
394
o.definedtype = typedef
397
expected_arytype = typedef.find_arytype
399
actual_arytype = XSD::QName.new(expected_arytype.namespace,
400
content_typename(expected_arytype.name) <<
401
content_ranksize(arytypestr))
402
o = SOAPArray.decode(elename, typename, actual_arytype)
404
o = SOAPArray.new(typename, 1, expected_arytype)
407
o.definedtype = typedef
410
o = SOAPNil.decode(elename)
411
o.definedtype = typedef
414
raise RuntimeError.new(
415
"Unknown kind of complexType: #{typedef.compoundtype}")
420
def decode_tag_by_type(ns, elename, typestr, parent, arytypestr, extraattr)
422
type = typestr ? ns.parse(typestr) : ValueArrayName
423
node = SOAPArray.decode(elename, type, ns.parse(arytypestr))
424
node.extraattr.update(extraattr)
430
type = ns.parse(typestr)
431
elsif parent.is_a?(SOAPArray)
432
type = parent.arytype
434
# Since it's in dynamic(without any type) encoding process,
435
# assumes entity as its type itself.
436
# <SOAP-ENC:Array ...> => type Array in SOAP-ENC.
437
# <Country xmlns="foo"> => type Country in foo.
441
if (klass = TypeMap[type])
442
node = decode_basetype(klass, elename)
443
node.extraattr.update(extraattr)
447
# Unknown type... Struct or String
448
SOAPUnknown.new(self, elename, type, extraattr)
451
def decode_textbuf(node)
453
when XSD::XSDHexBinary, XSD::XSDBase64Binary
454
node.set_encoded(@textbuf)
457
@textbuf = XSD::Charset.encoding_from_xml(@textbuf, @charset)
460
node.definedtype.check_lexical_format(@textbuf)
483
def decode_attrs(ns, attrs)
494
attrs.each do |key, value|
495
qname = ns.parse(key)
497
when XSD::InstanceNamespace
500
is_nil = NilLiteralMap[value] or
501
raise EncodingStyleError.new("cannot accept attribute value: #{value} as the value of xsi:#{XSD::NilLiteral} (expected 'true', 'false', '1', or '0')")
507
when EncodingNamespace
513
root = RootLiteralMap[value] or
514
raise EncodingStyleError.new(
515
"illegal root attribute value: #{value}")
532
qname = ns.parse_local(key)
533
extraattr[qname] = decode_attr_value(ns, qname, value)
536
return is_nil, type, arytype, root, offset, position, href, id, extraattr
539
def decode_attr_value(ns, qname, value)
541
o = SOAPReference.decode(nil, value)
549
def decode_arypos(position)
550
/^\[(.+)\]$/ =~ position
551
$1.split(',').collect { |s| s.to_i }
554
def decode_resolve_id
555
count = @refpool.length # To avoid infinite loop
556
while !@refpool.empty? && count > 0
557
@refpool = @refpool.find_all { |ref|
558
o = @idpool.find { |item|
561
if o.is_a?(SOAPReference)
566
elsif o = ref.rootnode.external_content[ref.refid]
570
raise EncodingStyleError.new("unresolved reference: #{ref.refid}")