Module: Lutaml::Xml::Adapter::AdapterHelpers

Included in:
NokogiriAdapter, OgaAdapter, OxAdapter, RexmlAdapter
Defined in:
lib/lutaml/xml/adapter/adapter_helpers.rb

Overview

Shared helper methods for XML adapters.

This module provides common functionality used by both OgaAdapter and RexmlAdapter to reduce code duplication.

Examples:

Including in an adapter

class OgaAdapter < BaseAdapter
  extend AdapterHelpers
end

Constant Summary collapse

TEXT_CLASSES =

Text node classes that should be handled specially

[Moxml::Text, Moxml::Cdata].freeze

Instance Method Summary collapse

Instance Method Details

#build_element_from_child(child) ⇒ Element

Build Element object from child node

Parameters:

  • child (Moxml::Node)

    The child node

Returns:



110
111
112
113
114
115
116
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 110

def build_element_from_child(child)
  if text_node?(child)
    Element.new("Text", "text", node_type: :text)
  else
    Element.new("Element", name_of(child), node_type: :element)
  end
end

#name_of(element) ⇒ String?

Get the name of an XML element

Parameters:

  • element (Moxml::Element, Moxml::Text, Moxml::Cdata, nil)

    The element

Returns:

  • (String, nil)

    The element name or nil



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 23

def name_of(element)
  return nil if element.nil?

  case element
  when Moxml::Text
    "text"
  when Moxml::Cdata
    "#cdata-section"
  when Moxml::ProcessingInstruction
    "processing_instruction"
  when Moxml::Comment
    "comment"
  else
    element.name
  end
end

#namespace_uri_hoisted?(namespace_uri, hoisted_declarations) ⇒ Boolean

Check if a namespace URI is already hoisted

Parameters:

  • namespace_uri (String)

    The namespace URI to check

  • hoisted_declarations (Hash{String, nil => String})

    The hoisted declarations

Returns:

  • (Boolean)

    true if the namespace is already hoisted



143
144
145
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 143

def namespace_uri_hoisted?(namespace_uri, hoisted_declarations)
  prefix_for_namespace_uri(namespace_uri, hoisted_declarations) != nil
end

#namespaced_attr_name(attribute) ⇒ String

Get the namespaced attribute name

Parameters:

  • attribute (Moxml::Attribute)

    The attribute

Returns:

  • (String)

    The namespaced attribute name



78
79
80
81
82
83
84
85
86
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 78

def namespaced_attr_name(attribute)
  attr_ns = attribute.namespace
  attr_name = attribute.name
  return attr_name unless attr_ns

  # Special handling for xml:lang - use prefix instead of URI
  prefix = attr_name == "lang" ? attr_ns.prefix : attr_ns.uri
  [prefix, attr_name].compact.join(":")
end

#namespaced_name_of(node) ⇒ String

Get the namespaced name of an XML node

Parameters:

  • node (Moxml::Node)

    The XML node

Returns:

  • (String)

    The namespaced name (e.g., “uri:name” or “name”)



92
93
94
95
96
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 92

def namespaced_name_of(node)
  return name_of(node) unless node.respond_to?(:namespace)

  [node&.namespace&.uri, node.name].compact.join(":")
end

#node_type_of(element) ⇒ Symbol?

Get the node type of an XML element

This returns the actual node type based on the underlying library’s classification, not inferred from the element name.

Parameters:

  • element (Moxml::Element, Moxml::Text, Moxml::Cdata, nil)

    The element

Returns:

  • (Symbol, nil)

    The node type (:element, :text, :cdata, :comment, :processing_instruction)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 47

def node_type_of(element)
  return nil if element.nil?

  case element
  when Moxml::Text
    :text
  when Moxml::Cdata
    :cdata
  when Moxml::ProcessingInstruction
    :processing_instruction
  when Moxml::Comment
    :comment
  else
    :element
  end
end

#prefix_for_namespace_uri(namespace_uri, hoisted_declarations) ⇒ String?

Get the prefix for a namespace URI from hoisted declarations

This checks if a namespace URI is already declared in hoisted_declarations and returns the prefix that should be used. This prevents duplicate xmlns declarations when both hoisted_declarations and attribute code try to add the same namespace.

Parameters:

  • namespace_uri (String)

    The namespace URI to look up

  • hoisted_declarations (Hash{String, nil => String})

    The hoisted declarations (keys are prefixes or nil for default, values are URIs)

Returns:

  • (String, nil)

    The prefix if found, nil if not hoisted



129
130
131
132
133
134
135
136
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 129

def prefix_for_namespace_uri(namespace_uri, hoisted_declarations)
  return nil unless hoisted_declarations

  hoisted_declarations.each do |prefix, uri|
    return prefix if uri == namespace_uri
  end
  nil
end

#prefixed_name_of(node) ⇒ String

Get the prefixed name of an XML node

Parameters:

  • node (Moxml::Node)

    The XML node

Returns:

  • (String)

    The prefixed name (e.g., “prefix:name” or “name”)



68
69
70
71
72
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 68

def prefixed_name_of(node)
  return name_of(node) if TEXT_CLASSES.include?(node.class)

  [node&.namespace&.prefix, node.name].compact.join(":")
end

#text_node?(node) ⇒ Boolean

Check if a node is a text node

Parameters:

  • node (Object)

    The node to check

Returns:

  • (Boolean)

    true if text or cdata node



102
103
104
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 102

def text_node?(node)
  TEXT_CLASSES.include?(node.class)
end