-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transformer.go
382 lines (331 loc) · 8.55 KB
/
transformer.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
// Copyright 2016 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package xmlstream
import (
"encoding/xml"
"io"
)
// Discard returns a TokenWriter on which all calls succeed without doing
// anything.
func Discard() TokenWriter {
return discard{}
}
type discard struct{}
func (discard) EncodeToken(_ xml.Token) error { return nil }
// TokenWriter is anything that can encode tokens to an XML stream, including an
// xml.Encoder.
type TokenWriter interface {
EncodeToken(t xml.Token) error
}
// The Flusher interface is implemented by TokenWriters that can flush buffered
// data to an underlying receiver.
type Flusher interface {
Flush() error
}
// WriterTo writes tokens to w until there are no more tokens to write or when
// an error occurs.
// The return value n is the number of tokens written.
// Any error encountered during the write is also returned.
//
// The Copy function uses WriterTo if available.
type WriterTo interface {
WriteXML(TokenWriter) (n int, err error)
}
// ReaderFrom reads tokens from r until EOF or error.
// The return value n is the number of tokens read.
// Any error except io.EOF encountered during the read is also returned.
//
// The Copy function uses ReaderFrom if available.
type ReaderFrom interface {
ReadXML(xml.TokenReader) (n int, err error)
}
// Marshaler is the interface implemented by objects that can marshal themselves
// into valid XML elements.
type Marshaler interface {
TokenReader() xml.TokenReader
}
// TokenReadWriter is the interface that groups the basic Token, EncodeToken,
// and Flush methods.
type TokenReadWriter interface {
xml.TokenReader
TokenWriter
}
// TokenReadWriteCloser is the interface that groups the basic Token,
// EncodeToken, Flush, and Close methods.
type TokenReadWriteCloser interface {
xml.TokenReader
TokenWriter
io.Closer
}
// TokenWriteCloser is the interface that groups the basic EncodeToken, and
// Close methods.
type TokenWriteCloser interface {
TokenWriter
io.Closer
}
// EncodeCloser is the interface that groups Encoder and io.Closer.
type EncodeCloser interface {
Encoder
io.Closer
}
// DecodeCloser is the interface that groups Decoder and io.Closer.
type DecodeCloser interface {
Decoder
io.Closer
}
// TokenWriteFlushCloser is the interface that groups the basic EncodeToken,
// Flush, and Close methods.
type TokenWriteFlushCloser interface {
TokenWriter
io.Closer
Flusher
}
// TokenWriteFlusher is the interface that groups the basic EncodeToken, and
// Flush methods.
type TokenWriteFlusher interface {
TokenWriter
Flusher
}
// TokenReadCloser is the interface that groups the basic Token and Close
// methods.
type TokenReadCloser interface {
xml.TokenReader
io.Closer
}
// Encoder is the interface that groups the Encode, EncodeElement, and
// EncodeToken methods.
// Encoder is implemented by xml.Encoder.
type Encoder interface {
TokenWriter
Encode(v interface{}) error
EncodeElement(v interface{}, start xml.StartElement) error
}
// Decoder is the interface that groups the Decode, DecodeElement, and Token
// methods.
// Decoder is implemented by xml.Decoder.
type Decoder interface {
xml.TokenReader
Decode(v interface{}) error
DecodeElement(v interface{}, start *xml.StartElement) error
}
// DecodeEncoder is the interface that groups the Encoder and Decoder
// interfaces.
type DecodeEncoder interface {
Decoder
Encoder
}
// TokenReadEncoder is the interface that groups the Encode, EncodeElement,
// EncodeToken, and Token methods.
type TokenReadEncoder interface {
xml.TokenReader
Encoder
}
// A Transformer returns a new TokenReader that returns transformed tokens
// read from src.
type Transformer func(src xml.TokenReader) xml.TokenReader
// Inspect performs an operation for each token in the stream without
// transforming the stream in any way.
func Inspect(f func(t xml.Token)) Transformer {
return func(src xml.TokenReader) xml.TokenReader {
return inspector{
d: src,
f: f,
}
}
}
type inspector struct {
d xml.TokenReader
f func(t xml.Token)
}
func (t inspector) Token() (xml.Token, error) {
tok, err := t.d.Token()
if err != nil {
return nil, err
}
t.f(tok)
return tok, nil
}
// Map returns a Transformer that maps the tokens in the input using the given
// mapping.
func Map(mapping func(t xml.Token) xml.Token) Transformer {
return func(src xml.TokenReader) xml.TokenReader {
return &mapper{
d: src,
f: mapping,
}
}
}
type mapper struct {
d xml.TokenReader
f func(t xml.Token) xml.Token
}
func (m *mapper) Token() (xml.Token, error) {
tok, err := m.d.Token()
if err != nil {
return nil, err
}
return m.f(tok), nil
}
// Remove returns a Transformer that removes tokens for which f matches.
func Remove(f func(t xml.Token) bool) Transformer {
return func(src xml.TokenReader) xml.TokenReader {
return remover{
d: src,
f: f,
}
}
}
type remover struct {
d xml.TokenReader
f func(t xml.Token) bool
}
func (r remover) Token() (t xml.Token, err error) {
for {
t, err = r.d.Token()
switch {
case err != nil:
return nil, err
case r.f(t):
continue
}
return
}
}
// RemoveElement returns a Transformer that removes entire elements (and their
// children) if f matches the elements start token.
func RemoveElement(f func(start xml.StartElement) bool) Transformer {
return func(src xml.TokenReader) xml.TokenReader {
return &elementremover{
d: src,
f: f,
}
}
}
type elementremover struct {
d xml.TokenReader
f func(start xml.StartElement) bool
}
func (er *elementremover) Token() (t xml.Token, err error) {
for {
t, err = er.d.Token()
if err != nil {
return nil, err
}
if start, ok := t.(xml.StartElement); ok && er.f(start) {
// Skip the element and read a new token.
if err = Skip(er.d); err != nil {
return t, err
}
continue
}
return
}
}
// BUG(ssw): Multiple uses of RemoveAttr will iterate over the attr list multiple times.
// RemoveAttr returns a Transformer that removes attributes from
// xml.StartElement's if f matches.
func RemoveAttr(f func(start xml.StartElement, attr xml.Attr) bool) Transformer {
return func(src xml.TokenReader) xml.TokenReader {
return &attrRemover{
d: src,
f: f,
}
}
}
type attrRemover struct {
d xml.TokenReader
f func(xml.StartElement, xml.Attr) bool
}
func (ar *attrRemover) Token() (xml.Token, error) {
tok, err := ar.d.Token()
if err != nil {
return tok, err
}
start, ok := tok.(xml.StartElement)
if !ok {
return tok, err
}
b := start.Attr[:0]
for _, attr := range start.Attr {
if !ar.f(start, attr) {
b = append(b, attr)
}
}
start.Attr = b
return start, nil
}
// InsertFunc calls f after writing any start element to the stream.
// The function can decide based on the passed in StartElement whether to insert
// any additional tokens into the stream by writing them to w.
func InsertFunc(f func(start xml.StartElement, level uint64, w TokenWriter) error) Transformer {
if f == nil {
f = func(xml.StartElement, uint64, TokenWriter) error { return nil }
}
var depth uint64
var pr *PipeReader
return func(r xml.TokenReader) xml.TokenReader {
return ReaderFunc(func() (xml.Token, error) {
if pr != nil {
tok, err := pr.Token()
if tok != nil || err != io.EOF {
return tok, err
}
pr = nil
}
tok, err := r.Token()
if err != nil {
return tok, err
}
switch t := tok.(type) {
case xml.StartElement:
depth++
var pw *PipeWriter
pr, pw = Pipe()
go func() {
pw.CloseWithError(f(t, depth, pw))
}()
case xml.EndElement:
if depth > 0 {
depth--
}
}
return tok, err
})
}
}
// Insert adds one XML stream to another just before the close token, matching
// on the token name.
// If either component of the name is empty it is considered a wildcard.
func Insert(name xml.Name, m Marshaler) Transformer {
return func(r xml.TokenReader) xml.TokenReader {
var inner xml.TokenReader
return ReaderFunc(func() (xml.Token, error) {
if inner != nil {
tok, err := inner.Token()
switch {
case tok != nil && err == io.EOF:
inner = nil
return tok, nil
case tok == nil && err == io.EOF:
inner = nil
default:
return tok, err
}
}
tok, err := r.Token()
if err != nil {
return tok, err
}
if end, ok := tok.(xml.EndElement); ok &&
((name.Space == "" && name.Local == "") ||
end.Name == name ||
(end.Name.Space == name.Space && name.Local == "") ||
(end.Name.Local == name.Local && name.Space == "")) {
inner = MultiReader(m.TokenReader(), Token(end))
return inner.Token()
}
return tok, err
})
}
}