Class: Canon::Comparison::XmlComparatorHelpers::NamespaceComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/comparison/xml_comparator/namespace_comparator.rb

Overview

Namespace declaration comparison logic Handles comparison of xmlns and xmlns:* attributes

Class Method Summary collapse

Class Method Details

.add_namespace_difference(node1, node2, missing, extra, changed, opts, differences) ⇒ Object

Add a namespace declaration difference

Parameters:

  • node1 (Object)

    First node

  • node2 (Object)

    Second node

  • missing (Array)

    Missing prefixes

  • extra (Array)

    Extra prefixes

  • changed (Array)

    Changed prefixes

  • opts (Hash)

    Options

  • differences (Array)

    Array to append difference to



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
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 150

def self.add_namespace_difference(node1, node2, missing, extra,
changed, opts, differences)
  # Build a descriptive reason
  reasons = []
  if missing.any?
    reasons << "removed: #{missing.map do |p|
      p.empty? ? 'xmlns' : "xmlns:#{p}"
    end.join(', ')}"
  end
  if extra.any?
    reasons << "added: #{extra.map do |p|
      p.empty? ? 'xmlns' : "xmlns:#{p}"
    end.join(', ')}"
  end
  if changed.any?
    reasons << "changed: #{changed.map do |p|
      p.empty? ? 'xmlns' : "xmlns:#{p}"
    end.join(', ')}"
  end

  # Import DiffNodeBuilder to avoid circular dependency
  require_relative "diff_node_builder"

  diff_node = DiffNodeBuilder.build(
    node1: node1,
    node2: node2,
    diff1: Comparison::UNEQUAL_ATTRIBUTES,
    diff2: Comparison::UNEQUAL_ATTRIBUTES,
    dimension: :namespace_declarations,
    **opts,
  )
  differences << diff_node if diff_node
end

.compare(node1, node2, opts, differences) ⇒ Symbol

Compare namespace declarations between two nodes

Parameters:

  • node1 (Object)

    First node

  • node2 (Object)

    Second node

  • opts (Hash)

    Comparison options

  • differences (Array)

    Array to append differences to

Returns:

  • (Symbol)

    Comparison result



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 16

def self.compare(node1, node2, opts, differences)
  ns_decls1 = extract_declarations(node1)
  ns_decls2 = extract_declarations(node2)

  # Find missing, extra, and changed namespace declarations
  missing = ns_decls1.keys - ns_decls2.keys  # In node1 but not node2
  extra = ns_decls2.keys - ns_decls1.keys    # In node2 but not node1
  changed = ns_decls1.select do |prefix, uri|
    ns_decls2[prefix] && ns_decls2[prefix] != uri
  end.keys

  # If there are any differences, create a DiffNode
  if missing.any? || extra.any? || changed.any?
    add_namespace_difference(node1, node2, missing, extra, changed,
                             opts, differences)
    return Comparison::UNEQUAL_ATTRIBUTES
  end

  Comparison::EQUIVALENT
end

.extract_declarations(node) ⇒ Hash

Extract namespace declarations from a node

Parameters:

  • node (Object)

    Node to extract namespace declarations from

Returns:

  • (Hash)

    Hash of prefix => URI mappings



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 41

def self.extract_declarations(node)
  declarations = {}

  # Handle Canon::Xml::Node (uses namespace_nodes)
  if node.respond_to?(:namespace_nodes)
    return extract_from_namespace_nodes(node.namespace_nodes,
                                        declarations)
  end

  # Handle Nokogiri/Moxml nodes (use attributes)
  raw_attrs = node.respond_to?(:attribute_nodes) ? node.attribute_nodes : node.attributes

  # Handle Canon::Xml::Node attribute format (array of AttributeNode)
  if raw_attrs.is_a?(Array)
    extract_from_array_attributes(raw_attrs, declarations)
  else
    # Handle Nokogiri and Moxml attribute formats (Hash-like)
    extract_from_hash_attributes(raw_attrs, declarations)
  end

  declarations
end

.extract_from_array_attributes(raw_attrs, declarations) ⇒ Hash

Extract from array-format attributes

Parameters:

  • raw_attrs (Array)

    Array of AttributeNode objects

  • declarations (Hash)

    Output hash to populate

Returns:

  • (Hash)

    Declarations hash



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 86

def self.extract_from_array_attributes(raw_attrs, declarations)
  raw_attrs.each do |attr|
    name = attr.name
    value = attr.value

    if namespace_declaration?(name)
      # Extract prefix: "xmlns" -> "", "xmlns:xmi" -> "xmi"
      prefix = name == "xmlns" ? "" : name.split(":", 2)[1]
      declarations[prefix] = value
    end
  end

  declarations
end

.extract_from_hash_attributes(raw_attrs, declarations) ⇒ Hash

Extract from hash-format attributes

Parameters:

  • raw_attrs (Hash)

    Hash-like attributes

  • declarations (Hash)

    Output hash to populate

Returns:

  • (Hash)

    Declarations hash



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
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 106

def self.extract_from_hash_attributes(raw_attrs, declarations)
  raw_attrs.each do |key, val|
    # Normalize key and value
    name = if key.is_a?(String)
             # Nokogiri format: key=name (String), val=attr object
             key
           else
             # Moxml format: key=attr object, val=nil
             key.respond_to?(:name) ? key.name : key.to_s
           end

    if namespace_declaration?(name)
      value = if val.respond_to?(:value)
                val.value
              else
                val.to_s
              end

      # Extract prefix: "xmlns" -> "", "xmlns:xmi" -> "xmi"
      prefix = name == "xmlns" ? "" : name.split(":", 2)[1]
      declarations[prefix] = value
    end
  end

  declarations
end

.extract_from_namespace_nodes(namespace_nodes, declarations) ⇒ Hash

Extract from Canon::Xml::Node namespace_nodes

Parameters:

  • namespace_nodes (Array)

    Array of NamespaceNode objects

  • declarations (Hash)

    Output hash to populate

Returns:

  • (Hash)

    Declarations hash



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 69

def self.extract_from_namespace_nodes(namespace_nodes, declarations)
  namespace_nodes.each do |ns|
    # Skip the implicit xml namespace (always present)
    next if ns.prefix == "xml" && ns.uri == "http://www.w3.org/XML/1998/namespace"

    prefix = ns.prefix || ""
    declarations[prefix] = ns.uri
  end

  declarations
end

.namespace_declaration?(attr_name) ⇒ Boolean

Check if an attribute name is a namespace declaration

Parameters:

  • attr_name (String)

    Attribute name

Returns:

  • (Boolean)

    true if it’s a namespace declaration



137
138
139
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 137

def self.namespace_declaration?(attr_name)
  attr_name == "xmlns" || attr_name.start_with?("xmlns:")
end