Class: Moxml::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter = nil) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/moxml/context.rb', line 7

def initialize(adapter = nil)
  @config = Config.new(adapter)
  # Bumped on any namespace-scope mutation; Element scope caches
  # compare against it so every wrapper sees changes without
  # needing wrapper identity.
  @namespace_scope_generation = 0
  # Native → wrapper identity map: repeated traversals hand back
  # the same wrapper instead of allocating a fresh one per
  # access. Keyed by object identity; re-keyed by
  # Node#refresh_native! when an adapter swaps a native.
  @wrappers = {}.compare_by_identity
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/moxml/context.rb', line 5

def config
  @config
end

Instance Method Details

#build(&block) ⇒ Object



119
120
121
# File 'lib/moxml/context.rb', line 119

def build(&block)
  Builder.new(self).build(&block)
end

#bump_namespace_scope_generationObject



43
44
45
# File 'lib/moxml/context.rb', line 43

def bump_namespace_scope_generation
  @namespace_scope_generation += 1
end

#create_document(native_doc = nil) ⇒ Object



51
52
53
# File 'lib/moxml/context.rb', line 51

def create_document(native_doc = nil)
  Document.new(config.adapter.create_document(native_doc), self)
end

#entity_registryObject



47
48
49
# File 'lib/moxml/context.rb', line 47

def entity_registry
  @entity_registry ||= build_entity_registry
end

#materialize(xml, options = {}, &block) ⇒ Object

Parse then flatten in one call — see Moxml::Materializer (issue #132). Yields records; returns an Enumerator when no block is given.



80
81
82
# File 'lib/moxml/context.rb', line 80

def materialize(xml, options = {}, &block)
  parse(xml, options).materialize(&block)
end

#namespace_scope_generationObject



39
40
41
# File 'lib/moxml/context.rb', line 39

def namespace_scope_generation
  @namespace_scope_generation
end

#parse(xml, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/moxml/context.rb', line 55

def parse(xml, options = {})
  xml_string = if xml.is_a?(String)
                 xml
               else
                 xml.read.tap do
                   xml.rewind if xml.is_a?(IO) || xml.is_a?(StringIO)
                 end
               end
  # Allocation-free declaration sniff: String#strip would copy the
  # whole buffer just to test its prefix.
  has_declaration = xml_string.match?(/\A[ \t\r\n\f\v\0]*<\?xml/)

  # Parse with adapter, passing self (context) so adapter can use our config
  parsed_options = default_options.merge(options)
  doc = config.adapter.parse(xml_string, parsed_options, self)

  # Set declaration flag on Document wrapper (proper OOP)
  doc.has_xml_declaration = has_declaration if doc.is_a?(Document)

  doc
end

#register_wrapper(native, wrapper) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/moxml/context.rb', line 24

def register_wrapper(native, wrapper)
  # Safety valve + adapter opt-in. Adapters whose natives are
  # recreated per access (libxml mints fresh Ruby objects for the
  # same C node) opt out so the map does not accumulate dead
  # entries; the default is opt-in.
  return if @config&.adapter&.wrappers_recyclable? == false

  @wrappers.clear if @wrappers.size >= 65_536
  @wrappers[native] = wrapper
end

#sax_parse(xml, handler = nil) {|block| ... } ⇒ void

This method returns an undefined value.

Parse XML using SAX (event-driven) parsing

SAX parsing is memory-efficient and suitable for large XML files. Provide either a handler object or a block with DSL.

Examples:

With handler object

handler = MyHandler.new
context.sax_parse(xml_string, handler)

With block

context.sax_parse(xml_string) do
  start_element { |name, attrs| puts name }
  characters { |text| puts text }
end

Parameters:

  • xml (String, IO)

    XML string or IO object to parse

  • handler (Moxml::SAX::Handler, nil) (defaults to: nil)

    Handler object (optional if block given)

Yields:

  • (block)

    DSL block for defining handlers (optional if handler given)

Raises:

  • (ArgumentError)

    if neither handler nor block is provided



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/moxml/context.rb', line 105

def sax_parse(xml, handler = nil, &block)
  # Create block handler if block given
  handler ||= SAX::BlockHandler.new(&block) if block

  # Validate handler
  raise ArgumentError, "Handler or block required" unless handler
  unless handler.is_a?(SAX::Handler)
    raise ArgumentError, "Handler must inherit from Moxml::SAX::Handler"
  end

  # Delegate to adapter
  config.adapter.sax_parse(xml, handler)
end

#unregister_wrapper(native) ⇒ Object



35
36
37
# File 'lib/moxml/context.rb', line 35

def unregister_wrapper(native)
  @wrappers.delete(native)
end

#wrapper_for(native) ⇒ Object



20
21
22
# File 'lib/moxml/context.rb', line 20

def wrapper_for(native)
  @wrappers[native]
end