Class: WSDL::Security::XmlBuilderHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl/security/xml_builder_helper.rb

Overview

Helper for building XML with or without explicit namespace prefixes.

Some SOAP servers have strict XML parsers that only accept elements with explicit namespace prefixes (e.g., ds:Signature instead of Signature xmlns="..."). This helper provides a unified interface for building XML that works in both modes.

Examples:

Building with default namespaces

helper = XmlBuilderHelper.new(explicit_prefixes: false)
helper.build_element(xml, :ds, 'Signature') do
  helper.build_child(xml, :ds, 'SignedInfo')
end
# => <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
#      <SignedInfo/>
#    </Signature>

Building with explicit prefixes

helper = XmlBuilderHelper.new(explicit_prefixes: true)
helper.build_element(xml, :ds, 'Signature') do
  helper.build_child(xml, :ds, 'SignedInfo')
end
# => <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
#      <ds:SignedInfo/>
#    </ds:Signature>

Constant Summary collapse

SecurityNS =

Local aliases for namespace constants

Constants::NS::Security
SignatureNS =

Alias for XML Signature namespace constants.

Returns:

  • (Module)
Constants::NS::Signature
NAMESPACE_URIS =

Namespace prefix to URI mapping

{
  ds: SignatureNS::DS,
  wsse: SecurityNS::WSSE,
  wsu: SecurityNS::WSU,
  ec: SignatureNS::EC
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(explicit_prefixes: false) ⇒ XmlBuilderHelper

Creates a new XmlBuilderHelper instance.

Parameters:

  • explicit_prefixes (Boolean) (defaults to: false)

    whether to use explicit namespace prefixes



57
58
59
# File 'lib/wsdl/security/xml_builder_helper.rb', line 57

def initialize(explicit_prefixes: false)
  @explicit_prefixes = explicit_prefixes
end

Instance Attribute Details

#explicit_prefixesBoolean (readonly)

Returns whether explicit namespace prefixes are enabled.

Returns:

  • (Boolean)


51
52
53
# File 'lib/wsdl/security/xml_builder_helper.rb', line 51

def explicit_prefixes
  @explicit_prefixes
end

Instance Method Details

#build_child(xml, ns_prefix, element_name, content_or_attributes = nil, attributes = {}) { ... } ⇒ Object

Builds a child element (inherits namespace from parent).

Parameters:

  • xml (Nokogiri::XML::Builder)

    the XML builder

  • ns_prefix (Symbol)

    the namespace prefix (:ds, :wsse, :wsu, :ec)

  • element_name (String, Symbol)

    the element name

  • content_or_attributes (String, Hash, nil) (defaults to: nil)

    text content or attributes

  • attributes (Hash) (defaults to: {})

    attributes (when content is provided)

Yields:

  • the block for building child elements



82
83
84
85
# File 'lib/wsdl/security/xml_builder_helper.rb', line 82

def build_child(xml, ns_prefix, element_name, content_or_attributes = nil, attributes = {}, &)
  content, attrs = resolve_arguments(content_or_attributes, attributes)
  invoke(xml, ns_prefix, element_name, attrs, content, &)
end

#build_child_with_ns(xml, ns_prefix, element_name, attributes = {}) { ... } ⇒ Object

Builds a child element with explicit namespace declaration.

Parameters:

  • xml (Nokogiri::XML::Builder)

    the XML builder

  • ns_prefix (Symbol)

    the namespace prefix

  • element_name (String, Symbol)

    the element name

  • attributes (Hash) (defaults to: {})

    additional attributes

Yields:

  • the block for building child elements



95
96
97
# File 'lib/wsdl/security/xml_builder_helper.rb', line 95

def build_child_with_ns(xml, ns_prefix, element_name, attributes = {}, &)
  invoke(xml, ns_prefix, element_name, ns_attributes(ns_prefix).merge(attributes), &)
end

#build_element(xml, ns_prefix, element_name, attributes = {}) { ... } ⇒ Object

Builds a root element with namespace declaration.

Parameters:

  • xml (Nokogiri::XML::Builder)

    the XML builder

  • ns_prefix (Symbol)

    the namespace prefix (:ds, :wsse, :wsu, :ec)

  • element_name (String, Symbol)

    the element name

  • attributes (Hash) (defaults to: {})

    additional attributes

Yields:

  • the block for building child elements



69
70
71
# File 'lib/wsdl/security/xml_builder_helper.rb', line 69

def build_element(xml, ns_prefix, element_name, attributes = {}, &)
  invoke(xml, ns_prefix, element_name, ns_attributes(ns_prefix).merge(attributes), &)
end