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
- .algorithm_shape_inclusive10?(node_or_xml) ⇒ Boolean
-
.canonicalize(node_or_xml, with_comments: false, algorithm: :inclusive10, inclusive_namespaces: []) ⇒ Object
Canonicalize using the named algorithm.
- .canonicalize_exclusive(node_or_xml, with_comments: false, inclusive_namespaces: []) ⇒ Object
- .canonicalize_inclusive10(node_or_xml, with_comments: false) ⇒ Object
- .canonicalize_inclusive11(node_or_xml, with_comments: false) ⇒ Object
-
.canonicalize_subset(node_or_xml, xpath, with_comments: false) ⇒ Object
Canonicalize an XPath-selected subset of the document (spec §3).
-
.equivalent?(left, right, with_comments: false, algorithm: :inclusive10, inclusive_namespaces: []) ⇒ Boolean
Compare two XML inputs by their canonical forms.
- .escape_attribute(value) ⇒ Object
- .escape_text(text) ⇒ Object
-
.native_inclusive10(node_or_xml, algorithm, with_comments, inclusive_namespaces) ⇒ Object
The leptris engine canonicalizes in C — 23x the Ruby reference.
Class Method Details
.algorithm_shape_inclusive10?(node_or_xml) ⇒ Boolean
70 71 72 |
# File 'lib/moxml/c14n.rb', line 70 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, algorithm, 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
99 100 101 102 103 104 105 106 |
# File 'lib/moxml/c14n.rb', line 99 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
74 75 76 |
# File 'lib/moxml/c14n.rb', line 74 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
95 96 97 |
# File 'lib/moxml/c14n.rb', line 95 def self.canonicalize_inclusive11(node_or_xml, with_comments: false) Inclusive11.new.canonicalize(node_or_xml, with_comments: with_comments) end |
.canonicalize_subset(node_or_xml, xpath, with_comments: false) ⇒ Object
Canonicalize an XPath-selected subset of the document (spec §3). The node-set is whatever the expression matches (elements, attributes, text): matched elements render with their namespaces, attributes and descendants; matched attributes render on their owner element alone. Enveloped-signature flows use this to digest the document minus the Signature element without deep-copying.
85 86 87 88 89 90 91 92 93 |
# File 'lib/moxml/c14n.rb', line 85 def self.canonicalize_subset(node_or_xml, xpath, with_comments: false) source = node_or_xml.is_a?(String) ? ::Moxml.parse(node_or_xml) : node_or_xml root = DataModel.from_node(source) adapter = source.context.config.adapter matched = source.xpath(xpath).to_a paths = matched.filter_map { |wrapper| DataModel.path_key_for(wrapper, source, adapter) } DataModel.mark_subset_paths(root, paths) Processor.new(with_comments: with_comments).process(root) end |
.equivalent?(left, right, with_comments: false, algorithm: :inclusive10, inclusive_namespaces: []) ⇒ Boolean
Compare two XML inputs by their canonical forms.
109 110 111 112 113 114 115 |
# File 'lib/moxml/c14n.rb', line 109 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
121 122 123 |
# File 'lib/moxml/c14n.rb', line 121 def self.escape_attribute(value) CharacterEncoder.new.encode_attribute(value) end |
.escape_text(text) ⇒ Object
117 118 119 |
# File 'lib/moxml/c14n.rb', line 117 def self.escape_text(text) CharacterEncoder.new.encode_text(text) end |
.native_inclusive10(node_or_xml, algorithm, 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 algorithm, no comments, no InclusiveNamespace prefix list) on wrappers whose adapter passed the byte-safety 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 67 68 |
# File 'lib/moxml/c14n.rb', line 61 def self.native_inclusive10(node_or_xml, algorithm, with_comments, inclusive_namespaces) return nil unless algorithm.nil? || algorithm == :inclusive10 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 |