-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmldecoder.go
467 lines (404 loc) · 10 KB
/
xmldecoder.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package goxml
import (
"encoding/xml"
"fmt"
"io"
"sort"
"strings"
)
var (
entitiesReplacer = strings.NewReplacer("&", "&", "<", "<", "\"", """)
ids chan int
)
func genIntegerSequence(ids chan int) {
i := int(0)
for {
ids <- i
i++
}
}
func init() {
ids = make(chan int)
go genIntegerSequence(ids)
}
// XMLNode is one of Document, Element, CharData, ProcInst, Comment
type XMLNode interface {
toxml(map[string]bool) string
setParent(XMLNode)
getID() int
Children() []XMLNode
}
// SortAndEliminateDuplicates returns the nodes sorted in document order and
// duplicates deleted.
func (xn SortByDocumentOrder) SortAndEliminateDuplicates() SortByDocumentOrder {
sort.Sort(xn)
if len(xn) < 2 {
return xn
}
var e int = 1
for i := 1; i < len(xn); i++ {
if xn[i].getID() == xn[i-1].getID() {
continue
}
xn[e] = xn[i]
e++
}
return xn[:e]
}
// Appender implements the function Append(XMLNode)
type Appender interface {
Append(n XMLNode)
}
// Attribute represents an attribute
type Attribute struct {
ID int
Name string
Namespace string
Prefix string
Value string
}
func (a Attribute) String() string {
return fmt.Sprintf("%s=%q", a.Name, a.Value)
}
// Stringvalue returns the attribute value.
func (a Attribute) Stringvalue() string {
return fmt.Sprintf("%s", a.Value)
}
// Children returns the empty sequence.
func (a Attribute) Children() []XMLNode {
return nil
}
func (a Attribute) setParent(n XMLNode) {
panic("nyi")
}
// getID returns the ID of this node
func (a Attribute) getID() int {
return a.ID
}
// toxml returns the XML representation of the attribute.
func (a Attribute) toxml(namespacePrinted map[string]bool) string {
panic("nyi")
}
// Element represents an XML element
type Element struct {
ID int
Name string
Prefix string
Parent XMLNode
Namespaces map[string]string
children []XMLNode
attributes []xml.Attr
Line int
Pos int
}
// NewElement returns an initialized Element.
func NewElement() *Element {
elt := Element{}
elt.Namespaces = make(map[string]string)
return &elt
}
func (elt Element) String() string {
var as []string
for _, attribs := range elt.Attributes() {
as = append(as, attribs.String())
}
return "<" + elt.Name + " " + strings.Join(as, " ") + ">"
}
// Stringvalue returns the text nodes of this elements and its children.
func (elt Element) Stringvalue() string {
var as []string
for _, cld := range elt.children {
switch t := cld.(type) {
case CharData:
as = append(as, string(t.Contents))
case *Element:
as = append(as, t.Stringvalue())
}
}
return strings.Join(as, "")
}
// Append appends an XML node to the element.
func (elt *Element) Append(n XMLNode) {
switch t := n.(type) {
case Attribute:
for i, attr := range elt.attributes {
if attr.Name.Local == t.Name && attr.Name.Space == t.Namespace {
elt.attributes[i].Value = t.Value
return
}
}
elt.attributes = append(elt.attributes, xml.Attr{
Name: xml.Name{Local: t.Name, Space: t.Namespace},
Value: t.Value,
})
return
case CharData:
// combine string cdata string if necessary
if l := len(elt.children); l > 0 {
if str, ok := elt.children[l-1].(CharData); ok {
elt.children[l-1] = CharData{Contents: str.Contents + t.Contents}
return
}
}
}
elt.children = append(elt.children, n)
}
// Children returns all child nodes from elt
func (elt Element) Children() []XMLNode {
return elt.children
}
// SetAttribute appends attr to the list of attributes of elt. If an attribute
// of this name already exists, the existing one will be discarded.
func (elt *Element) SetAttribute(attr xml.Attr) {
var newAttributes = make([]xml.Attr, 0, len(elt.attributes)+1)
for _, curattr := range elt.attributes {
if curattr.Name != attr.Name {
newAttributes = append(newAttributes, curattr)
}
}
newAttributes = append(newAttributes, attr)
elt.attributes = newAttributes
}
// Attributes returns all attributes for this element
func (elt Element) Attributes() []*Attribute {
var attribs []*Attribute
for _, xmlattr := range elt.attributes {
attr := Attribute{}
attr.Name = xmlattr.Name.Local
attr.Value = xmlattr.Value
attr.Namespace = xmlattr.Name.Space
attribs = append(attribs, &attr)
}
return attribs
}
func (elt *Element) setParent(n XMLNode) {
elt.Parent = n
}
// getID returns the ID of this node
func (elt Element) getID() int {
return elt.ID
}
// ToXML returns a valid XML document
func (elt Element) ToXML() string {
return elt.toxml(make(map[string]bool))
}
func (elt Element) toxml(namespacePrinted map[string]bool) string {
var sb strings.Builder
sb.WriteRune('<')
eltname := elt.Name
if elt.Prefix != "" {
eltname = elt.Prefix + ":" + eltname
}
sb.WriteString(eltname)
for prefix, ns := range elt.Namespaces {
if _, ok := namespacePrinted[ns]; !ok {
namespacePrinted[ns] = true
if prefix == "" {
prefix = "xmlns"
} else {
prefix = "xmlns:" + prefix
}
fmt.Fprintf(&sb, " %s=\"%s\"", prefix, ns)
}
}
for _, att := range elt.attributes {
fmt.Fprintf(&sb, " %s=\"%s\"", att.Name.Local, escape(att.Value))
}
if len(elt.children) == 0 {
sb.WriteString(" />")
return sb.String()
}
sb.WriteString(">")
for _, child := range elt.children {
sb.WriteString(child.toxml(namespacePrinted))
}
fmt.Fprintf(&sb, "</%s>", eltname)
return sb.String()
}
// CharData is a string
type CharData struct {
ID int
Contents string
}
// toxml returns the XML representation of the string.
func (cd CharData) toxml(namespacePrinted map[string]bool) string {
return escape(string(cd.Contents))
}
func (cd CharData) setParent(n XMLNode) {
// dummy
}
// Children is a dummy function
func (cd CharData) Children() []XMLNode {
return nil
}
// getID returns the ID of this node
func (cd CharData) getID() int {
return cd.ID
}
// Comment is a string
type Comment struct {
ID int
Contents string
}
// toxml returns the XML representation of the comment.
func (cmt Comment) toxml(namespacePrinted map[string]bool) string {
return fmt.Sprintf("<!--%s-->", cmt.Contents)
}
func (cmt Comment) setParent(n XMLNode) {
// dummy
}
// Children is a dummy function
func (cmt Comment) Children() []XMLNode {
return nil
}
// getID returns the ID of this node
func (cmt Comment) getID() int {
return cmt.ID
}
// ProcInst is a string
type ProcInst struct {
ID int
Target string
Inst []byte
}
// toxml returns the XML representation of the string.
func (pi ProcInst) toxml(namespacePrinted map[string]bool) string {
return fmt.Sprintf("<?%s %s?>", pi.Target, string(pi.Inst))
}
func (pi ProcInst) setParent(n XMLNode) {
// dummy
}
// Children is a dummy function
func (pi ProcInst) Children() []XMLNode {
return nil
}
// getID returns the ID of this node
func (pi ProcInst) getID() int {
return pi.ID
}
// XMLDocument represents an XML file for decoding
type XMLDocument struct {
ID int
children []XMLNode
}
func (xr XMLDocument) String() string {
return "<xmldoc>"
}
// Append appends an XML node to the document.
func (xr *XMLDocument) Append(n XMLNode) {
xr.children = append(xr.children, n)
n.setParent(xr)
}
// Children returns all child nodes from elt
func (xr *XMLDocument) Children() []XMLNode {
return xr.children
}
// Root returns the root node of the document
func (xr *XMLDocument) Root() (*Element, error) {
for _, c := range xr.children {
if elt, ok := c.(*Element); ok {
return elt, nil
}
}
return nil, fmt.Errorf("cannot find root element")
}
// ToXML returns a valid XML document
func (xr *XMLDocument) ToXML() string {
return xr.toxml(make(map[string]bool))
}
func (xr *XMLDocument) setParent(n XMLNode) {
// dummy,TODO error handling: document must not have a parent
}
// getID returns the ID of this node
func (xr XMLDocument) getID() int {
return xr.ID
}
// toxml returns the XML representation of the document.
func (xr *XMLDocument) toxml(namespacePrinted map[string]bool) string {
var sb strings.Builder
for _, v := range xr.children {
sb.WriteString(v.toxml(namespacePrinted))
}
return sb.String()
}
// Parse reads the XML file from r. r is not closed.
func Parse(r io.Reader) (*XMLDocument, error) {
var err error
var tok xml.Token
var cur XMLNode
doc := &XMLDocument{ID: <-ids}
eltstack := []XMLNode{doc}
cur = doc
dec := xml.NewDecoder(r)
for {
tok, err = dec.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch v := tok.(type) {
case xml.StartElement:
tmp := NewElement()
tmp.ID = <-ids
if c, ok := cur.(*Element); ok {
for k, v := range c.Namespaces {
tmp.Namespaces[k] = v
}
}
tmp.Line, tmp.Pos = dec.InputPos()
tmp.Name = v.Name.Local
tmp.Parent = cur
for _, att := range v.Attr {
if att.Name.Local == "xmlns" {
tmp.Namespaces[""] = att.Value
} else if att.Name.Space == "xmlns" {
tmp.Namespaces[att.Name.Local] = att.Value
} else {
tmp.attributes = append(tmp.attributes, att)
}
}
for prefix, ns := range tmp.Namespaces {
if v.Name.Space == ns {
tmp.Prefix = prefix
}
}
if c, ok := cur.(Appender); ok {
c.Append(tmp)
}
cur = tmp
eltstack = append(eltstack, cur)
case xml.CharData:
cd := CharData{ID: <-ids, Contents: string(v)}
if c, ok := cur.(Appender); ok {
c.Append(cd)
}
case xml.ProcInst:
pi := ProcInst{ID: <-ids}
pi.Target = v.Copy().Target
pi.Inst = v.Copy().Inst
if c, ok := cur.(Appender); ok {
c.Append(pi)
}
case xml.Comment:
cmt := Comment{ID: <-ids, Contents: string(v)}
if c, ok := cur.(Appender); ok {
c.Append(cmt)
}
case xml.EndElement:
cur, eltstack = eltstack[len(eltstack)-2], eltstack[:len(eltstack)-1]
}
}
return doc, nil
}
func escape(in string) string {
return entitiesReplacer.Replace(in)
}
// SortByDocumentOrder sorts the nodes by document order.
type SortByDocumentOrder []XMLNode
func (xn SortByDocumentOrder) Len() int { return len(xn) }
func (xn SortByDocumentOrder) Swap(i, j int) { xn[i], xn[j] = xn[j], xn[i] }
func (xn SortByDocumentOrder) Less(i, j int) bool {
return xn[i].getID() < xn[j].getID()
}