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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 137

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

  diff_node = Canon::Comparison::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
36
37
38
# 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)

  # Most element pairs declare nothing — skip the set algebra.
  return Comparison::EQUIVALENT if ns_decls1.empty? && ns_decls2.empty?

  # 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



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

def self.extract_declarations(node)
  declarations = {}

  if node.is_a?(Canon::Xml::Node)
    if node.namespace_nodes
      return extract_from_namespace_nodes(node.namespace_nodes,
                                          declarations)
    end

    raw_attrs = node.attribute_nodes
  else
    raw_attrs = node.attributes
  end

  if raw_attrs.is_a?(Array)
    extract_from_array_attributes(raw_attrs, declarations)
  else
    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



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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 109

def self.extract_from_hash_attributes(raw_attrs, declarations)
  raw_attrs.each do |key, val|
    name = key.is_a?(String) ? key : key.name

    if namespace_declaration?(name)
      value = val.is_a?(String) ? val : val.value

      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



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 72

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

Returns:

  • (Boolean)


124
125
126
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 124

def self.namespace_declaration?(attr_name)
  Canon::Xml::NamespaceHelper.namespace_declaration?(attr_name)
end