Class: SOAP::Parser

Inherits:
Object show all
Includes:
SOAP
Defined in:
lib/soap/parser.rb

Defined Under Namespace

Classes: FormatDecodeError, ParseError, ParseFrame, UnexpectedElementError

Constant Summary

Constants included from SOAP

AttrActor, AttrActorName, AttrArrayType, AttrArrayTypeName, AttrEncodingStyle, AttrEncodingStyleName, AttrHref, AttrHrefName, AttrId, AttrIdName, AttrMustUnderstand, AttrMustUnderstandName, AttrOffset, AttrOffsetName, AttrPosition, AttrPositionName, AttrRoot, AttrRootName, Base64Literal, EleBody, EleBodyName, EleEnvelope, EleEnvelopeName, EleFault, EleFaultActor, EleFaultActorName, EleFaultCode, EleFaultCodeName, EleFaultDetail, EleFaultDetailName, EleFaultName, EleFaultString, EleFaultStringName, EleHeader, EleHeaderName, EncodingNamespace, EnvelopeNamespace, LiteralNamespace, MediaType, NextActor, PropertyName, SOAPNamespaceTag, SOAPProxy, TypeMap, VERSION, ValueArray, ValueArrayName, XSDNamespaceTag, XSINamespaceTag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Parser

Returns a new instance of Parser.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/soap/parser.rb', line 78

def initialize(opt = {})
  @opt = opt
  @parser = XSD::XMLParser.create_parser(self, opt)
  @parsestack = nil
  @recycleframe = nil
  @lastnode = nil
  @handlers = {}
  @envelopenamespace = opt[:envelopenamespace] || EnvelopeNamespace
  @default_encodingstyle = opt[:default_encodingstyle] || EncodingNamespace
  @decode_typemap = opt[:decode_typemap] || nil
  @allow_unqualified_element = opt[:allow_unqualified_element] || false
end

Instance Attribute Details

#allow_unqualified_elementObject

Returns the value of attribute allow_unqualified_element.



76
77
78
# File 'lib/soap/parser.rb', line 76

def allow_unqualified_element
  @allow_unqualified_element
end

#decode_typemapObject

Returns the value of attribute decode_typemap.



75
76
77
# File 'lib/soap/parser.rb', line 75

def decode_typemap
  @decode_typemap
end

#default_encodingstyleObject

Returns the value of attribute default_encodingstyle.



74
75
76
# File 'lib/soap/parser.rb', line 74

def default_encodingstyle
  @default_encodingstyle
end

#envelopenamespaceObject

Returns the value of attribute envelopenamespace.



73
74
75
# File 'lib/soap/parser.rb', line 73

def envelopenamespace
  @envelopenamespace
end

Instance Method Details

#characters(text) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/soap/parser.rb', line 153

def characters(text)
  # Ignore Text outside of SOAP Envelope.
  if lastframe = @parsestack.last
    # Need not to be cloned because character does not have attr.
    decode_text(lastframe.ns, text, lastframe.handler)
  end
end

#charsetObject



91
92
93
# File 'lib/soap/parser.rb', line 91

def charset
  @parser.charset
end

#end_element(name) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/soap/parser.rb', line 161

def end_element(name)
  lastframe = @parsestack.pop
  unless name == lastframe.name
    raise UnexpectedElementError.new("Closing element name '#{ name }' does not match with opening element '#{ lastframe.name }'.")
  end
  decode_tag_end(lastframe.ns, lastframe.node, lastframe.handler)
  @lastnode = lastframe.node.node
  @recycleframe = lastframe
end

#parse(string_or_readable) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/soap/parser.rb', line 95

def parse(string_or_readable)
  @parsestack = []
  @lastnode = nil

  @handlers.each do |uri, handler|
    handler.decode_prologue
  end

  @parser.do_parse(string_or_readable)

  unless @parsestack.empty?
    raise FormatDecodeError.new("Unbalanced tag in XML.")
  end

  @handlers.each do |uri, handler|
    handler.decode_epilogue
  end

  @lastnode
end

#start_element(name, raw_attrs) ⇒ Object



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
# File 'lib/soap/parser.rb', line 116

def start_element(name, raw_attrs)
  lastframe = @parsestack.last
  ns = parent = parent_encodingstyle = nil
  if lastframe
    ns = lastframe.ns
    parent = lastframe.node
    parent_encodingstyle = lastframe.encodingstyle
  else
    ns = SOAP::NS.new
    parent = ParseFrame::NodeContainer.new(nil)
    parent_encodingstyle = nil
  end
  # ns might be the same
  ns, raw_attrs = XSD::XMLParser.filter_ns(ns, raw_attrs)
  attrs = decode_attrs(ns, raw_attrs)
  encodingstyle = attrs[AttrEncodingStyleName]
  # Children's encodingstyle is derived from its parent.
  if encodingstyle.nil?
    if parent.node.is_a?(SOAPHeader)
      encodingstyle = LiteralNamespace
    else
      encodingstyle = parent_encodingstyle || @default_encodingstyle
    end
  end
  handler = find_handler(encodingstyle)
  unless handler
    raise FormatDecodeError.new("Unknown encodingStyle: #{ encodingstyle }.")
  end
  node = decode_tag(ns, name, attrs, parent, handler)
  if @recycleframe
    @parsestack << @recycleframe.update(ns, name, node, encodingstyle, handler)
    @recycleframe = nil
  else
    @parsestack << ParseFrame.new(ns, name, node, encodingstyle, handler)
  end
end