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

Constant Summary

Constants included from SAX::NamespaceSplitter

SAX::NamespaceSplitter::EMPTY_NAMESPACES

Instance Method Summary collapse

Methods included from SAX::NamespaceSplitter

#split_attributes_and_namespaces

Constructor Details

#initialize(handler) ⇒ LibXMLSAXBridge

Returns a new instance of LibXMLSAXBridge.



1361
1362
1363
# File 'lib/moxml/adapter/libxml.rb', line 1361

def initialize(handler)
  @handler = handler
end

Instance Method Details

#on_cdata_block(content) ⇒ Object



1399
1400
1401
# File 'lib/moxml/adapter/libxml.rb', line 1399

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

#on_characters(chars) ⇒ Object



1395
1396
1397
# File 'lib/moxml/adapter/libxml.rb', line 1395

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

#on_comment(msg) ⇒ Object



1403
1404
1405
# File 'lib/moxml/adapter/libxml.rb', line 1403

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

#on_end_document ⇒ Object



1371
1372
1373
# File 'lib/moxml/adapter/libxml.rb', line 1371

def on_end_document
  @handler.on_end_document
end

#on_end_element_ns(name, prefix, _uri) ⇒ Object



1390
1391
1392
1393
# File 'lib/moxml/adapter/libxml.rb', line 1390

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



1411
1412
1413
# File 'lib/moxml/adapter/libxml.rb', line 1411

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

#on_processing_instruction(target, data) ⇒ Object



1407
1408
1409
# File 'lib/moxml/adapter/libxml.rb', line 1407

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

#on_start_document ⇒ Object

Map LibXML events to Moxml events



1367
1368
1369
# File 'lib/moxml/adapter/libxml.rb', line 1367

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).



1381
1382
1383
1384
1385
1386
1387
1388
# File 'lib/moxml/adapter/libxml.rb', line 1381

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