Sha256: d4ad466fd6eb8c3f1416f6a67ae732de91fc2768d84e398ade7feb951e8f71ce

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'nokogiri'

module Jacoco
  # Sax parser for quickly finding class elements in Jacoco report
  class SAXParser < Nokogiri::XML::SAX::Document
    attr_accessor :class_names
    attr_accessor :classes

    def initialize(classes)
      @class_names      = classes
      @classes          = []
      @current_class    = nil
      @subelement_index = 0
    end

    def start_element(name, attrs = [])
      case name
      when 'class'
        start_class(attrs)
      when 'counter'
        start_counter(attrs)
      end

      @subelement_index += 1
    end

    def start_counter(attrs)
      return unless !@current_class.nil? && @subelement_index == 1

      counter         = Jacoco::Counter.new
      counter.type    = Hash[attrs]['type']
      counter.missed  = Hash[attrs]['missed'].to_i
      counter.covered = Hash[attrs]['covered'].to_i

      @current_class.counters.push(counter)
    end

    def start_class(attrs)
      @subelement_index = 0

      if @class_names.include?(Hash[attrs]['name'])
        c              = Jacoco::Class.new
        c.name         = Hash[attrs]['name']
        c.counters     = []
        @current_class = c
        @classes.push c
      elsif @current_class.nil?
      end
    end

    def characters(string)
    end

    def end_element(name)
      @subelement_index -= 1
      @current_class = nil if name.eql? 'class'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
danger-jacoco-0.0.2 lib/jacoco/sax_parser.rb
danger-jacoco-0.0.1 lib/jacoco/sax_parser.rb