Class: Lutaml::Xml::AdapterElement

Inherits:
XmlElement show all
Defined in:
lib/lutaml/xml/adapter_element.rb

Overview

Base element wrapper for moxml-backed XML adapters. NokogiriElement, Ox::Element, Oga::Element, Rexml::Element all inherit from this class.

Constant Summary collapse

NamespaceData =
Lutaml::Xml::Adapter::NamespaceData

Constants inherited from XmlElement

XmlElement::CDATA_NODE_NAME, XmlElement::EMPTY_CHILDREN_ARRAY, XmlElement::EMPTY_NAMESPACES, XmlElement::NODE_TYPES, XmlElement::TEXT_NODE_NAME, XmlElement::XMLNS_PREFIX, XmlElement::XML_NAMESPACE_URI

Instance Attribute Summary

Attributes inherited from XmlElement

#adapter_node, #attribute_order, #attributes, #children, #namespace_prefix, #namespace_prefix_explicit, #node_type, #order_cache, #parent_document, #processing_instructions

Instance Method Summary collapse

Methods inherited from XmlElement

#[], #add_namespace, #attribute_is_namespace?, #cdata, #cdata?, #cdata_children, #comment?, #default_namespace, detect_explicit_no_namespace, #document, #element?, #element_children, #element_children_index, #ensure_attribute_index, #ensure_children_index, #find_attribute_value, #find_child_by_name, #find_children_by_name, fpi?, fpi_to_urn, #name, #namespace, #namespace_scope_config, #namespace_uri, #namespaced_name, #namespaces, #nil_element?, #order, #original_namespace_uri, #original_xml_element, #own_namespaces, #pretty_print_instance_variables, #processing_instruction?, #root, #text_children, #to_h, #unprefixed_name, #xml_declaration, #xml_namespace_prefix

Constructor Details

#initialize(node, parent: nil, default_namespace: nil) ⇒ AdapterElement

Returns a new instance of AdapterElement.



11
12
13
14
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
43
44
45
46
47
48
49
50
51
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/lutaml/xml/adapter_element.rb', line 11

def initialize(node, parent: nil, default_namespace: nil)
  @moxml_node = node

  node_type = case node
              when Moxml::Cdata then :cdata
              when Moxml::Text then :text
              when Moxml::Comment then :comment
              when Moxml::ProcessingInstruction then :processing_instruction
              else :element
              end

  text = case node
         when Moxml::Element
           namespace_name = node.namespace&.prefix
           ns_defs = node.namespaces

           has_empty_xmlns = ns_defs.any? do |ns|
             ns.prefix.nil? && ns.uri == ""
           end

           explicit_no_namespace = XmlElement.detect_explicit_no_namespace(
             has_empty_xmlns: has_empty_xmlns,
             node_namespace_nil: node.namespace.nil? || node.namespace&.uri == "",
           )

           add_namespaces_from_defs(ns_defs, is_root: parent.nil?)

           if parent.nil? && !namespace_name && node.namespace&.uri &&
               node.namespace.uri != ""
             default_namespace = node.namespace.uri
           end

           children = parse_children(node,
                                     default_namespace: default_namespace)
           attributes, attr_order = node_attributes_with_order(node)
           @root = node
           EncodingNormalizer.normalize_to_utf8(node.inner_text)
         when Moxml::Text
           EncodingNormalizer.normalize_to_utf8(node.content)
         when Moxml::Cdata
           EncodingNormalizer.normalize_to_utf8(node.content)
         when Moxml::Comment
           EncodingNormalizer.normalize_to_utf8(node.content)
         when Moxml::ProcessingInstruction
           EncodingNormalizer.normalize_to_utf8(
             node.content.to_s.sub(/\A\s+/, ""),
           )
         end

  name = adapter_class.name_of(node)
  super(
    node,
    Hash(attributes),
    Array(children),
    text,
    name: name,
    parent_document: parent,
    namespace_prefix: namespace_name,
    default_namespace: default_namespace,
    explicit_no_namespace: explicit_no_namespace || false,
    node_type: node_type,
    attribute_order: attr_order
  )
end

Instance Method Details

#build_xml(builder = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lutaml/xml/adapter_element.rb', line 88

def build_xml(builder = nil)
  if cdata?
    builder.add_text(builder.current_node, @text.to_s, cdata: true)
  elsif comment?
    builder.add_comment(builder.current_node, @text.to_s)
  elsif processing_instruction?
    builder.add_processing_instruction(name, @text.to_s)
  elsif text? && !element?
    builder.add_text(builder.current_node, build_text_for_xml.to_s)
  else
    build_element_xml(builder)
  end

  builder
end

#inner_xmlObject



104
105
106
# File 'lib/lutaml/xml/adapter_element.rb', line 104

def inner_xml
  children.map(&:to_xml).join
end

#textObject



80
81
82
# File 'lib/lutaml/xml/adapter_element.rb', line 80

def text
  super || @text
end

#text?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/lutaml/xml/adapter_element.rb', line 76

def text?
  %i[text cdata].include?(@node_type)
end

#to_xml(_builder = nil) ⇒ Object



84
85
86
# File 'lib/lutaml/xml/adapter_element.rb', line 84

def to_xml(_builder = nil)
  @moxml_node.to_xml(declaration: false, expand_empty: false)
end