-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_encoder.go
More file actions
109 lines (100 loc) · 3.67 KB
/
Copy pathxml_encoder.go
File metadata and controls
109 lines (100 loc) · 3.67 KB
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
package binxml
import (
"encoding/xml"
"fmt"
dt "wcfproxy/datatype"
)
type XMLEncoder struct {
dict *StringDict
typehint bool
}
/* Convert a slice of IRecords to the corresponding XML representation.
* If XMLEncoder.typehint is true, insert type hint information to enable
* unabiguous conversion back from XML to the binary representation. */
func (e *XMLEncoder) EncodeEnvelope(records []IRecord) ([]xml.Token, error) {
var xmlTokens []xml.Token
var openElements dt.Stack[IElementRecord]
lastRecordType := byte(0)
for _, rec := range records {
switch r := rec.(type) {
case IElementRecord:
// encountered an element record which represents an XML StartElement
// (optionally with attributes) but without XML EndElement
xmlTokens = append(xmlTokens, r.StartElement(e.dict, e.typehint))
openElements.Push(r)
lastRecordType = r.GetType()
case ITextRecord:
// encountered a text record which represents XML CharData or an
// attribute value, possibly including an XML EndElement
var charData xml.CharData
if isCombinable(r, lastRecordType) {
// don't emit a type hint in order to allow combining of char data
charData = r.ToXMLChars(e.dict, ContextText, false)
} else {
charData = r.ToXMLChars(e.dict, ContextText, e.typehint)
}
xmlTokens = append(xmlTokens, charData)
lastRecordType = r.GetType()
if r.HasEndElement() {
toClose, ok := openElements.Pop()
if !ok {
return nil, fmt.Errorf("invalid state: extra EndElement")
}
xmlTokens = append(xmlTokens, toClose.EndElement(e.dict))
lastRecordType = RTEndElement
}
case *ArrayRecord:
// encountered an array record which represents a sequence of XML elements
// an array always has an XML EndElement builtin which is expanded to get the full array represetnation
// arrayTokens := r.GetElements(e.dict, e.typehint)
arrayTokens := r.GetElements(e.dict, false)
if len(arrayTokens) > 0 {
// we need to mark the first array element in order to ensure it can be correctly decoded again
firstArrayToken, ok := arrayTokens[0].(xml.StartElement)
if !ok {
return nil, fmt.Errorf("array must start with StartElement")
}
// firstArrayToken.Attr = append(firstArrayToken.Attr, createArrayStartMarker())
rt, err := createArrayMarker(r.ItemType)
if err != nil {
return nil, err
}
firstArrayToken.Attr = append(firstArrayToken.Attr, rt)
arrayTokens[0] = firstArrayToken
xmlTokens = append(xmlTokens, arrayTokens...)
lastRecordType = RTEndElement // arrays end with an EndElementRecord
}
default:
// plain EndElements are handled here
switch rec.GetType() {
case RTEndElement:
toClose, ok := openElements.Pop()
if !ok {
return nil, fmt.Errorf("invalid state: extra EndElement")
}
xmlTokens = append(xmlTokens, toClose.EndElement(e.dict))
lastRecordType = RTEndElement
default:
return nil, fmt.Errorf("unhandled record type %v", rec.GetType())
}
}
}
if openElements.Size() != 0 {
return nil, fmt.Errorf("invalid state: unclosed elements left")
}
return xmlTokens, nil
}
func isCombinable(record ITextRecord, prevRecordType byte) bool {
t := record.GetType()
switch {
case t == RTBytes8Text || t == RTBytes8TextWithEndElement:
fallthrough
case t == RTBytes16Text || t == RTBytes16TextWithEndElement:
fallthrough
case t == RTBytes32Text || t == RTBytes32TextWithEndElement:
// last record type must not have had end element
return prevRecordType == RTBytes8Text || prevRecordType == RTBytes16Text || prevRecordType == RTBytes32Text
default:
return false
}
}