| 1 |
# SOAP4R - SOAP elements library |
|---|
| 2 |
# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. |
|---|
| 3 |
|
|---|
| 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. |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
require 'xsd/qname' |
|---|
| 10 |
require 'soap/baseData' |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
module SOAP |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
### |
|---|
| 17 |
## SOAP elements |
|---|
| 18 |
# |
|---|
| 19 |
module SOAPEnvelopeElement; end |
|---|
| 20 |
|
|---|
| 21 |
class SOAPFault < SOAPStruct |
|---|
| 22 |
include SOAPEnvelopeElement |
|---|
| 23 |
include SOAPCompoundtype |
|---|
| 24 |
|
|---|
| 25 |
public |
|---|
| 26 |
|
|---|
| 27 |
def faultcode |
|---|
| 28 |
self['faultcode'] |
|---|
| 29 |
end |
|---|
| 30 |
|
|---|
| 31 |
def faultstring |
|---|
| 32 |
self['faultstring'] |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
def faultactor |
|---|
| 36 |
self['faultactor'] |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
def detail |
|---|
| 40 |
self['detail'] |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
def faultcode=(rhs) |
|---|
| 44 |
self['faultcode'] = rhs |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def faultstring=(rhs) |
|---|
| 48 |
self['faultstring'] = rhs |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def faultactor=(rhs) |
|---|
| 52 |
self['faultactor'] = rhs |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def detail=(rhs) |
|---|
| 56 |
self['detail'] = rhs |
|---|
| 57 |
end |
|---|
| 58 |
|
|---|
| 59 |
def initialize(faultcode = nil, faultstring = nil, faultactor = nil, detail = nil) |
|---|
| 60 |
super(EleFaultName) |
|---|
| 61 |
@elename = EleFaultName |
|---|
| 62 |
@encodingstyle = EncodingNamespace |
|---|
| 63 |
if faultcode |
|---|
| 64 |
self.faultcode = faultcode |
|---|
| 65 |
self.faultstring = faultstring |
|---|
| 66 |
self.faultactor = faultactor |
|---|
| 67 |
self.detail = detail |
|---|
| 68 |
self.faultcode.elename = EleFaultCodeName if self.faultcode |
|---|
| 69 |
self.faultstring.elename = EleFaultStringName if self.faultstring |
|---|
| 70 |
self.faultactor.elename = EleFaultActorName if self.faultactor |
|---|
| 71 |
self.detail.elename = EleFaultDetailName if self.detail |
|---|
| 72 |
end |
|---|
| 73 |
faultcode.parent = self if faultcode |
|---|
| 74 |
faultstring.parent = self if faultstring |
|---|
| 75 |
faultactor.parent = self if faultactor |
|---|
| 76 |
detail.parent = self if detail |
|---|
| 77 |
end |
|---|
| 78 |
|
|---|
| 79 |
def encode(generator, ns, attrs = {}) |
|---|
| 80 |
Generator.assign_ns(attrs, ns, EnvelopeNamespace) |
|---|
| 81 |
Generator.assign_ns(attrs, ns, EncodingNamespace) |
|---|
| 82 |
attrs[ns.name(AttrEncodingStyleName)] = EncodingNamespace |
|---|
| 83 |
name = ns.name(@elename) |
|---|
| 84 |
generator.encode_tag(name, attrs) |
|---|
| 85 |
yield(self.faultcode) |
|---|
| 86 |
yield(self.faultstring) |
|---|
| 87 |
yield(self.faultactor) |
|---|
| 88 |
yield(self.detail) if self.detail |
|---|
| 89 |
generator.encode_tag_end(name, true) |
|---|
| 90 |
end |
|---|
| 91 |
end |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
class SOAPBody < SOAPStruct |
|---|
| 95 |
include SOAPEnvelopeElement |
|---|
| 96 |
|
|---|
| 97 |
attr_reader :is_fault |
|---|
| 98 |
|
|---|
| 99 |
def initialize(data = nil, is_fault = false) |
|---|
| 100 |
super(nil) |
|---|
| 101 |
@elename = EleBodyName |
|---|
| 102 |
@encodingstyle = nil |
|---|
| 103 |
if data |
|---|
| 104 |
if data.respond_to?(:to_xmlpart) |
|---|
| 105 |
data = SOAP::SOAPRawData.new(data) |
|---|
| 106 |
elsif defined?(::REXML) and data.is_a?(::REXML::Element) |
|---|
| 107 |
data = SOAP::SOAPRawData.new(SOAP::SOAPREXMLElementWrap.new(data)) |
|---|
| 108 |
end |
|---|
| 109 |
if data.respond_to?(:elename) |
|---|
| 110 |
add(data.elename.name, data) |
|---|
| 111 |
else |
|---|
| 112 |
data.to_a.each do |datum| |
|---|
| 113 |
add(datum.elename.name, datum) |
|---|
| 114 |
end |
|---|
| 115 |
end |
|---|
| 116 |
end |
|---|
| 117 |
@is_fault = is_fault |
|---|
| 118 |
end |
|---|
| 119 |
|
|---|
| 120 |
def encode(generator, ns, attrs = {}) |
|---|
| 121 |
name = ns.name(@elename) |
|---|
| 122 |
generator.encode_tag(name, attrs) |
|---|
| 123 |
@data.each do |data| |
|---|
| 124 |
yield(data) |
|---|
| 125 |
end |
|---|
| 126 |
generator.encode_tag_end(name, @data.size > 0) |
|---|
| 127 |
end |
|---|
| 128 |
|
|---|
| 129 |
def root_node |
|---|
| 130 |
@data.each do |node| |
|---|
| 131 |
if node.root == 1 |
|---|
| 132 |
return node |
|---|
| 133 |
end |
|---|
| 134 |
end |
|---|
| 135 |
# No specified root... |
|---|
| 136 |
@data.each do |node| |
|---|
| 137 |
if node.root != 0 |
|---|
| 138 |
return node |
|---|
| 139 |
end |
|---|
| 140 |
end |
|---|
| 141 |
raise Parser::FormatDecodeError.new('no root element') |
|---|
| 142 |
end |
|---|
| 143 |
end |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
class SOAPHeaderItem < XSD::NSDBase |
|---|
| 147 |
include SOAPEnvelopeElement |
|---|
| 148 |
include SOAPCompoundtype |
|---|
| 149 |
|
|---|
| 150 |
public |
|---|
| 151 |
|
|---|
| 152 |
attr_accessor :element |
|---|
| 153 |
attr_accessor :mustunderstand |
|---|
| 154 |
attr_accessor :encodingstyle |
|---|
| 155 |
attr_accessor :actor |
|---|
| 156 |
|
|---|
| 157 |
def initialize(element, mustunderstand = true, encodingstyle = nil, actor = nil) |
|---|
| 158 |
super() |
|---|
| 159 |
@type = nil |
|---|
| 160 |
@element = element |
|---|
| 161 |
@mustunderstand = mustunderstand |
|---|
| 162 |
@encodingstyle = encodingstyle |
|---|
| 163 |
@actor = actor |
|---|
| 164 |
element.parent = self if element |
|---|
| 165 |
element.qualified = true |
|---|
| 166 |
end |
|---|
| 167 |
|
|---|
| 168 |
def encode(generator, ns, attrs = {}) |
|---|
| 169 |
attrs.each do |key, value| |
|---|
| 170 |
@element.extraattr[key] = value |
|---|
| 171 |
end |
|---|
| 172 |
# to remove mustUnderstand attribute, set it to nil |
|---|
| 173 |
unless @mustunderstand.nil? |
|---|
| 174 |
@element.extraattr[AttrMustUnderstandName] = (@mustunderstand ? '1' : '0') |
|---|
| 175 |
end |
|---|
| 176 |
if @encodingstyle |
|---|
| 177 |
@element.extraattr[AttrEncodingStyleName] = @encodingstyle |
|---|
| 178 |
end |
|---|
| 179 |
unless @element.encodingstyle |
|---|
| 180 |
@element.encodingstyle = @encodingstyle |
|---|
| 181 |
end |
|---|
| 182 |
if @actor |
|---|
| 183 |
@element.extraattr[AttrActorName] = @actor |
|---|
| 184 |
end |
|---|
| 185 |
yield(@element) |
|---|
| 186 |
end |
|---|
| 187 |
end |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
class SOAPHeader < SOAPStruct |
|---|
| 191 |
include SOAPEnvelopeElement |
|---|
| 192 |
|
|---|
| 193 |
attr_writer :force_encode |
|---|
| 194 |
|
|---|
| 195 |
def initialize |
|---|
| 196 |
super(nil) |
|---|
| 197 |
@elename = EleHeaderName |
|---|
| 198 |
@encodingstyle = nil |
|---|
| 199 |
@force_encode = false |
|---|
| 200 |
end |
|---|
| 201 |
|
|---|
| 202 |
def encode(generator, ns, attrs = {}) |
|---|
| 203 |
name = ns.name(@elename) |
|---|
| 204 |
generator.encode_tag(name, attrs) |
|---|
| 205 |
@data.each do |data| |
|---|
| 206 |
yield(data) |
|---|
| 207 |
end |
|---|
| 208 |
generator.encode_tag_end(name, @data.size > 0) |
|---|
| 209 |
end |
|---|
| 210 |
|
|---|
| 211 |
def add(name, value) |
|---|
| 212 |
actor = value.extraattr[AttrActorName] |
|---|
| 213 |
mu = value.extraattr[AttrMustUnderstandName] |
|---|
| 214 |
encstyle = value.extraattr[AttrEncodingStyleName] |
|---|
| 215 |
mu_value = mu.nil? ? nil : (mu == '1') |
|---|
| 216 |
# to remove mustUnderstand attribute, set it to nil |
|---|
| 217 |
item = SOAPHeaderItem.new(value, mu_value, encstyle, actor) |
|---|
| 218 |
super(name, item) |
|---|
| 219 |
end |
|---|
| 220 |
|
|---|
| 221 |
def length |
|---|
| 222 |
@data.length |
|---|
| 223 |
end |
|---|
| 224 |
alias size length |
|---|
| 225 |
|
|---|
| 226 |
def encode? |
|---|
| 227 |
@force_encode or length > 0 |
|---|
| 228 |
end |
|---|
| 229 |
end |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
class SOAPEnvelope < XSD::NSDBase |
|---|
| 233 |
include SOAPEnvelopeElement |
|---|
| 234 |
include SOAPCompoundtype |
|---|
| 235 |
|
|---|
| 236 |
attr_reader :header |
|---|
| 237 |
attr_reader :body |
|---|
| 238 |
attr_reader :external_content |
|---|
| 239 |
|
|---|
| 240 |
def initialize(header = nil, body = nil) |
|---|
| 241 |
super() |
|---|
| 242 |
@type = nil |
|---|
| 243 |
@elename = EleEnvelopeName |
|---|
| 244 |
@encodingstyle = nil |
|---|
| 245 |
@header = header |
|---|
| 246 |
@body = body |
|---|
| 247 |
@external_content = {} |
|---|
| 248 |
header.parent = self if header |
|---|
| 249 |
body.parent = self if body |
|---|
| 250 |
end |
|---|
| 251 |
|
|---|
| 252 |
def header=(header) |
|---|
| 253 |
header.parent = self |
|---|
| 254 |
@header = header |
|---|
| 255 |
end |
|---|
| 256 |
|
|---|
| 257 |
def body=(body) |
|---|
| 258 |
body.parent = self |
|---|
| 259 |
@body = body |
|---|
| 260 |
end |
|---|
| 261 |
|
|---|
| 262 |
def encode(generator, ns, attrs = {}) |
|---|
| 263 |
Generator.assign_ns(attrs, ns, elename.namespace) |
|---|
| 264 |
name = ns.name(@elename) |
|---|
| 265 |
generator.encode_tag(name, attrs) |
|---|
| 266 |
yield(@header) if @header and @header.encode? |
|---|
| 267 |
yield(@body) |
|---|
| 268 |
generator.encode_tag_end(name, true) |
|---|
| 269 |
end |
|---|
| 270 |
|
|---|
| 271 |
def to_ary |
|---|
| 272 |
[header, body] |
|---|
| 273 |
end |
|---|
| 274 |
end |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
end |
|---|