Class: Nokogiri::LibXML::XmlDocument

Inherits:
FFI::ManagedStruct
  • Object
show all
Includes:
XmlDocumentMixin
Defined in:
lib/nokogiri/ffi/structs/xml_document.rb

Overview

use at the point of creation, so we can be sure the document will be GCed properly

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XmlDocumentMixin

#alloc_tuple, #document, included, #ruby_doc, #ruby_doc=, #ruby_node_pointer, #ruby_node_pointer=, #unlinked_nodes

Constructor Details

#initialize(ptr) ⇒ XmlDocument

Returns a new instance of XmlDocument.



72
73
74
75
# File 'lib/nokogiri/ffi/structs/xml_document.rb', line 72

def initialize(ptr)
  super(ptr)
  self.alloc_tuple
end

Class Method Details

.release(ptr) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nokogiri/ffi/structs/xml_document.rb', line 77

def self.release ptr
  doc = LibXML::XmlDocumentCast.new(ptr)
  func = LibXML.xmlDeregisterNodeDefault(nil)
  begin
    ns  = LibXML::XmlNodeSetCast.new(doc[:_private].get_pointer(LibXML.pointer_offset(1)))

    ns[:nodeNr].times do |j|
      node_cstruct = LibXML::XmlNode.new(ns[:nodeTab].get_pointer(LibXML.pointer_offset(j)))
      case node_cstruct[:type]
      when Nokogiri::XML::Node::ATTRIBUTE_NODE
        LibXML.xmlFreePropList(node_cstruct)
      when Nokogiri::XML::Node::NAMESPACE_DECL
        LibXML.xmlFree(node_cstruct)
      else
        LibXML.xmlAddChild(doc, node_cstruct) if node_cstruct[:parent].null?
      end
    end
    LibXML::XmlNodeSet.release(ns.pointer)

    LibXML.free(doc[:_private])
  rescue
    puts "Nokogiri::LibXML::XmlDocument.release: exception: '#{$!}'"
  ensure
    LibXML.xmlFreeDoc(ptr)
    LibXML.xmlDeregisterNodeDefault(func)
  end
end