Class: Serialbench::Serializers::Xml::RexmlSerializer

Inherits:
BaseXmlSerializer show all
Defined in:
lib/serialbench/serializers/xml/rexml_serializer.rb

Instance Method Summary collapse

Methods inherited from BaseXmlSerializer

#features, format, #generate_xml, #parse_dom, #parse_sax, #supports_generation?

Methods inherited from BaseSerializer

#get_version, #initialize, #require_library

Constructor Details

This class inherits a constructor from Serialbench::Serializers::BaseSerializer

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 9

def available?
  require_library('rexml/document')
end

#generate(data, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 29

def generate(data, options = {})
  require 'rexml/document'

  # If data is already a REXML::Document, use it directly
  if data.is_a?(REXML::Document)
    document = data
  else
    # Convert Hash/other data to XML document
    document = REXML::Document.new
    root = document.add_element('root')
    hash_to_xml(data, root)
  end

  indent = options.fetch(:indent, 0)
  output = String.new
  if indent.positive?
    document.write(output, indent)
  else
    document.write(output)
  end
  output
end

#nameObject



13
14
15
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 13

def name
  'rexml'
end

#parse(xml_string) ⇒ Object



24
25
26
27
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 24

def parse(xml_string)
  require 'rexml/document'
  REXML::Document.new(xml_string)
end

#stream_parse(xml_string, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 52

def stream_parse(xml_string, &block)
  require 'rexml/parsers/sax2parser'
  handler = SaxHandler.new(&block)
  parser = REXML::Parsers::SAX2Parser.new(xml_string)
  parser.listen(handler)
  parser.parse
  handler.result
rescue LoadError
  # Fallback if SAX2 is not available
  require 'rexml/document'
  doc = REXML::Document.new(xml_string)
  handler = SaxHandler.new(&block)
  # Simulate SAX events by walking the document
  doc.root.each_recursive do |element|
    if element.is_a?(REXML::Element)
      handler.start_element(nil, element.name, element.name, element.attributes)
      handler.end_element(nil, element.name, element.name)
    elsif element.is_a?(REXML::Text)
      handler.characters(element.to_s)
    end
  end
  handler.result
end

#supports_namespaces?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 84

def supports_namespaces?
  true
end

#supports_streaming?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 76

def supports_streaming?
  false
end

#supports_validation?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 88

def supports_validation?
  false
end

#supports_xpath?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 80

def supports_xpath?
  true
end

#versionObject



17
18
19
20
21
22
# File 'lib/serialbench/serializers/xml/rexml_serializer.rb', line 17

def version
  require 'rexml/rexml'
  REXML::VERSION
rescue LoadError, NameError
  'built-in'
end