-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
174 lines (153 loc) · 4.7 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//Copyright (c) 2013, The Impendulo Authors
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification,
//are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package pmd
import (
"encoding/gob"
"encoding/xml"
"fmt"
"github.com/godfried/impendulo/tool"
"github.com/godfried/impendulo/tool/result"
"html/template"
"labix.org/v2/mgo/bson"
"sort"
"strings"
)
type (
//Report is the result of running PMD on a Java source file.
Report struct {
Id bson.ObjectId
Version string `xml:"version,attr"`
Files []*File `xml:"file"`
Errors int
}
//File defines a source file analysed by PMD and all of the errors
//found in it.
File struct {
Name string `xml:"name,attr"`
Violations Violations `xml:"violation"`
}
//Violations
Violations []*Violation
//Violation describes an error detected by PMD.
Violation struct {
Id bson.ObjectId
Begin int `xml:"beginline,attr"`
End int `xml:"endline,attr"`
Rule string `xml:"rule,attr"`
RuleSet string `xml:"ruleset,attr"`
Url template.URL `xml:"externalInfoUrl,attr"`
Priority int `xml:"priority,attr"`
Description string `xml:",innerxml"`
//The locations where the error was detected.
Starts, Ends []int
}
)
func init() {
gob.Register(new(Report))
}
//NewReport generates a new Report from XML generated by PMD.
func NewReport(id bson.ObjectId, data []byte) (*Report, error) {
var r *Report
if e := xml.Unmarshal(data, &r); e != nil {
return nil, tool.NewXMLError(e, "pmd/pmdResult.go")
}
r.Id = id
r.Errors = 0
for _, f := range r.Files {
r.Errors += len(f.Violations)
f.CompressViolations()
}
return r, nil
}
func (r *Report) Lines() []*result.Line {
lines := make([]*result.Line, 0, len(r.Files)*10)
for _, f := range r.Files {
for _, v := range f.Violations {
for i, s := range v.Starts {
lines = append(lines, &result.Line{Title: v.Rule, Description: v.Description, Start: s, End: v.Ends[i]})
}
}
}
return lines
}
//Success is true if no errors were found.
func (r *Report) Success() bool {
return r.Errors == 0
}
//String
func (r *Report) String() string {
if r.Files == nil {
return fmt.Sprintf("Report{ Errors: %d\n.", r.Errors)
}
s := fmt.Sprintf("Report{ Errors: %d\nFiles:\n.", r.Errors)
for _, f := range r.Files {
s += f.String()
}
return s + "}\n"
}
//File retrieves a File whose name ends with the provided name.
func (r *Report) File(name string) *File {
for _, f := range r.Files {
if strings.HasSuffix(f.Name, name) {
return f
}
}
return nil
}
//CompressViolations packs all Violations of the same
//type into a single Violation by storing their location seperately.
func (f *File) CompressViolations() {
indices := make(map[string]int)
c := make(Violations, 0, len(f.Violations))
for _, v := range f.Violations {
i, ok := indices[v.Rule]
if !ok {
//Only store if the Violation type has not been stored yet.
v.Starts = make([]int, 0, len(f.Violations))
v.Ends = make([]int, 0, len(f.Violations))
v.Id = bson.NewObjectId()
c = append(c, v)
i = len(c) - 1
indices[v.Rule] = i
}
//Add Violation location.
c[i].Starts = append(c[i].Starts, v.Begin)
c[i].Ends = append(c[i].Ends, v.End)
}
sort.Sort(c)
f.Violations = c
}
//String
func (f *File) String() string {
return fmt.Sprintf("File{ Name: %s\n}\n.", f.Name)
}
func (v Violations) Len() int {
return len(v)
}
func (v Violations) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
func (v Violations) Less(i, j int) bool {
return v[i].Rule < v[j].Rule
}