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

Inherits:
Object
  • Object
show all
Includes:
LibXML::XML::SaxParser::Callbacks, SAX::NamespaceSplitter
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

Methods included from SAX::NamespaceSplitter

#split_attributes_and_namespaces

Constructor Details

#initialize(handler) ⇒ LibXMLSAXBridge

Returns a new instance of LibXMLSAXBridge.



1757
1758
1759
# File 'lib/moxml/adapter/libxml.rb', line 1757

def initialize(handler)
  @handler = handler
end

Instance Method Details

#on_cdata_block(content) ⇒ Object



1795
1796
1797
# File 'lib/moxml/adapter/libxml.rb', line 1795

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

#on_characters(chars) ⇒ Object



1791
1792
1793
# File 'lib/moxml/adapter/libxml.rb', line 1791

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

#on_comment(msg) ⇒ Object



1799
1800
1801
# File 'lib/moxml/adapter/libxml.rb', line 1799

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

#on_end_documentObject



1767
1768
1769
# File 'lib/moxml/adapter/libxml.rb', line 1767

def on_end_document
  @handler.on_end_document
end

#on_end_element_ns(name, prefix, _uri) ⇒ Object



1786
1787
1788
1789
# File 'lib/moxml/adapter/libxml.rb', line 1786

def on_end_element_ns(name, prefix, _uri)
  qualified = prefix ? "#{prefix}:#{name}" : name.to_s
  @handler.on_end_element(qualified)
end

#on_error(msg) ⇒ Object



1807
1808
1809
# File 'lib/moxml/adapter/libxml.rb', line 1807

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

#on_processing_instruction(target, data) ⇒ Object



1803
1804
1805
# File 'lib/moxml/adapter/libxml.rb', line 1803

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

#on_start_documentObject

Map LibXML events to Moxml events



1763
1764
1765
# File 'lib/moxml/adapter/libxml.rb', line 1763

def on_start_document
  @handler.on_start_document
end

#on_start_element_ns(name, attributes, prefix, _uri, namespaces) ⇒ Object

libxml strips prefixes and namespace declarations from the plain on_start_element callback; the _ns variants carry them. The namespaces hash uses nil for the default declaration — the same key convention as every other bridge's split. Attribute name prefixes are lost by the binding in both callbacks (documented limitation).



1777
1778
1779
1780
1781
1782
1783
1784
# File 'lib/moxml/adapter/libxml.rb', line 1777

def on_start_element_ns(name, attributes, prefix, _uri, namespaces)
  normalized = (attributes || {}).map { |k, v| [k.to_s, v] }
  attr_hash, = split_attributes_and_namespaces(normalized) do |v|
    Moxml::Adapter::Base.decode_entities(v)
  end
  qualified = prefix ? "#{prefix}:#{name}" : name.to_s
  @handler.on_start_element(qualified, attr_hash, namespaces || {})
end