-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.go
174 lines (147 loc) · 3.56 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
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
package properties
import (
"encoding/xml"
"errors"
"io"
"io/ioutil"
"strings"
)
// xml file header title
const Header = `<!DOCTYPE properties SYSTEM "http://github.com/zooyer/properties">` + "\n"
// interface
type XmlSupport interface {
load(props *Properties, in io.Reader) error
store(props *Properties, out io.Writer, comment []byte, encoding string) error
}
// global single SmlSupport object
var PROVIDER = newXmlSupport()
func load(props *Properties, in io.Reader) error {
return PROVIDER.load(props, in)
}
func save(props *Properties, out io.Writer, comment []byte, encoding string) error {
return PROVIDER.store(props, out, comment, encoding)
}
// XmlSupport implement
type xmlSupport struct{}
func newXmlSupport() XmlSupport {
return new(xmlSupport)
}
func (x *xmlSupport) load(props *Properties, in io.Reader) error {
data, err := ioutil.ReadAll(in)
if err != nil {
return err
}
var m XMLProperties
if err = xml.Unmarshal(data, &m); err != nil {
return err
}
p := props.New()
if err = x.toProperties(p, &m); err != nil {
return err
}
props.Hashtable = p.Hashtable
return nil
}
func (x *xmlSupport) store(props *Properties, out io.Writer, comment []byte, encoding string) error {
if props == nil {
return errors.New("props(Properties) is <nil>")
}
if out == nil {
return errors.New("out(io.Writer) is <nil>")
}
switch strings.ToUpper(encoding) {
case "UTF-8":
default:
return errors.New("not support encoding <" + encoding + ">")
}
xp, err := x.toXML(props)
if err != nil {
return err
}
data, err := xp.ToXML(comment)
if err != nil {
return err
}
if _, err = out.Write([]byte(Header)); err != nil {
return err
}
if _, err = out.Write(data); err != nil {
return err
}
return nil
}
func (x *xmlSupport) toProperties(props *Properties, xp *XMLProperties) error {
if props == nil {
return errors.New("props(Properties) is <nil>")
}
if xp == nil {
return errors.New("xp(XMLProperties) is <nil>")
}
for _, v := range xp.Entry {
props.Put(v.Key, v.CDATA)
}
return nil
}
func (x *xmlSupport) toXML(props *Properties) (*XMLProperties, error) {
if props == nil {
return nil, errors.New("props(Properties) is <nil>")
}
var xp = new(XMLProperties)
xp.Entry = make([]XMLReaderEntry, props.Size())
for i, key := range props.StringPropertyNames() {
val, exist := props.GetProperty(key)
if exist {
xp.Entry[i] = XMLReaderEntry{
Key: key,
CDATA: val,
}
}
}
return xp, nil
}
// entity conversion
type XMLReaderEntry struct {
XMLName xml.Name `xml:"entry"`
Key string `xml:"key,attr"`
CDATA string `xml:",cdata"`
}
type XMLWriterEntry struct {
XMLName xml.Name `xml:"entry"`
Key string `xml:"key,attr"`
Data string `xml:",chardata"`
}
type XMLReader struct {
XMLName xml.Name `xml:"properties"`
Comment string `xml:"comment"`
Entry []XMLReaderEntry `xml:"entry"`
}
type XMLWriter struct {
XMLName xml.Name `xml:"properties"`
Entry []XMLWriterEntry `xml:"entry"`
}
type XMLWriterComment struct {
Comment string `xml:"comment"`
XMLWriter
}
type XMLProperties struct {
XMLReader
}
func (x *XMLProperties) ToXML(comments []byte) ([]byte, error) {
w := new(XMLWriter)
w.XMLName = x.XMLName
w.Entry = make([]XMLWriterEntry, len(x.Entry))
for i, e := range x.Entry {
w.Entry[i] = XMLWriterEntry{
XMLName: e.XMLName,
Key: e.Key,
Data: e.CDATA,
}
}
if comments == nil {
return xml.MarshalIndent(w, "", " ")
}
wc := new(XMLWriterComment)
wc.Comment = string(comments)
wc.XMLWriter = *w
return xml.MarshalIndent(wc, "", " ")
}