Class: Lutaml::Model::Xml::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/xml/document.rb

Direct Known Subclasses

NokogiriAdapter, OgaAdapter, OxAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, encoding = nil, register: nil) ⇒ Document



13
14
15
16
17
# File 'lib/lutaml/model/xml/document.rb', line 13

def initialize(root, encoding = nil, register: nil)
  @root = root
  @encoding = encoding
  @register = setup_register(register)
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



11
12
13
# File 'lib/lutaml/model/xml/document.rb', line 11

def encoding
  @encoding
end

#registerObject (readonly)

Returns the value of attribute register.



11
12
13
# File 'lib/lutaml/model/xml/document.rb', line 11

def register
  @register
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/lutaml/model/xml/document.rb', line 11

def root
  @root
end

Class Method Details

.encoding(xml, options) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/lutaml/model/xml/document.rb', line 31

def self.encoding(xml, options)
  if options.key?(:encoding)
    options[:encoding]
  else
    xml.encoding.to_s
  end
end

.name_of(element) ⇒ Object



417
418
419
# File 'lib/lutaml/model/xml/document.rb', line 417

def self.name_of(element)
  element.name
end

.namespaced_name_of(element) ⇒ Object



425
426
427
# File 'lib/lutaml/model/xml/document.rb', line 425

def self.namespaced_name_of(element)
  element.namespaced_name
end

.order_of(element) ⇒ Object



413
414
415
# File 'lib/lutaml/model/xml/document.rb', line 413

def self.order_of(element)
  element.order
end

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

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/lutaml/model/xml/document.rb', line 19

def self.parse(xml, _options = {})
  raise NotImplementedError, "Subclasses must implement `parse`."
end

.text_of(element) ⇒ Object



421
422
423
# File 'lib/lutaml/model/xml/document.rb', line 421

def self.text_of(element)
  element.text
end

.typeObject



409
410
411
# File 'lib/lutaml/model/xml/document.rb', line 409

def self.type
  Utils.snake_case(self).split("/").last.split("_").first
end

Instance Method Details

#add_to_xml(xml, element, prefix, value, options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/lutaml/model/xml/document.rb', line 147

def add_to_xml(xml, element, prefix, value, options = {})
  attribute = options[:attribute]
  rule = options[:rule]

  if rule.custom_methods[:to]
    options[:mapper_class].new.send(rule.custom_methods[:to], element, xml.parent, xml)
    return
  end

  # Only transform when recursion is not called
  if !attribute.collection? || attribute.collection_instance?(value)
    value = ExportTransformer.call(value, rule, attribute)
  end

  if attribute.collection_instance?(value) && !Utils.empty_collection?(value)
    value.each do |item|
      add_to_xml(xml, element, prefix, item, options)
    end

    return
  end

  return if !render_element?(rule, element, value)

  value = rule.render_value_for(value)

  if value && (attribute&.type(register)&.<= Lutaml::Model::Serialize)
    handle_nested_elements(
      xml,
      value,
      options.merge({ rule: rule, attribute: attribute }),
    )
  elsif value.nil?
    xml.create_and_add_element(rule.name, attributes: { "xsi:nil" => true })
  elsif Utils.empty?(value)
    xml.create_and_add_element(rule.name)
  elsif rule.raw_mapping?
    xml.add_xml_fragment(xml, value)
  elsif rule.prefix_set?
    xml.create_and_add_element(rule.name, prefix: prefix) do
      add_value(xml, value, attribute, cdata: rule.cdata)
    end
  else
    xml.create_and_add_element(rule.name) do
      add_value(xml, value, attribute, cdata: rule.cdata)
    end
  end
end

#add_value(xml, value, attribute, cdata: false) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/lutaml/model/xml/document.rb', line 196

def add_value(xml, value, attribute, cdata: false)
  if !value.nil?
    serialized_value = attribute.serialize(value, :xml, register)
    if attribute.raw?
      xml.add_xml_fragment(xml, value)
    elsif attribute.type(register) == Lutaml::Model::Type::Hash
      serialized_value.each do |key, val|
        xml.create_and_add_element(key) do |element|
          element.text(val)
        end
      end
    else
      xml.add_text(xml, serialized_value, cdata: cdata)
    end
  end
end

#attribute_definition_for(element, rule, mapper_class: nil) ⇒ Object



389
390
391
392
393
394
# File 'lib/lutaml/model/xml/document.rb', line 389

def attribute_definition_for(element, rule, mapper_class: nil)
  klass = mapper_class || element.class
  return klass.attributes[rule.to] unless rule.delegate

  element.send(rule.delegate).class.attributes[rule.to]
end

#attribute_value_for(element, rule) ⇒ Object



396
397
398
399
400
# File 'lib/lutaml/model/xml/document.rb', line 396

def attribute_value_for(element, rule)
  return element.send(rule.to) unless rule.delegate

  element.send(rule.delegate).send(rule.to)
end

#attributesObject



27
28
29
# File 'lib/lutaml/model/xml/document.rb', line 27

def attributes
  root.attributes
end

#attributes_hash(element) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lutaml/model/xml/document.rb', line 121

def attributes_hash(element)
  result = Lutaml::Model::MappingHash.new

  element.attributes.each_value do |attr|
    if attr.unprefixed_name == "schemaLocation"
      result["__schema_location"] = {
        namespace: attr.namespace,
        prefix: attr.namespace_prefix,
        schema_location: attr.value,
      }
    else
      result[attr.namespaced_name] = attr.value
    end
  end

  result
end

#build_attributes(element, xml_mapping, options = {}) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/lutaml/model/xml/document.rb', line 348

def build_attributes(element, xml_mapping, options = {})
  attrs = if options.fetch(:set_namespace, true)
            namespace_attributes(xml_mapping)
          else
            {}
          end

  if element.respond_to?(:schema_location) && element.schema_location.is_a?(Lutaml::Model::SchemaLocation) && !options[:except]&.include?(:schema_location)
    attrs.merge!(element.schema_location.to_xml_attributes)
  end

  xml_mapping.attributes.each_with_object(attrs) do |mapping_rule, hash|
    next if options[:except]&.include?(mapping_rule.to)
    next if mapping_rule.custom_methods[:to]

    mapping_rule_name = mapping_rule.multiple_mappings? ? mapping_rule.name.first : mapping_rule.name

    if mapping_rule.namespace && mapping_rule.prefix && mapping_rule_name != "lang"
      hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
    end

    value = mapping_rule.to_value_for(element)
    attr = attribute_definition_for(element, mapping_rule, mapper_class: options[:mapper_class])
    value = attr.serialize(value, :xml, register) if attr

    value = ExportTransformer.call(value, mapping_rule, attr)

    if render_element?(mapping_rule, element, value)
      hash[mapping_rule.prefixed_name] = value ? value.to_s : value
    end
  end

  xml_mapping.elements.each_with_object(attrs) do |mapping_rule, hash|
    next if options[:except]&.include?(mapping_rule.to)

    if mapping_rule.namespace && mapping_rule.prefix
      hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
    end
  end
end

#build_element(xml, element, options = {}) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/lutaml/model/xml/document.rb', line 139

def build_element(xml, element, options = {})
  if ordered?(element, options)
    build_ordered_element(xml, element, options)
  else
    build_unordered_element(xml, element, options)
  end
end

#build_namespace_attributes(klass, processed = {}, options = {}) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/lutaml/model/xml/document.rb', line 304

def build_namespace_attributes(klass, processed = {}, options = {})
  xml_mappings = klass.mappings_for(:xml)
  attributes = klass.attributes

  attrs = {}

  if xml_mappings.namespace_uri && set_namespace?(options[:caller_rule])
    prefixed_name = [
      "xmlns",
      xml_mappings.namespace_prefix,
    ].compact.join(":")

    attrs[prefixed_name] = xml_mappings.namespace_uri
  end

  xml_mappings.mappings.each do |mapping_rule|
    processed[klass] ||= {}

    next if processed[klass][mapping_rule.name]

    processed[klass][mapping_rule.name] = true

    type = if mapping_rule.delegate
             attributes[mapping_rule.delegate].type(register)
               .attributes[mapping_rule.to].type(register)
           else
             attributes[mapping_rule.to]&.type(register)
           end

    next unless type

    if type <= Lutaml::Model::Serialize
      attrs = attrs.merge(build_namespace_attributes(type, processed,
                                                     { caller_rule: mapping_rule }))
    end

    if mapping_rule.namespace && mapping_rule.prefix && mapping_rule.name != "lang"
      attrs["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
    end
  end

  attrs
end

#build_options_for_nested_elements(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lutaml/model/xml/document.rb', line 71

def build_options_for_nested_elements(options = {})
  attribute = options.delete(:attribute)
  rule = options.delete(:rule)

  return {} unless rule

  # options = {}

  options[:namespace_prefix] = rule.prefix if rule&.namespace_set?
  options[:mixed_content] = rule.mixed_content
  options[:tag_name] = rule.name

  options[:mapper_class] = attribute&.type(register) if attribute
  options[:set_namespace] = set_namespace?(rule)

  options
end

#build_unordered_element(xml, element, options = {}) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/lutaml/model/xml/document.rb', line 213

def build_unordered_element(xml, element, options = {})
  mapper_class = determine_mapper_class(element, options)
  xml_mapping = mapper_class.mappings_for(:xml)
  return xml unless xml_mapping

  attributes = build_element_attributes(element, xml_mapping, options)
  prefix = determine_namespace_prefix(options, xml_mapping)

  prefixed_xml = xml.add_namespace_prefix(prefix)
  tag_name = options[:tag_name] || xml_mapping.root_element

  prefixed_xml.create_and_add_element(tag_name, prefix: prefix,
                                                attributes: attributes) do
    if options.key?(:namespace_prefix) && !options[:namespace_prefix]
      prefixed_xml.add_namespace_prefix(nil)
    end

    xml_mapping.attributes.each do |attribute_rule|
      attribute_rule.serialize_attribute(element, prefixed_xml.parent,
                                         xml)
    end

    mappings = xml_mapping.elements + [xml_mapping.raw_mapping].compact
    mappings.each do |element_rule|
      attribute_def = attribute_definition_for(element, element_rule,
                                               mapper_class: mapper_class)

      if attribute_def
        value = attribute_value_for(element, element_rule)

        next if !element_rule.render?(value, element)

        value = attribute_def.build_collection(value) if attribute_def.collection? && !attribute_def.collection_instance?(value)
      end

      add_to_xml(
        prefixed_xml,
        element,
        element_rule.prefix,
        value,
        options.merge({ attribute: attribute_def, rule: element_rule,
                        mapper_class: mapper_class }),
      )
    end

    process_content_mapping(element, xml_mapping.content_mapping,
                            prefixed_xml, mapper_class)
  end
end

#cdataObject



435
436
437
# File 'lib/lutaml/model/xml/document.rb', line 435

def cdata
  @root.cdata
end

#childrenObject



23
24
25
# File 'lib/lutaml/model/xml/document.rb', line 23

def children
  @root.children
end

#declaration(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lutaml/model/xml/document.rb', line 39

def declaration(options)
  version = "1.0"
  version = options[:declaration] if options[:declaration].is_a?(String)

  encoding = options[:encoding] ? "UTF-8" : nil
  encoding = options[:encoding] if options[:encoding].is_a?(String)

  declaration = "<?xml version=\"#{version}\""
  declaration += " encoding=\"#{encoding}\"" if encoding
  declaration += "?>\n"
  declaration
end

#handle_nested_elements(builder, value, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/lutaml/model/xml/document.rb', line 60

def handle_nested_elements(builder, value, options = {})
  element_options = build_options_for_nested_elements(options)

  case value
  when Array
    value.each { |val| build_element(builder, val, element_options) }
  else
    build_element(builder, value, element_options)
  end
end

#namespace_attributes(xml_mapping) ⇒ Object



402
403
404
405
406
407
# File 'lib/lutaml/model/xml/document.rb', line 402

def namespace_attributes(xml_mapping)
  return {} unless xml_mapping.namespace_uri

  key = ["xmlns", xml_mapping.namespace_prefix].compact.join(":")
  { key => xml_mapping.namespace_uri }
end

#orderObject



56
57
58
# File 'lib/lutaml/model/xml/document.rb', line 56

def order
  @root.order
end

#ordered?(element, options = {}) ⇒ Boolean



281
282
283
284
285
286
287
288
# File 'lib/lutaml/model/xml/document.rb', line 281

def ordered?(element, options = {})
  return false unless element.respond_to?(:element_order)
  return element.ordered? if element.respond_to?(:ordered?)
  return options[:mixed_content] if options.key?(:mixed_content)

  mapper_class = options[:mapper_class]
  mapper_class ? mapper_class.mappings_for(:xml).mixed_content? : false
end

#parse_element(element, klass = nil, format = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lutaml/model/xml/document.rb', line 89

def parse_element(element, klass = nil, format = nil)
  result = Lutaml::Model::MappingHash.new
  result.node = element
  result.item_order = self.class.order_of(element)

  element.children.each do |child|
    if klass&.<= Serialize
      attr = klass.attribute_for_child(self.class.name_of(child),
                                       format)
    end

    if child.respond_to?(:text?) && child.text?
      result.assign_or_append_value(
        self.class.name_of(child),
        self.class.text_of(child),
      )
      next
    end

    result["elements"] ||= Lutaml::Model::MappingHash.new
    result["elements"].assign_or_append_value(
      self.class.namespaced_name_of(child),
      parse_element(child, attr&.type(register) || klass, format),
    )
  end

  result["attributes"] = attributes_hash(element) if element.attributes&.any?

  result.merge(attributes_hash(element))
  result
end

#process_content_mapping(element, content_rule, xml, mapper_class) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/lutaml/model/xml/document.rb', line 263

def process_content_mapping(element, content_rule, xml, mapper_class)
  return unless content_rule

  if content_rule.custom_methods[:to]
    mapper_class.new.send(
      content_rule.custom_methods[:to],
      element,
      xml.parent,
      xml,
    )
  else
    text = content_rule.serialize(element)
    text = text.join if text.is_a?(Array)

    xml.add_text(xml, text, cdata: content_rule.cdata)
  end
end

#render_default?(rule, element) ⇒ Boolean



298
299
300
301
302
# File 'lib/lutaml/model/xml/document.rb', line 298

def render_default?(rule, element)
  !element.respond_to?(:using_default?) ||
    rule.render_default? ||
    !element.using_default?(rule.to)
end

#render_element?(rule, element, value) ⇒ Boolean



294
295
296
# File 'lib/lutaml/model/xml/document.rb', line 294

def render_element?(rule, element, value)
  rule.render?(value, element)
end

#set_namespace?(rule) ⇒ Boolean



290
291
292
# File 'lib/lutaml/model/xml/document.rb', line 290

def set_namespace?(rule)
  rule.nil? || !rule.namespace_set?
end

#textObject



429
430
431
432
433
# File 'lib/lutaml/model/xml/document.rb', line 429

def text
  return @root.text_children.map(&:text) if @root.children.count > 1

  @root.text
end

#to_hObject



52
53
54
# File 'lib/lutaml/model/xml/document.rb', line 52

def to_h
  parse_element(@root)
end