Class: Moxml::SAX::ElementHandler
- 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
Instance Attribute Summary collapse
-
#current_path ⇒ Array<String>
readonly
Current path from root to current element.
-
#element_stack ⇒ Array<String>
readonly
Stack of currently open elements.
Instance Method Summary collapse
-
#current_element ⇒ String?
Get the name of the current (innermost) element.
-
#depth ⇒ Integer
Get current depth in the document tree.
-
#in_element?(name) ⇒ Boolean
Check if currently inside an element with the given name.
-
#initialize ⇒ ElementHandler
constructor
A new instance of ElementHandler.
-
#on_end_element(name) ⇒ void
Removes element from stack before calling super.
-
#on_start_element(name, attributes = {}, namespaces = {}) ⇒ void
Tracks element on stack before calling super.
-
#parent_element ⇒ String?
Get the name of the parent element.
-
#path_matches?(pattern) ⇒ Boolean
Check if current path matches a pattern.
-
#path_string(separator = "/") ⇒ String
Get the full path as a string.
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
#initialize ⇒ ElementHandler
Returns a new instance of ElementHandler.
30 31 32 33 34 |
# File 'lib/moxml/sax/element_handler.rb', line 30 def initialize super @element_stack = [] @current_path = [] end |
Instance Attribute Details
#current_path ⇒ Array<String> (readonly)
Returns Current path from root to current element.
28 29 30 |
# File 'lib/moxml/sax/element_handler.rb', line 28 def current_path @current_path end |
#element_stack ⇒ Array<String> (readonly)
Returns Stack of currently open elements.
25 26 27 |
# File 'lib/moxml/sax/element_handler.rb', line 25 def element_stack @element_stack end |
Instance Method Details
#current_element ⇒ String?
Get the name of the current (innermost) element
73 74 75 |
# File 'lib/moxml/sax/element_handler.rb', line 73 def current_element @element_stack.last end |
#depth ⇒ Integer
Get current depth in the document tree
91 92 93 |
# File 'lib/moxml/sax/element_handler.rb', line 91 def depth @element_stack.length end |
#in_element?(name) ⇒ Boolean
Check if currently inside an element with the given name
64 65 66 |
# File 'lib/moxml/sax/element_handler.rb', line 64 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
52 53 54 55 56 |
# File 'lib/moxml/sax/element_handler.rb', line 52 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
42 43 44 45 46 |
# File 'lib/moxml/sax/element_handler.rb', line 42 def on_start_element(name, attributes = {}, namespaces = {}) @element_stack.push(name) @current_path.push(name) super end |
#parent_element ⇒ String?
Get the name of the parent element
82 83 84 |
# File 'lib/moxml/sax/element_handler.rb', line 82 def parent_element @element_stack[-2] end |
#path_matches?(pattern) ⇒ Boolean
Check if current path matches a pattern
102 103 104 105 106 107 108 109 |
# File 'lib/moxml/sax/element_handler.rb', line 102 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
117 118 119 |
# File 'lib/moxml/sax/element_handler.rb', line 117 def path_string(separator = "/") separator + @current_path.join(separator) end |