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.



129
130
131
132
133
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 129

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

Instance Attribute Details

#elements_processedObject (readonly)

Returns the value of attribute elements_processed.



127
128
129
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 127

def elements_processed
  @elements_processed
end

Instance Method Details

#cdata_block(string) ⇒ Object



167
168
169
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 167

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

#characters(string) ⇒ Object



161
162
163
164
165
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 161

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

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

#comment(_string) ⇒ Object



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

def comment(_string); end

#end_documentObject



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

def end_document; end

#end_element(_name) ⇒ Object



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

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



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

def end_prefix_mapping(_prefix); end

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



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

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

#processing_instruction(_name, _content) ⇒ Object



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

def processing_instruction(_name, _content); end

#start_documentObject

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



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

def start_document; end

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



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

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



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

def start_prefix_mapping(_prefix, _uri); end

#warning(_string) ⇒ Object



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

def warning(_string); end

#xmldecl(_version, _encoding, _standalone) ⇒ Object



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

def xmldecl(_version, _encoding, _standalone); end