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



402
403
404
405
# File 'lib/moxml/adapter/nokogiri.rb', line 402

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

Instance Method Details

#cdata_block(string) ⇒ Object



446
447
448
# File 'lib/moxml/adapter/nokogiri.rb', line 446

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

#characters(string) ⇒ Object



442
443
444
# File 'lib/moxml/adapter/nokogiri.rb', line 442

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

#comment(string) ⇒ Object



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

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

#end_documentObject



413
414
415
# File 'lib/moxml/adapter/nokogiri.rb', line 413

def end_document
  @handler.on_end_document
end

#end_element(name) ⇒ Object



438
439
440
# File 'lib/moxml/adapter/nokogiri.rb', line 438

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

#error(string) ⇒ Object



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

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

#processing_instruction(target, data) ⇒ Object



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

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

#start_documentObject

Map Nokogiri events to Moxml events



409
410
411
# File 'lib/moxml/adapter/nokogiri.rb', line 409

def start_document
  @handler.on_start_document
end

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



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/moxml/adapter/nokogiri.rb', line 417

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



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

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