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.



465
466
467
# File 'lib/moxml/adapter/oga.rb', line 465

def initialize(handler)
  @handler = handler
end

Instance Method Details

#after_element(namespace, name) ⇒ Object

Oga: after_element(namespace, name)



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

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

#on_cdata(text) ⇒ Object



509
510
511
# File 'lib/moxml/adapter/oga.rb', line 509

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

#on_comment(text) ⇒ Object



513
514
515
# File 'lib/moxml/adapter/oga.rb', line 513

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



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/moxml/adapter/oga.rb', line 472

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



517
518
519
# File 'lib/moxml/adapter/oga.rb', line 517

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

#on_text(text) ⇒ Object



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

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