Debian Codegraph @

xml_generator.go

raw symbols SCIP
1 package plist
2
3 import (
4 "bufio"
5 "encoding/base64"
6 "encoding/xml"
7 "io"
8 "math"
9 "strconv"
10 "time"
11 )
12
13 const (
14 xmlHEADER string = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
15 xmlDOCTYPE = `<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">` + "\n"
16 xmlArrayTag = "array"
17 xmlDataTag = "data"
18 xmlDateTag = "date"
19 xmlDictTag = "dict"
20 xmlFalseTag = "false"
21 xmlIntegerTag = "integer"
22 xmlKeyTag = "key"
23 xmlPlistTag = "plist"
24 xmlRealTag = "real"
25 xmlStringTag = "string"
26 xmlTrueTag = "true"
27 )
28
29 func formatXMLFloat(f float64) string {
30 switch {
31 case math.IsInf(f, 1):
32 return "inf"
33 case math.IsInf(f, -1):
34 return "-inf"
35 case math.IsNaN(f):
36 return "nan"
37 }
38 return strconv.FormatFloat(f, 'g', -1, 64)
39 }
40
41 type xmlPlistGenerator struct {
42 *bufio.Writer
43
44 indent string
45 depth int
46 putNewline bool
47 }
48
49 func (p *xmlPlistGenerator) generateDocument(root cfValue) {
50 p.WriteString(xmlHEADER)
51 p.WriteString(xmlDOCTYPE)
52
53 p.openTag(`plist version="1.0"`)
54 p.writePlistValue(root)
55 p.closeTag(xmlPlistTag)
56 p.Flush()
57 }
58
59 func (p *xmlPlistGenerator) openTag(n string) {
60 p.writeIndent(1)
61 p.WriteByte('<')
62 p.WriteString(n)
63 p.WriteByte('>')
64 }
65
66 func (p *xmlPlistGenerator) closeTag(n string) {
67 p.writeIndent(-1)
68 p.WriteString("</")
69 p.WriteString(n)
70 p.WriteByte('>')
71 }
72
73 func (p *xmlPlistGenerator) element(n string, v string) {
74 p.writeIndent(0)
75 if len(v) == 0 {
76 p.WriteByte('<')
77 p.WriteString(n)
78 p.WriteString("/>")
79 } else {
80 p.WriteByte('<')
81 p.WriteString(n)
82 p.WriteByte('>')
83
84 err := xml.EscapeText(p.Writer, []byte(v))
85 if err != nil {
86 panic(err)
87 }
88
89 p.WriteString("</")
90 p.WriteString(n)
91 p.WriteByte('>')
92 }
93 }
94
95 func (p *xmlPlistGenerator) writeDictionary(dict *cfDictionary) {
96 dict.sort()
97 p.openTag(xmlDictTag)
98 for i, k := range dict.keys {
99 p.element(xmlKeyTag, k)
100 p.writePlistValue(dict.values[i])
101 }
102 p.closeTag(xmlDictTag)
103 }
104
105 func (p *xmlPlistGenerator) writeArray(a *cfArray) {
106 p.openTag(xmlArrayTag)
107 for _, v := range a.values {
108 p.writePlistValue(v)
109 }
110 p.closeTag(xmlArrayTag)
111 }
112
113 func (p *xmlPlistGenerator) writePlistValue(pval cfValue) {
114 if pval == nil {
115 return
116 }
117
118 switch pval := pval.(type) {
119 case cfString:
120 p.element(xmlStringTag, string(pval))
121 case *cfNumber:
122 if pval.signed {
123 p.element(xmlIntegerTag, strconv.FormatInt(int64(pval.value), 10))
124 } else {
125 p.element(xmlIntegerTag, strconv.FormatUint(pval.value, 10))
126 }
127 case *cfReal:
128 p.element(xmlRealTag, formatXMLFloat(pval.value))
129 case cfBoolean:
130 if bool(pval) {
131 p.element(xmlTrueTag, "")
132 } else {
133 p.element(xmlFalseTag, "")
134 }
135 case cfData:
136 p.element(xmlDataTag, base64.StdEncoding.EncodeToString([]byte(pval)))
137 case cfDate:
138 p.element(xmlDateTag, time.Time(pval).In(time.UTC).Format(time.RFC3339))
139 case *cfDictionary:
140 p.writeDictionary(pval)
141 case *cfArray:
142 p.writeArray(pval)
143 case cfUID:
144 p.writePlistValue(pval.toDict())
145 }
146 }
147
148 func (p *xmlPlistGenerator) writeIndent(delta int) {
149 if len(p.indent) == 0 {
150 return
151 }
152
153 if delta < 0 {
154 p.depth--
155 }
156
157 if p.putNewline {
158 // from encoding/xml/marshal.go; it seems to be intended
159 // to suppress the first newline.
160 p.WriteByte('\n')
161 } else {
162 p.putNewline = true
163 }
164 for i := 0; i < p.depth; i++ {
165 p.WriteString(p.indent)
166 }
167 if delta > 0 {
168 p.depth++
169 }
170 }
171
172 func (p *xmlPlistGenerator) Indent(i string) {
173 p.indent = i
174 }
175
176 func newXMLPlistGenerator(w io.Writer) *xmlPlistGenerator {
177 return &xmlPlistGenerator{Writer: bufio.NewWriter(w)}
178 }