Class: Moxml::Adapter::Nokogiri::NokogiriSAXBridge

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/moxml/adapter/nokogiri.rb

Overview

Bridge between Nokogiri SAX and Moxml SAX

Translates Nokogiri::XML::SAX::Document events to Moxml::SAX::Handler events

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ NokogiriSAXBridge

Returns a new instance of NokogiriSAXBridge.



415
416
417
418
# File 'lib/moxml/adapter/nokogiri.rb', line 415

def initialize(handler)
  super()
  @handler = handler
end

Instance Method Details

#cdata_block(string) ⇒ Object



459
460
461
# File 'lib/moxml/adapter/nokogiri.rb', line 459

def cdata_block(string)
  @handler.on_cdata(string)
end

#characters(string) ⇒ Object



455
456
457
# File 'lib/moxml/adapter/nokogiri.rb', line 455

def characters(string)
  @handler.on_characters(string)
end

#comment(string) ⇒ Object



463
464
465
# File 'lib/moxml/adapter/nokogiri.rb', line 463

def comment(string)
  @handler.on_comment(string)
end

#end_documentObject



426
427
428
# File 'lib/moxml/adapter/nokogiri.rb', line 426

def end_document
  @handler.on_end_document
end

#end_element(name) ⇒ Object



451
452
453
# File 'lib/moxml/adapter/nokogiri.rb', line 451

def end_element(name)
  @handler.on_end_element(name)
end

#error(string) ⇒ Object



471
472
473
# File 'lib/moxml/adapter/nokogiri.rb', line 471

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

#processing_instruction(target, data) ⇒ Object



467
468
469
# File 'lib/moxml/adapter/nokogiri.rb', line 467

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

#start_documentObject

Map Nokogiri events to Moxml events



422
423
424
# File 'lib/moxml/adapter/nokogiri.rb', line 422

def start_document
  @handler.on_start_document
end

#start_element(name, attributes = []) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/moxml/adapter/nokogiri.rb', line 430

def start_element(name, attributes = [])
  # Convert Nokogiri attributes array to hash
  attr_hash = {}
  namespaces_hash = {}

  attributes.each do |attr|
    attr_name = attr[0]
    attr_value = attr[1]

    if attr_name.start_with?("xmlns")
      # Namespace declaration
      prefix = attr_name == "xmlns" ? nil : attr_name.sub("xmlns:", "")
      namespaces_hash[prefix] = attr_value
    else
      attr_hash[attr_name] = attr_value
    end
  end

  @handler.on_start_element(name, attr_hash, namespaces_hash)
end

#warning(string) ⇒ Object



475
476
477
# File 'lib/moxml/adapter/nokogiri.rb', line 475

def warning(string)
  @handler.on_warning(string)
end