Issues (2)

checkstyle.go (1 issue)

Severity
1
package checkstyle
2
3
import "encoding/xml"
4
import "io/ioutil"
5
6
// DefaultCheckStyleVersion defines the default "version" attribute on "<checkstyle>" lememnt
7
var DefaultCheckStyleVersion = "1.0.0"
8
9
// Severity defines a checkstyle severity code
10
type Severity string
11
12
var (
13
	SeverityError   Severity = "error"
0 ignored issues
show
exported var SeverityError should have comment or be unexported
Loading history...
14
	SeverityInfo    Severity = "info"
15
	SeverityWarning Severity = "warning"
16
	SeverityIgnore  Severity = "ignore"
17
	SeverityNone    Severity
18
)
19
20
// CheckStyle represents a <checkstyle> xml element found in a checkstyle_report.xml file.
21
type CheckStyle struct {
22
	XMLName xml.Name `xml:"checkstyle"`
23
	Version string   `xml:"version,attr"`
24
	File    []*File  `xml:"file"`
25
}
26
27
// AddFile adds a checkstyle.File with the given filename.
28
func (cs *CheckStyle) AddFile(csf *File) {
29
	cs.File = append(cs.File, csf)
30
}
31
32
// GetFile gets a CheckStyleFile with the given filename.
33
func (cs *CheckStyle) GetFile(filename string) (csf *File, ok bool) {
34
	for _, file := range cs.File {
35
		if file.Name == filename {
36
			csf = file
37
			ok = true
38
			return
39
		}
40
	}
41
	return
42
}
43
44
// EnsureFile ensures that a CheckStyleFile with the given name exists
45
// Returns either an exiting CheckStyleFile (if a file with that name exists)
46
// or a new CheckStyleFile (if a file with that name does not exists)
47
func (cs *CheckStyle) EnsureFile(filename string) (csf *File) {
48
	csf, ok := cs.GetFile(filename)
49
	if !ok {
50
		csf = NewFile(filename)
51
		cs.AddFile(csf)
52
	}
53
	return csf
54
}
55
56
// String implements Stringer. Returns as xml.
57
func (cs *CheckStyle) String() string {
58
	checkStyleXML, err := xml.Marshal(cs)
59
	if err != nil {
60
		panic(err)
61
	}
62
	return string(checkStyleXML)
63
}
64
65
// New returns a new CheckStyle
66
func New() *CheckStyle {
67
	return &CheckStyle{Version: DefaultCheckStyleVersion, File: []*File{}}
68
}
69
70
// File represents a <file> xml element.
71
type File struct {
72
	XMLName xml.Name `xml:"file"`
73
	Name    string   `xml:"name,attr"`
74
	Error   []*Error `xml:"error"`
75
}
76
77
// AddError adds a checkstyle.Error to the file.
78
func (csf *File) AddError(cse *Error) {
79
	csf.Error = append(csf.Error, cse)
80
}
81
82
// NewFile creates a new checkstyle.File
83
func NewFile(filename string) *File {
84
	return &File{Name: filename, Error: []*Error{}}
85
}
86
87
// Error represents a <error> xml element
88
type Error struct {
89
	XMLName  xml.Name `xml:"error"`
90
	Line     int      `xml:"line,attr"`
91
	Column   int      `xml:"column,attr,omitempty"`
92
	Severity Severity `xml:"severity,attr,omitempty"`
93
	Message  string   `xml:"message,attr"`
94
	Source   string   `xml:"source,attr"`
95
}
96
97
// NewError creates a new checkstyle.Error
98
// Note that line starts at 0, and column starts at 1
99
func NewError(line int, column int, severity Severity, message string, source string) *Error {
100
	return &Error{Line: line, Column: column, Severity: severity, Message: message, Source: source}
101
}
102
103
// ReadFile reads a checkfile.xml file and returns a CheckStyle object.
104
func ReadFile(filename string) (*CheckStyle, error) {
105
	checkStyleXML, err := ioutil.ReadFile(filename)
106
	if err != nil {
107
		return nil, err
108
	}
109
	checkStyle := New()
110
	err = xml.Unmarshal(checkStyleXML, checkStyle)
111
	return checkStyle, err
112
}
113