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.



367
368
369
370
# File 'lib/moxml/adapter/nokogiri.rb', line 367

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

Instance Method Details

#cdata_block(string) ⇒ Object



411
412
413
# File 'lib/moxml/adapter/nokogiri.rb', line 411

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

#characters(string) ⇒ Object



407
408
409
# File 'lib/moxml/adapter/nokogiri.rb', line 407

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

#comment(string) ⇒ Object



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

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

#end_documentObject



378
379
380
# File 'lib/moxml/adapter/nokogiri.rb', line 378

def end_document
  @handler.on_end_document
end

#end_element(name) ⇒ Object



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

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

#error(string) ⇒ Object



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

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

#processing_instruction(target, data) ⇒ Object



419
420
421
# File 'lib/moxml/adapter/nokogiri.rb', line 419

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

#start_documentObject

Map Nokogiri events to Moxml events



374
375
376
# File 'lib/moxml/adapter/nokogiri.rb', line 374

def start_document
  @handler.on_start_document
end

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/moxml/adapter/nokogiri.rb', line 382

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



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

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