Module: PmdTranslateCheckstyleFormat::Translate

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

Instance Method Summary collapse

Instance Method Details

#create_cpd_message(duplication, file) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/pmd_translate_checkstyle_format/translate.rb', line 84

def create_cpd_message(duplication, file)
  files = duplication['file'].reject { |item| item == file }
  file_names = files.map { |item|
    Pathname.new(item['@path']).relative_path_from(Pathname.new(Dir.pwd)).to_s
  }
  "[PMD-CPD] #{duplication['@lines']} lines duplicated.\n#{file_names.join("\n")}"
end

#get_severity(priority) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/pmd_translate_checkstyle_format/translate.rb', line 73

def get_severity(priority)
  case priority
  when 1, 2
    'error'
  when 3, 4
    'warning'
  when 5
    'info'
  end
end

#parse(xml) ⇒ Object



7
8
9
10
11
# File 'lib/pmd_translate_checkstyle_format/translate.rb', line 7

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

#set_dummy(checkstyle) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/pmd_translate_checkstyle_format/translate.rb', line 92

def set_dummy(checkstyle)
  checkstyle.add_element('file',
                         'name' => 'dummy.java'
                        )

  checkstyle
end

#trans(xml) ⇒ Object



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
39
40
41
# File 'lib/pmd_translate_checkstyle_format/translate.rb', line 13

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

  checkstyle = doc.add_element("checkstyle")
  if xml['pmd'].blank? || xml['pmd']['file'].blank?
    set_dummy(checkstyle)
    return doc
  end

  files = xml['pmd']['file']
  files = [files] if files.is_a?(Hash)
  files.each do |file|
    violations = file['violation']
    violations = [violations] unless violations.is_a?(Array)
    violations.each do |violation|
      file_element = checkstyle.add_element("file", {
        'name' => file['@name']
        })
      file_element.add_element("error", {
        'line' => violation.attributes['beginline'],
        'severity' => get_severity(violation.attributes['priority'].to_i),
        'message' => "[#{violation.attributes['rule']}] #{violation.strip}\n#{violation.attributes['externalInfoUrl']}"
        })
    end
  end

  doc
end

#trans_cpd(xml) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pmd_translate_checkstyle_format/translate.rb', line 43

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

  checkstyle = doc.add_element("checkstyle")
  if xml['pmd_cpd'].blank? || xml['pmd_cpd']['duplication'].blank?
    set_dummy(checkstyle)
    return doc
  end

  duplications = xml['pmd_cpd']['duplication']
  duplications = [duplications] if duplications.is_a?(Hash)
  duplications.each do |duplication|
    files = duplication['file']
    files = [files] unless files.is_a?(Array)
    files.each do |file|
      file_element = checkstyle.add_element("file", {
        'name' => file['@path']
        })
      file_element.add_element("error", {
        'line' => file['@line'],
        'severity' => 'error',
        'message' => create_cpd_message(duplication, file)
        })
    end
  end

  doc
end