Module: Moxml::C14n

Defined in:
lib/moxml/c14n.rb,
lib/moxml/c14n/node.rb,
lib/moxml/c14n/nodes.rb,
lib/moxml/c14n/writer.rb,
lib/moxml/c14n/exclusive.rb,
lib/moxml/c14n/processor.rb,
lib/moxml/c14n/data_model.rb,
lib/moxml/c14n/inclusive_10.rb,
lib/moxml/c14n/inclusive_11.rb,
lib/moxml/c14n/nodes/root_node.rb,
lib/moxml/c14n/nodes/text_node.rb,
lib/moxml/c14n/xml_base_handler.rb,
lib/moxml/c14n/attribute_handler.rb,
lib/moxml/c14n/character_encoder.rb,
lib/moxml/c14n/namespace_context.rb,
lib/moxml/c14n/namespace_handler.rb,
lib/moxml/c14n/nodes/comment_node.rb,
lib/moxml/c14n/nodes/element_node.rb,
lib/moxml/c14n/nodes/attribute_node.rb,
lib/moxml/c14n/nodes/namespace_node.rb,
lib/moxml/c14n/nodes/processing_instruction_node.rb

Overview

Canonicalization engine. Core moxml feature — sibling to Moxml::XPath, Moxml::Builder, Moxml::SAX.

Inclusive C14N (1.0 and 1.1) is ported from canon (lutaml/canon): mature, tested, full-featured (xml:base fixup, inheritable xml:* attribute resolution, node-set subset canonicalization).

Exclusive C14N (1.0) is a moxml-native implementation; canon does not implement exclusive. It is required by XML signature to keep signatures valid when subdocuments are moved between contexts.

Defined Under Namespace

Modules: Nodes Classes: AttributeHandler, CharacterEncoder, DataModel, Exclusive, Inclusive10, Inclusive11, NamespaceContext, NamespaceHandler, Node, Processor, Writer, XmlBaseHandler

Constant Summary collapse

XMLNS_URI =
"http://www.w3.org/2000/xmlns/"
XML_URI =
"http://www.w3.org/XML/1998/namespace"

Class Method Summary collapse

Class Method Details

.algorithm_shape_inclusive10?(node_or_xml) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/moxml/c14n.rb', line 68

def self.algorithm_shape_inclusive10?(node_or_xml)
  node_or_xml.is_a?(Moxml::Element) || node_or_xml.is_a?(Moxml::Document)
end

.canonicalize(node_or_xml, with_comments: false, algorithm: :inclusive10, inclusive_namespaces: []) ⇒ Object

Canonicalize using the named algorithm.

algorithm is one of:

:inclusive10  Canonical XML 1.0 (default; W3C REC-xml-c14n-20010315)
:inclusive11  Canonical XML 1.1 (W3C REC-xml-c14n11-20080502)
:exclusive10  Exclusive C14N 1.0 (W3C REC-xml-exc-c14n-20020718)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/moxml/c14n.rb', line 40

def self.canonicalize(node_or_xml, with_comments: false,
                      algorithm: :inclusive10, inclusive_namespaces: [])
  if (native = native_inclusive10(node_or_xml, with_comments,
                                  inclusive_namespaces))
    return native
  end

  engine_for(algorithm).canonicalize(
    node_or_xml,
    with_comments: with_comments,
    inclusive_namespaces: inclusive_namespaces,
  )
end

.canonicalize_exclusive(node_or_xml, with_comments: false, inclusive_namespaces: []) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/moxml/c14n.rb', line 80

def self.canonicalize_exclusive(node_or_xml, with_comments: false,
                                inclusive_namespaces: [])
  Exclusive.new.canonicalize(
    node_or_xml,
    with_comments: with_comments,
    inclusive_namespaces: inclusive_namespaces,
  )
end

.canonicalize_inclusive10(node_or_xml, with_comments: false) ⇒ Object



72
73
74
# File 'lib/moxml/c14n.rb', line 72

def self.canonicalize_inclusive10(node_or_xml, with_comments: false)
  Inclusive10.new.canonicalize(node_or_xml, with_comments: with_comments)
end

.canonicalize_inclusive11(node_or_xml, with_comments: false) ⇒ Object



76
77
78
# File 'lib/moxml/c14n.rb', line 76

def self.canonicalize_inclusive11(node_or_xml, with_comments: false)
  Inclusive11.new.canonicalize(node_or_xml, with_comments: with_comments)
end

.equivalent?(left, right, with_comments: false, algorithm: :inclusive10, inclusive_namespaces: []) ⇒ Boolean

Compare two XML inputs by their canonical forms.

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/moxml/c14n.rb', line 90

def self.equivalent?(left, right, with_comments: false,
                     algorithm: :inclusive10, inclusive_namespaces: [])
  canonicalize(left, with_comments: with_comments, algorithm: algorithm,
                     inclusive_namespaces: inclusive_namespaces) ==
    canonicalize(right, with_comments: with_comments, algorithm: algorithm,
                        inclusive_namespaces: inclusive_namespaces)
end

.escape_attribute(value) ⇒ Object



102
103
104
# File 'lib/moxml/c14n.rb', line 102

def self.escape_attribute(value)
  CharacterEncoder.new.encode_attribute(value)
end

.escape_text(text) ⇒ Object



98
99
100
# File 'lib/moxml/c14n.rb', line 98

def self.escape_text(text)
  CharacterEncoder.new.encode_text(text)
end

.native_inclusive10(node_or_xml, with_comments, inclusive_namespaces) ⇒ Object

The leptris engine canonicalizes in C — 23x the Ruby reference. Delegated only for the exact default shape (Inclusive 1.0, no comments, no InclusiveNamespace prefix list) on wrappers whose adapter passed the NATIVE_C14N_BYTE_SAFE load probe; every other combination keeps the Ruby engine (node-set subsets, xml:base fixup, exclusive, 1.1 stay on the ported implementation regardless).



61
62
63
64
65
66
# File 'lib/moxml/c14n.rb', line 61

def self.native_inclusive10(node_or_xml, with_comments, inclusive_namespaces)
  return nil if with_comments || !inclusive_namespaces.empty?
  return nil unless algorithm_shape_inclusive10?(node_or_xml)

  node_or_xml.context.config.adapter.native_inclusive10(node_or_xml.native)
end