Class: Lutaml::Model::Xml::OxElement

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

Constant Summary

Constants inherited from XmlElement

XmlElement::XML_NAMESPACE_URI

Instance Attribute Summary

Attributes inherited from XmlElement

#adapter_node, #attributes, #children, #namespace_prefix, #parent_document

Instance Method Summary collapse

Methods inherited from XmlElement

#[], #add_namespace, #attribute_is_namespace?, #cdata_children, #default_namespace, #document, #find_attribute_value, #find_child_by_name, #find_children_by_name, #name, #namespace, #namespaced_name, #namespaces, #nil_element?, #order, #own_namespaces, #pretty_print_instance_variables, #root, #text_children, #to_h, #unprefixed_name

Constructor Details

#initialize(node, root_node: nil) ⇒ OxElement



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 104

def initialize(node, root_node: nil)
  case node
  when String
    super("text", {}, [], node, parent_document: root_node, name: "text")
  when Ox::Comment
    super("comment", {}, [], node.value, parent_document: root_node, name: "comment")
  when Ox::CData
    super("#cdata-section", {}, [], node.value, parent_document: root_node, name: "#cdata-section")
  else
    namespace_attributes(node.attributes).each do |(name, value)|
      if root_node
        root_node.add_namespace(XmlNamespace.new(value, name))
      else
        add_namespace(XmlNamespace.new(value, name))
      end
    end

    attributes = node.attributes.each_with_object({}) do |(name, value), hash|
      next if attribute_is_namespace?(name)

      namespace_prefix = name.to_s.split(":").first
      if (n = name.to_s.split(":")).length > 1
        namespace = (root_node || self).namespaces[namespace_prefix]&.uri
        namespace ||= XML_NAMESPACE_URI
        prefix = n.first
      end

      hash[name.to_s] = XmlAttribute.new(
        name.to_s,
        value,
        namespace: namespace,
        namespace_prefix: prefix,
      )
    end

    prefix, name = separate_name_and_prefix(node)

    super(
      node,
      attributes,
      parse_children(node, root_node: root_node || self),
      node.text,
      parent_document: root_node,
      name: name,
      namespace_prefix: prefix,
    )
  end
end

Instance Method Details

#build_attributes(node) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 197

def build_attributes(node)
  attrs = node.attributes.transform_values(&:value)

  node.own_namespaces.each_value do |namespace|
    attrs[namespace.attr_name] = namespace.uri
  end

  attrs
end

#build_xml(builder = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 173

def build_xml(builder = nil)
  builder ||= Builder::Ox.build
  attrs = build_attributes(self)

  if text?
    builder.add_text(builder, text)
  else
    builder.create_and_add_element(name, attributes: attrs) do |el|
      children.each { |child| child.build_xml(el) }
    end
  end

  builder
end

#cdataObject



211
212
213
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 211

def cdata
  super || cdata_children.first&.text
end

#inner_xmlObject



168
169
170
171
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 168

def inner_xml
  # Ox builder by default, adds a newline at the end, so `chomp` is used
  children.map { |child| child.to_xml.chomp }.join
end

#namespace_attributes(attributes) ⇒ Object



188
189
190
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 188

def namespace_attributes(attributes)
  attributes.select { |attr| attribute_is_namespace?(attr) }
end

#nodesObject



207
208
209
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 207

def nodes
  children
end

#separate_name_and_prefix(node) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 153

def separate_name_and_prefix(node)
  name = node.name.to_s

  return [nil, name] unless name.include?(":")

  prefix, _, name = name.partition(":")
  [prefix, name]
end

#textObject



215
216
217
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 215

def text
  super || cdata
end

#text?Boolean



192
193
194
195
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 192

def text?
  # false
  children.empty? && text&.length&.positive?
end

#to_xmlObject



162
163
164
165
166
# File 'lib/lutaml/model/xml/ox_adapter.rb', line 162

def to_xml
  return text if text?

  build_xml.xml.to_s
end