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.



1315
1316
1317
# File 'lib/moxml/adapter/libxml.rb', line 1315

def initialize(handler)
  @handler = handler
end

Instance Method Details

#on_cdata_block(content) ⇒ Object



1353
1354
1355
# File 'lib/moxml/adapter/libxml.rb', line 1353

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

#on_characters(chars) ⇒ Object



1349
1350
1351
# File 'lib/moxml/adapter/libxml.rb', line 1349

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

#on_comment(msg) ⇒ Object



1357
1358
1359
# File 'lib/moxml/adapter/libxml.rb', line 1357

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

#on_end_documentObject



1325
1326
1327
# File 'lib/moxml/adapter/libxml.rb', line 1325

def on_end_document
  @handler.on_end_document
end

#on_end_element_ns(name, prefix, _uri) ⇒ Object



1344
1345
1346
1347
# File 'lib/moxml/adapter/libxml.rb', line 1344

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



1365
1366
1367
# File 'lib/moxml/adapter/libxml.rb', line 1365

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

#on_processing_instruction(target, data) ⇒ Object



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

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

#on_start_documentObject

Map LibXML events to Moxml events



1321
1322
1323
# File 'lib/moxml/adapter/libxml.rb', line 1321

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



1335
1336
1337
1338
1339
1340
1341
1342
# File 'lib/moxml/adapter/libxml.rb', line 1335

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