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.



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

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.



82
83
84
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 82

def elements_processed
  @elements_processed
end

Instance Method Details

#after_element(_namespace, _name) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 107

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



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

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

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



91
92
93
94
95
# File 'lib/serialbench/serializers/xml/oga_serializer.rb', line 91

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



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

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

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