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.



1500
1501
1502
# File 'lib/moxml/adapter/libxml.rb', line 1500

def initialize(handler)
  @handler = handler
end

Instance Method Details

#on_cdata_block(content) ⇒ Object



1546
1547
1548
# File 'lib/moxml/adapter/libxml.rb', line 1546

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

#on_characters(chars) ⇒ Object



1542
1543
1544
# File 'lib/moxml/adapter/libxml.rb', line 1542

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

#on_comment(msg) ⇒ Object



1550
1551
1552
# File 'lib/moxml/adapter/libxml.rb', line 1550

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

#on_end_documentObject



1510
1511
1512
# File 'lib/moxml/adapter/libxml.rb', line 1510

def on_end_document
  @handler.on_end_document
end

#on_end_element(name) ⇒ Object



1538
1539
1540
# File 'lib/moxml/adapter/libxml.rb', line 1538

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

#on_error(msg) ⇒ Object



1558
1559
1560
# File 'lib/moxml/adapter/libxml.rb', line 1558

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

#on_processing_instruction(target, data) ⇒ Object



1554
1555
1556
# File 'lib/moxml/adapter/libxml.rb', line 1554

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

#on_start_documentObject

Map LibXML events to Moxml events



1506
1507
1508
# File 'lib/moxml/adapter/libxml.rb', line 1506

def on_start_document
  @handler.on_start_document
end

#on_start_element(name, attributes) ⇒ Object



1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
# File 'lib/moxml/adapter/libxml.rb', line 1514

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