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.



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

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

Instance Attribute Details

#elements_processedObject (readonly)

Returns the value of attribute elements_processed.



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

def elements_processed
  @elements_processed
end

Instance Method Details

#cdata_block(string) ⇒ Object



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

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

#characters(string) ⇒ Object



157
158
159
160
161
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 157

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

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

#comment(_string) ⇒ Object



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

def comment(_string); end

#end_documentObject



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

def end_document; end

#end_element(_name) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 148

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



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

def end_prefix_mapping(_prefix); end

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



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

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

#processing_instruction(_name, _content) ⇒ Object



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

def processing_instruction(_name, _content); end

#start_documentObject

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



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

def start_document; end

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



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

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



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

def start_prefix_mapping(_prefix, _uri); end

#warning(_string) ⇒ Object



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

def warning(_string); end

#xmldecl(_version, _encoding, _standalone) ⇒ Object



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

def xmldecl(_version, _encoding, _standalone); end