Sha256: 50f1683219b7c66e5d5762b55673de34f7abeab3350164836e3c58ce0528a2d6

Contents?: true

Size: 1.98 KB

Versions: 229

Compression:

Stored size: 1.98 KB

Contents

require 'nokogiri'

class AbstractXMLInstantiator
    
  class Visitor < Nokogiri::XML::SAX::Document
    
    def initialize(inst, gcSuspendCount)
      @instantiator = inst
      @gcSuspendCount = gcSuspendCount
      @namespaces = {}
    end
    
    def start_element_namespace(tag, attributes, prefix, uri, ns)
      controlGC
      ns.each{|n| @namespaces[n[0]] = n[1]}
      attrs = attributes.collect{|a| [a.prefix ? a.prefix+":"+a.localname : a.localname, a.value]}
      @instantiator.start_tag(prefix, tag, @namespaces, Hash[*(attrs.flatten)])
      attrs.each { |pair| @instantiator.set_attribute(pair[0], pair[1]) }
    end
    
    def end_element_namespace(tag, prefix, uri)
      @instantiator.end_tag(prefix, tag)
    end
    
    def characters(str)
      @instantiator.text(str)
    end
    
    def controlGC
      return unless @gcSuspendCount > 0
      @gcCounter ||= 0
      @gcCounter += 1
      if @gcCounter == @gcSuspendCount
        @gcCounter = 0
        GC.enable
        ObjectSpace.garbage_collect
        GC.disable 
      end 
    end
  end

  # Parses str and calls start_tag, end_tag, set_attribute and text methods of a subclass.
  # 
  # If gcSuspendCount is specified, the garbage collector will be disabled for that
  # number of start or end tags. After that period it will clean up and then be disabled again.
  # A value of about 1000 can significantly improve overall performance.
  # The memory usage normally does not increase.
  # Depending on the work done for every xml tag the value might have to be adjusted.
  # 
  def instantiate(str, gcSuspendCount=0)
    gcDisabledBefore = GC.disable
    gcSuspendCount = 0 if gcDisabledBefore
    begin
      visitor = Visitor.new(self, gcSuspendCount)
      parser = Nokogiri::XML::SAX::Parser.new(visitor)
      parser.parse(str) do |ctx|
        @parserContext = ctx
      end
     ensure 
      GC.enable unless gcDisabledBefore
    end
  end
  
  def text(str)
  end
end

Version data entries

229 entries across 229 versions & 3 rubygems

Version Path
rgen-0.10.2 lib/rgen/instantiator/abstract_xml_instantiator.rb
rgen-0.10.0 lib/rgen/instantiator/abstract_xml_instantiator.rb
rgen-0.9.1 ./lib/rgen/instantiator/abstract_xml_instantiator.rb
rgen-0.8.3 lib/rgen/instantiator/abstract_xml_instantiator.rb
rgen-0.8.4 lib/rgen/instantiator/abstract_xml_instantiator.rb
rgen-0.9.0 lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.12 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.12-x86-mingw32 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.12-x64-mingw32 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.12-universal-darwin lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.11 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.11-x86-mingw32 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.11-x64-mingw32 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.11-universal-darwin lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.10 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.10-x86-mingw32 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.10-x64-mingw32 lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb
puppet-4.10.10-universal-darwin lib/puppet/vendor/rgen/lib/rgen/instantiator/abstract_xml_instantiator.rb