Class: Serialbench::Serializers::Xml::OgaSerializer::StreamingHandler

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

Overview

SAX handler for streaming

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StreamingHandler

Returns a new instance of StreamingHandler.



88
89
90
91
92
93
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 88

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

Instance Attribute Details

#elements_processedObject (readonly)

Returns the value of attribute elements_processed.



86
87
88
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 86

def elements_processed
  @elements_processed
end

Instance Method Details

#after_element(_namespace, _name) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 111

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

#on_cdata(text) ⇒ Object



107
108
109
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 107

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

#on_element(namespace, name, attributes = {}) ⇒ Object



95
96
97
98
99
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 95

def on_element(namespace, name, attributes = {})
  @elements_processed += 1
  @current_element = { name: name, namespace: namespace, attributes: attributes, children: [], text: '' }
  @element_stack.push(@current_element)
end

#on_text(text) ⇒ Object



101
102
103
104
105
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 101

def on_text(text)
  return if text.strip.empty?

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