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.



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

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

Instance Attribute Details

#elements_processedObject (readonly)

Returns the value of attribute elements_processed.



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

def elements_processed
  @elements_processed
end

Instance Method Details

#cdata_block(string) ⇒ Object



183
184
185
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 183

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

#characters(string) ⇒ Object



177
178
179
180
181
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 177

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

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

#comment(_string) ⇒ Object



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

def comment(_string); end

#end_documentObject



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

def end_document; end

#end_element(_name) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 168

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



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

def end_prefix_mapping(_prefix); end

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



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

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

#processing_instruction(_name, _content) ⇒ Object



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

def processing_instruction(_name, _content); end

#start_documentObject

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



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

def start_document; end

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



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

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



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

def start_prefix_mapping(_prefix, _uri); end

#warning(_string) ⇒ Object



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

def warning(_string); end

#xmldecl(_version, _encoding, _standalone) ⇒ Object



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

def xmldecl(_version, _encoding, _standalone); end