<commit_before>require 'nori'
require 'rexml/document'

module FindbugsTranslateCheckstyleFormat
  module Translate
    def parse(xml)
      Nori
        .new(parser: :rexml)
        .parse(xml)
    end

    def trans(xml)
      doc = REXML::Document.new
      doc << REXML::XMLDecl.new('1.0', 'UTF-8')

      checkstyle = doc.add_element('checkstyle')
      bug_instances = xml['BugCollection']['BugInstance']
      if bug_instances.blank?
        FindbugsTranslateCheckstyleFormat::Translate.set_dummy(xml, checkstyle)
        return doc
      end

      bug_instances = [bug_instances] if bug_instances.is_a?(Hash)
      bug_instances.each do |bug_instance|
        source_line = bug_instance['SourceLine']
        file = checkstyle.add_element('file',
                                      'name' => FindbugsTranslateCheckstyleFormat::Translate.fqcn_to_path(source_line['@classname'], xml)
                                     )
        file.add_element('error',
                         'line' => source_line['@start'],
                         'severity' => 'error',
                         'message' => FindbugsTranslateCheckstyleFormat::Translate.create_message(bug_instance)
                        )
      end

      doc
    end

    def self.fqcn_to_path(fqcn, xml)
      path = fqcn.tr('.', '/').gsub(/\$[0-9]+/, '') + '.java'
      puts path
      src_dirs = xml['BugCollection']['Project']['SrcDir']
      src_dirs = [src_dirs] unless src_dirs.is_a?(Array)
      src_dirs.find { |src| !src.index(path).nil? }
    end

    def self.set_dummy(xml, checkstyle)
      dummy_src_dir = xml['BugCollection']['Project']['SrcDir']
      dummy_src_dir = dummy_src_dir.first if dummy_src_dir.is_a?(Array)

      checkstyle.add_element('file',
                             'name' => dummy_src_dir
                            )

      checkstyle
    end

    def self.create_message(bug_instance)
      link = "http://findbugs.sourceforge.net/bugDescriptions.html##{bug_instance['@type']}"<commit_msg>remove debug log<commit_after>require 'nori'
require 'rexml/document'

module FindbugsTranslateCheckstyleFormat
  module Translate
    def parse(xml)
      Nori
        .new(parser: :rexml)
        .parse(xml)
    end

    def trans(xml)
      doc = REXML::Document.new
      doc << REXML::XMLDecl.new('1.0', 'UTF-8')

      checkstyle = doc.add_element('checkstyle')
      bug_instances = xml['BugCollection']['BugInstance']
      if bug_instances.blank?
        FindbugsTranslateCheckstyleFormat::Translate.set_dummy(xml, checkstyle)
        return doc
      end

      bug_instances = [bug_instances] if bug_instances.is_a?(Hash)
      bug_instances.each do |bug_instance|
        source_line = bug_instance['SourceLine']
        file = checkstyle.add_element('file',
                                      'name' => FindbugsTranslateCheckstyleFormat::Translate.fqcn_to_path(source_line['@classname'], xml)
                                     )
        file.add_element('error',
                         'line' => source_line['@start'],
                         'severity' => 'error',
                         'message' => FindbugsTranslateCheckstyleFormat::Translate.create_message(bug_instance)
                        )
      end

      doc
    end

    def self.fqcn_to_path(fqcn, xml)
      path = fqcn.tr('.', '/').gsub(/\$[0-9]+/, '') + '.java'
      src_dirs = xml['BugCollection']['Project']['SrcDir']
      src_dirs = [src_dirs] unless src_dirs.is_a?(Array)
      src_dirs.find { |src| !src.index(path).nil? }
    end

    def self.set_dummy(xml, checkstyle)
      dummy_src_dir = xml['BugCollection']['Project']['SrcDir']
      dummy_src_dir = dummy_src_dir.first if dummy_src_dir.is_a?(Array)

      checkstyle.add_element('file',
                             'name' => dummy_src_dir
                            )

      checkstyle
    end

    def self.create_message(bug_instance)
      link = "http://findbugs.sourceforge.net/bugDescriptions.html##{bug_instance['@type']}"
{'fraction_non_alphanumeric': 0.1247182569496619, 'fraction_numerical': 0.002504382669671926, 'mean_word_length': 2.234008097165992, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6982603', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1219, 'n_words': 187}