Class: Moxml::Adapter::OxSAXBridge

Inherits:
Object
  • Object
show all
Includes:
SAX::NamespaceSplitter
Defined in:
lib/moxml/adapter/ox.rb

Overview

Bridge between Ox SAX and Moxml SAX

Translates Ox::Sax events to Moxml::SAX::Handler events. Ox has a unique SAX pattern where attributes are delivered AFTER start_element.

Constant Summary

Constants included from SAX::NamespaceSplitter

SAX::NamespaceSplitter::EMPTY_NAMESPACES

Instance Method Summary collapse

Methods included from SAX::NamespaceSplitter

#split_attributes_and_namespaces

Constructor Details

#initialize(handler) ⇒ OxSAXBridge

Returns a new instance of OxSAXBridge.



999
1000
1001
1002
1003
1004
1005
# File 'lib/moxml/adapter/ox.rb', line 999

def initialize(handler)
  @handler = handler
  @pending_attrs = {}
  @pending_element_name = nil
  @element_started = false
  @document_started = false
end

Instance Method Details

#attr(name, value) ⇒ Object

Ox delivers attributes AFTER start_element



1008
1009
1010
1011
1012
# File 'lib/moxml/adapter/ox.rb', line 1008

def attr(name, value)
  # Attributes arriving before the first start_element belong
  # to the XML declaration, not to any element
  @pending_attrs[name.to_s] = value if @pending_element_name
end

#end_documentObject

Called at end of parsing (not automatically by Ox)



1058
1059
1060
1061
1062
1063
1064
1065
# File 'lib/moxml/adapter/ox.rb', line 1058

def end_document
  # Finalize any pending element
  if @pending_element_name
    finalize_pending_element
  end

  @handler.on_end_document if @document_started
end

#end_element(name) ⇒ Object



1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/moxml/adapter/ox.rb', line 1032

def end_element(name)
  # Finalize any pending element before ending
  if @pending_element_name
    finalize_pending_element
  end

  # Convert symbol to string
  @handler.on_end_element(name.to_s)
end

#error(message, line, column) ⇒ Object



1052
1053
1054
1055
# File 'lib/moxml/adapter/ox.rb', line 1052

def error(message, line, column)
  error = Moxml::ParseError.new(message, line: line, column: column)
  @handler.on_error(error)
end

#start_element(name) ⇒ Object

Called when element starts (but attributes come AFTER this)



1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/moxml/adapter/ox.rb', line 1015

def start_element(name)
  # If we had a previous element waiting, we need to finalize it first
  if @pending_element_name
    finalize_pending_element
  end

  # Store this element name (convert symbol to string)
  @pending_element_name = name.to_s
  @element_started = true

  # Call on_start_document if this is the first element
  unless @document_started
    @handler.on_start_document
    @document_started = true
  end
end

#text(string) ⇒ Object

Ox only has text() - no separate CDATA, comment, or PI events



1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/moxml/adapter/ox.rb', line 1043

def text(string)
  # Finalize any pending element before text
  if @pending_element_name
    finalize_pending_element
  end

  @handler.on_characters(string)
end