Class: Lutaml::Xml::Decisions::Rules::FormatPreservationRule

Inherits:
DecisionRule
  • Object
show all
Defined in:
lib/lutaml/xml/decisions/rules/format_preservation_rule.rb

Overview

Priority 1: Preserve input format during round-trip

When parsing XML, the input format is preserved and reused during serialization to maintain format consistency

Instance Method Summary collapse

Methods inherited from DecisionRule

#<=>, #name

Instance Method Details

#applies?(context) ⇒ Boolean

Applies when input format is preserved



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
# File 'lib/lutaml/xml/decisions/rules/format_preservation_rule.rb', line 19

def applies?(context)
  return false unless context.has_namespace?
  return false if context.preserved_input_format.nil?

  # When a child element has its own namespace that differs from the
  # parent's, preserving the input format is critical for round-trip
  # fidelity. Using prefix format (xmlns:prefix="uri") preserves the
  # parent's default namespace scope, while switching to default format
  # (xmlns="uri") would override it, changing the namespace context for
  # the element and its descendants.
  #
  # Skip this check for root elements - they should always preserve format
  if !context.root? && context.namespace_class
    parent_ns = context.parent_namespace_class
    # If parent has no namespace but child has a namespace,
    # don't preserve format - use default format
    # Check both nil and empty URI
    parent_ns_uri = parent_ns&.uri
    if parent_ns.nil? || parent_ns_uri.nil? || parent_ns_uri.empty?
      return false
    end
  end

  true
end

#decide(context) ⇒ Object

Decision: Use the format from input



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lutaml/xml/decisions/rules/format_preservation_rule.rb', line 46

def decide(context)
  input_format = context.preserved_input_format

  if input_format == :default
    Decision.default(
      namespace_class: context.namespace_class,
      reason: "Priority 1: Input used default format - preserve it",
    )
  else
    # For nested elements, use element_used_prefix which is correctly set
    # during deserialization. For root elements, use element_namespace_prefix
    # which reads from the original XmlElement.
    prefix = if context.root?
               context.element_namespace_prefix
             else
               context.element_used_prefix
             end
    Decision.prefix(
      prefix: prefix,
      namespace_class: context.namespace_class,
      reason: "Priority 1: Input used prefix format - preserve it",
    )
  end
end

#priorityObject

Priority 1



14
15
16
# File 'lib/lutaml/xml/decisions/rules/format_preservation_rule.rb', line 14

def priority
  1
end