Class: MultiXML::Parsers::Ox::Handler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_xml/parsers/ox.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

SAX event handler that builds a hash tree while parsing.

Ox's SAX callbacks expose element and attribute names in prefixed form (e.g. "atom:feed"). Under :preserve we keep the source form verbatim; under :strip we drop the prefix and filter xmlns declarations out of the attribute stream.

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ Handler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new SAX handler

Parameters:

  • mode (Symbol)

    Namespace handling mode



43
44
45
46
# File 'lib/multi_xml/parsers/ox.rb', line 43

def initialize(mode)
  @mode = mode
  @stack = [{}]
end

Instance Method Details

#attr(name, value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Handle an attribute

Ignored outside an element (e.g. attributes on the XML declaration such as <?xml version="1.0"?>, which fire before any start_element).

Parameters:

  • name (Symbol, String)

    Attribute name

  • value (String)

    Attribute value



74
75
76
77
78
79
80
81
# File 'lib/multi_xml/parsers/ox.rb', line 74

def attr(name, value)
  return if @stack.size < 2

  name = name.to_s
  return if xmlns_decl?(name) && @mode != :preserve

  add_attribute_value(current, format_name(name), value)
end

#end_element(_name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Handle end of an element

Parameters:

  • _name (Symbol, String)

    Element name (unused)



96
97
98
99
# File 'lib/multi_xml/parsers/ox.rb', line 96

def end_element(_name)
  strip_whitespace_content if current.key?(TEXT_CONTENT_KEY)
  @stack.pop
end

#error(message, line, column) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Handle parse errors

Parameters:

  • message (String)

    Error message

  • line (Integer)

    Line number

  • column (Integer)

    Column number

Raises:



109
110
111
# File 'lib/multi_xml/parsers/ox.rb', line 109

def error(message, line, column)
  raise ::Ox::ParseError, "#{message} at #{line}:#{column}"
end

#resultHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the parsed result

Returns:

  • (Hash)

    the parsed hash



52
# File 'lib/multi_xml/parsers/ox.rb', line 52

def result = @stack.first

#start_element(name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Handle start of an element

Parameters:

  • name (Symbol, String)

    Element name



59
60
61
62
63
# File 'lib/multi_xml/parsers/ox.rb', line 59

def start_element(name)
  child = {}
  add_value(current, format_name(name.to_s), child)
  @stack << child
end

#text(value) ⇒ void Also known as: cdata

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Handle text content (also aliased as cdata)

Parameters:

  • value (String)

    Text content



88
# File 'lib/multi_xml/parsers/ox.rb', line 88

def text(value) = append_text(current, value)