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
|
=begin
SOAP4R - ASP.NET EncodingStyle handler library
Copyright (C) 2001 NAKAMURA Hiroshi.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PRATICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.
=end
require 'soap/encodingStyleHandler'
module SOAP
class EncodingStyleHandlerASPDotNet < EncodingStyleHandler
Namespace = 'http://tempuri.org/ASP.NET'
addHandler
def initialize( charset = nil )
super( charset )
@textBuf = ''
end
###
## encode interface.
#
def encodeData( buf, ns, qualified, data, parent )
attrs = {}
name = nil
if qualified and data.elementName.namespace
if !ns.assigned?( data.elementName.namespace )
tag = ns.assign( data.elementName.namespace )
attrs[ 'xmlns:' << tag ] = data.elementName.namespace
end
name = ns.name( data.elementName )
else
name = data.elementName.name
end
case data
when SOAPRawString
SOAPGenerator.encodeTag( buf, name, attrs, false )
buf << data.to_s
when XSDString
SOAPGenerator.encodeTag( buf, name, attrs, false )
buf << SOAPGenerator.encodeStr( @charset ?
Charset.encodingToXML( data.to_s, @charset ) : data.to_s )
when XSDAnySimpleType
SOAPGenerator.encodeTag( buf, name, attrs, false )
buf << SOAPGenerator.encodeStr( data.to_s )
when SOAPStruct
SOAPGenerator.encodeTag( buf, name, attrs, true )
data.each do | key, value |
value.elementName.namespace = data.elementName.namespace if !value.elementName.namespace
yield( value, true )
end
when SOAPArray
SOAPGenerator.encodeTag( buf, name, attrs, true )
data.traverse do | child, *rank |
data.position = nil
yield( child, true )
end
else
raise EncodingStyleError.new( "Unknown object:#{ data } in this encodingSt
yle." )
end
end
def encodeDataEnd( buf, ns, qualified, data, parent )
name = nil
if qualified and data.elementName.namespace
name = ns.name( data.elementName )
else
name = data.elementName.name
end
SOAPGenerator.encodeTagEnd( buf, name, true )
end
###
## decode interface.
#
class SOAPTemporalObject
attr_accessor :parent
def initialize
@parent = nil
end
end
class SOAPUnknown < SOAPTemporalObject
def initialize( handler, elementName )
super()
@handler = handler
@elementName = elementName
end
def toStruct
o = SOAPStruct.decode( @elementName, XSD::AnyTypeName )
o.parent = @parent
o.type.name = @name
@handler.decodeParent( @parent, o )
o
end
def toString
o = SOAPString.decode( @elementName )
o.parent = @parent
@handler.decodeParent( @parent, o )
o
end
def toNil
o = SOAPNil.decode( @elementName )
o.parent = @parent
@handler.decodeParent( @parent, o )
o
end
end
def decodeTag( ns, elementName, attrs, parent )
# ToDo: check if @textBuf is empty...
@textBuf = ''
o = SOAPUnknown.new( self, elementName )
o.parent = parent
o
end
def decodeTagEnd( ns, node )
o = node.node
if o.is_a?( SOAPUnknown )
newNode = o.toString
# if /\A\s*\z/ =~ @textBuf
# o.toStruct
# else
# o.toString
# end
node.replaceNode( newNode )
o = node.node
end
decodeTextBuf( o )
@textBuf = ''
end
def decodeText( ns, text )
# @textBuf is set at decodeTagEnd.
@textBuf << text
end
def decodePrologue
end
def decodeEpilogue
end
def decodeParent( parent, node )
case parent.node
when SOAPUnknown
newParent = parent.node.toStruct
node.parent = newParent
parent.replaceNode( newParent )
decodeParent( parent, node )
when SOAPStruct
data = parent.node[ node.name ]
case data
when nil
parent.node.add( node.name, node )
when SOAPArray
name, typeNamespace = node.name, node.type.namespace
data.add( node )
node.name, node.type.namespace = name, typeNamespace
else
parent.node[ node.name ] = SOAPArray.new
name, typeNamespace = data.elementName.name, data.type.namespace
parent.node[ node.name ].add( data )
data.elementName.name, data.type.namespace = name, typeNamespace
name, typeNamespace = node.elementName.name, node.type.namespace
parent.node[ node.name ].add( node )
node.elementName.name, node.type.namespace = name, typeNamespace
end
when SOAPArray
if node.position
parent.node[ *( decodeArrayPosition( node.position )) ] = node
parent.node.sparse = true
else
parent.node.add( node )
end
when SOAPBasetype
raise EncodingStyleError.new( "SOAP base type must not have a child." )
else
# SOAPUnknown does not have parent.
# raise EncodingStyleError.new( "Illegal parent: #{ parent }." )
end
end
private
def decodeTextBuf( node )
if node.is_a?( XSDString )
if @charset
node.set( Charset.encodingFromXML( @textBuf, @charset ))
else
node.set( @textBuf )
end
else
# Nothing to do...
end
end
end
EncodingStyleHandlerASPDotNet.new
end
|