-
Notifications
You must be signed in to change notification settings - Fork 3
/
xml.go
91 lines (80 loc) · 2.34 KB
/
xml.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
package ctoc
import (
"encoding/xml"
"fmt"
)
// XMLResultType is the result type in XML format.
type XMLResultType int8
const (
// XMLResultWithLangs is the result type for each language in XML format
XMLResultWithLangs XMLResultType = iota
// XMLResultWithFiles is the result type for each file in XML format
XMLResultWithFiles
)
// XMLTotalLanguages is the total result in XML format.
type XMLTotalLanguages struct {
SumFiles int32 `xml:"sum_files,attr"`
Code int32 `xml:"code,attr"`
Comment int32 `xml:"comment,attr"`
Blank int32 `xml:"blank,attr"`
Tokens int32 `xml:"tokens,attr"`
}
// XMLResultLanguages stores the results in XML format.
type XMLResultLanguages struct {
Languages []ClocLanguage `xml:"language"`
Total XMLTotalLanguages `xml:"total"`
}
// XMLTotalFiles is the total result per file in XML format.
type XMLTotalFiles struct {
Code int32 `xml:"code,attr"`
Comment int32 `xml:"comment,attr"`
Blank int32 `xml:"blank,attr"`
Tokens int32 `xml:"tokens,attr"`
}
// XMLResultFiles stores per file results in XML format.
type XMLResultFiles struct {
Files []ClocFile `xml:"file"`
Total XMLTotalFiles `xml:"total"`
}
// XMLResult stores the results in XML format.
type XMLResult struct {
XMLName xml.Name `xml:"results"`
XMLFiles *XMLResultFiles `xml:"files,omitempty"`
XMLLanguages *XMLResultLanguages `xml:"languages,omitempty"`
}
// Encode outputs XMLResult in a human readable format.
func (x *XMLResult) Encode() {
if output, err := xml.MarshalIndent(x, "", " "); err == nil {
fmt.Printf(xml.Header)
fmt.Println(string(output))
}
}
// NewXMLResultFromCloc returns XMLResult with default data set.
func NewXMLResultFromCloc(total *Language, sortedLanguages Languages, _ XMLResultType) *XMLResult {
var langs []ClocLanguage
for _, language := range sortedLanguages {
c := ClocLanguage{
Name: language.Name,
FilesCount: int32(len(language.Files)),
Code: language.Code,
Comments: language.Comments,
Blanks: language.Blanks,
Tokens: language.Tokens,
}
langs = append(langs, c)
}
t := XMLTotalLanguages{
Code: total.Code,
Comment: total.Comments,
Blank: total.Blanks,
Tokens: total.Tokens,
SumFiles: total.Total,
}
f := &XMLResultLanguages{
Languages: langs,
Total: t,
}
return &XMLResult{
XMLLanguages: f,
}
}