-
Notifications
You must be signed in to change notification settings - Fork 1
/
plist.go
92 lines (75 loc) · 2.29 KB
/
plist.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
package dawg
import "encoding/xml"
type PList PDict
const (
PListHeader = `<?xml version="1.0" encoding="UTF-8"?>`
PListDoctype = `<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">`
)
func (p PList) PListWithHeader() ([]byte, error) {
serialized, err := xml.MarshalIndent(p, "", " ")
if err != nil {
return nil, err
}
data := []byte{}
data = append(data, []byte(PListHeader)...)
data = append(data, []byte("\n")...)
data = append(data, []byte(PListDoctype)...)
data = append(data, []byte("\n")...)
data = append(data, serialized...)
return data, nil
}
func (p PList) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "plist"}
start.Attr = []xml.Attr{xml.Attr{Name: xml.Name{Local: "version"}, Value: "1.0"}}
e.EncodeToken(start)
PDict(p).MarshalXML(e, xml.StartElement{})
e.EncodeToken(start.End())
return nil
}
type PArray []xml.Marshaler
func (a PArray) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "array"}
e.EncodeToken(start)
for _, el := range a {
el.(xml.Marshaler).MarshalXML(e, start)
}
e.EncodeToken(start.End())
return nil
}
type PInteger int
func (i PInteger) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
e.EncodeElement(int(i), xml.StartElement{Name: xml.Name{Local: "integer"}})
return nil
}
type PReal int
func (i PReal) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
e.EncodeElement(float64(i), xml.StartElement{Name: xml.Name{Local: "real"}})
return nil
}
type PString string
func (s PString) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
e.EncodeElement(string(s), xml.StartElement{Name: xml.Name{Local: "string"}})
return nil
}
type PBool bool
func (b PBool) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
var val string
if b {
val = "true"
} else {
val = "false"
}
e.EncodeElement("", xml.StartElement{Name: xml.Name{Local: val}})
return nil
}
type PDict map[string]xml.Marshaler
func (d PDict) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "dict"}
e.EncodeToken(start)
for k, v := range d {
e.EncodeElement(k, xml.StartElement{Name: xml.Name{Local: "key"}})
v.MarshalXML(e, start)
}
e.EncodeToken(start.End())
return nil
}