aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/phayes/checkstyle/checkstyle.go
blob: cabbd4b40ed2bffa768bec14d72cef2d5217486d (plain)
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
package checkstyle

import "encoding/xml"
import "io/ioutil"

// DefaultCheckStyleVersion defines the default "version" attribute on "<checkstyle>" lememnt
var DefaultCheckStyleVersion = "1.0.0"

// Severity defines a checkstyle severity code
type Severity string

var (
	SeverityError   Severity = "error"
	SeverityInfo    Severity = "info"
	SeverityWarning Severity = "warning"
	SeverityIgnore  Severity = "ignore"
	SeverityNone    Severity
)

// CheckStyle represents a <checkstyle> xml element found in a checkstyle_report.xml file.
type CheckStyle struct {
	XMLName xml.Name `xml:"checkstyle"`
	Version string   `xml:"version,attr"`
	File    []*File  `xml:"file"`
}

// AddFile adds a checkstyle.File with the given filename.
func (cs *CheckStyle) AddFile(csf *File) {
	cs.File = append(cs.File, csf)
}

// GetFile gets a CheckStyleFile with the given filename.
func (cs *CheckStyle) GetFile(filename string) (csf *File, ok bool) {
	for _, file := range cs.File {
		if file.Name == filename {
			csf = file
			ok = true
			return
		}
	}
	return
}

// EnsureFile ensures that a CheckStyleFile with the given name exists
// Returns either an exiting CheckStyleFile (if a file with that name exists)
// or a new CheckStyleFile (if a file with that name does not exists)
func (cs *CheckStyle) EnsureFile(filename string) (csf *File) {
	csf, ok := cs.GetFile(filename)
	if !ok {
		csf = NewFile(filename)
		cs.AddFile(csf)
	}
	return csf
}

// String implements Stringer. Returns as xml.
func (cs *CheckStyle) String() string {
	checkStyleXML, err := xml.Marshal(cs)
	if err != nil {
		panic(err)
	}
	return string(checkStyleXML)
}

// New returns a new CheckStyle
func New() *CheckStyle {
	return &CheckStyle{Version: DefaultCheckStyleVersion, File: []*File{}}
}

// File represents a <file> xml element.
type File struct {
	XMLName xml.Name `xml:"file"`
	Name    string   `xml:"name,attr"`
	Error   []*Error `xml:"error"`
}

// AddError adds a checkstyle.Error to the file.
func (csf *File) AddError(cse *Error) {
	csf.Error = append(csf.Error, cse)
}

// NewFile creates a new checkstyle.File
func NewFile(filename string) *File {
	return &File{Name: filename, Error: []*Error{}}
}

// Error represents a <error> xml element
type Error struct {
	XMLName  xml.Name `xml:"error"`
	Line     int      `xml:"line,attr"`
	Column   int      `xml:"column,attr,omitempty"`
	Severity Severity `xml:"severity,attr,omitempty"`
	Message  string   `xml:"message,attr"`
	Source   string   `xml:"source,attr"`
}

// NewError creates a new checkstyle.Error
// Note that line starts at 0, and column starts at 1
func NewError(line int, column int, severity Severity, message string, source string) *Error {
	return &Error{Line: line, Column: column, Severity: severity, Message: message, Source: source}
}

// ReadFile reads a checkfile.xml file and returns a CheckStyle object.
func ReadFile(filename string) (*CheckStyle, error) {
	checkStyleXML, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	checkStyle := New()
	err = xml.Unmarshal(checkStyleXML, checkStyle)
	return checkStyle, err
}