Class: Lutaml::Model::XmlAdapter::NokogiriAdapter

Inherits:
XmlDocument
  • Object
show all
Defined in:
lib/lutaml/model/xml_adapter/nokogiri_adapter.rb

Instance Attribute Summary

Attributes inherited from XmlDocument

#encoding, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XmlDocument

#add_to_xml, #add_value, #attribute_definition_for, #attribute_value_for, #attributes, #attributes_hash, #build_attributes, #build_element, #build_namespace_attributes, #build_options_for_nested_elements, #build_unordered_element, #cdata, #children, #declaration, encoding, #handle_nested_elements, #initialize, name_of, #namespace_attributes, namespaced_name_of, #order, order_of, #ordered?, #parse_element, #process_content_mapping, #render_default?, #render_element?, #set_namespace?, #text, text_of, #to_h, type

Constructor Details

This class inherits a constructor from Lutaml::Model::XmlAdapter::XmlDocument

Class Method Details

.parse(xml, options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 9

def self.parse(xml, options = {})
  parsed = Nokogiri::XML(xml, nil, encoding(xml, options))
  @root = NokogiriElement.new(parsed.root)
  new(@root, parsed.encoding)
end

Instance Method Details

#to_xml(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb', line 15

def to_xml(options = {})
  builder_options = {}

  if options.key?(:encoding)
    builder_options[:encoding] = options[:encoding] unless options[:encoding].nil?
  elsif options.key?(:parse_encoding)
    builder_options[:encoding] = options[:parse_encoding]
  else
    builder_options[:encoding] = "UTF-8"
  end

  builder = Builder::Nokogiri.build(builder_options) do |xml|
    if root.is_a?(Lutaml::Model::XmlAdapter::NokogiriElement)
      root.build_xml(xml)
    else
      mapper_class = options[:mapper_class] || @root.class
      options[:xml_attributes] =
        build_namespace_attributes(mapper_class)
      build_element(xml, @root, options)
    end
  end

  xml_options = {}
  xml_options[:indent] = 2 if options[:pretty]

  xml_data = builder.doc.root.to_xml(xml_options)
  options[:declaration] ? declaration(options) + xml_data : xml_data
end