-
Notifications
You must be signed in to change notification settings - Fork 471
/
unmarshal.go
294 lines (226 loc) 路 7.09 KB
/
unmarshal.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2023, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*--------------------------------------------------------------------------- */
package dynamic
import (
"encoding/xml"
"fmt"
"io"
)
// Dynamic template parsing
// There is two type of data in a TEMPLATE, Pair and Vector.
// A Vector contains a collection of Pair.
//
// In order to parse dynamically a TEMPLATE, we need to look at three next token
// to distinguish between these types.
//
// There is three types of tokens: StartElement, CharData, EndElement.
// While parsing a TEMPLATE, a problem may occur: when there is some chars between two opening tokens.
//
// Here is the two cases we want to avoid:
// tokPrev tok tokNext
// CharData StartElement StartElement
// StartElement CharData StartElement
// xmlPair contains temporary pair informations
type xmlPair struct {
XMLName xml.Name
Content string `xml:",chardata"`
}
// UnmarshalXML parse dynamically a template under the TEMPLATE token
func (t *Template) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// Ensure that first token is a StartElement
for {
tok, err := d.Token()
if err != nil {
if err.Error() == io.EOF.Error() {
break
}
return err
}
tokPrev := xml.CopyToken(tok)
switch tokPrev.(type) {
case xml.CharData:
// CharData X X
case xml.StartElement:
// StartElement X X
startTokPrev, _ := tokPrev.(xml.StartElement)
err := unmarshalTemplateElement(d, startTokPrev, t)
if err != nil {
return err
}
case xml.EndElement:
// EndElement X X
break
}
}
return nil
}
// unmarshalTemplateElement unmarshal only one element, a pair, or a vector
func unmarshalTemplateElement(d *xml.Decoder, tokPrev xml.StartElement, t *Template) error {
// Ensure that (tok, tokNext) != (Chardata, StartElement)
// if tokNext is a start element, then we should have a vector
tok, err := d.Token()
if err != nil {
if err.Error() == io.EOF.Error() {
return nil
}
return err
}
switch tok.(type) {
case xml.EndElement:
// StartElement EndElement X
case xml.StartElement:
// StartElement StartElement X --> it's a vector
var pair xmlPair
tokPairStart, ok := tok.(xml.StartElement)
if !ok {
return fmt.Errorf("unmarshalTemplateElement UnmarshalXML: start element attended")
}
vec := t.AddVector(tokPrev.Name.Local)
// Add first pair from tok
err := d.DecodeElement(&pair, &tokPairStart)
if err != nil {
return err
}
vec.AddPair(pair.XMLName.Local, pair.Content)
// unmarshal the rest of the vector
err = vec.UnmarshalXML(d, tokPrev)
if err != nil {
return fmt.Errorf("unmarshalTemplateElement vector UnmarshalXML: %s", err)
}
case xml.CharData:
// StartElement CharData X
// We need to know what is the token X
// As we call Token method again, we must save the chardata buffer in case we need it later
tokSav := xml.CopyToken(tok)
loop:
for {
// need to look at a third token to distinguish between pair and vector
tokNext, err := d.Token()
if err != nil {
if err.Error() == io.EOF.Error() {
return nil
}
return err
}
switch tokNext.(type) {
case xml.StartElement:
// StartElement CharData StartElement
// There is some characters between two opening tags,
// we shift last element to the left:
// StartElement StartElement X
tok = xml.CopyToken(tokNext)
case xml.EndElement:
// StartElement CharData EndElement --> it's a pair
// Or, after shift:
// StartElement StartElement EndElement --> not handled below
// It's a pair
tokData, ok := tokSav.(xml.CharData)
if ok {
t.AddPair(tokPrev.Name.Local, string(tokData))
}
break loop
case xml.CharData:
// StartElement CharData CharData --> should not occur
// Or, after shift:
// StartElement StartElement CharData --> it's a vector
// Need to copy, to avoid next Token call to rewrite the chardata buffer
tokData, _ := tokNext.(xml.CharData)
cdata := tokData.Copy()
startVec, ok := tok.(xml.StartElement)
if !ok {
return fmt.Errorf("unmarshalTemplateElement: start element expected")
}
vec := t.AddVector(tokPrev.Name.Local)
// consume EndElement of the first pair
_, err = d.Token()
if err != nil {
return err
}
vec.AddPair(startVec.Name.Local, string(cdata))
// unmarshal the rest of the vector
err = vec.UnmarshalXML(d, tokPrev)
if err != nil {
return fmt.Errorf("unmarshalTemplateElement vector UnmarshalXML: %s", err)
}
break loop
}
}
}
return nil
}
// UnmarshalXML parse dynamically a bunch of pairs
func (t *Pairs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// In case we unmarshal a simple vector, then we need to initialize it
if t == nil {
pairs := make(Pairs, 0, 2)
t = &pairs
}
loop:
for {
// Retrieve the next token
token, err := d.Token()
if err != nil {
if err.Error() == io.EOF.Error() {
return nil
}
return err
}
// Add a pair to the vector on a StartElement
switch e := token.(type) {
case xml.StartElement:
// unexpected
case xml.CharData:
t.AddPair(start.Name.Local, string(e))
case xml.EndElement:
break loop
}
}
return nil
}
// UnmarshalXML parse dynamically a vector. Either a single vector, or inside of a TEMPLATE.
func (t *Vector) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// In case we unmarshal a simple vector, then we need to initialize it
if t.Pairs == nil {
t.XMLName.Local = start.Name.Local
t.Pairs = make([]Pair, 0, 2)
}
var pair xmlPair
loop:
for {
// Retrieve the next token
token, err := d.Token()
if err != nil {
if err.Error() == io.EOF.Error() {
return nil
}
return err
}
// Add a pair to the vector on a StartElement
switch token.(type) {
case xml.StartElement:
startEl, _ := token.(xml.StartElement)
err = d.DecodeElement(&pair, &startEl)
if err != nil {
return err
}
t.AddPair(pair.XMLName.Local, pair.Content)
case xml.CharData:
case xml.EndElement:
break loop
}
}
return nil
}