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.



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

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

Instance Method Details

#cdata_block(string) ⇒ Object



497
498
499
# File 'lib/moxml/adapter/nokogiri.rb', line 497

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

#characters(string) ⇒ Object



493
494
495
# File 'lib/moxml/adapter/nokogiri.rb', line 493

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

#comment(string) ⇒ Object



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

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

#end_documentObject



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

def end_document
  @handler.on_end_document
end

#end_element(name) ⇒ Object



489
490
491
# File 'lib/moxml/adapter/nokogiri.rb', line 489

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

#error(string) ⇒ Object



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

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

#processing_instruction(target, data) ⇒ Object



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

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

#start_documentObject

Map Nokogiri events to Moxml events



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

def start_document
  @handler.on_start_document
end

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



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/moxml/adapter/nokogiri.rb', line 468

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



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

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