-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlCrush.go
489 lines (405 loc) · 10.4 KB
/
xmlCrush.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package xmlCrush
import (
"bufio"
"io"
//"log"
"strconv"
"strings"
)
type Node struct {
Ind int `bson:"ind,omitempty" json:"ind,omitempty"`
Tag string `bson:"tag,omitempty" json:"tag,omitempty"`
Attr map[string]string `bson:"attr,omitempty" json:"attr,omitempty"`
Closed bool `bson:"closed,omitempty" json:"closed,omitempty"`
}
type inNode struct {
Tag string
Ind int
}
type Ext struct {
Tags []string
Callback func(Node, string, string) error
}
func Crush(reader io.Reader) (nodes []Node, content string, err error) {
s := bufio.NewScanner(reader)
start := 0
end := 0
ind := 0
inNodes := []inNode{}
for s.Scan() {
line := strings.Replace(s.Text(), "~~", "~~", -1)
start = 0
for {
//log.Println("find start:", line)
start = strings.Index(line, "<")
if start == -1 {
break
}
end = strings.Index(line, ">")
if end == -1 {
break
}
//start node
tagStr := line[start+1 : end]
if tagStr[0:1] == "/" { //is a close
tag := tagStr[1:]
tag = strings.Replace(tag, " ", "", -1)
//find if in tag
found := false
for i := len(inNodes) - 1; i >= 0; i-- {
if inNodes[i].Tag == tag {
//log.Println("close:", inNodes[i][0], start, end)
found = true
line = line[0:start] + "~~/" + strconv.Itoa(inNodes[i].Ind) + "~~" + line[end+1:]
if len(inNodes) == 1 {
inNodes = []inNode{}
} else {
inNodes = append(inNodes[:i], inNodes[i+1:]...) //remove node
}
break
}
}
if !found {
line = line[0:start] + line[end+1:] //ignore tag
}
} else if len(tagStr) > 2 && tagStr[0:3] == "!--" { //remove comments
end = strings.Index(line, "-->")
if end == -1 {
line = line[0:start]
} else {
line = line[0:start] + line[end+3:]
}
} else if tagStr[0:1] == "?" || tagStr[0:1] == "!" { //remove doc type and xml declarations
line = line[0:start] + line[end+1:]
} else {
node := Node{}
node.Attr = make(map[string]string)
node.Ind = ind
spaceInd := strings.Index(tagStr, " ")
if spaceInd == -1 {
node.Tag = tagStr
if len(node.Tag) > 1 && node.Tag[len(node.Tag)-1:] == "/" { //remove "/" after <a/>
node.Tag = node.Tag[:len(node.Tag)-1]
}
} else { //tag has a space (may have attrs)
node.Tag = tagStr[0:spaceInd]
tagStr = tagStr[spaceInd+1:]
//log.Println("tag attrs:", tagStr)
tagA := strings.Split(tagStr, "\"")
if len(tagA) > 0 {
for i := 0; i < len(tagA); i += 2 {
if len(tagA[i]) < 1 {
continue
}
//get rid of space at start
tagA[i] = strings.Replace(tagA[i], " ", "", -1)
tagA[i] = strings.Replace(tagA[i], "=", "", -1)
if len(tagA) > i+1 {
node.Attr[tagA[i]] = tagA[i+1]
} else { //no attr prop
node.Attr[tagA[i]] = ""
}
}
}
}
line = line[0:start] + "~~" + strconv.Itoa(node.Ind) + "~~" + line[end+1:]
//if ended with "/", close
if tagStr[len(tagStr)-1:] == "/" {
node.Closed = true
} else { //find end tag
inN := inNode{
Tag: node.Tag,
Ind: node.Ind,
}
inNodes = append(inNodes, inN)
}
nodes = append(nodes, node)
ind++
}
}
content += line
}
return
}
func Extract(nodes *[]Node, content string, exts []Ext) (err error) {
contentA := strings.Split(content, "~~")
if len(contentA) < 2 {
return
}
pos := ""
for i := 1; i < len(contentA); i += 2 {
//loop though nodes
//log.Println("node:", contentA[i])
if contentA[i][0:1] == "/" { //is an ending tag, remove from pos
var closeInd int
closeInd, err = strconv.Atoi(contentA[i][1:])
closeNode := getNode(nodes, closeInd)
posInd := strings.LastIndex(pos, closeNode.Tag)
if posInd == -1 {
continue
}
pos = pos[:posInd] + pos[posInd+len(closeNode.Tag)+1:]
//log.Println("close Tag", closeNode.Tag)
} else {
//not a close tag, attach to call
//set possible matches
//loop through set of rules and eliminate those that are not matching
ind, err := strconv.Atoi(contentA[i])
if err != nil {
return err
}
node := getNode(nodes, ind)
//posPrevLen := len(pos)
//if node.Tag != "" {
pos += node.Tag + " "
//}
//log.Println("pos:", pos)
for e := 0; e < len(exts); e++ {
match := false
ext := exts[e]
for m := 0; m < len(ext.Tags); m++ {
tagPos := strings.Index(pos, ext.Tags[m])
//last tag must match end of pos
if m == len(ext.Tags)-1 {
//log.Println("last match:", tagPos, posPrevLen)
if ext.Tags[m] == node.Tag {
match = true
break
} else {
match = false
break
}
}
if tagPos != -1 {
match = true
} else {
match = false
break
}
}
if match {
//log.Println("matched:", pos)
content := ""
if !node.Closed {
for j := i + 1; j < len(contentA); j++ {
if j%2 == 0 { //even, text
content += contentA[j]
} else if contentA[j][0:1] == "/" && contentA[j][1:] == contentA[i] {
break
} else {
content += "~~" + contentA[j] + "~~"
}
//log.Println("ext pos:", pos)
}
}
//log.Println("extract:", node, content)
err = ext.Callback(node, content, pos)
}
}
if node.Closed {
pos = pos[0 : len(pos)-len(node.Tag)-1]
}
}
}
return
}
func ExtractOne(nodes *[]Node, content string, tag string) (node Node, c string, err error) {
contentA := strings.Split(content, "~~")
if len(contentA) < 2 {
return
}
pos := ""
for i := 1; i < len(contentA); i += 2 {
//loop though nodes
//log.Println("node:", contentA[i])
if contentA[i][0:1] == "/" { //is an ending tag, remove from pos
var closeInd int
closeInd, err = strconv.Atoi(contentA[i][1:])
closeNode := getNode(nodes, closeInd)
posInd := strings.LastIndex(pos, closeNode.Tag)
if posInd == -1 {
continue
}
pos = pos[:posInd] + pos[posInd+len(closeNode.Tag):]
} else {
//not a close tag, attach to call
//set possible matches
//loop through set of rules and eliminate those that are not matching
ind, err := strconv.Atoi(contentA[i])
if err != nil {
return node, c, err
}
node := getNode(nodes, ind)
pos += node.Tag + " "
//log.Println("pos:", pos)
tagPos := strings.Index(pos, tag)
if tagPos == -1 {
continue
}
if !node.Closed {
for j := i + 1; j < len(contentA); j++ {
if j%2 == 0 { //even, text
c += contentA[j]
} else if contentA[j][0:1] == "/" && contentA[j][1:] == contentA[i] {
break
} else {
c += "~~" + contentA[j] + "~~"
}
}
}
//log.Println("extract:", node, content)
break
}
}
return
}
type ExtractCell struct {
Pos string
Node Node
Content string
}
func ExtractAll(nodes *[]Node, content string, tag string) (cells []ExtractCell, err error) {
contentA := strings.Split(content, "~~")
if len(contentA) < 2 {
return
}
pos := ""
for i := 1; i < len(contentA); i += 2 {
//loop though nodes
//log.Println("node:", contentA[i])
if contentA[i][0:1] == "/" { //is an ending tag, remove from pos
var closeInd int
closeInd, err = strconv.Atoi(contentA[i][1:])
closeNode := getNode(nodes, closeInd)
posInd := strings.LastIndex(pos, closeNode.Tag)
if posInd == -1 {
continue
}
pos = pos[:posInd] + pos[posInd+len(closeNode.Tag):]
} else {
//not a close tag, attach to call
cell := ExtractCell{}
//set possible matches
//loop through set of rules and eliminate those that are not matching
ind, err := strconv.Atoi(contentA[i])
if err != nil {
return cells, err
}
cell.Node = getNode(nodes, ind)
pos += cell.Node.Tag + " "
//log.Println("pos:", pos)
tagPos := strings.Index(pos, tag)
if tagPos == -1 {
continue
}
if !cell.Node.Closed {
for j := i + 1; j < len(contentA); j++ {
if j%2 == 0 { //even, text
cell.Content += contentA[j]
} else if contentA[j][0:1] == "/" && contentA[j][1:] == contentA[i] {
break
} else {
cell.Content += "~~" + contentA[j] + "~~"
}
}
}
cells = append(cells, cell)
//log.Println("extract:", node, content)
//break
}
}
return
}
type Crawl struct {
Nodes *[]Node
Content string //string of content, this can be updated
Pos string // "div a "
Done bool
}
func (this *Crawl) Next() (node Node, content string, pos string, err error) {
start := strings.Index(this.Content, "~~")
pos = this.Pos
if start == -1 { //no tags found, return full content string
content = this.Content
this.Done = true
return
}
if start != 0 { //tag not start, return everything before tag
content = this.Content[0:start]
this.Content = this.Content[start:] //trim off start of content
return
}
//is a tag
startClose := strings.Index(this.Content[2:], "~~")
if startClose == -1 {
this.Done = true
return
}
startClose += 2
//log.Println("content:", this.Content)
nodeStr := this.Content[2:startClose]
//log.Println("nodeStr:", nodeStr)
if len(nodeStr) < 1 {
this.Content = this.Content[startClose+2:]
if len(this.Content) < 1 {
this.Done = true
}
return
}
if nodeStr[0:1] == "/" { // is a close tag - assume there was a parsing bug and ignore
this.Content = this.Content[startClose+2:]
if len(this.Content) < 1 {
this.Done = true
}
return
}
ind, err := strconv.Atoi(nodeStr)
if err != nil {
return
}
nodes := *this.Nodes
node = nodes[ind]
pos += node.Tag + " "
if node.Closed {
this.Content = this.Content[startClose+2:]
} else {
//find node close
endNode := "~~/" + nodeStr + "~~"
end := strings.Index(this.Content, endNode)
content = this.Content[startClose+2 : end]
this.Content = this.Content[end+len(endNode):]
}
if len(this.Content) < 1 {
this.Done = true
}
return
}
func getNode(nodes *[]Node, ind int) (node Node) {
nP := *nodes
node = nP[ind]
return
}
func GetNodeAsContent(nodes *[]Node, c string, ind int, wrap bool) (content string) {
start := "~~" + strconv.Itoa(ind) + "~~"
end := "~~/" + strconv.Itoa(ind) + "~~"
startI := strings.Index(c, start)
endI := strings.Index(c, end)
if startI == -1 || endI == -1 {
return
}
if wrap {
endI += len(end)
} else {
startI += len(start)
}
//log.Println("start:", startI, endI)
content = c[startI:endI]
return
}
func StripNodes(content string) (s string) {
contentA := strings.Split(content, "~~")
for i := 0; i < len(contentA); i += 2 {
s += contentA[i]
}
return
}