Class: Moxml::Adapter::Libxml::LibXMLSAXBridge

Inherits:
Object
  • Object
show all
Includes:
LibXML::XML::SaxParser::Callbacks
Defined in:
lib/moxml/adapter/libxml.rb

Overview

Bridge between LibXML SAX and Moxml SAX

Translates LibXML::XML::SaxParser events to Moxml::SAX::Handler events

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ LibXMLSAXBridge

Returns a new instance of LibXMLSAXBridge.



1639
1640
1641
# File 'lib/moxml/adapter/libxml.rb', line 1639

def initialize(handler)
  @handler = handler
end

Instance Method Details

#on_cdata_block(content) ⇒ Object



1685
1686
1687
# File 'lib/moxml/adapter/libxml.rb', line 1685

def on_cdata_block(content)
  @handler.on_cdata(content)
end

#on_characters(chars) ⇒ Object



1681
1682
1683
# File 'lib/moxml/adapter/libxml.rb', line 1681

def on_characters(chars)
  @handler.on_characters(chars)
end

#on_comment(msg) ⇒ Object



1689
1690
1691
# File 'lib/moxml/adapter/libxml.rb', line 1689

def on_comment(msg)
  @handler.on_comment(msg)
end

#on_end_documentObject



1649
1650
1651
# File 'lib/moxml/adapter/libxml.rb', line 1649

def on_end_document
  @handler.on_end_document
end

#on_end_element(name) ⇒ Object



1677
1678
1679
# File 'lib/moxml/adapter/libxml.rb', line 1677

def on_end_element(name)
  @handler.on_end_element(name.to_s)
end

#on_error(msg) ⇒ Object



1697
1698
1699
# File 'lib/moxml/adapter/libxml.rb', line 1697

def on_error(msg)
  @handler.on_error(Moxml::ParseError.new(msg))
end

#on_processing_instruction(target, data) ⇒ Object



1693
1694
1695
# File 'lib/moxml/adapter/libxml.rb', line 1693

def on_processing_instruction(target, data)
  @handler.on_processing_instruction(target, data || "")
end

#on_start_documentObject

Map LibXML events to Moxml events



1645
1646
1647
# File 'lib/moxml/adapter/libxml.rb', line 1645

def on_start_document
  @handler.on_start_document
end

#on_start_element(name, attributes) ⇒ Object



1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
# File 'lib/moxml/adapter/libxml.rb', line 1653

def on_start_element(name, attributes)
  # Convert LibXML attributes hash to separate attrs and namespaces
  attr_hash = {}
  ns_hash = {}

  attributes&.each do |attr_name, attr_value|
    if attr_name.to_s.start_with?("xmlns")
      # Namespace declaration
      prefix = if attr_name.to_s == "xmlns"
                 nil
               else
                 attr_name.to_s.sub(
                   "xmlns:", ""
                 )
               end
      ns_hash[prefix] = attr_value
    else
      attr_hash[attr_name.to_s] = attr_value
    end
  end

  @handler.on_start_element(name.to_s, attr_hash, ns_hash)
end