Class: Moxml::Document

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

Constant Summary

Constants inherited from Node

Node::TYPES

Instance Attribute Summary collapse

Attributes inherited from Node

#context, #native, #parent_node

Instance Method Summary collapse

Methods inherited from Node

#==, adapter, #add_next_sibling, #add_previous_sibling, #after, #ancestors, #before, #blank?, #children, #content, #descendants, #dup, #each, #each_node, #first_child, #following_siblings, #has_children?, #identifier, #invalidate_namespace_cache!, #last_child, #line_number, #namespace, #namespaces, #next_sibling, node_type_map, #outer_xml, #parent, #path, #preceding_siblings, #previous_sibling, #refresh_native!, #remove, #replace, #text, #to_xml, wrap

Methods included from XmlUtils

#encode_entities, #normalize_xml_value, #validate_comment_content, #validate_declaration_encoding, #validate_declaration_standalone, #validate_declaration_version, #validate_element_name, #validate_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri

Constructor Details

#initialize(native, context) ⇒ Document

Returns a new instance of Document.



7
8
9
10
# File 'lib/moxml/document.rb', line 7

def initialize(native, context)
  super
  @has_xml_declaration = false
end

Instance Attribute Details

#has_xml_declarationObject

Returns the value of attribute has_xml_declaration.



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

def has_xml_declaration
  @has_xml_declaration
end

Instance Method Details

#add_child(node) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/moxml/document.rb', line 103

def add_child(node)
  node = prepare_node(node)

  if node.is_a?(Declaration)
    # Mark that document now has a declaration
    @has_xml_declaration = true
    adapter.add_child(@native, node.native)
  elsif root && !node.is_a?(ProcessingInstruction) && !node.is_a?(Comment) && !node.is_a?(Doctype)
    raise Error, "Document already has a root element"
  else
    adapter.add_child(@native, node.native)
    # Refresh native for adapters where identity changes (e.g., LibXML doc.root=)
    refreshed = adapter.actual_native(node.native, @native)
    node.refresh_native!(refreshed) if refreshed && refreshed != node.native
  end
  node.parent_node = self
  invalidate_children_cache!
  self
end

#add_element(name, attributes = {}, &block) ⇒ Object

Quick element creation and addition



148
149
150
151
152
153
154
# File 'lib/moxml/document.rb', line 148

def add_element(name, attributes = {}, &block)
  elem = create_element(name)
  attributes.each { |k, v| elem[k] = v }
  add_child(elem)
  block&.call(elem)
  elem
end

#at_xpath(expression, namespaces = nil) ⇒ Object



141
142
143
144
145
# File 'lib/moxml/document.rb', line 141

def at_xpath(expression, namespaces = nil)
  if (native_node = adapter.at_xpath(@native, expression, namespaces))
    Moxml::Node.wrap(native_node, context)
  end
end

#create_cdata(content) ⇒ Object



71
72
73
# File 'lib/moxml/document.rb', line 71

def create_cdata(content)
  Cdata.new(adapter.create_cdata(content, owner_doc: @native), context)
end

#create_comment(content) ⇒ Object



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

def create_comment(content)
  Comment.new(adapter.create_comment(content, owner_doc: @native), context)
end

#create_declaration(version = "1.0", encoding = "UTF-8", standalone = nil) ⇒ Object



93
94
95
96
97
# File 'lib/moxml/document.rb', line 93

def create_declaration(version = "1.0", encoding = "UTF-8",
                       standalone = nil)
  decl = adapter.create_declaration(version, encoding, standalone)
  Declaration.new(decl, context)
end

#create_doctype(name, external_id, system_id) ⇒ Object



79
80
81
82
83
84
# File 'lib/moxml/document.rb', line 79

def create_doctype(name, external_id, system_id)
  Doctype.new(
    adapter.create_doctype(name, external_id, system_id),
    context,
  )
end

#create_element(name) ⇒ Object



63
64
65
# File 'lib/moxml/document.rb', line 63

def create_element(name)
  Element.new(adapter.create_element(name, owner_doc: @native), context)
end

#create_entity_reference(name) ⇒ Object



99
100
101
# File 'lib/moxml/document.rb', line 99

def create_entity_reference(name)
  EntityReference.new(adapter.create_entity_reference(name), context)
end

#create_processing_instruction(target, content) ⇒ Object



86
87
88
89
90
91
# File 'lib/moxml/document.rb', line 86

def create_processing_instruction(target, content)
  ProcessingInstruction.new(
    adapter.create_processing_instruction(target, content),
    context,
  )
end

#create_text(content) ⇒ Object



67
68
69
# File 'lib/moxml/document.rb', line 67

def create_text(content)
  Text.new(adapter.create_text(content, owner_doc: @native), context)
end

#documentObject



12
13
14
# File 'lib/moxml/document.rb', line 12

def document
  self
end

#find(xpath) ⇒ Object

Convenience find methods



157
158
159
# File 'lib/moxml/document.rb', line 157

def find(xpath)
  at_xpath(xpath)
end

#find_all(xpath) ⇒ Object



161
162
163
# File 'lib/moxml/document.rb', line 161

def find_all(xpath)
  xpath(xpath).to_a
end

#freeObject

Deterministically release the adapter's native memory for this document (issue #134) — batch workloads parsing thousands of documents otherwise hold C trees until GC finalizers run. GC-managed engines no-op. Further access raises the engine's use-after-free error; ordinary garbage-collected documents keep working via the finalizer either way.



48
49
50
51
# File 'lib/moxml/document.rb', line 48

def free
  adapter.free_document(@native)
  nil
end

#materialize(&block) ⇒ Object

Materialize the root subtree — see Moxml::Materializer.



28
29
30
31
32
# File 'lib/moxml/document.rb', line 28

def materialize(&block)
  return to_enum(:materialize) unless block

  root&.materialize(&block)
end

#materialize_fields(&block) ⇒ Object

Zero-allocation streaming form over the root subtree (issue #143) — see Moxml::Materializer.

Raises:

  • (ArgumentError)


36
37
38
39
40
# File 'lib/moxml/document.rb', line 36

def materialize_fields(&block)
  raise ArgumentError, "materialize_fields requires a block" unless block

  root&.materialize_fields(&block)
end

#parse_errorsObject

Parse diagnostics from the engine's recover path (issue #147): [] when the parse was clean, otherwise the recorded error messages. A non-strict leptris parse that came back empty reports the fatal error that emptied it; Nokogiri reports its recover-mode syntax errors; engines without an error channel answer [].



59
60
61
# File 'lib/moxml/document.rb', line 59

def parse_errors
  adapter.parse_errors(@native)
end

#rootObject



22
23
24
25
# File 'lib/moxml/document.rb', line 22

def root
  root_element = adapter.root(@native)
  root_element ? Moxml::Node.wrap(root_element, context) : nil
end

#root=(element) ⇒ Object



16
17
18
19
20
# File 'lib/moxml/document.rb', line 16

def root=(element)
  adapter.set_root(@native, element.native)
  element.parent_node = self
  invalidate_children_cache!
end

#xpath(expression, namespaces = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/moxml/document.rb', line 123

def xpath(expression, namespaces = nil)
  result = adapter.xpath(@native, expression, namespaces)

  # Handle different result types:
  # - Scalar values (from functions): return directly
  # - NodeSet: already wrapped, return directly
  # - Array: wrap in NodeSet
  case result
  when NodeSet, Float, String, TrueClass, FalseClass, NilClass
    result
  when Array
    NodeSet.new(result, context)
  else
    # For other types, try to wrap in NodeSet
    NodeSet.new(result, context)
  end
end