/
libxml.go
349 lines (285 loc) · 7.49 KB
/
libxml.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
// Thanks to the following mail thread for the original impetus:
// https://groups.google.com/group/golang-nuts/browse_thread/thread/d8b220af6fdb7075
// Package libxml provides access libxml2, an XML parsing library, found here:
// http://xmlsoft.org/
package libxml
/*
#cgo CFLAGS: -I/usr/include/libxml2
#cgo LDFLAGS: -L/usr/lib
#cgo LDFLAGS: -lxml2
#include <libxml/xmlversion.h>
#include <libxml/parser.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlstring.h>
#include <libxml/xpath.h>
xmlNodePtr nodeSetGetItem(xmlNodeSetPtr ns, int n) {
return xmlXPathNodeSetItem(ns, n);
}
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
const (
//parser option
HTML_PARSE_RECOVER = 1 << 0 //relaxed parsing
HTML_PARSE_NOERROR = 1 << 5 //suppress error reports
HTML_PARSE_NOWARNING = 1 << 6 //suppress warning reports
HTML_PARSE_PEDANTIC = 1 << 7 //pedantic error reporting
HTML_PARSE_NOBLANKS = 1 << 8 //remove blank nodes
HTML_PARSE_NONET = 1 << 11 //forbid network access
HTML_PARSE_COMPACT = 1 << 16 //compact small text nodes
//element type
XML_ELEMENT_NODE = 1
XML_ATTRIBUTE_NODE = 2
XML_TEXT_NODE = 3
XML_CDATA_SECTION_NODE = 4
XML_ENTITY_REF_NODE = 5
XML_ENTITY_NODE = 6
XML_PI_NODE = 7
XML_COMMENT_NODE = 8
XML_DOCUMENT_NODE = 9
XML_DOCUMENT_TYPE_NODE = 10
XML_DOCUMENT_FRAG_NODE = 11
XML_NOTATION_NODE = 12
XML_HTML_DOCUMENT_NODE = 13
XML_DTD_NODE = 14
XML_ELEMENT_DECL = 15
XML_ATTRIBUTE_DECL = 16
XML_ENTITY_DECL = 17
XML_NAMESPACE_DECL = 18
XML_XINCLUDE_START = 19
XML_XINCLUDE_END = 20
XML_DOCB_DOCUMENT_NODE = 21
)
const (
// enum xmlXPathObjectType:
XPATH_UNDEFINED = iota
XPATH_NODESET
XPATH_BOOLEAN
XPATH_NUMBER
XPATH_STRING
XPATH_POINT
XPATH_RANGE
XPATH_LOCATIONSET
XPATH_USERS
XPATH_XSLT_TREE
)
const (
// enum xmlXPathError:
XPATH_EXPRESSION_OK = iota
XPATH_NUMBER_ERROR
XPATH_UNFINISHED_LITERAL_ERROR
XPATH_START_LITERAL_ERROR
XPATH_VARIABLE_REF_ERROR
XPATH_UNDEF_VARIABLE_ERROR
XPATH_INVALID_PREDICATE_ERROR
XPATH_EXPR_ERROR
XPATH_UNCLOSED_ERROR
XPATH_UNKNOWN_FUNC_ERROR
XPATH_INVALID_OPERAND
XPATH_INVALID_TYPE
XPATH_INVALID_ARITY
XPATH_INVALID_CTXT_SIZE
XPATH_INVALID_CTXT_POSITION
XPATH_MEMORY_ERROR
XPTR_SYNTAX_ERROR
XPTR_RESOURCE_ERROR
XPTR_SUB_RESOURCE_ERROR
XPATH_UNDEF_PREFIX_ERROR
XPATH_ENCODING_ERROR
XPATH_INVALID_CHAR_ERROR
XPATH_INVALID_CTXT
)
func freeCString(s *C.char) {
C.free(unsafe.Pointer(s))
}
func xmlCharToString(s *C.xmlChar) string {
cstr := unsafe.Pointer(s)
str := C.GoString((*C.char)(cstr))
return str
}
func stringToXmlChar(s string) *C.xmlChar {
cstr := C.CString(s)
defer freeCString(cstr)
c := C.xmlCharStrdup(cstr)
return c
}
type XmlNode struct {
Ptr *C.xmlNode
// cached attrs
attrs map[string]string
}
func (n *XmlNode) Name() string {
return xmlCharToString(n.Ptr.name)
}
func (n *XmlNode) Type() int {
return int(n.Ptr._type)
}
func (n *XmlNode) Next() *XmlNode {
next := (*C.xmlNode)(unsafe.Pointer(n.Ptr.next))
if next == nil {
return nil
}
return &XmlNode{Ptr: next}
}
func (n *XmlNode) Children() *XmlNode {
children := (*C.xmlNode)(unsafe.Pointer(n.Ptr.children))
if children == nil {
return nil
}
return &XmlNode{Ptr: children}
}
func (n *XmlNode) IsText() bool {
return C.xmlNodeIsText(n.Ptr) != 0
}
func (n *XmlNode) Text() string {
if !n.IsText() {
return ""
}
return xmlCharToString(C.xmlNodeListGetString(nil, n.Ptr, 0))
}
func (n *XmlNode) Attr(name string) string {
xname := stringToXmlChar(name)
return xmlCharToString(C.xmlGetProp(n.Ptr, xname))
}
func (n *XmlNode) Attrs() map[string]string {
if n.attrs != nil {
return n.attrs
}
propList := n.Ptr.properties
attrs := make(map[string]string)
for prop := propList; prop != nil; prop = prop.next {
name := xmlCharToString(prop.name)
attrs[name] = n.Attr(name)
}
n.attrs = attrs
return attrs
}
func (n *XmlNode) XPath(xpathExpr string) *XPathResult {
context := C.xmlXPathNewContext((C.xmlDocPtr)(unsafe.Pointer(n.Ptr.doc)))
context.node = n.Ptr
defer C.xmlXPathFreeContext(context)
result := C.xmlXPathEvalExpression(stringToXmlChar(xpathExpr), context)
fmt.Println("XPath: ", xpathExpr, " Type: ", result._type, " Result: ", result)
return &XPathResult{ptr: result}
}
type XmlDoc struct {
Ptr *C.xmlDoc
root *C.xmlNode
}
func (d *XmlDoc) Root() *XmlNode {
return &XmlNode{Ptr: d.root}
}
func (d *XmlDoc) XPath(xpathExpr string) *XPathResult {
context := C.xmlXPathNewContext(d.Ptr)
defer C.xmlXPathFreeContext(context)
result := C.xmlXPathEvalExpression(stringToXmlChar(xpathExpr), context)
fmt.Println("XPath: ", xpathExpr, " Type: ", result._type, " Result: ", result)
return &XPathResult{ptr: result}
}
func (d *XmlDoc) Close() error {
XmlFreeDoc(d.Ptr)
d.Ptr = nil
d.root = nil
return nil
}
type XPathResult struct {
ptr C.xmlXPathObjectPtr
}
func (r *XPathResult) Type() uint {
return uint(r.ptr._type)
}
func (r *XPathResult) Nodes() []*XmlNode {
if r.Type() != XPATH_NODESET {
return nil
}
nodeSet := r.ptr.nodesetval
fmt.Println("nodeSet: ", nodeSet)
nodeSetLen := int(nodeSet.nodeNr)
fmt.Println("nodeSetLen: ", nodeSetLen)
nodes := make([]*XmlNode, nodeSetLen)
for i := 0; i < nodeSetLen; i++ {
node := C.nodeSetGetItem(nodeSet, C.int(i))
nodes[i] = &XmlNode{Ptr: node}
}
return nodes
}
func (r *XPathResult) String() string {
if r.Type() != XPATH_STRING {
panic("libxml: Not a string")
}
return xmlCharToString(C.xmlXPathCastToString(r.ptr))
}
func (r *XPathResult) Number() float64 {
if r.Type() != XPATH_NUMBER {
panic("libxml: Not a number")
}
return float64(C.xmlXPathCastToNumber(r.ptr))
}
func (r *XPathResult) Boolean() bool {
if r.Type() != XPATH_BOOLEAN {
panic("libxml: Not a boolean")
}
return C.xmlXPathCastToBoolean(r.ptr) != 0
}
const DEFAULT_HTML_PARSE_FLAGS = HTML_PARSE_COMPACT | HTML_PARSE_NOBLANKS |
HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING
func ParseHTML(src string) (*XmlDoc, error) {
d := HtmlReadDoc(src, "", "", DEFAULT_HTML_PARSE_FLAGS)
//get root node
root := XmlDocGetRootElement(d)
if root == nil {
//no nodes
XmlFreeDoc(d)
return nil, errors.New("No nodes")
}
return &XmlDoc{Ptr: d, root: root}, nil
}
func XmlCheckVersion() int {
var v C.int
C.xmlCheckVersion(v)
return int(v)
}
func XmlCleanUpParser() {
C.xmlCleanupParser()
}
func XmlFreeDoc(d *C.xmlDoc) {
C.xmlFreeDoc(d)
}
func HtmlReadFile(url string, encoding string, opts int) *C.xmlDoc {
return C.htmlReadFile(C.CString(url), C.CString(encoding), C.int(opts))
}
func HtmlReadDoc(content string, url string, encoding string, opts int) *C.xmlDoc {
c := C.xmlCharStrdup(C.CString(content))
return C.htmlReadDoc(c, C.CString(url), C.CString(encoding), C.int(opts))
}
func HtmlGetMetaEncoding(d *C.xmlDoc) string {
s := C.htmlGetMetaEncoding(d)
return xmlCharToString(s)
}
func XmlDocGetRootElement(d *C.xmlDoc) *C.xmlNode {
return C.xmlDocGetRootElement(d)
}
func XmlGetProp(n *C.xmlNode, name string) string {
c := C.xmlCharStrdup(C.CString(name))
s := C.xmlGetProp(n, c)
return xmlCharToString(s)
}
func HtmlTagLookup(name string) *C.htmlElemDesc {
c := C.xmlCharStrdup(C.CString(name))
return C.htmlTagLookup(c)
}
func HtmlEntityLookup(name string) *C.htmlEntityDesc {
c := C.xmlCharStrdup(C.CString(name))
return C.htmlEntityLookup(c)
}
func HtmlEntityValueLookup(value uint) *C.htmlEntityDesc {
return C.htmlEntityValueLookup(C.uint(value))
}
//Helpers
func NewDoc() (doc *C.xmlDoc) { return }
func NewNode() (node *C.xmlNode) { return }