Class: Serialbench::Serializers::Xml::LeptrisSerializer

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

Defined Under Namespace

Classes: StreamingHandler

Instance Method Summary collapse

Methods inherited from BaseXmlSerializer

#features, format, #generate_xml, #parse_dom, #parse_sax, #xslt_apply

Methods inherited from BaseSerializer

#get_version, #initialize, #require_library, #stream_parse, #supports?

Constructor Details

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

Instance Method Details

#available?Boolean

The gem's require succeeds even when the libtaurus shared library is missing -- the FFI load happens lazily at first use. Probe with a real parse so "available" means "actually usable".

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 89

def available?
  return @available if defined?(@available)

  @available = begin
    require 'leptris'
  require 'leptris/xml'
    Leptris::XML.parse('<probe/>')
    true
  rescue StandardError, LoadError => e
    warn "#{name} unavailable: #{e.class}: #{e.message}"
    false
  end
end

#capabilitiesObject



39
40
41
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 39

def capabilities
  super | Set.new(i[xpath xquery xslt xslt30 validation sax stax])
end

#generate(data) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 19

def generate(data)
  require 'leptris'
  require 'leptris/xml'
  if data.is_a?(Leptris::XML::Document) || data.is_a?(Leptris::XML::Node)
    data.to_xml(indent: 2)
  else
    build_document_from_data(data).to_xml(indent: 2)
  end
end

#library_require_nameObject



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

def library_require_name
  'leptris'
end

#nameObject



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

def name
  'leptris'
end

#parse(xml_string) ⇒ Object



13
14
15
16
17
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 13

def parse(xml_string)
  require 'leptris'
  require 'leptris/xml'
  Leptris::XML.parse(xml_string)
end

#parse_streaming(xml_string, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 29

def parse_streaming(xml_string, &block)
  require 'leptris'
  require 'leptris/xml'

  handler = StreamingHandler.new(&block)
  parser = Leptris::XML::SAX::Parser.new(handler)
  parser.parse_memory(xml_string)
  handler.elements_processed
end

#validate(document, schema) ⇒ Object



56
57
58
59
60
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 56

def validate(document, schema)
  require 'leptris'
  require 'leptris/xml'
  Leptris::XML::RelaxNG.parse(schema).valid?(document)
end

#versionObject



74
75
76
77
78
79
80
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 74

def version
  return 'unknown' unless available?

  require 'leptris'
  require 'leptris/xml'
  Leptris::VERSION
end

#xpath_query(document, expression) ⇒ Object



62
63
64
65
66
67
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 62

def xpath_query(document, expression)
  require 'leptris'
  require 'leptris/xml'
  result = document.xpath(expression)
  result.size
end

#xquery_eval(document, expression) ⇒ Object



43
44
45
46
47
48
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 43

def xquery_eval(document, expression)
  require 'leptris'
  require 'leptris/xml'
  result = Leptris::XML::XQuery.parse(expression).eval(document)
  result.respond_to?(:length) ? result.length : 1
end

#xslt_transform(document, stylesheet) ⇒ Object



50
51
52
53
54
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 50

def xslt_transform(document, stylesheet)
  require 'leptris'
  require 'leptris/xml'
  Leptris::XML::XSLT.parse(stylesheet).apply_to(document).serialize
end