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.



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

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

Instance Method Details

#cdata_block(string) ⇒ Object



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

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

#characters(string) ⇒ Object



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

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

#comment(string) ⇒ Object



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

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

#end_documentObject



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

def end_document
  @handler.on_end_document
end

#end_element(name) ⇒ Object



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

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

#error(string) ⇒ Object



482
483
484
# File 'lib/moxml/adapter/nokogiri.rb', line 482

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

#processing_instruction(target, data) ⇒ Object



478
479
480
# File 'lib/moxml/adapter/nokogiri.rb', line 478

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

#start_documentObject

Map Nokogiri events to Moxml events



433
434
435
# File 'lib/moxml/adapter/nokogiri.rb', line 433

def start_document
  @handler.on_start_document
end

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



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/moxml/adapter/nokogiri.rb', line 441

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



486
487
488
# File 'lib/moxml/adapter/nokogiri.rb', line 486

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