Class: XSD::XMLParser::OxParser

Inherits:
Parser show all
Defined in:
lib/xsd/xmlparser/oxparser.rb

Instance Attribute Summary

Attributes inherited from Parser

#charset

Instance Method Summary collapse

Methods inherited from Parser

add_factory, create_parser, factory, #initialize, #parse

Constructor Details

This class inherits a constructor from XSD::XMLParser::Parser

Instance Method Details

#attr(key, val) ⇒ Object



83
84
85
86
87
88
# File 'lib/xsd/xmlparser/oxparser.rb', line 83

def attr(key,val)
  return if @element_stack[-1].nil?

  attr_hash = @element_stack[-1]
  attr_hash[key.to_s] = val
end

#attrs_doneObject



90
91
92
93
94
95
# File 'lib/xsd/xmlparser/oxparser.rb', line 90

def attrs_done
  attr_hash = @element_stack[-1]
  name = @element_stack[-2]

  base_start_element(name, attr_hash) unless attr_hash.nil?
end

#base_end_elementObject



43
# File 'lib/xsd/xmlparser/oxparser.rb', line 43

alias_method :base_end_element, :end_element

#base_start_elementObject



36
# File 'lib/xsd/xmlparser/oxparser.rb', line 36

alias_method :base_start_element, :start_element

#cdata(t) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/xsd/xmlparser/oxparser.rb', line 60

def cdata(t)
  ## CAUTION:  Ox Parser removes leading/trailing whitespace or blank lines. ##
  ## This behavior is evident if you run the regression test:
  ##
  ##    SOAP4R_PARSERS=oxparser bundle exec rake test:single test/soap/test_response_as_xml.rb
  ##
  @decoder.nil? ? characters(t) : characters(@decoder.decode(t))
end

#comment(t) ⇒ Object



69
70
71
# File 'lib/xsd/xmlparser/oxparser.rb', line 69

def comment(t)
  @decoder.nil? ? characters(t) : characters(@decoder.decode(t))
end

#do_parse(string_or_readable) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xsd/xmlparser/oxparser.rb', line 16

def do_parse(string_or_readable)
  @element_stack = []
  begin
    require 'htmlentities' # Used to unescape html-escaped chars, if available
    @decoder = ::HTMLEntities.new(:expanded)
  rescue LoadError
    @decoder = nil
  end

  string = string_or_readable.respond_to?(:read) ? string_or_readable.read : StringIO.new(string_or_readable)
  if @decoder.nil?
    # Use the built-in conversion with Ox.
    ::Ox.sax_parse(self, string, {:symbolize=> false, :convert_special=> true, :skip=> :skip_return} )
  else
    # Use HTMLEntities Decoder.  Leave the special-character conversion alone and let HTMLEntities decode it for us.
    ::Ox.sax_parse(self, string, {})
  end
end

#end_element(n) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/xsd/xmlparser/oxparser.rb', line 44

def end_element(n)
  # $stderr.puts "OxParser.end_element INVOKED [#{n}]"
  if @element_stack[-2].to_s == n.to_s
    attr_hash = @element_stack.pop
    attr_key  = @element_stack.pop
  else
    $stderr.puts "!!!! OxParser.end_element FAILED TO FIND STACK PAIR [#{n}] IN STACK [#{PP.pp(@element_stack,'')}]"
  end
  base_end_element(n.to_s)
end

#end_instruct(n) ⇒ Object



78
79
80
# File 'lib/xsd/xmlparser/oxparser.rb', line 78

def end_instruct(n)
  # Do nothing.
end

#instruct(n) ⇒ Object



74
75
76
# File 'lib/xsd/xmlparser/oxparser.rb', line 74

def instruct(n)
  # Do nothing. This is the outer "XML" tag.
end

#start_element(n) ⇒ Object



37
38
39
40
41
# File 'lib/xsd/xmlparser/oxparser.rb', line 37

def start_element(n)
  # $stderr.puts "OxParser.start_element INVOKED [#{n}]"
  @element_stack.push n.to_s # Push the Element Name
  @element_stack.push Hash.new
end

#text(t) ⇒ Object



56
57
58
# File 'lib/xsd/xmlparser/oxparser.rb', line 56

def text(t)
  @decoder.nil? ? characters(t) : characters(@decoder.decode(t))
end