-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpath.c
435 lines (378 loc) · 10.3 KB
/
xpath.c
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
#include <stdio.h>
#include <string.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include "napi.h"
#include "xpath.h"
napi_status
xpath_init(napi_env env, napi_value exports)
{
napi_status status;
napi_value fn;
NAPI_EXPORT_FN(status, env, xmlXPathNewContext, fn, exports);
NAPI_EXPORT_FN(status, env, xmlXPathEvalExpression, fn, exports);
NAPI_EXPORT_FN(status, env, xmlXPathContext_set_node, fn, exports);
NAPI_EXPORT_FN(status, env, xmlXPathObject_nodesetval, fn, exports);
NAPI_EXPORT_FN(status, env, xmlXPathRegisterNs, fn, exports);
NAPI_EXPORT_FN(status, env, xmlNodeSet_nodeNr, fn, exports);
NAPI_EXPORT_FN(status, env, xmlNodeSet_nodeMax, fn, exports);
NAPI_EXPORT_FN(status, env, xmlNodeSet_pos_nodeTab, fn, exports);
return napi_ok;
}
napi_value
_xmlXPathNewContext(napi_env env, napi_callback_info info)
{
napi_status status;
napi_valuetype vt;
size_t argc = 1;
napi_value argv[1], export;
xmlDocPtr doc;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok)
return NULL;
if (argc != 1) {
napi_throw_error(env, NULL, "You must provide all arguments: "
"'doc'");
return NULL;
}
// Check types
status = napi_typeof(env, argv[0], &vt);
if (vt != napi_external) {
napi_throw_error(env, NULL, "'doc' must to be an external");
return NULL;
}
// Get xmlDoc
status = napi_get_value_external(env, argv[0], (void **)&doc);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlDoc reference");
return NULL;
}
// Creates the context
xmlXPathContextPtr ctxp = xmlXPathNewContext(doc);
if (ctxp == NULL) {
napi_throw_error(env, NULL, "Error creating context");
return NULL;
}
// creating ctxp external
status = napi_create_external(env, ctxp, _xmlXPathNewContextFree, NULL,
&export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create external reference");
xmlXPathFreeContext(ctxp);
return NULL;
}
return export;
}
void
_xmlXPathNewContextFree(napi_env env, void *finalize_data, void *finalize_hint)
{
//
xmlXPathFreeContext(finalize_data);
}
napi_value
_xmlXPathContext_set_node(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 2;
napi_value argv[2];
napi_valuetype vt;
xmlXPathContextPtr ctxt;
xmlNodePtr node;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlXPathContext
status = napi_get_value_external(env, argv[0], (void **)&ctxt);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlXPathContext at arg "
"0");
return NULL;
}
// Get xmlNode
status = napi_typeof(env, argv[1], &vt);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read value type at arg 1");
return NULL;
}
if (vt == napi_undefined || vt == napi_null) {
node = NULL;
} else {
status = napi_get_value_external(env, argv[1], (void **)&node);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlNode at arg "
"1");
return NULL;
}
}
// Sets the node to context
ctxt->node = node;
return NULL;
}
napi_value
_xmlXPathEvalExpression(napi_env env, napi_callback_info info)
{
napi_status status;
napi_valuetype vt;
size_t argc = 2, str_l;
napi_value argv[2], export;
xmlChar *str;
xmlXPathContextPtr ctxt;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok)
return NULL;
if (argc != 2) {
napi_throw_error(env, NULL, "You must provide all arguments: "
"'str', 'ctxt'");
return NULL;
}
// Check each argument
// str
status = napi_typeof(env, argv[0], &vt);
if (vt != napi_string) {
napi_throw_error(env, NULL, "'str' must to be a string");
return NULL;
}
status = napi_get_value_string_latin1(env, argv[0], NULL, 0, &str_l);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't get 'str' length");
return NULL;
}
str_l++;
str = malloc(str_l);
if (str == NULL) {
napi_throw_error(env, NULL, "Can't allocate 'str' mem");
return NULL;
}
status = napi_get_value_string_latin1(env, argv[0], (char *)str, str_l,
NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read 'str' argument");
free(str);
return NULL;
}
// ctxt
status = napi_typeof(env, argv[1], &vt);
if (vt != napi_external) {
napi_throw_error(env, NULL, "'ctxt' must to be an external");
free(str);
return NULL;
}
status = napi_get_value_external(env, argv[1], (void **)&ctxt);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't retrieve xmlXPathContext");
free(str);
return NULL;
}
// Creates the xmlXPathObject from expression
xmlXPathObjectPtr xpathobjp = xmlXPathEvalExpression(str, ctxt);
free(str);
if (!xpathobjp || xpathobjp->type != XPATH_NODESET) {
napi_throw_error(env, NULL, "XPath query did not return a "
"nodeset");
if (xpathobjp)
xmlXPathFreeObject(xpathobjp);
return NULL;
}
// creating xpathObj external
status = napi_create_external(env, xpathobjp, _xmlXPathObjectFree,
NULL, &export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create external reference");
xmlXPathFreeObject(xpathobjp);
return NULL;
}
return export;
}
void
_xmlXPathObjectFree(napi_env env, void *finalize_data, void *finalize_hint)
{
//
xmlXPathFreeObject(finalize_data);
}
napi_value
_xmlXPathObject_nodesetval(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
xmlXPathObjectPtr xpathobjp;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get XPathObject
status = napi_get_value_external(env, argv[0], (void **)&xpathobjp);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlXPathObject "
"reference");
return NULL;
}
// create the export
status = napi_create_external(env, xpathobjp->nodesetval, NULL, NULL,
&export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create export");
return NULL;
}
return export;
}
napi_value
_xmlXPathRegisterNs(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 3, len;
napi_value argv[3];
xmlXPathContextPtr ctxt;
xmlChar *prefix, *href;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get XPathObject
status = napi_get_value_external(env, argv[0], (void **)&ctxt);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlXPathContext "
"reference");
return NULL;
}
// Get prefix string
status = napi_get_value_string_latin1(env, argv[1], NULL, 0, &len);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Cant't get 'prefix' length");
return NULL;
}
prefix = malloc(++len);
status = napi_get_value_string_latin1(env, argv[1], (char *)prefix, len,
NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't get 'prefix' value");
free(prefix);
return NULL;
}
// Get href string
status = napi_get_value_string_latin1(env, argv[1], NULL, 0, &len);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Cant't get 'href' length");
return NULL;
}
href = malloc((int)++len);
status = napi_get_value_string_latin1(env, argv[1], (char *)href, len,
NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't get 'href' value");
free(prefix);
free(href);
return NULL;
}
// try to register namespace
if (xmlXPathRegisterNs(ctxt, prefix, href) != 0) {
size_t len = 200 + strlen((char *)prefix) + strlen(
(char *)href);
char *msg = malloc(len);
snprintf(msg, len, "Register NS error: prefix '%s', href '%s'",
prefix, href);
napi_throw_error(env, NULL, msg);
free(msg);
}
free(prefix);
free(href);
return NULL;
}
napi_value
_xmlNodeSet_nodeNr(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
xmlNodeSetPtr nodesetp;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlNodeSet
status = napi_get_value_external(env, argv[0], (void **)&nodesetp);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlNodeSet reference");
return NULL;
}
// Create export
status = napi_create_int32(env, nodesetp->nodeNr, &export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create export value");
return NULL;
}
return export;
}
napi_value
_xmlNodeSet_nodeMax(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
xmlNodeSetPtr nodesetp;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlNodeSet
status = napi_get_value_external(env, argv[0], (void **)&nodesetp);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlNodeSet reference");
return NULL;
}
// Create export
status = napi_create_int32(env, nodesetp->nodeMax, &export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create export value");
return NULL;
}
return export;
}
napi_value
_xmlNodeSet_pos_nodeTab(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 2;
napi_value argv[2], export;
xmlNodeSetPtr nodesetp;
int32_t pos;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlNodeSet
status = napi_get_value_external(env, argv[0], (void **)&nodesetp);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlNodeSet reference");
return NULL;
}
// Get xmlNodeSet
status = napi_get_value_int32(env, argv[1], &pos);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read pos");
return NULL;
}
// Create export
status = napi_create_external(env, nodesetp->nodeTab[pos], NULL, NULL,
&export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create export value");
return NULL;
}
return export;
}