-
Notifications
You must be signed in to change notification settings - Fork 11
/
xpath.go
202 lines (166 loc) · 4.68 KB
/
xpath.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
package golibxml
/*
#cgo pkg-config: libxml-2.0
#include <libxml/xpath.h>
static inline void free_string(char* s) { free(s); }
static inline xmlChar *to_xmlcharptr(const char *s) { return (xmlChar *)s; }
static inline char *to_charptr(const xmlChar *s) { return (char *)s; }
*/
import "C"
import "unsafe"
////////////////////////////////////////////////////////////////////////////////
// TYPES/STRUCTS
////////////////////////////////////////////////////////////////////////////////
type XPath struct {
Ptr C.xmlXPathCompExprPtr
}
type XPathContext struct {
Ptr C.xmlXPathContextPtr
}
type XPathObject struct {
Ptr C.xmlXPathObjectPtr
}
type NodeSet struct {
Ptr C.xmlNodeSetPtr
}
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS/ENUM
////////////////////////////////////////////////////////////////////////////////
type XpathObjectType int
const (
XPATH_UNDEFINED XpathObjectType = 0
XPATH_NODESET = 1
XPATH_BOOLEAN = 2
XPATH_NUMBER = 3
XPATH_STRING = 4
XPATH_POINT = 5
XPATH_RANGE = 6
XPATH_LOCATIONSET = 7
XPATH_USERS = 8
XPATH_XSLT_TREE = 9 //: An XSLT value tree, non modifiable
)
////////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
func makeXpath(ptr C.xmlXPathCompExprPtr) *XPath {
if ptr == nil {
return nil
}
return &XPath{ptr}
}
func makeXpathObj(ptr C.xmlXPathObjectPtr) *XPathObject {
if ptr == nil {
return nil
}
return &XPathObject{ptr}
}
func makeNodeSet(ptr C.xmlNodeSetPtr) *NodeSet {
if ptr == nil {
return nil
}
return &NodeSet{ptr}
}
////////////////////////////////////////////////////////////////////////////////
// INTERFACE
////////////////////////////////////////////////////////////////////////////////
// xmlXPathCastToNumber
func (obj *XPathObject) CastToNumber() float32 {
cdbl := C.xmlXPathCastToNumber(obj.Ptr)
return float32(cdbl)
}
// xmlXPathCastToString
func (obj *XPathObject) String() string {
cstr := C.xmlXPathCastToString(obj.Ptr)
defer C.free(unsafe.Pointer(cstr))
return C.GoString(C.to_charptr(cstr))
}
// xmlXPathCompile
func CompileXPath(str string) *XPath {
ptr := C.CString(str)
defer C.free_string(ptr)
return makeXpath(C.xmlXPathCompile(C.to_xmlcharptr(ptr)))
}
// xmlXPathConvertBoolean
func (obj *XPathObject) ConvertBoolean() *XPathObject {
return makeXpathObj(C.xmlXPathConvertBoolean(obj.Ptr))
}
// xmlXPathConvertNumber
func (obj *XPathObject) ConvertNumber() *XPathObject {
return makeXpathObj(C.xmlXPathConvertNumber(obj.Ptr))
}
// xmlXPathConvertString
func (obj *XPathObject) ConvertString() *XPathObject {
return makeXpathObj(C.xmlXPathConvertString(obj.Ptr))
}
// xmlXPathCtxtCompile
func (ctx *XPathContext) Compile(str string) *XPath {
ptr := C.CString(str)
defer C.free_string(ptr)
return makeXpath(C.xmlXPathCtxtCompile(ctx.Ptr, C.to_xmlcharptr(ptr)))
}
// xmlXPathEval
func (ctx *XPathContext) Eval(str string) *XPathObject {
ptr := C.CString(str)
defer C.free_string(ptr)
return makeXpathObj(C.xmlXPathEval(C.to_xmlcharptr(ptr), ctx.Ptr))
}
// xmlXPathEvalPredicate
func (ctx *XPathContext) EvalPredicate(res *XPathObject) bool {
result := C.xmlXPathEvalPredicate(ctx.Ptr, res.Ptr)
return int(result) != 0
}
// xmlXPathEvalExpression
func (ctx *XPathContext) EvalExpression(str string) *XPathObject {
ptr := C.CString(str)
defer C.free_string(ptr)
return makeXpathObj(C.xmlXPathEvalExpression(C.to_xmlcharptr(ptr), ctx.Ptr))
}
// xmlXPathFreeCompExpr
func (xpath *XPath) Free() {
C.xmlXPathFreeCompExpr(xpath.Ptr)
xpath.Ptr = nil
}
// xmlXPathFreeContext
func (ctx *XPathContext) Free() {
C.xmlXPathFreeContext(ctx.Ptr)
ctx.Ptr = nil
}
// xmlXPathFreeNodeSet
func (nodeset *NodeSet) Free() {
C.xmlXPathFreeNodeSet(nodeset.Ptr)
nodeset.Ptr = nil
}
// xmlXPathFreeNodeSetList
func (obj *XPathObject) FreeList() {
C.xmlXPathFreeNodeSetList(obj.Ptr)
obj.Ptr = nil
}
// xmlXPathFreeObject
func (obj *XPathObject) Free() {
C.xmlXPathFreeObject(obj.Ptr)
obj.Ptr = nil
}
// xmlXPathIsInf
func xmlXPathIsInf(val float32) int {
return int(C.xmlXPathIsInf(C.double(val)))
}
// xmlXPathIsNaN
func XpathIsNaN(val float32) int {
return int(C.xmlXPathIsNaN(C.double(val)))
}
// xmlXPathNewContext
func NewXPathContext(doc *Document) *XPathContext {
return &XPathContext{C.xmlXPathNewContext(doc.Ptr)}
}
// xmlXPathNodeSetCreate
func NodeSetCreate(node *Node) *NodeSet {
return makeNodeSet(C.xmlXPathNodeSetCreate(node.Ptr))
}
// xmlXPathObjectCopy
func (obj *XPathObject) Copy() *XPathObject {
return makeXpathObj(C.xmlXPathObjectCopy(obj.Ptr))
}
// xmlXPathOrderDocElems
func (doc *Document) OrderDocElems() int {
return int(C.xmlXPathOrderDocElems(doc.Ptr))
}