Class: Moxml::Adapter::OgaSAXBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/adapter/oga.rb

Overview

Bridge between Oga SAX and Moxml SAX

Translates Oga SAX events to Moxml::SAX::Handler events. Oga has different event naming and namespace as first param.

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ OgaSAXBridge

Returns a new instance of OgaSAXBridge.



452
453
454
# File 'lib/moxml/adapter/oga.rb', line 452

def initialize(handler)
  @handler = handler
end

Instance Method Details

#after_element(namespace, name) ⇒ Object

Oga: after_element(namespace, name)



487
488
489
490
# File 'lib/moxml/adapter/oga.rb', line 487

def after_element(namespace, name)
  element_name = namespace ? "#{namespace}:#{name}" : name
  @handler.on_end_element(element_name)
end

#on_cdata(text) ⇒ Object



496
497
498
# File 'lib/moxml/adapter/oga.rb', line 496

def on_cdata(text)
  @handler.on_cdata(text)
end

#on_comment(text) ⇒ Object



500
501
502
# File 'lib/moxml/adapter/oga.rb', line 500

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

#on_element(namespace, name, attributes) ⇒ Object

Oga: on_element(namespace, name, attributes) namespace may be nil attributes is an array of [name, value] pairs



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/moxml/adapter/oga.rb', line 459

def on_element(namespace, name, attributes)
  # Build full qualified name if namespace present
  element_name = namespace ? "#{namespace}:#{name}" : name

  # Convert Oga attributes to hash
  attr_hash = {}
  ns_hash = {}

  # Oga delivers attributes as array of [name, value] pairs
  attributes.each do |attr_name, attr_value|
    if attr_name.to_s.start_with?("xmlns")
      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(element_name, attr_hash, ns_hash)
end

#on_processing_instruction(name, text) ⇒ Object



504
505
506
# File 'lib/moxml/adapter/oga.rb', line 504

def on_processing_instruction(name, text)
  @handler.on_processing_instruction(name, text || "")
end

#on_text(text) ⇒ Object



492
493
494
# File 'lib/moxml/adapter/oga.rb', line 492

def on_text(text)
  @handler.on_characters(text)
end