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=, #unlinked_nodes

Constructor Details

#initialize(ptr) ⇒ XmlDocument

Returns a new instance of XmlDocument.



65
66
67
68
# File 'lib/nokogiri/ffi/structs/xml_document.rb', line 65

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

Class Method Details

.release(ptr) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nokogiri/ffi/structs/xml_document.rb', line 70

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)
      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