Module: Moxml::SAX::NamespaceSplitter

Overview

Splits a flat attribute hash into regular attributes and namespace declarations.

Every adapter SAX bridge performs the same split: attributes whose names start with "xmlns" are namespace declarations, everything else is a regular attribute. This module provides a single implementation.

Constant Summary collapse

EMPTY_NAMESPACES =

Shared for the overwhelmingly common no-declaration element: one Hash allocation per start_element event saved. Frozen — event hashes are read-only data, not scratch.

Yield Parameters:

  • value (Object)

    raw attribute/namespace value

Yield Returns:

  • (Object)

    transformed value to store

Returns:

  • (Array(Hash, Hash))

    [regular_attrs, namespaces]

{}.freeze

Instance Method Summary collapse

Instance Method Details

#split_attributes_and_namespaces(attributes) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moxml/sax/namespace_splitter.rb', line 20

def split_attributes_and_namespaces(attributes)
  attrs = {}
  ns = nil

  each_attribute(attributes) do |name, value|
    name_s = name.to_s
    if name_s.start_with?("xmlns")
      if name_s == "xmlns"
        (ns ||= {})[nil] = block_given? ? yield(value) : value
      elsif name_s.bytesize > 5 && name_s.getbyte(5) == 58 # ":"
        (ns ||= {})[name_s[6..]] = block_given? ? yield(value) : value
      else
        attrs[name_s] = block_given? ? yield(value) : value
      end
    else
      attrs[name_s] = block_given? ? yield(value) : value
    end
  end

  [attrs, ns || EMPTY_NAMESPACES]
end