forked from groob/plist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_writer.go
231 lines (200 loc) · 5.33 KB
/
xml_writer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package plist
import (
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"math"
"reflect"
"time"
)
const xmlDOCTYPE = `<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">`
type xmlEncoder struct {
writer io.Writer
*xml.Encoder
}
func newXMLEncoder(w io.Writer) *xmlEncoder {
return &xmlEncoder{w, xml.NewEncoder(w)}
}
func (e *xmlEncoder) generateDocument(pval *plistValue) error {
// xml version=1.0
_, err := e.writer.Write([]byte(xml.Header))
if err != nil {
return err
}
//!DOCTYPE plist
_, err = e.writer.Write([]byte(xmlDOCTYPE))
if err != nil {
return err
}
// newline after doctype
// <plist> tag starts on new line
_, err = e.writer.Write([]byte("\n"))
if err != nil {
return err
}
tokenFunc := func(pval *plistValue) error {
if err := e.writePlistValue(pval); err != nil {
return err
}
return nil
}
err = e.writeElement("plist", pval, tokenFunc)
if err != nil {
return err
}
// newline at the end of a plist document
_, err = e.writer.Write([]byte("\n"))
if err != nil {
return err
}
return nil
}
func (e *xmlEncoder) writePlistValue(pval *plistValue) error {
switch pval.kind {
case String:
return e.writeStringValue(pval)
case Boolean:
return e.writeBoolValue(pval)
case Integer:
return e.writeIntegerValue(pval)
case Dictionary:
return e.writeDictionaryValue(pval)
case Date:
return e.writeDateValue(pval)
case Array:
return e.writeArrayValue(pval)
case Real:
return e.writeRealValue(pval)
case Data:
return e.writeDataValue(pval)
default:
return &UnsupportedTypeError{reflect.ValueOf(pval.value).Type()}
}
}
func (e *xmlEncoder) writeDataValue(pval *plistValue) error {
encodedValue := base64.StdEncoding.EncodeToString(pval.value.([]byte))
return e.EncodeElement(encodedValue, xml.StartElement{Name: xml.Name{Local: "data"}})
}
func (e *xmlEncoder) writeRealValue(pval *plistValue) error {
encodedValue := pval.value
switch {
case math.IsInf(pval.value.(sizedFloat).value, 1):
encodedValue = "inf"
case math.IsInf(pval.value.(sizedFloat).value, -1):
encodedValue = "-inf"
case math.IsNaN(pval.value.(sizedFloat).value):
encodedValue = "nan"
default:
encodedValue = pval.value.(sizedFloat).value
}
return e.EncodeElement(encodedValue, xml.StartElement{Name: xml.Name{Local: "real"}})
}
// writeElement writes an xml element like <plist>, <array> or <dict>
func (e *xmlEncoder) writeElement(name string, pval *plistValue, valFunc func(*plistValue) error) error {
startElement := xml.StartElement{
Name: xml.Name{
Space: "",
Local: name,
}}
if name == "plist" {
startElement.Attr = []xml.Attr{{
Name: xml.Name{
Space: "",
Local: "version"},
Value: "1.0"},
}
}
// Encode xml.StartElement token
if err := e.EncodeToken(startElement); err != nil {
return err
}
// flush
if err := e.Flush(); err != nil {
return err
}
// execute valFunc()
if err := valFunc(pval); err != nil {
return err
}
// Encode xml.EndElement token
if err := e.EncodeToken(startElement.End()); err != nil {
return err
}
// flush
return e.Flush()
}
func (e *xmlEncoder) writeArrayValue(pval *plistValue) error {
tokenFunc := func(pval *plistValue) error {
encodedValue := pval.value
values := encodedValue.([]*plistValue)
for _, v := range values {
if err := e.writePlistValue(v); err != nil {
return err
}
}
return nil
}
return e.writeElement("array", pval, tokenFunc)
}
func (e *xmlEncoder) writeDictionaryValue(pval *plistValue) error {
tokenFunc := func(pval *plistValue) error {
encodedValue := pval.value
dict := encodedValue.(*dictionary)
dict.populateArrays()
for i, k := range dict.keys {
if err := e.EncodeElement(k, xml.StartElement{Name: xml.Name{Local: "key"}}); err != nil {
return err
}
if err := e.writePlistValue(dict.values[i]); err != nil {
return err
}
}
return nil
}
return e.writeElement("dict", pval, tokenFunc)
}
// encode strings as CharData, which doesn't escape newline
// see https://github.com/golang/go/issues/9204
func (e *xmlEncoder) writeStringValue(pval *plistValue) error {
startElement := xml.StartElement{Name: xml.Name{Local: "string"}}
// Encode xml.StartElement token
if err := e.EncodeToken(startElement); err != nil {
return err
}
// flush
if err := e.Flush(); err != nil {
return err
}
stringValue := pval.value.(string)
if err := e.EncodeToken(xml.CharData(stringValue)); err != nil {
return err
}
// flush
if err := e.Flush(); err != nil {
return err
}
// Encode xml.EndElement token
if err := e.EncodeToken(startElement.End()); err != nil {
return err
}
// flush
return e.Flush()
}
func (e *xmlEncoder) writeBoolValue(pval *plistValue) error {
b := pval.value.(bool)
return e.EncodeElement("", xml.StartElement{Name: xml.Name{Local: fmt.Sprintf("%t", b)}})
}
func (e *xmlEncoder) writeIntegerValue(pval *plistValue) error {
encodedValue := pval.value
if pval.value.(signedInt).signed {
encodedValue = int64(pval.value.(signedInt).value)
} else {
encodedValue = pval.value.(signedInt).value
}
return e.EncodeElement(encodedValue, xml.StartElement{Name: xml.Name{Local: "integer"}})
}
func (e *xmlEncoder) writeDateValue(pval *plistValue) error {
encodedValue := pval.value.(time.Time).In(time.UTC).Format(time.RFC3339)
return e.EncodeElement(encodedValue, xml.StartElement{Name: xml.Name{Local: "date"}})
}