Class: Serialbench::Serializers::Xml::LeptrisSerializer::StreamingHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/serialbench/serializers/xml/leptris_serializer.rb

Overview

SAX handler counting elements, mirroring the Nokogiri adapter's StreamingHandler shape (Leptris SAX delivers attrs as [name, value] pairs, same as Nokogiri::XML::SAX). Plain duck-typed class -- Leptris::XML::SAX::Parser needs no base class, and subclassing it would trigger the FFI library load at file-load time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StreamingHandler

Returns a new instance of StreamingHandler.



122
123
124
125
126
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 122

def initialize(&block)
  @block = block
  @elements_processed = 0
  @element_stack = []
end

Instance Attribute Details

#elements_processedObject (readonly)

Returns the value of attribute elements_processed.



120
121
122
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 120

def elements_processed
  @elements_processed
end

Instance Method Details

#cdata_block(string) ⇒ Object



160
161
162
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 160

def cdata_block(string)
  @element_stack.last[:text] += string if @element_stack.any?
end

#characters(string) ⇒ Object



154
155
156
157
158
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 154

def characters(string)
  return if string.strip.empty?

  @element_stack.last[:text] += string if @element_stack.any?
end

#comment(_string) ⇒ Object



133
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 133

def comment(_string); end

#end_documentObject



131
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 131

def end_document; end

#end_element(_name) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 145

def end_element(_name)
  element = @element_stack.pop
  if @element_stack.empty?
    @block&.call(element)
  else
    @element_stack.last[:children] << element
  end
end

#end_prefix_mapping(_prefix) ⇒ Object



136
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 136

def end_prefix_mapping(_prefix); end

#error(_message, _line = 0, _column = 0) ⇒ Object



138
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 138

def error(_message, _line = 0, _column = 0); end

#processing_instruction(_name, _content) ⇒ Object



134
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 134

def processing_instruction(_name, _content); end

#start_documentObject

No-op callbacks the parser invokes unconditionally (the base Leptris::XML::SAX::Document normally provides these).



130
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 130

def start_document; end

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



140
141
142
143
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 140

def start_element(name, attrs = [])
  @elements_processed += 1
  @element_stack.push({ name: name, attributes: Hash[attrs], children: [], text: '' })
end

#start_prefix_mapping(_prefix, _uri) ⇒ Object



135
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 135

def start_prefix_mapping(_prefix, _uri); end

#warning(_string) ⇒ Object



137
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 137

def warning(_string); end

#xmldecl(_version, _encoding, _standalone) ⇒ Object



132
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 132

def xmldecl(_version, _encoding, _standalone); end