Class: Moxml::Adapter::OxSAXBridge

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(handler) ⇒ OxSAXBridge

Returns a new instance of OxSAXBridge.



692
693
694
695
696
697
698
# File 'lib/moxml/adapter/ox.rb', line 692

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



701
702
703
# File 'lib/moxml/adapter/ox.rb', line 701

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

#end_documentObject

Called at end of parsing (not automatically by Ox)



749
750
751
752
753
754
755
756
# File 'lib/moxml/adapter/ox.rb', line 749

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



723
724
725
726
727
728
729
730
731
# File 'lib/moxml/adapter/ox.rb', line 723

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



743
744
745
746
# File 'lib/moxml/adapter/ox.rb', line 743

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)



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/moxml/adapter/ox.rb', line 706

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



734
735
736
737
738
739
740
741
# File 'lib/moxml/adapter/ox.rb', line 734

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

  @handler.on_characters(string)
end