Sha256: 824598ff274de1e7c9deb0e634c5675ecbbe38a6196933d7cb5a82478400d181

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'nokogiri'

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

    def initialize(classes)
      super()
      @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

      return unless @class_names.include?(Hash[attrs]['name'])

      c              = Jacoco::Class.new
      c.name         = Hash[attrs]['name']
      c.counters     = []
      @current_class = c
      @classes.push c
    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

13 entries across 13 versions & 2 rubygems

Version Path
danger-jacoco-0.1.9 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.15 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.14 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.13.SNAPSHOT.1 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.13 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.12.SNAPSHOT.2 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.12.SNAPSHOT.1 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.12 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.11 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.10 lib/jacoco/sax_parser.rb
danger-jacoco-instacart-0.1.9 lib/jacoco/sax_parser.rb
danger-jacoco-0.1.8 lib/jacoco/sax_parser.rb
danger-jacoco-0.1.7 lib/jacoco/sax_parser.rb