forked from tinyhubs/tinydom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinydom.go
1377 lines (1108 loc) · 28.8 KB
/
tinydom.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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package tinydom 实现了一个简单的XML的DOM树构造工具.
package tinydom
import (
"bytes"
"container/list"
"encoding/xml"
"errors"
"io"
"os"
"unicode/utf8"
)
// XMLAttribute 是一个元素的属性的接口.
//
// 这是一份关于属性的注释.
type XMLAttribute interface {
Name() string
Value() string
SetValue(string)
}
// XMLNode 定义了XML所有节点的基础设施,提供了基本的元素遍历、增删等操作,也提供了逆向转换能力.
type XMLNode interface {
ToElement() XMLElement
ToText() XMLText
ToComment() XMLComment
ToDocument() XMLDocument
ToProcInst() XMLProcInst
ToDirective() XMLDirective
Value() string
SetValue(newValue string)
Document() XMLDocument
NoChildren() bool
Parent() XMLNode
FirstChild() XMLNode
LastChild() XMLNode
Prev() XMLNode
Next() XMLNode
FirstChildElement(name string) XMLElement
LastChildElement(name string) XMLElement
PrevElement(name string) XMLElement
NextElement(name string) XMLElement
InsertBack(node XMLNode) XMLNode
InsertFront(node XMLNode) XMLNode
InsertEndChild(node XMLNode) XMLNode
InsertFirstChild(node XMLNode) XMLNode
InsertElementBack(name string) XMLElement
InsertElementFront(name string) XMLElement
InsertElementEndChild(name string) XMLElement
InsertElementFirstChild(name string) XMLElement
DeleteChildren()
DeleteChild(node XMLNode)
Split() XMLNode
Accept(visitor XMLVisitor) bool
// 被迫入侵的接口
insertBeforeChild(beforeThis XMLNode, addThis XMLNode) XMLNode
insertAfterChild(afterThis XMLNode, addThis XMLNode) XMLNode
setParent(node XMLNode)
setPrev(node XMLNode)
setNext(node XMLNode)
setDocument(doc XMLDocument)
//impl() XMLNode
unlink(child XMLNode)
}
// XMLElement 提供了访问XML基本节点元素的能力
//
// Name、SetName其实是Value和SetValue的别名,目的是为了使得接口更加符合直观理解。
//
// Text、SetText的作用是设置<node>与</node>之间的文字,虽然文字都是有XMLText对象来承载的,但是通常来说直接在XMLElement中访问会更加方便。
//
// FindAttribute和ForeachAttribute分别用于查找特定的XML节点的属性和遍历XML属性列表。
//
// Attribute、SetAttribute、DeleteAttribute用于读取和删除属性。
type XMLElement interface {
XMLNode
Name() string
SetName(name string)
FindAttribute(name string) XMLAttribute
ForeachAttribute(callback func(attribute XMLAttribute) int) int
AttributeCount() int
Attribute(name string, def string) string
SetAttribute(name string, value string) XMLAttribute
DeleteAttribute(name string) XMLAttribute
ClearAttributes()
Text() string
SetText(text string)
}
// XMLText 提供了对XML元素间文本的封装
type XMLText interface {
XMLNode
SetCDATA(isCData bool)
CDATA() bool
}
// XMLComment 提供了对注释的封装
type XMLComment interface {
XMLNode
Comment() string
SetComment(string)
}
// XMLProcInst 常用于表达XML处理指令,类似:<?xml version="1.0" encoding="UTF-8"?>
type XMLProcInst interface {
XMLNode
Target() string
Instruction() string
}
// XMLDirective 用于表达`<!`与`>`之间的部分,一般为DTD
type XMLDirective interface {
XMLNode
}
// XMLDocument 用于表达一个XML文档,这是整个XML文档的根
type XMLDocument interface {
XMLNode
}
// XMLVisitor XML文档访问器,常用于遍历文档或者格式化输出XML文档
type XMLVisitor interface {
VisitEnterDocument(XMLDocument) bool
VisitExitDocument(XMLDocument) bool
VisitEnterElement(XMLElement) bool
VisitExitElement(XMLElement) bool
VisitProcInst(XMLProcInst) bool
VisitText(XMLText) bool
VisitComment(XMLComment) bool
VisitDirective(XMLDirective) bool
}
// XMLHandle XML文档处理器,其主要
type XMLHandle interface {
Parent() XMLHandle
FirstChild() XMLHandle
LastChild() XMLHandle
Prev() XMLHandle
Next() XMLHandle
FirstChildElement(name string) XMLHandle
LastChildElement(name string) XMLHandle
PrevElement(name string) XMLHandle
NextElement(name string) XMLHandle
ToNode() XMLNode
ToElement() XMLElement
ToText() XMLText
ToComment() XMLComment
ToDocument() XMLDocument
ToProcInst() XMLProcInst
ToDirective() XMLDirective
}
// =========================================================
type xmlAttributeImpl struct {
name string
value string
}
func (a *xmlAttributeImpl) Name() string {
return a.name
}
func (a *xmlAttributeImpl) Value() string {
return a.value
}
func (a *xmlAttributeImpl) SetValue(newValue string) {
a.value = newValue
}
// ==================================================================
type xmlNodeImpl struct {
implobj XMLNode
document XMLDocument
parent XMLNode
value string
firstChild XMLNode
lastChild XMLNode
prev XMLNode
next XMLNode
}
func (n *xmlNodeImpl) setParent(node XMLNode) {
n.parent = node
}
func (n *xmlNodeImpl) setPrev(node XMLNode) {
n.prev = node
}
func (n *xmlNodeImpl) setNext(node XMLNode) {
n.next = node
}
func (n *xmlNodeImpl) setDocument(doc XMLDocument) {
n.document = doc
}
func (n *xmlNodeImpl) ToElement() XMLElement {
return nil
}
func (n *xmlNodeImpl) ToText() XMLText {
return nil
}
func (n *xmlNodeImpl) ToComment() XMLComment {
return nil
}
func (n *xmlNodeImpl) ToDocument() XMLDocument {
return nil
}
func (n *xmlNodeImpl) ToProcInst() XMLProcInst {
return nil
}
func (n *xmlNodeImpl) ToDirective() XMLDirective {
return nil
}
func (n *xmlNodeImpl) Value() string {
return n.value
}
func (n *xmlNodeImpl) SetValue(newValue string) {
n.value = newValue
}
func (n *xmlNodeImpl) Document() XMLDocument {
return n.document
}
func (n *xmlNodeImpl) Parent() XMLNode {
return n.parent
}
func (n *xmlNodeImpl) NoChildren() bool {
return nil == n.firstChild
}
func (n *xmlNodeImpl) FirstChild() XMLNode {
return n.firstChild
}
func (n *xmlNodeImpl) LastChild() XMLNode {
return n.lastChild
}
func (n *xmlNodeImpl) Prev() XMLNode {
return n.prev
}
func (n *xmlNodeImpl) Next() XMLNode {
return n.next
}
func (n *xmlNodeImpl) FirstChildElement(name string) XMLElement {
for item := n.firstChild; nil != item; item = item.Next() {
elem := item.ToElement()
if nil == elem {
continue
}
if ("" == name) || (elem.Name() == name) {
return elem
}
}
return nil
}
func (n *xmlNodeImpl) LastChildElement(name string) XMLElement {
for item := n.lastChild; nil != item; item = item.Prev() {
elem := item.ToElement()
if nil == elem {
continue
}
if ("" == name) || (elem.Name() == name) {
return elem
}
}
return nil
}
func (n *xmlNodeImpl) PrevElement(name string) XMLElement {
for item := n.prev; nil != item; item = item.Prev() {
elem := item.ToElement()
if nil == elem {
continue
}
if ("" == name) || (elem.Name() == name) {
return elem
}
}
return nil
}
func (n *xmlNodeImpl) NextElement(name string) XMLElement {
for item := n.next; nil != item; item = item.Next() {
elem := item.ToElement()
if nil == elem {
continue
}
if ("" == name) || (elem.Name() == name) {
return elem
}
}
return nil
}
func (n *xmlNodeImpl) Split() XMLNode {
if nil != n.parent {
n.parent.unlink(n.implobj)
}
return n.implobj
}
func (n *xmlNodeImpl) unlink(child XMLNode) {
//if child.impl() == n.firstChild {
if child == n.firstChild {
n.firstChild = n.firstChild.Next()
}
//if child.impl() == n.lastChild {
if child == n.lastChild {
n.lastChild = n.lastChild.Prev()
}
if nil != child.Prev() {
child.Prev().setNext(child.Next())
}
if nil != child.Next() {
child.Next().setPrev(child.Prev())
}
child.setParent(nil)
child.setDocument(nil)
}
func (n *xmlNodeImpl) InsertEndChild(addThis XMLNode) XMLNode {
addThis.Split()
if nil != n.lastChild {
n.lastChild.setNext(addThis)
addThis.setPrev(n.lastChild)
n.lastChild = addThis
addThis.setNext(nil)
} else {
n.firstChild = addThis
n.lastChild = addThis
addThis.setPrev(nil)
addThis.setNext(nil)
}
addThis.setParent(n.implobj)
addThis.setDocument(n.document)
return addThis
}
func (n *xmlNodeImpl) InsertFirstChild(addThis XMLNode) XMLNode {
addThis.Split()
if nil != n.firstChild {
n.firstChild.setPrev(addThis)
addThis.setNext(n.firstChild)
n.firstChild = addThis
addThis.setPrev(nil)
} else {
n.firstChild = addThis
n.lastChild = addThis
addThis.setPrev(nil)
addThis.setNext(nil)
}
addThis.setParent(n.implobj)
addThis.setDocument(n.document)
return addThis
}
func (n *xmlNodeImpl) insertAfterChild(afterThis XMLNode, addThis XMLNode) XMLNode {
// if afterThis.Parent() != a.implobj {
// return nil
// }
if afterThis.Next() == nil {
return n.InsertEndChild(addThis)
}
addThis.Split()
addThis.setPrev(afterThis)
addThis.setNext(afterThis.Next())
afterThis.Next().setPrev(addThis)
afterThis.setNext(addThis)
addThis.setParent(n.implobj)
addThis.setDocument(n.document)
return addThis
}
func (n *xmlNodeImpl) insertBeforeChild(beforeThis XMLNode, addThis XMLNode) XMLNode {
// if beforeThis.Parent() != a.implobj {
// return nil
// }
if beforeThis.Prev() == nil {
return n.InsertFirstChild(addThis)
}
addThis.Split()
addThis.setPrev(beforeThis.Prev())
addThis.setNext(beforeThis)
beforeThis.Prev().setNext(addThis)
beforeThis.setPrev(addThis)
addThis.setParent(n.implobj)
addThis.setDocument(n.document)
return addThis
}
func (n *xmlNodeImpl) InsertBack(addThis XMLNode) XMLNode {
if nil == n.parent {
return nil
}
return n.parent.insertAfterChild(n.implobj, addThis)
}
func (n *xmlNodeImpl) InsertFront(addThis XMLNode) XMLNode {
if nil == n.parent {
return nil
}
return n.parent.insertBeforeChild(n.implobj, addThis)
}
func (n *xmlNodeImpl) InsertElementFront(name string) XMLElement {
return n.InsertFront(NewElement(name)).ToElement()
}
func (n *xmlNodeImpl) InsertElementBack(name string) XMLElement {
return n.InsertBack(NewElement(name)).ToElement()
}
func (n *xmlNodeImpl) InsertElementEndChild(name string) XMLElement {
return n.InsertEndChild(NewElement(name)).ToElement()
}
func (n *xmlNodeImpl) InsertElementFirstChild(name string) XMLElement {
return n.InsertFirstChild(NewElement(name)).ToElement()
}
func (n *xmlNodeImpl) DeleteChildren() {
for nil != n.firstChild {
n.DeleteChild(n.firstChild)
}
n.firstChild = nil
n.lastChild = nil
}
func (n *xmlNodeImpl) DeleteChild(node XMLNode) {
n.unlink(node)
}
//func (n *xmlNodeImpl) Accept(visitor XMLVisitor) bool {
// return n.implobj.Accept(visitor)
//}
// ------------------------------------------------------------------
type xmlElementImpl struct {
xmlNodeImpl
// rootAttribute XMLAttribute
attrlist *list.List
attrsmap map[string]*list.Element
}
func (e *xmlElementImpl) ToElement() XMLElement {
return e
}
func (e *xmlElementImpl) Accept(visitor XMLVisitor) bool {
if visitor.VisitEnterElement(e) {
for node := e.FirstChild(); nil != node; node = node.Next() {
if !node.Accept(visitor) {
break
}
}
}
return visitor.VisitExitElement(e)
}
func (e *xmlElementImpl) Name() string {
return e.Value()
}
func (e *xmlElementImpl) SetName(name string) {
e.SetValue(name)
}
func (e *xmlElementImpl) FindAttribute(name string) XMLAttribute {
elem, ok := e.attrsmap[name]
if !ok {
return nil
}
return elem.Value.(*xmlAttributeImpl)
}
func (e *xmlElementImpl) AttributeCount() int {
return len(e.attrsmap)
}
func (e *xmlElementImpl) Attribute(name string, def string) string {
attr, ok := e.attrsmap[name]
if !ok {
return def
}
return attr.Value.(*xmlAttributeImpl).Value()
}
func (e *xmlElementImpl) SetAttribute(name string, value string) XMLAttribute {
elem, ok := e.attrsmap[name]
if ok {
elem.Value.(*xmlAttributeImpl).SetValue(value)
return elem.Value.(*xmlAttributeImpl)
}
attr := newAttribute(name, value)
e.attrsmap[name] = e.attrlist.PushBack(attr)
return attr
}
func (e *xmlElementImpl) DeleteAttribute(name string) XMLAttribute {
elem, ok := e.attrsmap[name]
if !ok {
return nil
}
attr := elem.Value.(*xmlAttributeImpl)
e.attrlist.Remove(elem)
delete(e.attrsmap, name)
return attr
}
func (e *xmlElementImpl) Text() string {
if text := e.FirstChild(); (nil != text) && (nil != text.ToText()) {
return text.Value()
}
return ""
}
func (e *xmlElementImpl) SetText(inText string) {
if node := e.FirstChild(); (nil != node) && (nil != node.ToText()) {
node.SetValue(inText)
} else {
theText := NewText(inText)
e.InsertFirstChild(theText)
}
}
func (e *xmlElementImpl) ForeachAttribute(callback func(attribute XMLAttribute) int) int {
for elem := e.attrlist.Front(); nil != elem; elem = elem.Next() {
if ret := callback(elem.Value.(*xmlAttributeImpl)); 0 != ret {
return ret
}
}
return 0
}
func (e *xmlElementImpl) ClearAttributes() {
e.attrlist = list.New()
e.attrsmap = make(map[string]*list.Element)
}
// ------------------------------------------------------------------
type xmlCommentImpl struct {
xmlNodeImpl
}
func (c *xmlCommentImpl) ToComment() XMLComment {
return c
}
func (c *xmlCommentImpl) Comment() string {
return c.value
}
func (c *xmlCommentImpl) SetComment(newComment string) {
c.value = newComment
}
func (c *xmlCommentImpl) Accept(visitor XMLVisitor) bool {
return visitor.VisitComment(c)
}
// ------------------------------------------------------------------
type xmlProcInstImpl struct {
xmlNodeImpl
instruction string
}
func (p *xmlProcInstImpl) ToProcInst() XMLProcInst {
return p
}
func (p *xmlProcInstImpl) Accept(visitor XMLVisitor) bool {
return visitor.VisitProcInst(p)
}
func (p *xmlProcInstImpl) Target() string {
return p.value
}
func (p *xmlProcInstImpl) Instruction() string {
return p.instruction
}
// ------------------------------------------------------------------
type xmlDocumentImpl struct {
xmlNodeImpl
}
func (d *xmlDocumentImpl) ToDocument() XMLDocument {
return d
}
func (d *xmlDocumentImpl) Accept(visitor XMLVisitor) bool {
if visitor.VisitEnterDocument(d) {
for node := d.FirstChild(); nil != node; node = node.Next() {
if !node.Accept(visitor) {
break
}
}
}
return visitor.VisitExitDocument(d)
}
// ------------------------------------------------------------------
type xmlTextImpl struct {
xmlNodeImpl
cdata bool
}
func (t *xmlTextImpl) ToText() XMLText {
return t
}
func (t *xmlTextImpl) Accept(visitor XMLVisitor) bool {
return visitor.VisitText(t)
}
func (t *xmlTextImpl) SetCDATA(isCData bool) {
t.cdata = isCData
}
func (t *xmlTextImpl) CDATA() bool {
return t.cdata
}
// ------------------------------------------------------------------
type xmlDirectiveImpl struct {
xmlNodeImpl
}
func (d *xmlDirectiveImpl) ToDirective() XMLDirective {
return d
}
func (d *xmlDirectiveImpl) Accept(visitor XMLVisitor) bool {
return visitor.VisitDirective(d)
}
// ------------------------------------------------------------------
// NewText 创建一个新的XMLText对象
func NewText(text string) XMLText {
node := new(xmlTextImpl)
node.implobj = node
node.value = text
return node
}
// NewComment 创建一个新的XMLComment对象
func NewComment(comment string) XMLComment {
node := new(xmlCommentImpl)
node.implobj = node
node.value = comment
return node
}
// NewElement 创建一个新的XMLElement对象
func NewElement(name string) XMLElement {
node := new(xmlElementImpl)
node.implobj = node
node.value = name
node.attrsmap = make(map[string]*list.Element)
node.attrlist = list.New()
return node
}
// NewProcInst 创建一个新的XMLProcInst对象
func NewProcInst(target string, inst string) XMLProcInst {
node := new(xmlProcInstImpl)
node.implobj = node
node.value = target
node.instruction = inst
return node
}
// NewDirective 创建一个新的XMLDirective对象
func NewDirective(directive string) XMLDirective {
node := new(xmlDirectiveImpl)
node.implobj = node
node.value = directive
return node
}
// newAttribute 创建一个新的XMLAttribute对象.
// name和value分别用于指定属性的名称和值
func newAttribute(name string, value string) *xmlAttributeImpl {
attr := new(xmlAttributeImpl)
attr.name = name
attr.value = value
return attr
}
// NewDocument 创建一个全新的XMLDocument对象
func NewDocument() XMLDocument {
doc := new(xmlDocumentImpl)
doc.implobj = doc
doc.document = doc
return doc
}
type context struct {
doc XMLDocument
parent XMLNode
rootElemExist bool
}
func handleStartElement(startElement xml.StartElement, ctx *context) error {
//startElement := token.(xml.StartElement)
// 一个XML文档只允许有唯一一个根节点
if ctx.doc == ctx.parent {
if ctx.rootElemExist {
return errors.New("Root element has been exist:" + startElement.Name.Local)
}
// 标记一下根节点已经存在了
ctx.rootElemExist = true
}
node := NewElement(startElement.Name.Local)
for _, item := range startElement.Attr {
if nil != node.FindAttribute(item.Name.Local) {
return errors.New("Attributes have the same name:" + item.Name.Local)
}
node.SetAttribute(item.Name.Local, item.Value)
}
ctx.parent.InsertEndChild(node)
ctx.parent = node
return nil
}
func handleCharData(charData xml.CharData, ctx *context) error {
shortCharData := bytes.TrimSpace(charData)
if (nil != shortCharData) && (len(shortCharData) > 0) {
if ctx.doc == ctx.parent {
return errors.New("Text should be in the element")
}
node := NewText(string(charData))
ctx.parent.InsertEndChild(node)
}
return nil
}
// LoadDocument 从rd流中读取XML码流并构建成XMLDocument对象
func LoadDocument(rd io.Reader) (XMLDocument, error) {
// 创建一个context
ctx := new(context)
ctx.doc = NewDocument()
ctx.parent = ctx.doc
ctx.rootElemExist = false
// 创建一个decoder
decoder := xml.NewDecoder(rd)
token, err := decoder.Token()
for ; err == nil; token, err = decoder.Token() {
switch token.(type) {
case xml.StartElement:
err := handleStartElement(token.(xml.StartElement), ctx)
if nil != err {
return nil, err
}
case xml.EndElement:
ctx.parent = ctx.parent.Parent()
case xml.Comment:
ctx.parent.InsertEndChild(NewComment(string(token.(xml.Comment))))
case xml.Directive:
ctx.parent.InsertEndChild(NewDirective(string(token.(xml.Directive))))
case xml.ProcInst:
procInst := token.(xml.ProcInst)
ctx.parent.InsertEndChild(NewProcInst(procInst.Target, string(procInst.Inst)))
case xml.CharData:
if err := handleCharData(token.(xml.CharData), ctx); nil != err {
return nil, err
}
default:
return nil, errors.New("Unsupported token type")
}
}
if err == io.EOF {
// 不能是空文档
if nil == ctx.doc.FirstChildElement("") {
return nil, errors.New("XML document missing the root element")
}
return ctx.doc, nil
}
return nil, err
}
func LoadDocumentFromFile(name string) (XMLDocument, error) {
file, err := os.Open(name)
if nil != err {
return nil, err
}
defer file.Close()
return LoadDocument(file)
}
// SaveDocumentToFile Print the xml-dom objects to the writer.
func SaveDocument(doc XMLDocument, writer io.Writer, options PrintOptions) error {
doc.Accept(NewSimplePrinter(writer, options))
return nil
}
// SaveDocumentToFile Print the xml-dom objects to the file.
func SaveDocumentToFile(doc XMLDocument, name string, options PrintOptions) error {
file, err := os.Create(name)
if nil != err {
return err
}
defer file.Close()
doc.Accept(NewSimplePrinter(file, options))
return nil
}
// DefaultVisitor 这个类的目的是简化编写定制扫描的visitor,使得我们不需要定制XMLVisitor的所有接口
type DefaultVisitor struct {
EnterDocument func(XMLDocument) bool
ExitDocument func(XMLDocument) bool
EnterElement func(XMLElement) bool
ExitElement func(XMLElement) bool
ProcInst func(XMLProcInst) bool
Text func(XMLText) bool
Comment func(XMLComment) bool
Directive func(XMLDirective) bool
}
// VisitEnterDocument is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitEnterDocument(doc XMLDocument) bool {
if nil == v.EnterDocument {
return true
}
return v.EnterDocument(doc)
}
// VisitExitDocument is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitExitDocument(doc XMLDocument) bool {
if nil == v.ExitDocument {
return true
}
return v.ExitDocument(doc)
}
// VisitEnterElement is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitEnterElement(elem XMLElement) bool {
if nil == v.EnterElement {
return true
}
return v.EnterElement(elem)
}
// VisitExitElement is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitExitElement(elem XMLElement) bool {
if nil == v.ExitElement {
return true
}
return v.ExitElement(elem)
}
// VisitProcInst is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitProcInst(pi XMLProcInst) bool {
if nil == v.ProcInst {
return true
}
return v.ProcInst(pi)
}
// VisitText is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitText(text XMLText) bool {
if nil == v.Text {
return true
}
return v.Text(text)
}
// VisitComment is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitComment(c XMLComment) bool {
if nil == v.Comment {
return true
}
return v.Comment(c)
}
// VisitDirective is the default implement of XMLVisitor
func (v *DefaultVisitor) VisitDirective(d XMLDirective) bool {
if nil == v.Directive {
return true
}
return v.Directive(d)
}
// ------------------------------------------------------------------
type xmlSimplePrinter struct {
writer io.Writer // 输出目的地