Class: Moxml::SAX::ElementHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/moxml/sax/element_handler.rb

Overview

Element-focused SAX handler with stack tracking

Extends the base Handler with utilities for tracking element context:

  • Element stack (current hierarchy)

  • Current path (array of element names from root)

  • Helper methods for checking context

Examples:

Using element context

class MyHandler < Moxml::SAX::ElementHandler
  def on_start_element(name, attributes = {}, namespaces = {})
    super # Important: call super to update stack

    if path_matches?(%r{/library/book/title$})
      puts "Found title at: #{current_path.join('/')}"
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Handler

#on_cdata, #on_characters, #on_comment, #on_end_document, #on_error, #on_processing_instruction, #on_start_document, #on_warning

Constructor Details

#initializeElementHandler

Returns a new instance of ElementHandler.



32
33
34
35
36
# File 'lib/moxml/sax/element_handler.rb', line 32

def initialize
  super
  @element_stack = []
  @current_path = []
end

Instance Attribute Details

#current_pathArray<String> (readonly)

Returns Current path from root to current element.

Returns:

  • (Array<String>)

    Current path from root to current element



30
31
32
# File 'lib/moxml/sax/element_handler.rb', line 30

def current_path
  @current_path
end

#element_stackArray<String> (readonly)

Returns Stack of currently open elements.

Returns:

  • (Array<String>)

    Stack of currently open elements



27
28
29
# File 'lib/moxml/sax/element_handler.rb', line 27

def element_stack
  @element_stack
end

Instance Method Details

#current_elementString?

Get the name of the current (innermost) element

Examples:

current_element # => "title"

Returns:

  • (String, nil)

    Current element name, or nil if at document level



75
76
77
# File 'lib/moxml/sax/element_handler.rb', line 75

def current_element
  @element_stack.last
end

#depthInteger

Get current depth in the document tree

Examples:

depth # => 3 (e.g., /library/book/title)

Returns:

  • (Integer)

    Current nesting level (0 at document root)



93
94
95
# File 'lib/moxml/sax/element_handler.rb', line 93

def depth
  @element_stack.length
end

#in_element?(name) ⇒ Boolean

Check if currently inside an element with the given name

Examples:

in_element?("book") # true if inside any <book> element

Parameters:

  • name (String)

    Element name to check

Returns:

  • (Boolean)

    true if inside the element



66
67
68
# File 'lib/moxml/sax/element_handler.rb', line 66

def in_element?(name)
  @element_stack.include?(name)
end

#on_end_element(name) ⇒ void

This method returns an undefined value.

Removes element from stack before calling super

Parameters:

  • name (String)

    Element name



54
55
56
57
58
# File 'lib/moxml/sax/element_handler.rb', line 54

def on_end_element(name)
  @element_stack.pop
  @current_path.pop
  super
end

#on_start_element(name, attributes = {}, namespaces = {}) ⇒ void

This method returns an undefined value.

Tracks element on stack before calling super

Parameters:

  • name (String)

    Element name

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

    Element attributes

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

    Namespace declarations



44
45
46
47
48
# File 'lib/moxml/sax/element_handler.rb', line 44

def on_start_element(name, attributes = {}, namespaces = {})
  @element_stack.push(name)
  @current_path.push(name)
  super
end

#parent_elementString?

Get the name of the parent element

Examples:

parent_element # => "book"

Returns:

  • (String, nil)

    Parent element name, or nil if no parent



84
85
86
# File 'lib/moxml/sax/element_handler.rb', line 84

def parent_element
  @element_stack[-2]
end

#path_matches?(pattern) ⇒ Boolean

Check if current path matches a pattern

Examples:

path_matches?(/book\/title$/) # true if at /*/book/title
path_matches?("/library/book/title") # exact path match

Parameters:

  • pattern (String, Regexp)

    Pattern to match against path

Returns:

  • (Boolean)

    true if path matches



104
105
106
107
108
109
110
111
# File 'lib/moxml/sax/element_handler.rb', line 104

def path_matches?(pattern)
  path_str = "/#{@current_path.join('/')}"
  if pattern.is_a?(Regexp)
    !path_str.match?(pattern).nil?
  else
    path_str == pattern.to_s
  end
end

#path_string(separator = "/") ⇒ String

Get the full path as a string

Examples:

path_string # => "/library/book/title"

Parameters:

  • separator (String) (defaults to: "/")

    Path separator (default: “/”)

Returns:

  • (String)

    Full path string



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

def path_string(separator = "/")
  separator + @current_path.join(separator)
end