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.

Instance Method Summary collapse

Methods included from SAX::NamespaceSplitter

#split_attributes_and_namespaces

Constructor Details

#initialize(handler) ⇒ OxSAXBridge

Returns a new instance of OxSAXBridge.



928
929
930
931
932
933
934
# File 'lib/moxml/adapter/ox.rb', line 928

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



937
938
939
# File 'lib/moxml/adapter/ox.rb', line 937

def attr(name, value)
  @pending_attrs[name] = value
end

#end_documentObject

Called at end of parsing (not automatically by Ox)



985
986
987
988
989
990
991
992
# File 'lib/moxml/adapter/ox.rb', line 985

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



959
960
961
962
963
964
965
966
967
# File 'lib/moxml/adapter/ox.rb', line 959

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



979
980
981
982
# File 'lib/moxml/adapter/ox.rb', line 979

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)



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/moxml/adapter/ox.rb', line 942

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



970
971
972
973
974
975
976
977
# File 'lib/moxml/adapter/ox.rb', line 970

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

  @handler.on_characters(string)
end