github.com/vipcoin-gold/reviewdog@v1.0.2/parser/checkstyle.go (about) 1 package parser 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 "io" 7 8 "github.com/vipcoin-gold/reviewdog/proto/rdf" 9 ) 10 11 var _ Parser = &CheckStyleParser{} 12 13 // CheckStyleParser is checkstyle parser. 14 type CheckStyleParser struct{} 15 16 // NewCheckStyleParser returns a new CheckStyleParser. 17 func NewCheckStyleParser() Parser { 18 return &CheckStyleParser{} 19 } 20 21 func (p *CheckStyleParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) { 22 var cs = new(CheckStyleResult) 23 if err := xml.NewDecoder(r).Decode(cs); err != nil { 24 return nil, err 25 } 26 var ds []*rdf.Diagnostic 27 for _, file := range cs.Files { 28 for _, cerr := range file.Errors { 29 d := &rdf.Diagnostic{ 30 Location: &rdf.Location{ 31 Path: file.Name, 32 Range: &rdf.Range{ 33 Start: &rdf.Position{ 34 Line: int32(cerr.Line), 35 Column: int32(cerr.Column), 36 }, 37 }, 38 }, 39 Message: cerr.Message, 40 Severity: severity(cerr.Severity), 41 OriginalOutput: fmt.Sprintf("%v:%d:%d: %v: %v (%v)", 42 file.Name, cerr.Line, cerr.Column, cerr.Severity, cerr.Message, cerr.Source), 43 } 44 if s := cerr.Source; s != "" { 45 d.Code = &rdf.Code{Value: s} 46 } 47 ds = append(ds, d) 48 } 49 } 50 return ds, nil 51 } 52 53 // CheckStyleResult represents checkstyle XML result. 54 // <?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file ...></file>...</checkstyle> 55 // 56 // References: 57 // - http://checkstyle.sourceforge.net/ 58 // - http://eslint.org/docs/user-guide/formatters/#checkstyle 59 type CheckStyleResult struct { 60 XMLName xml.Name `xml:"checkstyle"` 61 Version string `xml:"version,attr"` 62 Files []*CheckStyleFile `xml:"file,omitempty"` 63 } 64 65 // CheckStyleFile represents <file name="fname"><error ... />...</file> 66 type CheckStyleFile struct { 67 Name string `xml:"name,attr"` 68 Errors []*CheckStyleError `xml:"error"` 69 } 70 71 // CheckStyleError represents <error line="1" column="10" severity="error" message="msg" source="src" /> 72 type CheckStyleError struct { 73 Column int `xml:"column,attr,omitempty"` 74 Line int `xml:"line,attr"` 75 Message string `xml:"message,attr"` 76 Severity string `xml:"severity,attr,omitempty"` 77 Source string `xml:"source,attr,omitempty"` 78 }