Module: FindbugsTranslateCheckstyleFormat::Translate

Included in:
CLI
Defined in:
lib/findbugs_translate_checkstyle_format/translate.rb

Instance Method Summary collapse

Instance Method Details

#fqcn_to_path(fqcn, xml) ⇒ Object



40
41
42
43
44
45
# File 'lib/findbugs_translate_checkstyle_format/translate.rb', line 40

def fqcn_to_path(fqcn, xml)
  path = fqcn.gsub('.', '/') + '.java'
  xml['BugCollection']['Project']['SrcDir'].find do |src|
    src.index(path) != nil
  end
end

#parse(xml) ⇒ Object



5
6
7
8
9
# File 'lib/findbugs_translate_checkstyle_format/translate.rb', line 5

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

#trans(xml) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/findbugs_translate_checkstyle_format/translate.rb', line 11

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

  checkstyle = doc.add_element("checkstyle")
  if xml['BugCollection']['BugInstance']
    bugInstances = xml['BugCollection']['BugInstance'].is_a?(Array) ? xml['BugCollection']['BugInstance'] : [xml['BugCollection']['BugInstance']]
    bugInstances.each do |bugInstance|
      file = checkstyle.add_element("file", {
        'name' => fqcn_to_path(bugInstance['SourceLine']['@classname'], xml)
        })
      file.add_element("error", {
        'line' => bugInstance['SourceLine']['@start'],
        'severity' => '',
        'message' => "[#{bugInstance['@category']}] #{bugInstance['LongMessage']}"
        })
    end
  else
    # create dummy
    dummy_src_dir = xml['BugCollection']['Project']['SrcDir'].first
    file = checkstyle.add_element("file", {
      'name' => dummy_src_dir
      })
  end

  doc
end