PageRenderTime 92ms CodeModel.GetById 49ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/soap/element.rb

http://github.com/agross/netopenspace
Ruby | 258 lines | 199 code | 50 blank | 9 comment | 24 complexity | 83b790c47eca4c9ed1202a7cf3c66907 MD5 | raw file
  1# SOAP4R - SOAP elements library
  2# Copyright (C) 2000, 2001, 2003-2005  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
  9require 'xsd/qname'
 10require 'soap/baseData'
 11
 12
 13module SOAP
 14
 15
 16###
 17## SOAP elements
 18#
 19module SOAPEnvelopeElement; end
 20
 21class SOAPFault < SOAPStruct
 22  include SOAPEnvelopeElement
 23  include SOAPCompoundtype
 24
 25public
 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
 64    if faultcode
 65      self.faultcode = faultcode
 66      self.faultstring = faultstring
 67      self.faultactor = faultactor
 68      self.detail = detail
 69      self.faultcode.elename = EleFaultCodeName if self.faultcode
 70      self.faultstring.elename = EleFaultStringName if self.faultstring
 71      self.faultactor.elename = EleFaultActorName if self.faultactor
 72      self.detail.elename = EleFaultDetailName if self.detail
 73    end
 74    faultcode.parent = self if faultcode
 75    faultstring.parent = self if faultstring
 76    faultactor.parent = self if faultactor
 77    detail.parent = self if detail
 78  end
 79
 80  def encode(generator, ns, attrs = {})
 81    SOAPGenerator.assign_ns(attrs, ns, EnvelopeNamespace)
 82    SOAPGenerator.assign_ns(attrs, ns, EncodingNamespace)
 83    attrs[ns.name(AttrEncodingStyleName)] = EncodingNamespace
 84    name = ns.name(@elename)
 85    generator.encode_tag(name, attrs)
 86    yield(self.faultcode)
 87    yield(self.faultstring)
 88    yield(self.faultactor)
 89    yield(self.detail) if self.detail
 90    generator.encode_tag_end(name, true)
 91  end
 92end
 93
 94
 95class SOAPBody < SOAPStruct
 96  include SOAPEnvelopeElement
 97
 98  def initialize(data = nil, is_fault = false)
 99    super(nil)
100    @elename = EleBodyName
101    @encodingstyle = nil
102    if data
103      if data.respond_to?(:elename)
104        add(data.elename.name, data)
105      else
106        data.to_a.each do |datum|
107          add(datum.elename.name, datum)
108        end
109      end
110    end
111    @is_fault = is_fault
112  end
113
114  def encode(generator, ns, attrs = {})
115    name = ns.name(@elename)
116    generator.encode_tag(name, attrs)
117    if @is_fault
118      yield(@data)
119    else
120      @data.each do |data|
121	yield(data)
122      end
123    end
124    generator.encode_tag_end(name, true)
125  end
126
127  def root_node
128    @data.each do |node|
129      if node.root == 1
130	return node
131      end
132    end
133    # No specified root...
134    @data.each do |node|
135      if node.root != 0
136	return node
137      end
138    end
139
140    raise Parser::FormatDecodeError.new('no root element')
141  end
142end
143
144
145class SOAPHeaderItem < XSD::NSDBase
146  include SOAPEnvelopeElement
147  include SOAPCompoundtype
148
149public
150
151  attr_accessor :element
152  attr_accessor :mustunderstand
153  attr_accessor :encodingstyle
154
155  def initialize(element, mustunderstand = true, encodingstyle = nil)
156    super()
157    @type = nil
158    @element = element
159    @mustunderstand = mustunderstand
160    @encodingstyle = encodingstyle
161    element.parent = self if element
162  end
163
164  def encode(generator, ns, attrs = {})
165    attrs.each do |key, value|
166      @element.extraattr[key] = value
167    end
168    @element.extraattr[ns.name(AttrMustUnderstandName)] =
169      (@mustunderstand ? '1' : '0')
170    if @encodingstyle
171      @element.extraattr[ns.name(AttrEncodingStyleName)] = @encodingstyle
172    end
173    @element.encodingstyle = @encodingstyle if !@element.encodingstyle
174    yield(@element)
175  end
176end
177
178
179class SOAPHeader < SOAPStruct
180  include SOAPEnvelopeElement
181
182  def initialize
183    super(nil)
184    @elename = EleHeaderName
185    @encodingstyle = nil
186  end
187
188  def encode(generator, ns, attrs = {})
189    name = ns.name(@elename)
190    generator.encode_tag(name, attrs)
191    @data.each do |data|
192      yield(data)
193    end
194    generator.encode_tag_end(name, true)
195  end
196
197  def add(name, value)
198    mu = (value.extraattr[AttrMustUnderstandName] == '1')
199    encstyle = value.extraattr[AttrEncodingStyleName]
200    item = SOAPHeaderItem.new(value, mu, encstyle)
201    super(name, item)
202  end
203
204  def length
205    @data.length
206  end
207  alias size length
208end
209
210
211class SOAPEnvelope < XSD::NSDBase
212  include SOAPEnvelopeElement
213  include SOAPCompoundtype
214
215  attr_reader :header
216  attr_reader :body
217  attr_reader :external_content
218
219  def initialize(header = nil, body = nil)
220    super()
221    @type = nil
222    @elename = EleEnvelopeName
223    @encodingstyle = nil
224    @header = header
225    @body = body
226    @external_content = {}
227    header.parent = self if header
228    body.parent = self if body
229  end
230
231  def header=(header)
232    header.parent = self
233    @header = header
234  end
235
236  def body=(body)
237    body.parent = self
238    @body = body
239  end
240
241  def encode(generator, ns, attrs = {})
242    SOAPGenerator.assign_ns(attrs, ns, elename.namespace, SOAPNamespaceTag)
243    name = ns.name(@elename)
244    generator.encode_tag(name, attrs)
245
246    yield(@header) if @header and @header.length > 0
247    yield(@body)
248
249    generator.encode_tag_end(name, true)
250  end
251
252  def to_ary
253    [header, body]
254  end
255end
256
257
258end