forked from lestrrat-go/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_document.go
executable file
·363 lines (302 loc) · 9.49 KB
/
node_document.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
package dom
import (
"github.com/pkg/errors"
"github.com/wayf-dk/go-libxml2/clib"
"github.com/wayf-dk/go-libxml2/types"
)
// CreateDocument creates a new document with version="1.0", and no encoding
func CreateDocument() *Document {
return NewDocument("1.0", "")
}
// NewDocument creates a new document
func NewDocument(version, encoding string) *Document {
ptr := clib.XMLCreateDocument(version, encoding)
return WrapDocument(ptr)
}
// Pointer returns the pointer to the underlying C struct
func (d *Document) Pointer() uintptr {
return d.ptr
}
// AutoFree calls Free() if the document is moral.
func (d *Document) AutoFree() {
if !d.mortal {
return
}
d.Free()
}
// MakeMortal sets the flag
func (d *Document) MakeMortal() {
d.mortal = true
}
// MakePersistent unsets the flag
func (d *Document) MakePersistent() {
d.mortal = false
}
// IsSameNode checks if the underlying C pointer points to the same C struct
func (d *Document) IsSameNode(n types.Node) bool {
return d.ptr == n.Pointer()
}
// HasChildNodes returns true if the document node is available
func (d *Document) HasChildNodes() bool {
_, err := d.DocumentElement()
return err != nil
}
// FirstChild returns the document element
func (d *Document) FirstChild() (types.Node, error) {
root, err := d.DocumentElement()
if err != nil {
return nil, errors.Wrap(err, "failed to get document element")
}
return root, nil
}
// LastChild returns the document element
func (d *Document) LastChild() (types.Node, error) {
root, err := d.DocumentElement()
if err != nil {
return nil, errors.Wrap(err, "failed to get document element")
}
return root, nil
}
// NextSibling always returns nil for Document
func (d *Document) NextSibling() (types.Node, error) {
return nil, errors.New("document has no siblings")
}
// PreviousSibling always returns nil for Document
func (d *Document) PreviousSibling() (types.Node, error) {
return nil, errors.New("document has no siblings")
}
// NodeName always returns an empty string for Document
func (d *Document) NodeName() string {
return ""
}
// SetNodeName is a no op for document
func (d *Document) SetNodeName(s string) {
// return errors.New("cannot set node name on a document")
}
// NodeValue always returns an empty string for Document
func (d *Document) NodeValue() string {
return ""
}
// SetNodeValue is a no op for document
func (d *Document) SetNodeValue(s string) {
// return errors.New("cannot set node value on a document")
}
// OwnerDocument always returns the document itself
func (d *Document) OwnerDocument() (types.Document, error) {
return d, nil
}
// SetDocument always returns an error for a document
func (d *Document) SetDocument(n types.Document) error {
return errors.New("cannot set document on a document")
}
// ParentNode always returns an error for a document
func (d *Document) ParentNode() (types.Node, error) {
return nil, errors.New("document has no parent node")
}
// ParseInContext is currently unimplemented
func (d *Document) ParseInContext(s string, n int) (types.Node, error) {
return nil, errors.New("unimplemented")
}
// Literal is currently just an alias to Dump(false)
func (d *Document) Literal() (string, error) {
return d.Dump(false), nil
}
// TextContent returns the text content
func (d *Document) TextContent() string {
return clib.XMLTextContent(d)
}
// ToString is currently just an alias to Dump(false)
func (d *Document) ToString(x int, b bool) string {
return d.Dump(b)
}
// ChildNodes returns the document element
func (d *Document) ChildNodes() (types.NodeList, error) {
root, err := d.DocumentElement()
if err != nil {
return nil, errors.Wrap(err, "failed to get document element")
}
return []types.Node{root}, nil
}
// Copy is currently unimplemented
func (d *Document) Copy() (types.Node, error) {
// Unimplemented
return nil, errors.New("unimplemented")
}
// AddChild is a no op for Document
func (d *Document) AddChild(n types.Node) error {
return errors.New("method AddChild is not available for Document node")
}
// AddPrevSibling is a no op for Document
func (d *Document) AddPrevSibling(n types.Node) error {
return errors.New("method AddChild is not available for Document node")
}
// CreateAttribute creates a new attribute
func (d *Document) CreateAttribute(k, v string) (*Attribute, error) {
attr, err := clib.XMLNewDocProp(d, k, v)
if err != nil {
return nil, errors.Wrap(err, "failed to get document property")
}
return wrapAttributeNode(attr), nil
}
// CreateAttributeNS creates a new attribute with the given XML namespace
func (d *Document) CreateAttributeNS(nsuri, k, v string) (*Attribute, error) {
if nsuri == "" {
return d.CreateAttribute(k, v)
}
ptr, err := clib.XMLCreateAttributeNS(d, nsuri, k, v)
if err != nil {
return nil, errors.Wrap(err, "failed to create attribute")
}
return wrapAttributeNode(ptr), nil
}
// CreateCDataSection creates a new CDATA section node
func (d *Document) CreateCDataSection(txt string) (*CDataSection, error) {
cdata, err := clib.XMLNewCDataBlock(d, txt)
if err != nil {
return nil, errors.Wrap(err, "failed to create CDATA block")
}
return wrapCDataSectionNode(cdata), nil
}
// CreateCommentNode creates a new comment node
func (d *Document) CreateCommentNode(txt string) (*Comment, error) {
ptr, err := clib.XMLNewComment(txt)
if err != nil {
return nil, errors.Wrap(err, "failed to create comment")
}
return wrapCommentNode(ptr), nil
}
// CreateElement creates a new element node
func (d *Document) CreateElement(name string) (types.Element, error) {
ptr, err := clib.XMLCreateElement(d, name)
if err != nil {
return nil, errors.Wrap(err, "failed to create element")
}
return wrapElementNode(ptr), nil
}
// CreateElementNS creates a new element node in the given XML namespace
func (d *Document) CreateElementNS(nsuri, name string) (types.Element, error) {
ptr, err := clib.XMLCreateElementNS(d, nsuri, name)
if err != nil {
return nil, errors.Wrap(err, "failed to create element")
}
return wrapElementNode(ptr), nil
}
// CreateTextNode creates a new text node
func (d *Document) CreateTextNode(txt string) (*Text, error) {
ptr, err := clib.XMLNewText(txt)
if err != nil {
return nil, errors.Wrap(err, "failed to create text node")
}
return wrapTextNode(ptr), nil
}
// DocumentElement returns the root node of the document
func (d *Document) DocumentElement() (types.Node, error) {
n, err := clib.XMLDocumentElement(d)
if err != nil {
return nil, errors.Wrap(err, "failed to get document element")
}
return WrapNode(n)
}
// Find returns the nodes that can be selected with the
// given xpath string
func (d *Document) Find(xpath string) (types.XPathResult, error) {
root, err := d.DocumentElement()
if err != nil {
return nil, errors.Wrap(err, "failed to get document element")
}
return root.Find(xpath)
}
// Encoding returns the d
func (d *Document) Encoding() string {
return clib.XMLDocumentEncoding(d)
}
// Free releases the underlying C struct
func (d *Document) Free() {
clib.XMLFreeDoc(d)
d.ptr = 0
docPool.Put(*d)
}
// String formats the document, always without formatting.
func (d *Document) String() string {
return clib.XMLDocumentString(d, d.Encoding(), false)
}
// Dump formats the document with or withour formatting.
func (d *Document) Dump(format bool) string {
f := 0
if format {
f = 1
}
// return clib.XMLToString(d, f, false)
return clib.XMLToString(d, f, true)
// return clib.XMLDocumentString(d, d.Encoding(), format)
}
// NodeType returns the XMLNodeType
func (d *Document) NodeType() clib.XMLNodeType {
return DocumentNode
}
// SetBaseURI sets the base URI
func (d *Document) SetBaseURI(s string) {
clib.XMLNodeSetBase(d, s)
}
// SetDocumentElement sets the document element
func (d *Document) SetDocumentElement(n types.Node) error {
return clib.XMLSetDocumentElement(d, n)
}
// SetEncoding sets the encoding of the document
func (d *Document) SetEncoding(e string) {
clib.XMLSetDocumentEncoding(d, e)
}
// SetStandalone sets the standalone flag
func (d *Document) SetStandalone(v int) {
clib.XMLSetDocumentStandalone(d, v)
}
// SetVersion sets the version of the document
func (d *Document) SetVersion(v string) {
clib.XMLSetDocumentVersion(d, v)
}
// Standalone returns the value of the standalone flag
func (d *Document) Standalone() int {
return clib.XMLDocumentStandalone(d)
}
// URI returns the document URI
func (d *Document) URI() string {
return clib.XMLDocumentURI(d)
}
// Version returns the version of the document
func (d *Document) Version() string {
return clib.XMLDocumentVersion(d)
}
// Walk traverses the nodes in the document
func (d *Document) Walk(fn func(types.Node) error) error {
root, err := d.DocumentElement()
if err != nil {
return errors.Wrap(err, "failed to get document element")
}
walk(root, fn)
return nil
}
// LookupNamespacePrefix looks for a namespace prefix that matches
// the given namespace URI
func (d *Document) LookupNamespacePrefix(href string) (string, error) {
root, err := d.DocumentElement()
if err != nil {
return "", errors.Wrap(err, "failed to get document element")
}
return root.LookupNamespacePrefix(href)
}
// LookupNamespaceURI looks for a namespace uri that matches
// the given namespace prefix
func (d *Document) LookupNamespaceURI(prefix string) (string, error) {
root, err := d.DocumentElement()
if err != nil {
return "", errors.Wrap(err, "failed to get document element")
}
return root.LookupNamespaceURI(prefix)
}
func (d *Document) RemoveChild(n types.Node) error {
root, err := d.DocumentElement()
if err != nil {
return errors.Wrap(err, "failed to get document element")
}
return root.RemoveChild(n)
}