Class: Lutaml::Xsd::Validation::XmlNavigator
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::XmlNavigator
- Defined in:
- lib/lutaml/xsd/validation/xml_navigator.rb
Overview
XmlNavigator wraps Moxml for XML navigation with XPath tracking
This class provides a consistent interface for navigating XML documents while maintaining context for validation. It tracks the current position in the document tree and provides XPath information for error reporting.
Instance Attribute Summary collapse
-
#current_path ⇒ Object
readonly
Returns the value of attribute current_path.
-
#moxml_document ⇒ Object
readonly
Returns the value of attribute moxml_document.
Instance Method Summary collapse
-
#at_root? ⇒ Boolean
Check if currently at root.
-
#current_xpath ⇒ String
Get the current XPath location.
-
#depth ⇒ Integer
Get the depth of the current position.
-
#document ⇒ XmlDocument
Get the XML document.
-
#initialize(xml_content, adapter: :nokogiri) ⇒ XmlNavigator
constructor
Initialize a new XmlNavigator.
-
#inspect ⇒ String
Detailed string representation.
-
#parent_xpath ⇒ String?
Get the parent XPath.
-
#reset ⇒ void
Reset navigation to root.
-
#root_element ⇒ XmlElement?
Get the root element.
-
#to_s ⇒ String
Convert to string representation.
-
#with_element(element) { ... } ⇒ Object
Execute a block within an element’s context.
-
#with_indexed_element(element, index) { ... } ⇒ Object
Navigate to a specific element by index.
-
#xpath(xpath_expr) ⇒ Array<XmlElement>
Find elements by XPath expression.
Constructor Details
#initialize(xml_content, adapter: :nokogiri) ⇒ XmlNavigator
Initialize a new XmlNavigator
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 36 def initialize(xml_content, adapter: :nokogiri) raise ArgumentError, "XML content cannot be nil" if xml_content.nil? if xml_content.empty? raise ArgumentError, "XML content cannot be empty" end @moxml_document = parse_xml(xml_content, adapter) @current_path = [] end |
Instance Attribute Details
#current_path ⇒ Object (readonly)
Returns the value of attribute current_path.
27 28 29 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 27 def current_path @current_path end |
#moxml_document ⇒ Object (readonly)
Returns the value of attribute moxml_document.
27 28 29 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 27 def moxml_document @moxml_document end |
Instance Method Details
#at_root? ⇒ Boolean
Check if currently at root
121 122 123 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 121 def at_root? @current_path.empty? end |
#current_xpath ⇒ String
Get the current XPath location
Returns the XPath of the current position in the document tree based on the navigation context.
68 69 70 71 72 73 74 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 68 def current_xpath return "/" if @current_path.empty? "/" + @current_path.map.with_index do |segment, idx| format_xpath_segment(segment, idx) end.join("/") end |
#depth ⇒ Integer
Get the depth of the current position
114 115 116 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 114 def depth @current_path.length end |
#document ⇒ XmlDocument
Get the XML document
51 52 53 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 51 def document @document ||= XmlDocument.new(@moxml_document, self) end |
#inspect ⇒ String
Detailed string representation
161 162 163 164 165 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 161 def inspect "#<#{self.class.name} " \ "current_xpath=#{current_xpath.inspect} " \ "depth=#{depth}>" end |
#parent_xpath ⇒ String?
Get the parent XPath
128 129 130 131 132 133 134 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 128 def parent_xpath return nil if @current_path.length <= 1 "/" + @current_path[0..-2].map.with_index do |segment, idx| format_xpath_segment(segment, idx) end.join("/") end |
#reset ⇒ void
This method returns an undefined value.
Reset navigation to root
139 140 141 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 139 def reset @current_path.clear end |
#root_element ⇒ XmlElement?
Get the root element
58 59 60 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 58 def root_element document.root_element end |
#to_s ⇒ String
Convert to string representation
154 155 156 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 154 def to_s "XmlNavigator(xpath: #{current_xpath})" end |
#with_element(element) { ... } ⇒ Object
Execute a block within an element’s context
This method maintains the navigation path stack, adding the element to the path before executing the block and removing it afterwards.
90 91 92 93 94 95 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 90 def with_element(element) @current_path.push(build_path_segment(element)) yield ensure @current_path.pop end |
#with_indexed_element(element, index) { ... } ⇒ Object
Navigate to a specific element by index
103 104 105 106 107 108 109 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 103 def with_indexed_element(element, index) segment = build_path_segment(element, index) @current_path.push(segment) yield ensure @current_path.pop end |
#xpath(xpath_expr) ⇒ Array<XmlElement>
Find elements by XPath expression
147 148 149 |
# File 'lib/lutaml/xsd/validation/xml_navigator.rb', line 147 def xpath(xpath_expr) document.xpath(xpath_expr) end |