-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlReader.go
327 lines (305 loc) · 6.42 KB
/
xmlReader.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
/*
* Copyright (c) 2021 BlueStorm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Package XmlReader xml fast parser
//
package XmlReader
import (
"bytes"
"github.com/BlueStorm001/bufferPool"
)
type XReader struct {
bufferPool *bufferPool.BufferPool
bufferMax int //最大缓冲字节数
}
type XDecoder struct {
bus *bufferPool.ByteBuffer //原始记录
len int //bus长度
index int //Read位置
isc bool
b byte //当前Read byte
//buffer bytes.Buffer //存储的缓冲节点
buffer []byte //存储的缓冲节点
bufferMax int //最大缓冲字节数
bufferIndex int //缓冲位置
reader *XReader
}
type XToken struct {
Name string
Attr map[string]string
Data string
StartElement bool
EndElement bool
IsText bool
Finish bool
}
// New 可在全局声明XReader
// max1:buffer池数量,给予最大并发量数值,默认500。
// max2:xml节点最大长度,默认500。例如:<root id="a"> 长度是13。
func New(max ...int) *XReader {
var m1 = 500
if len(max) > 0 {
m1 = max[0]
}
var m2 = 500
if len(max) > 1 {
m2 = max[1]
}
return &XReader{
bufferPool: bufferPool.New(m1),
bufferMax: m2,
}
}
// Load 载入xml文档
func (r *XReader) Load(input []byte) *XDecoder {
d := &XDecoder{}
d.bus = r.bufferPool.Get()
d.bus.Write(input)
d.len = d.bus.Len()
d.index = 0
d.bufferMax = r.bufferMax
d.buffer = make([]byte, d.bufferMax)
d.bufferIndex = -1
d.reader = r
return d
}
func NewDefault(input string) *XDecoder {
return Create(input, 500)
}
func Create(input string, bufferMax int) *XDecoder {
return CreateBytes([]byte(input), bufferMax)
}
var byteBuffer = bufferPool.NewDefault()
func CreateBytes(input []byte, bufferMax int) *XDecoder {
d := &XDecoder{}
d.bus = byteBuffer.Get()
d.bus.Write(input)
d.len = d.bus.Len()
d.index = 0
d.bufferMax = bufferMax
d.buffer = make([]byte, d.bufferMax)
d.bufferIndex = -1
return d
}
// Text 获取文本
func (d *XDecoder) Text() string {
var b bytes.Buffer
for i := d.index; i < d.len; i++ {
if d.pred(i-1) == '/' && d.pred(i) == '>' {
return ""
}
if d.pred(i) == '<' && d.pred(i+1) == '/' {
d.index = i
return b.String()
}
//fmt.Println(d.pred(i-1), d.pred(i), d.pred(i+1), d.pred(i+2), d.pred(i+3))
if d.get(i) == '.' {
return ""
}
b.WriteByte(d.b)
if d.pred(i+1) == '<' && d.pred(i+2) == '/' {
d.index = i
return b.String()
}
}
return ""
}
// Read 循环内读取xml
func (d *XDecoder) Read() XToken {
var token = XToken{}
for i := d.index; i < d.len; i++ {
el := d.get(i)
if el == '.' {
return d.complete()
}
//只查找<
if el == '<' {
//获取ElementName
for {
i++
el1 := d.get(i)
if el1 == '.' {
return d.complete()
}
//结束
if el1 == '/' {
for {
i++
el2 := d.get(i)
if el2 == '.' {
return d.complete()
}
if el2 == '_' {
continue
}
if el2 == '@' {
d.append()
} else {
token.Name = d.str()
token.StartElement = false
token.EndElement = true
return d.next(i, token)
}
}
}
if el1 == '@' {
d.append()
} else {
token.Name = d.str()
token.StartElement = true
token.EndElement = false
break
}
}
//结束本次
if d.end() {
return d.next(i, token)
}
//开始获取属性
var k string
for {
i++
e := d.get(i)
if e == '.' {
return d.complete()
}
if e == '_' {
continue
}
if d.end() {
return d.next(i, token)
}
if e == '@' {
d.append()
} else {
if k == "" {
k = d.str()
}
}
//获得了KEY 再获取属性value
if e == '=' && k != "" {
do := 0
//获取属性value
for {
i++
if d.get(i) == '"' {
do++
if do == 1 {
continue
}
}
if do == 1 {
d.append()
} else if do == 2 {
if token.Attr == nil {
token.Attr = make(map[string]string)
}
token.Attr[k] = d.str()
k = ""
break
}
}
}
}
}
}
return d.complete()
}
func (d *XDecoder) pred(i int) byte {
if i >= d.len {
return '.' //结束
}
return match(d.bus.B[i])
}
func (d *XDecoder) get(i int) byte {
if i >= d.len {
return '.' //结束
}
d.b = d.bus.B[i]
return match(d.b)
}
func match(b byte) byte {
switch b {
case 60:
return '<'
case 47:
return '/'
case 62:
return '>'
case 32: // 空格
return '_'
case 39, 34: //单双引
return '"'
case 61:
return '='
default:
return '@'
}
}
func (d *XDecoder) end() bool {
switch d.b {
case '/':
return true
case '>':
return true
}
return false
}
func (d *XDecoder) str() string {
if d.bufferIndex < 0 {
return ""
}
//切片
buffer := d.buffer[0 : d.bufferIndex+1]
//初始化
d.bufferIndex = -1
return string(buffer)
}
func (d *XDecoder) append() {
//d.buffer = append(d.buffer, d.b)
if d.bufferIndex >= d.bufferMax-1 {
return
}
d.bufferIndex++
d.buffer[d.bufferIndex] = d.b
}
func (d *XDecoder) next(i int, token XToken) XToken {
d.index = i + 1
return token
}
func (d *XDecoder) clear() {
if d.reader == nil {
byteBuffer.Put(d.bus)
} else {
d.reader.bufferPool.Put(d.bus)
}
d.len = 0
d.b = 0
d.index = 0
d.isc = false
d.buffer = nil
}
func (d *XDecoder) complete() XToken {
var token = XToken{}
token.Finish = true
d.clear()
return token
}