forked from lestrrat-go/libxml2
/
node.go
836 lines (695 loc) · 17.9 KB
/
node.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
package libxml2
/*
#cgo pkg-config: libxml-2.0
#include <stdbool.h>
#include "libxml/globals.h"
#include "libxml/tree.h"
#include "libxml/parser.h"
#include "libxml/parserInternals.h"
#include "libxml/xpath.h"
#include "libxml/c14n.h"
// Macro wrapper function
static inline void MY_xmlFree(void *p) {
xmlFree(p);
}
// Change xmlIndentTreeOutput global, return old value, so caller can
// change it back to old value later
static inline int MY_setXmlIndentTreeOutput(int i) {
int old = xmlIndentTreeOutput;
xmlIndentTreeOutput = i;
return old;
}
// Parse a single char out of cur
// Stolen from XML::LibXML
static inline int
MY_parseChar( xmlChar *cur, int *len )
{
unsigned char c;
unsigned int val;
// We are supposed to handle UTF8, check it's valid
// From rfc2044: encoding of the Unicode values on UTF-8:
//
// UCS-4 range (hex.) UTF-8 octet sequence (binary)
// 0000 0000-0000 007F 0xxxxxxx
// 0000 0080-0000 07FF 110xxxxx 10xxxxxx
// 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
//
// Check for the 0x110000 limit too
if ( cur == NULL || *cur == 0 ) {
*len = 0;
return(0);
}
c = *cur;
if ( (c & 0x80) == 0 ) {
*len = 1;
return((int)c);
}
if ((c & 0xe0) == 0xe0) {
if ((c & 0xf0) == 0xf0) {
// 4-byte code
*len = 4;
val = (cur[0] & 0x7) << 18;
val |= (cur[1] & 0x3f) << 12;
val |= (cur[2] & 0x3f) << 6;
val |= cur[3] & 0x3f;
} else {
// 3-byte code
*len = 3;
val = (cur[0] & 0xf) << 12;
val |= (cur[1] & 0x3f) << 6;
val |= cur[2] & 0x3f;
}
} else {
// 2-byte code
*len = 2;
val = (cur[0] & 0x1f) << 6;
val |= cur[1] & 0x3f;
}
if ( !IS_CHAR(val) ) {
*len = -1;
return 0;
}
return val;
}
// Checks if the given name is a valid name in XML
// stolen from XML::LibXML
static inline int
MY_test_node_name( xmlChar * name )
{
xmlChar * cur = name;
int tc = 0;
int len = 0;
if ( cur == NULL || *cur == 0 ) {
return 0;
}
tc = MY_parseChar( cur, &len );
if ( !( IS_LETTER( tc ) || (tc == '_') || (tc == ':')) ) {
return 0;
}
tc = 0;
cur += len;
while (*cur != 0 ) {
tc = MY_parseChar( cur, &len );
if (!(IS_LETTER(tc) || IS_DIGIT(tc) || (tc == '_') ||
(tc == '-') || (tc == ':') || (tc == '.') ||
IS_COMBINING(tc) || IS_EXTENDER(tc)) )
{
return 0;
}
tc = 0;
cur += len;
}
return(1);
}
*/
import "C"
import (
"bytes"
"errors"
"fmt"
"strings"
"unsafe"
)
type XmlNodeType int
const (
ElementNode XmlNodeType = iota + 1
AttributeNode
TextNode
CDataSectionNode
EntityRefNode
EntityNode
PiNode
CommentNode
DocumentNode
DocumentTypeNode
DocumentFragNode
NotationNode
HTMLDocumentNode
DTDNode
ElementDecl
AttributeDecl
EntityDecl
NamespaceDecl
XIncludeStart
XIncludeEnd
DocbDocumentNode
)
var _XmlNodeType_index = [...]uint8{0, 11, 24, 32, 48, 61, 71, 77, 88, 100, 116, 132, 144, 160, 167, 178, 191, 201, 214, 227, 238, 254}
const _XmlNodeType_name = `ElementNodeAttributeNodeTextNodeCDataSectionNodeEntityRefNodeEntityNodePiNodeCommentNodeDocumentNodeDocumentTypeNodeDocumentFragNodeNotationNodeHTMLDocumentNodeDTDNodeElementDeclAttributeDeclEntityDeclNamespaceDeclXIncludeStartXIncludeEndDocbDocumentNode`
func (i XmlNodeType) String() string {
i -= 1
if i < 0 || i+1 >= XmlNodeType(len(_XmlNodeType_index)) {
return fmt.Sprintf("XmlNodeType(%d)", i+1)
}
return _XmlNodeType_name[_XmlNodeType_index[i]:_XmlNodeType_index[i+1]]
}
var (
ErrNodeNotFound = errors.New("node not found")
ErrInvalidArgument = errors.New("invalid argument")
ErrInvalidNodeName = errors.New("invalid node name")
)
// Node defines the basic DOM interface
type Node interface {
// pointer() returns the underlying C pointer. Only we are allowed to
// slice it, dice it, do whatever the heck with it.
pointer() unsafe.Pointer
AddChild(Node)
AppendChild(Node) error
ChildNodes() NodeList
OwnerDocument() *Document
FindNodes(string) (NodeList, error)
FirstChild() Node
HasChildNodes() bool
IsSameNode(Node) bool
LastChild() Node
// Literal is almost the same as String(), except for things like Element
// and Attribute nodes. String() will return the XML stringification of
// these, but Literal() will return the "value" associated with them.
Literal() string
NextSibling() Node
NodeName() string
NodeType() XmlNodeType
NodeValue() string
ParetNode() Node
PreviousSibling() Node
SetNodeName(string)
SetNodeValue(string)
String() string
TextContent() string
ToString(int, bool) string
ToStringC14N(bool) (string, error)
Walk(func(Node) error)
}
type NodeList []Node
func (n NodeList) String() string {
buf := bytes.Buffer{}
for _, x := range n {
buf.WriteString(x.String())
}
return buf.String()
}
func (n NodeList) Literal() string {
buf := bytes.Buffer{}
for _, x := range n {
buf.WriteString(x.Literal())
}
return buf.String()
}
type xmlNode struct {
ptr *C.xmlNode
}
type XmlNode struct {
*xmlNode
}
type Attribute struct {
*XmlNode
}
type CDataSection struct {
*XmlNode
}
type Comment struct {
*XmlNode
}
type Element struct {
*XmlNode
}
type Document struct {
ptr *C.xmlDoc
root *C.xmlNode
}
type Text struct {
*XmlNode
}
func wrapAttribute(n *C.xmlAttr) *Attribute {
return &Attribute{wrapXmlNode((*C.xmlNode)(unsafe.Pointer(n)))}
}
func wrapCDataSection(n *C.xmlNode) *CDataSection {
return &CDataSection{wrapXmlNode(n)}
}
func wrapComment(n *C.xmlNode) *Comment {
return &Comment{wrapXmlNode(n)}
}
func wrapElement(n *C.xmlElement) *Element {
return &Element{wrapXmlNode((*C.xmlNode)(unsafe.Pointer(n)))}
}
func wrapXmlNode(n *C.xmlNode) *XmlNode {
return &XmlNode{
&xmlNode{
ptr: (*C.xmlNode)(unsafe.Pointer(n)),
},
}
}
func wrapText(n *C.xmlNode) *Text {
return &Text{wrapXmlNode(n)}
}
func wrapToNode(n *C.xmlNode) Node {
switch XmlNodeType(n._type) {
case ElementNode:
return wrapElement((*C.xmlElement)(unsafe.Pointer(n)))
case TextNode:
return &Text{&XmlNode{&xmlNode{ptr: n}}}
default:
return &XmlNode{&xmlNode{ptr: n}}
}
}
func nodeName(n Node) string {
switch n.NodeType() {
case XIncludeStart, XIncludeEnd, EntityRefNode, EntityNode, DTDNode, EntityDecl, DocumentTypeNode, NotationNode, NamespaceDecl:
return xmlCharToString((*C.xmlNode)(n.pointer()).name)
case CommentNode:
return "#comment"
case CDataSectionNode:
return "#cdata-section"
case TextNode:
return "#text"
case DocumentNode, HTMLDocumentNode, DocbDocumentNode:
return "#document"
case DocumentFragNode:
return "#document-fragment"
case ElementNode, AttributeNode:
ptr := (*C.xmlNode)(n.pointer())
if ns := ptr.ns; ns != nil {
if nsstr := xmlCharToString(ns.prefix); nsstr != "" {
return fmt.Sprintf("%s:%s", xmlCharToString(ns.prefix), xmlCharToString(ptr.name))
}
}
return xmlCharToString(ptr.name)
case ElementDecl, AttributeDecl:
panic("unimplemented")
default:
panic("unknown")
}
}
func nodeValue(n Node) string {
switch n.NodeType() {
case AttributeNode, TextNode, CommentNode, CDataSectionNode, PiNode, EntityRefNode:
return xmlCharToString(C.xmlXPathCastNodeToString((*C.xmlNode)(n.pointer())))
case EntityDecl:
np := (*C.xmlNode)(n.pointer())
if np.content != nil {
return xmlCharToString(C.xmlStrdup(np.content))
}
panic("unimplmented")
}
return ""
}
func (n *xmlNode) pointer() unsafe.Pointer {
return unsafe.Pointer(n.ptr)
}
func (n *xmlNode) AddChild(child Node) {
C.xmlAddChild(n.ptr, (*C.xmlNode)(child.pointer()))
}
func (n *xmlNode) AppendChild(child Node) error {
// XXX There must be lots more checks here because AddChild does things
// under the table like merging text nodes, freeing some nodes implicitly,
// et al
n.AddChild(child)
return nil
}
func (n *xmlNode) ChildNodes() NodeList {
return childNodes(n)
}
func wrapDocument(n *C.xmlDoc) *Document {
r := C.xmlDocGetRootElement(n) // XXX Should check for n == nil
return &Document{ptr: n, root: r}
}
func (n *xmlNode) OwnerDocument() *Document {
return wrapDocument(n.ptr.doc)
}
func (n *xmlNode) FindNodes(xpath string) (NodeList, error) {
ctx, err := NewXPathContext(n)
if err != nil {
return nil, err
}
defer ctx.Free()
return ctx.FindNodes(xpath)
}
func (n *xmlNode) FindNodesExpr(expr *XPathExpression) (NodeList, error) {
ctx, err := NewXPathContext(n)
if err != nil {
return nil, err
}
defer ctx.Free()
return ctx.FindNodesExpr(expr)
}
func (n *xmlNode) FirstChild() Node {
if !n.HasChildNodes() {
return nil
}
return wrapToNode(((*C.xmlNode)(n.pointer())).children)
}
func (n *xmlNode) HasChildNodes() bool {
return n.ptr.children != nil
}
func (n *xmlNode) IsSameNode(other Node) bool {
return n.pointer() == other.pointer()
}
func (n *xmlNode) LastChild() Node {
return wrapToNode(n.ptr.last)
}
func (n xmlNode) Literal() string {
return n.String()
}
func (n *xmlNode) LocalName() string {
switch n.NodeType() {
case ElementNode, AttributeNode, ElementDecl, AttributeDecl:
return xmlCharToString(n.ptr.name)
}
return ""
}
func (n *xmlNode) NamespaceURI() string {
switch n.NodeType() {
case ElementNode, AttributeNode, PiNode:
if ns := n.ptr.ns; ns != nil && ns.href != nil {
return xmlCharToString(ns.href)
}
}
return ""
}
func (n *xmlNode) NodeName() string {
return nodeName(n)
}
func (n *xmlNode) NodeValue() string {
return nodeValue(n)
}
func (n *xmlNode) NextSibling() Node {
return wrapToNode(n.ptr.next)
}
func (n *xmlNode) ParetNode() Node {
return wrapToNode(n.ptr.parent)
}
func (n *xmlNode) Prefix() string {
switch n.NodeType() {
case ElementNode, AttributeNode, PiNode:
if ns := n.ptr.ns; ns != nil && ns.prefix != nil {
return xmlCharToString(ns.prefix)
}
}
return ""
}
func (n *xmlNode) PreviousSibling() Node {
return wrapToNode(n.ptr.prev)
}
func (n *xmlNode) SetNodeName(name string) {
C.xmlNodeSetName(n.ptr, stringToXmlChar(name))
}
func (n *xmlNode) SetNodeValue(value string) {
// TODO: Implement this in C
if n.NodeType() != AttributeNode {
C.xmlNodeSetContent(n.ptr, stringToXmlChar(value))
return
}
ptr := n.ptr
if ptr.children != nil {
ptr.last = nil
C.xmlFreeNodeList(ptr.children)
}
ptr.children = C.xmlNewText(stringToXmlChar(value))
ptr.children.parent = ptr
ptr.children.doc = ptr.doc
ptr.last = ptr.children
}
func (n *xmlNode) String() string {
return n.ToString(0, false)
}
func (n *xmlNode) TextContent() string {
return xmlCharToString(C.xmlXPathCastNodeToString(n.ptr))
}
func (n *xmlNode) ToString(format int, docencoding bool) string {
// TODO: Implement htis in C
buffer := C.xmlBufferCreate()
defer C.xmlBufferFree(buffer)
if format <= 0 {
C.xmlNodeDump(buffer, n.ptr.doc, n.ptr, 0, 0)
} else {
oIndentTreeOutput := C.MY_setXmlIndentTreeOutput(1)
C.xmlNodeDump(buffer, n.ptr.doc, n.ptr, 0, C.int(format))
C.MY_setXmlIndentTreeOutput(oIndentTreeOutput)
}
return xmlCharToString(C.xmlBufferContent(buffer))
}
func (n *xmlNode) ToStringC14N(exclusive bool) (string, error) {
var result *C.xmlChar
var exclInt C.int
if exclusive {
exclInt = 1
}
ret := C.xmlC14NDocDumpMemory(
n.ptr.doc,
nil,
exclInt,
nil,
0,
&result,
)
if ret == 0 {
return "", errors.New("Boo")
}
return xmlCharToString(result), nil
}
func (n *xmlNode) NodeType() XmlNodeType {
return XmlNodeType(n.ptr._type)
}
func (n *xmlNode) Walk(fn func(Node) error) {
panic("should not call walk on internal struct")
}
func (n *XmlNode) Walk(fn func(Node) error) {
walk(n, fn)
}
func walk(n Node, fn func(Node) error) {
if err := fn(n); err != nil {
return
}
for _, c := range n.ChildNodes() {
walk(c, fn)
}
}
func childNodes(n Node) NodeList {
ret := NodeList(nil)
for chld := ((*C.xmlNode)(n.pointer())).children; chld != nil; chld = chld.next {
ret = append(ret, wrapToNode(chld))
}
return ret
}
func CreateDocument() *Document {
return NewDocument("1.0", "")
}
func NewDocument(version, encoding string) *Document {
doc := C.xmlNewDoc(stringToXmlChar(version))
if encoding != "" {
doc.encoding = C.xmlStrdup(stringToXmlChar(encoding))
}
return wrapDocument(doc)
}
func (d *Document) pointer() unsafe.Pointer {
return unsafe.Pointer(d.ptr)
}
func (d *Document) CreateAttribute(k, v string) (*Attribute, error) {
kx := stringToXmlChar(k)
vx := stringToXmlChar(v)
if C.MY_test_node_name(kx) == 0 {
return nil, ErrInvalidNodeName
}
buf := C.xmlEncodeEntitiesReentrant(d.ptr, vx)
newAttr := C.xmlNewDocProp(d.ptr, kx, buf)
return wrapAttribute((*C.xmlAttr)(unsafe.Pointer(newAttr))), nil
}
func (d *Document) CreateAttributeNS(nsuri, k, v string) (*Attribute, error) {
if nsuri == "" {
return d.CreateAttribute(k, v)
}
kx := stringToXmlChar(k)
if C.MY_test_node_name(kx) == 0 {
return nil, ErrInvalidNodeName
}
root := d.DocumentElement()
if root == nil {
return nil, errors.New("attribute with namespaces require a root node")
}
prefix, local := splitPrefixLocal(k)
ns := C.xmlSearchNsByHref(d.ptr, (*C.xmlNode)(root.pointer()), stringToXmlChar(nsuri))
if ns == nil {
ns = C.xmlNewNs((*C.xmlNode)(root.pointer()), stringToXmlChar(nsuri), stringToXmlChar(prefix))
if ns == nil {
return nil, errors.New("failed to create namespace")
}
}
vx := stringToXmlChar(v)
buf := C.xmlEncodeEntitiesReentrant(d.ptr, vx)
newAttr := C.xmlNewDocProp(d.ptr, stringToXmlChar(local), buf)
C.xmlSetNs((*C.xmlNode)(unsafe.Pointer(newAttr)), ns)
return wrapAttribute((*C.xmlAttr)(unsafe.Pointer(newAttr))), nil
}
func (d *Document) CreateCDataSection(txt string) (*CDataSection, error) {
return wrapCDataSection(C.xmlNewCDataBlock(d.ptr, stringToXmlChar(txt), C.int(len(txt)))), nil
}
func (d *Document) CreateCommentNode(txt string) (*Comment, error) {
return wrapComment(C.xmlNewComment(stringToXmlChar(txt))), nil
}
func (d *Document) CreateElement(name string) (*Element, error) {
if C.MY_test_node_name(stringToXmlChar(name)) == 0 {
return nil, ErrInvalidNodeName
}
newNode := C.xmlNewNode(nil, stringToXmlChar(name))
if newNode == nil {
return nil, errors.New("element creation failed")
}
// XXX hmmm...
newNode.doc = d.ptr
return wrapElement((*C.xmlElement)(unsafe.Pointer(newNode))), nil
}
func (d *Document) CreateElementNS(nsuri, name string) (*Element, error) {
if C.MY_test_node_name(stringToXmlChar(name)) == 0 {
return nil, ErrInvalidNodeName
}
i := strings.IndexByte(name, ':')
nsuriDup := stringToXmlChar(nsuri)
prefix := stringToXmlChar(name[:i])
localname := stringToXmlChar(name[i+1:])
ns := C.xmlNewNs(nil, nsuriDup, prefix)
newNode := C.xmlNewDocNode(d.ptr, ns, localname, nil)
newNode.nsDef = ns
return wrapElement((*C.xmlElement)(unsafe.Pointer(newNode))), nil
}
func (d *Document) CreateTextNode(txt string) (*Text, error) {
return wrapText(C.xmlNewText(stringToXmlChar(txt))), nil
}
func (d *Document) DocumentElement() Node {
if d.ptr == nil {
return nil
}
if d.root == nil {
n := C.xmlDocGetRootElement(d.ptr)
if n == nil {
return nil
}
d.root = n
}
return wrapToNode(d.root)
}
func (d *Document) FindNodes(xpath string) (NodeList, error) {
root := d.DocumentElement()
if root == nil {
return nil, ErrNodeNotFound
}
return root.FindNodes(xpath)
}
func (d *Document) Encoding() string {
return xmlCharToString(d.ptr.encoding)
}
func (d *Document) Free() {
C.xmlFreeDoc(d.ptr)
d.ptr = nil
d.root = nil
}
func (d *Document) String() string {
var xc *C.xmlChar
i := C.int(0)
C.xmlDocDumpMemory(d.ptr, &xc, &i)
return xmlCharToString(xc)
}
func (d *Document) NodeType() XmlNodeType {
return XmlNodeType(d.ptr._type)
}
func (d *Document) SetBaseURI(s string) {
C.xmlNodeSetBase((*C.xmlNode)(unsafe.Pointer(d.ptr)), stringToXmlChar(s))
}
func (d *Document) SetDocumentElement(n Node) {
C.xmlDocSetRootElement(d.ptr, (*C.xmlNode)(n.pointer()))
d.root = (*C.xmlNode)(n.pointer())
}
func (d *Document) SetEncoding(e string) {
if d.ptr.encoding != nil {
C.MY_xmlFree(unsafe.Pointer(d.ptr.encoding))
}
d.ptr.encoding = C.xmlStrdup(stringToXmlChar(e))
}
func (d *Document) SetStandalone(v int) {
d.ptr.standalone = C.int(v)
}
func (d *Document) SetVersion(e string) {
if d.ptr.version != nil {
C.MY_xmlFree(unsafe.Pointer(d.ptr.version))
}
d.ptr.version = C.xmlStrdup(stringToXmlChar(e))
}
func (d *Document) Standalone() int {
return int(d.ptr.standalone)
}
func (d *Document) ToString(skipXmlDecl bool) string {
buf := &bytes.Buffer{}
for _, n := range childNodes(wrapXmlNode((*C.xmlNode)(d.pointer()))) {
if n.NodeType() == DTDNode {
continue
}
buf.WriteString(n.String())
}
return buf.String()
}
func (d *Document) ToStringC14N(exclusive bool) (string, error) {
return d.DocumentElement().ToStringC14N(exclusive)
}
func (d *Document) URI() string {
return xmlCharToString(C.xmlStrdup(d.ptr.URL))
}
func (d *Document) Version() string {
return xmlCharToString(d.ptr.version)
}
func (d *Document) Walk(fn func(Node) error) {
walk(wrapXmlNode(d.root), fn)
}
func (n *Element) AppendText(s string) error {
txt, err := n.OwnerDocument().CreateTextNode(s)
if err != nil {
return err
}
return n.AppendChild(txt)
}
func splitPrefixLocal(s string) (string, string) {
i := strings.IndexByte(s, ':')
if i == -1 {
return "", s
}
return s[:i], s[i+1:]
}
func (n *Element) getAttributeNode(name string) (*C.xmlAttr, error) {
prop := C.xmlHasNsProp(n.ptr, stringToXmlChar(name), nil)
if prop != nil {
prefix, local := splitPrefixLocal(name)
if local != "" {
ns := C.xmlSearchNs(n.ptr.doc, n.ptr, stringToXmlChar(prefix))
if ns != nil {
prop = C.xmlHasNsProp(n.ptr, stringToXmlChar(local), ns.href)
}
}
}
if prop == nil || XmlNodeType(prop._type) != AttributeNode {
return nil, errors.New("attribute not found")
}
return prop, nil
}
func (n *Element) RemoveAttribute(name string) error {
prop, err := n.getAttributeNode(name)
if err != nil {
return err
}
C.xmlUnlinkNode((*C.xmlNode)(unsafe.Pointer(prop)))
C.xmlFreeProp(prop)
return nil
}
func (n Element) Literal() string {
buf := bytes.Buffer{}
for _, c := range n.ChildNodes() {
buf.WriteString(c.Literal())
}
return buf.String()
}
func (n Text) Data() string {
return xmlCharToString(n.ptr.content)
}
func (n *Text) Walk(fn func(Node) error) {
walk(n, fn)
}
func (n *Attribute) HasChildNodes() bool {
return false
}