Sha256: 8b4427ef13628df971df95d0d3fc5431dc05419dff362caca5edd71666916cce

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

require 'rexml/document'

module Pronto
  module Formatter
    class CheckstyleFormatter
      def initialize
        @output = ''
      end

      def format(messages, _, _)
        open_xml
        process_messages(messages)
        close_xml

        @output
      end

      private

      def open_xml
        @document = REXML::Document.new.tap do |d|
          d << REXML::XMLDecl.new
        end
        @checkstyle = REXML::Element.new('checkstyle', @document)
      end

      def process_messages(messages)
        messages.group_by(&:path).map do |path, path_messages|
          REXML::Element.new('file', @checkstyle).tap do |file|
            file.attributes['name'] = path
            add_file_messages(path_messages, file)
          end
        end
      end

      def add_file_messages(path_messages, file)
        path_messages.each do |message|
          REXML::Element.new('error', file).tap do |e|
            e.attributes['line'] = message.line.new_lineno if message.line
            e.attributes['severity'] = to_checkstyle_severity(message.level)
            e.attributes['message'] = message.msg
            e.attributes['source'] = 'com.puppycrawl.tools.checkstyle.pronto'
          end
        end
      end

      def close_xml
        @document.write(@output, 2)
      end

      def to_checkstyle_severity(pronto_level)
        case pronto_level
        when :error, :fatal then 'error'
        else pronto_level.to_s
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pronto-0.8.2 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.8.1 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.8.0 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.7.1 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.7.0 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.6.0 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.5.3 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.5.2 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.5.1 lib/pronto/formatter/checkstyle_formatter.rb
pronto-0.5.0 lib/pronto/formatter/checkstyle_formatter.rb