Class: Canon::Comparison::XmlComparatorHelpers::NamespaceComparator
- Inherits:
-
Object
- Object
- Canon::Comparison::XmlComparatorHelpers::NamespaceComparator
- 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
-
.add_namespace_difference(node1, node2, missing, extra, changed, opts, differences) ⇒ Object
Add a namespace declaration difference.
-
.compare(node1, node2, opts, differences) ⇒ Symbol
Compare namespace declarations between two nodes.
-
.extract_declarations(node) ⇒ Hash
Extract namespace declarations from a node.
-
.extract_from_array_attributes(raw_attrs, declarations) ⇒ Hash
Extract from array-format attributes.
-
.extract_from_hash_attributes(raw_attrs, declarations) ⇒ Hash
Extract from hash-format attributes.
-
.extract_from_namespace_nodes(namespace_nodes, declarations) ⇒ Hash
Extract from Canon::Xml::Node namespace_nodes.
- .namespace_declaration?(attr_name) ⇒ Boolean
Class Method Details
.add_namespace_difference(node1, node2, missing, extra, changed, opts, differences) ⇒ Object
Add a namespace declaration difference
136 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 167 168 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 136 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 18 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 43 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 88 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 108 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
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 71 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
123 124 125 |
# File 'lib/canon/comparison/xml_comparator/namespace_comparator.rb', line 123 def self.namespace_declaration?(attr_name) Canon::Xml::NamespaceHelper.namespace_declaration?(attr_name) end |