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.
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_path ⇒ Array<String> (readonly)
Returns 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_stack ⇒ Array<String> (readonly)
Returns 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_element ⇒ String?
Get the name of the current (innermost) element
75 76 77 |
# File 'lib/moxml/sax/element_handler.rb', line 75 def current_element @element_stack.last end |
#depth ⇒ Integer
Get current depth in the document tree
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
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
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
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_element ⇒ String?
Get the name of the parent element
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
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
119 120 121 |
# File 'lib/moxml/sax/element_handler.rb', line 119 def path_string(separator = "/") separator + @current_path.join(separator) end |