-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml.go
214 lines (168 loc) · 3.53 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
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
package parser
import (
"encoding/xml"
"io"
"github.com/ChrisTrenkamp/xsel/node"
"golang.org/x/net/html/charset"
)
const xmlns = "xmlns"
type XmlElement struct {
space, local string
}
func (x XmlElement) Space() string {
return x.space
}
func (x XmlElement) Local() string {
return x.local
}
type XmlNamespace struct {
prefix, value string
}
func (x XmlNamespace) Prefix() string {
return x.prefix
}
func (x XmlNamespace) NamespaceValue() string {
return x.value
}
type XmlAttribute struct {
space, local, value string
}
func (x XmlAttribute) Space() string {
return x.space
}
func (x XmlAttribute) Local() string {
return x.local
}
func (x XmlAttribute) AttributeValue() string {
return x.value
}
type XmlCharData struct {
value string
}
func (x XmlCharData) CharDataValue() string {
return x.value
}
type XmlComment struct {
value string
}
func (x XmlComment) CommentValue() string {
return x.value
}
type XmlProcInst struct {
target, value string
}
func (x XmlProcInst) Target() string {
return x.target
}
func (x XmlProcInst) ProcInstValue() string {
return x.value
}
var emptyXmlAttrs = make([]XmlAttribute, 0)
var emptyXmlNamespaces = make([]XmlNamespace, 0)
type xmlParser struct {
xmlReader *xml.Decoder
namespaces []XmlNamespace
nsPos int
attrs []XmlAttribute
attrPos int
}
func (x *xmlParser) Pull() (node.Node, bool, error) {
if x.nsPos < len(x.namespaces) {
n := x.namespaces[x.nsPos]
x.nsPos++
return n, false, nil
}
if x.attrPos < len(x.attrs) {
a := x.attrs[x.attrPos]
x.attrPos++
return a, false, nil
}
x.attrs = emptyXmlAttrs
x.attrPos = 0
x.namespaces = emptyXmlNamespaces
x.nsPos = 0
tok, err := x.xmlReader.Token()
if err != nil {
return nil, false, err
}
switch n := tok.(type) {
case xml.StartElement:
x.namespaces = createXmlNamespaces(n.Attr)
x.attrs = createXmlAttrs(n.Attr)
return XmlElement{
space: n.Name.Space,
local: n.Name.Local,
}, false, nil
case xml.CharData:
return XmlCharData{
value: (string)(n),
}, false, nil
case xml.Comment:
return XmlComment{
value: (string)(n),
}, false, nil
case xml.ProcInst:
return XmlProcInst{
target: n.Target,
value: string(n.Inst),
}, false, nil
}
//case xml.EndElement:
return nil, true, nil
}
func createXmlNamespaces(attrs []xml.Attr) []XmlNamespace {
ret := make([]XmlNamespace, 0, 1)
ns := XmlNamespace{
prefix: "xml",
value: "http://www.w3.org/XML/1998/namespace",
}
ret = append(ret, ns)
for _, i := range attrs {
if i.Name.Space == "" && i.Name.Local == xmlns {
ns = XmlNamespace{
prefix: "",
value: i.Value,
}
ret = append(ret, ns)
}
if i.Name.Local == xmlns {
ns = XmlNamespace{
prefix: i.Name.Space,
value: i.Value,
}
ret = append(ret, ns)
}
}
return ret
}
func createXmlAttrs(attrs []xml.Attr) []XmlAttribute {
ret := make([]XmlAttribute, 0, len(attrs))
for _, i := range attrs {
if i.Name.Space == xmlns || i.Name.Local == xmlns {
continue
}
next := XmlAttribute{
space: i.Name.Space,
local: i.Name.Local,
value: i.Value,
}
ret = append(ret, next)
}
return ret
}
type XmlParseOptions func(d *xml.Decoder)
// Creates a Parser that reads the given XML document.
func ReadXml(in io.Reader, opts ...XmlParseOptions) Parser {
xmlReader := xml.NewDecoder(in)
xmlReader.CharsetReader = charset.NewReaderLabel
for _, i := range opts {
i(xmlReader)
}
return &xmlParser{
xmlReader: xmlReader,
namespaces: emptyXmlNamespaces,
nsPos: 0,
attrs: emptyXmlAttrs,
attrPos: 0,
}
}