-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdom.go
829 lines (762 loc) · 15.9 KB
/
xdom.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
* XML DOM for GOPL
* Copyright 2024 John Douglas Pritchard, Syntelos
*/
package xdom
import (
"bytes"
"errors"
"fmt"
"os"
span "github.com/syntelos/go-span"
"strings"
)
/*
* Principal user interface.
*/
type Node interface {
KindOf() (Kind)
Content() (Text)
String() (string)
Print()
Depth() (uint8)
Read(string, Text) (Node, error)
}
/*
*/
type NodeList interface {
CountChildren() (uint32)
GetChild(uint32) (Node)
}
/*
*/
type AttributeList interface {
CountAttributes() (uint32)
GetAttribute(uint32) (Node)
}
/*
* Node type includes parser states, e.g. "code" and "text",
* element as "open", "solitary" and "close".
*/
type Kind uint8
const (
KindUndefined Kind = 0
KindCode Kind = 0b10000000
KindDocument Kind = 0b10000001
KindAttribute Kind = 0b10000010
KindDeclaration Kind = 0b10000100
KindInstruction Kind = 0b10000101
KindOpen Kind = 0b10001000
KindSolitary Kind = 0b10001001
KindClose Kind = 0b10001010
KindText Kind = 0b00010000
KindData Kind = 0b00010001
)
type Document struct {
source string
content Text
children []Node
}
type Element struct {
parent Node
content Text
name string
attributes []Attribute
children []Node
}
type Attribute struct {
content Text
name string
value string
}
type Text []byte
type TextList []Text
func (this Kind) IsCode() (bool) {
return (KindCode == (this & KindCode))
}
func (this Kind) IsText() (bool) {
return (KindText == (this & KindText))
}
func (this Kind) IsHead() (bool) {
return (KindDeclaration == (this & KindDeclaration))
}
func (this Kind) IsBody() (bool) {
return (KindOpen == this & KindOpen)
}
func (this Kind) IsOpen() (bool) {
return (KindOpen == this)
}
func (this Kind) String() (string) {
switch this {
case KindDeclaration:
return "<DECL>"
case KindInstruction:
return "<INSTR>"
case KindDocument:
return "<DOC>"
case KindOpen:
return "<OPEN>"
case KindSolitary:
return "<SOL>"
case KindClose:
return "<CLOSE>"
case KindText:
return "<TEXT>"
case KindData:
return "<DATA>"
default:
return "<UNKN>"
}
}
func (this Document) KindOf() (Kind){
return KindDocument
}
func (this Document) Content() (Text){
return this.content
}
func (this Document) Source() (string){
return this.source
}
func (this Document) String() (string){
return this.content.String()
}
func (this Document) Print() {
for index, node := range this.children {
var kind Kind = node.KindOf()
switch kind {
case KindDeclaration, KindInstruction, KindDocument, KindOpen, KindSolitary, KindClose, KindText, KindData:
fmt.Printf("%03o\t%s\t%s\n",index,kind,node)
node.Print()
}
}
}
func (this Document) Depth() (uint8) {
return 0
}
func (this Document) CountChildren() (index uint32) {
return uint32(len(this.children))
}
func (this Document) GetChild(index uint32) (Node) {
if index < this.CountChildren() {
return this.children[index]
} else {
return nil
}
}
/*
* XML document parser.
*/
func (this Document) ReadFile (src *os.File) (n Node, er error){
var fi os.FileInfo
fi, er = src.Stat()
if nil != er {
return this, fmt.Errorf("Read error file not found: %w",er)
} else {
var sz int64 = fi.Size()
var content Text = make([]byte,sz)
var ct int
ct, er = src.Read(content)
if nil != er {
return this, fmt.Errorf("ReadFile error '%s': %w",fi.Name(),er)
} else if int64(ct) != sz {
return this, fmt.Errorf("ReadFile error '%s': expected (%d) found (%d).",fi.Name(),sz,ct)
} else {
var url string = "file:"+src.Name()
return this.Read(url,content)
}
}
}
/*
* XML document parser.
*/
func (this Document) Read (url string, content Text) (n Node, er error){
this.source = url
this.content = content
{
var source TextList
var kind Kind
var text Text
var body []byte
n, er = source.Read(url,content)
if nil == er {
source = n.(TextList)
for _, text = range source {
kind = text.KindOf()
if kind.IsCode() {
if kind.IsBody() {
/*
* Document body
*/
body = span.Cat(body,text)
} else {
/*
* Document head
*/
var el Element
n, er = el.Read(url,text)
if nil != er {
return this, er
} else {
el = n.(Element)
this.children = append(this.children,el)
}
}
} else if kind.IsText() {
if 0 == len(body) {
this.children = append(this.children,text)
} else {
body = span.Cat(body,text)
}
}
}
}
/*
* Document body
*/
var el Element
n, er = el.Read(url,body)
if nil != er {
return this, er
} else {
el = n.(Element)
this.children = append(this.children,el)
}
}
return this, nil
}
func (this Element) KindOf() (Kind){
if 0 != len(this.content) {
return this.content.KindOf()
} else {
return KindUndefined
}
}
func (this Element) Parent() (Node){
return this.parent
}
func (this Element) Content() (Text){
return this.content
}
func (this Element) Name() (string){
return this.name
}
func (this Element) String() (string){
var str strings.Builder
{
str.WriteString(this.name)
str.WriteByte(' ')
for ix, at := range this.attributes {
if 0 != ix {
str.WriteByte(' ')
}
str.WriteString(at.name)
}
}
return str.String()
}
func (this Element) Print() {
var indent string
{
var depth uint8 = this.Depth()
var str Text = make([]byte,depth)
var ix uint8
for ix = 0; ix < depth; ix++ {
str[ix] = '\t'
}
indent = string(str)
}
for index, node := range this.children {
var kind Kind = node.KindOf()
switch kind {
case KindDeclaration, KindInstruction, KindOpen, KindSolitary:
fmt.Printf("%s%03o\t%s\t%s\n",indent,index,kind,node)
node.Print()
}
}
}
func (this Element) Depth() (uint8) {
var p Node = this.parent
var c uint8 = 1
for nil != p {
if KindOpen == p.KindOf() {
c += 1
{
var e Element = p.(Element)
p = e.parent
}
} else {
c += 1
break
}
}
return c
}
func (this Element) Read(url string, content Text) (n Node, er error) {
this.content = content
/*
* Element attributes
*/
var kind Kind = this.KindOf()
var w, x, y, z int = 0, 0, 0, len(content)
switch kind {
case KindDeclaration:
x = 2
case KindInstruction:
x = 2
case KindOpen:
x = 1
case KindSolitary:
x = 1
case KindClose:
x = 2
}
this.name = this.content.identifier(x)
switch kind {
case KindDeclaration, KindInstruction, KindOpen, KindSolitary:
x += len(this.name)
for x < z {
w = span.Class(this.content,x,z,span.WS)
if 0 < w {
x = (w+1)
if '"' == this.content[x] {
y = span.Forward(this.content,x,z,'"','"')
if 0 < y {
var at_be, at_en int = x, (y+1)
var atx Text = this.content[at_be:at_en]
var at Attribute
n, er = at.Read(url,atx)
if nil != er {
return this, er
} else {
at = n.(Attribute)
this.attributes = append(this.attributes,at)
x = at_en
}
} else {
break
}
} else if '\'' == this.content[x] {
y = span.Forward(this.content,x,z,'\'','\'')
if 0 < y {
var at_be, at_en int = x, (y+1)
var atx Text = this.content[at_be:at_en]
var at Attribute
n, er = at.Read(url,atx)
if nil != er {
return this, er
} else {
at = n.(Attribute)
this.attributes = append(this.attributes,at)
x = at_en
}
} else {
break
}
} else {
y = span.Class(this.content,x,z,span.XI)
if 0 < y {
if '=' == this.content[y] {
y += 1
if '"' == this.content[y] {
y = span.Forward(this.content,y,z,'"','"')
if 0 < y {
var at_be, at_en int = x, (y+1)
var atx Text = this.content[at_be:at_en]
var at Attribute
n, er = at.Read(url,atx)
if nil != er {
return this, er
} else {
at = n.(Attribute)
this.attributes = append(this.attributes,at)
x = at_en
}
} else {
break
}
} else if '\'' == this.content[y] {
y = span.Forward(this.content,y,z,'\'','\'')
if 0 < y {
var at_be, at_en int = x, (y+1)
var atx Text = this.content[at_be:at_en]
var at Attribute
n, er = at.Read(url,atx)
if nil != er {
return this, er
} else {
at = n.(Attribute)
this.attributes = append(this.attributes,at)
x = at_en
}
} else {
break
}
} else {
y = span.Class(this.content,y,z,span.XA)
if 0 < y {
var at_be, at_en int = x, (y+1)
var atx Text = this.content[at_be:at_en]
var at Attribute
n, er = at.Read(url,atx)
if nil != er {
return this, er
} else {
at = n.(Attribute)
this.attributes = append(this.attributes,at)
x = at_en
}
} else {
break
}
}
} else {
var at_be, at_en int = x, (y+1)
var atx Text = this.content[at_be:at_en]
var at Attribute
n, er = at.Read(url,atx)
if nil != er {
return this, er
} else {
at = n.(Attribute)
this.attributes = append(this.attributes,at)
x = at_en
}
}
} else {
break
}
}
} else {
break
}
}
}
/*
* Element content [TODO] (review)
*/
if KindOpen == kind {
w = span.First(content,x,z,'>')
if x < w && w < z {
y = span.Last(content,(z-1),z,'<')
if x < y && y < z {
var text []byte = content[w:y]
var list TextList
var n Node
n, er = list.Read(url,text)
if nil != er {
return this, fmt.Errorf("Parsing '%s': %w", text, er)
} else {
list = n.(TextList)
var text Text
var stack int = 0
var body []byte
for _, text = range list {
switch text.KindOf() {
case KindOpen:
body = span.Cat(body,text)
stack += 1
case KindSolitary:
if 0 == stack {
var el Element
n, er = el.Read(url,text)
if nil != er {
return this, er
} else {
el = n.(Element)
this.children = append(this.children,el)
}
} else {
body = span.Cat(body,text)
}
case KindClose:
body = span.Cat(body,text)
stack -= 1
if 0 == stack {
var el Element
n, er = el.Read(url,body)
if nil != er {
return this, er
} else {
el = n.(Element)
this.children = append(this.children,el)
body = make([]byte,0)
}
}
case KindText, KindData:
body = span.Cat(body,text)
}
}
}
}
}
}
return this,nil
}
func (this Element) CountChildren() (index uint32) {
return uint32(len(this.children))
}
func (this Element) GetChild(index uint32) (ch Node) {
if index < this.CountChildren() {
return this.children[index]
} else {
return ch
}
}
func (this Element) CountAttributes() (index uint32) {
return uint32(len(this.attributes))
}
func (this Element) GetAttribute(index uint32) (at Attribute) {
if index < this.CountAttributes() {
return this.attributes[index]
} else {
return at
}
}
func (this Attribute) KindOf() (Kind) {
return KindAttribute
}
func (this Attribute) Content() (Text) {
return this.content
}
func (this Attribute) Name() (string){
return this.name
}
func (this Attribute) Value() (string){
return this.value
}
func (this Attribute) String() (string) {
if "" != this.name {
return this.name
} else if "" != this.value {
return this.value
} else {
return ""
}
}
func (this Attribute) Print() {
}
func (this Attribute) Depth() (uint8) {
return 0
}
func (this Attribute) Read(url string, content Text) (n Node, er error) {
this.content = content
var x, z int = 0, len(this.content)
if x < z {
var y int = (z-1)
if '"' == content[x] {
if '"' == content[x] && '"' == content[y] {
this.value = string(content)
} else {
return this, fmt.Errorf("Attribute quote missing in '%s'.",content)
}
} else {
y = span.Class(this.content,x,z,span.XI)
if 0 < y {
y += 1
if y < z {
if '=' == this.content[y] {
this.name = string(this.content[x:y])
this.value = string(this.content[y+1])
} else {
return this, fmt.Errorf("Attribute syntax of content '%s'.",content)
}
} else {
this.name = string(content)
}
} else {
this.value = string(content)
}
}
return this, nil
} else {
return this, errors.New("Attribute empty.")
}
}
func (this Text) KindOf() (Kind){
var x int = 0
var z int = len(this)
if x < z {
var y int = (z-1)
if x < y {
if '<' == this[x] && '>' == this[y] {
x += 1
y -= 1
if x < y {
switch this[x] {
case '?':
return KindInstruction
case '!':
x += 1
if x < y {
if '[' == this[x] {
return KindData
} else {
return KindDeclaration
}
}
case '/':
return KindClose
default:
if '/' == this[y] {
return KindSolitary
} else {
return KindOpen
}
}
}
} else {
var w int = span.Class(this,x,z,span.WS)
if w == y {
return KindUndefined
} else {
return KindText
}
}
}
}
return KindUndefined
}
func (this Text) Content() (Text){
return this
}
func (this Text) String() (string) {
var x, z = 0, len(this)
if x < z {
var y int = z
/*
* Clamp to line
*/
for ; x < y; x++ {
if '\n' == this[x] {
y = x
break
} else if 20 == x {
y = 20
break
}
}
/*
* Clamp to twenty
*/
if 20 < y {
return string(this[0:20])
} else if y < z {
return string(this[0:y])
} else {
return string(this)
}
}
return ""
}
func (this Text) Print() {
}
func (this Text) Depth() (uint8) {
return 0
}
func (this Text) Read(url string, content Text) (n Node, er error) {
this = content
return this,nil
}
/*
* Text span operator.
*/
func (this Text) identifier(ofs int) (string) {
var x, z int = ofs, len(this)
var y int = span.Class(this,x,z,span.XI)
if x <= y && y <= z {
return string(this[x:y+1])
} else {
return string(this)
}
}
func (this TextList) KindOf() (Kind) {
return this.Content().KindOf()
}
func (this TextList) Content() (Text) {
var buf bytes.Buffer
for _, txt := range this {
buf.Write(txt)
}
return buf.Bytes()
}
func (this TextList) String() (string) {
var content string = string(this.Content())
var x, z = 0, len(content)
if x < z {
var y int = z
/*
* Clamp to line
*/
for ; x < y; x++ {
if '\n' == content[x] {
y = x
break
} else if 20 == x {
y = 20
break
}
}
/*
* Clamp to twenty
*/
if 20 < y {
return string(content[0:20])
} else if y < z {
return string(content[0:y])
} else {
return string(content)
}
}
return content
}
func (this TextList) Print() {
for index, node := range this {
fmt.Printf("%03o\t%s\n",index,node)
}
}
func (this TextList) Depth() (uint8) {
return 0
}
/*
* XML stream parser.
*/
func (this TextList) ReadFile (src *os.File) (n Node, er error){
var fi os.FileInfo
fi, er = src.Stat()
if nil != er {
return this, fmt.Errorf("Read error file not found: %w",er)
} else {
var sz int64 = fi.Size()
var content Text = make([]byte,sz)
var ct int
ct, er = src.Read(content)
if nil != er {
return this, fmt.Errorf("ReadFile error '%s': %w",fi.Name(),er)
} else if int64(ct) != sz {
return this, fmt.Errorf("ReadFile error '%s': expected (%d) found (%d).",fi.Name(),sz,ct)
} else {
var url string = "file:"+src.Name()
return this.Read(url,content)
}
}
}
/*
* XML stream parser.
*/
func (this TextList) Read (url string, content Text) (n Node, er error){
var x, z int = 0, len(content)
for x < z {
var first, last int = x, span.Forward(content,x,z,'<','>')
if first < last {
var begin, end int = first, (last+1)
var text Text = content[begin:end]
if KindUndefined != text.KindOf() {
this = append(this,text)
}
x = end
} else {
x += 1
}
}
return this, nil
}