forked from smartdevicelink/sdl_atf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_xml.cc
437 lines (405 loc) · 12 KB
/
lua_xml.cc
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
#line 5 "main.nw"
#include <stdarg.h>
#include <string.h>
extern "C" {
#include <lua5.2/lua.h>
#include <lua5.2/lualib.h>
#include <lua5.2/lauxlib.h>
}
#line 6 "xml.nw"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xmlsave.h>
#include <iostream>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
namespace {
void genericErrorHandler(void *ctx, const char* message, ...){
lua_State *L = static_cast<lua_State*>(ctx);
va_list args;
va_start(args, message);
char msg[1024] = { 0 };
snprintf(msg, 1023, message, args);
va_end(args);
lua_pushstring(L, msg);
lua_error(L);
}
void structuredErrorHandler(void *userData, xmlErrorPtr err) {
lua_State *L = static_cast<lua_State*>(userData);
const size_t msg_size = 1024;
char msg[msg_size];
int pos = 0;
switch(err->domain) {
case XML_FROM_XPATH:
pos += snprintf(msg, msg_size, "Error evaluating xpath query:\n");
break;
case XML_FROM_PARSER:
pos += snprintf(msg, msg_size, "Error parsing xml file:\n");
break;
// TODO(EZamakhov): add more domain errors from xmlErrorDomain enum
default:
pos += snprintf(msg, msg_size, "Other error:\n");
break;
}
// append error information to out msg
if (err->file &&
pos < msg_size) {
pos += snprintf(msg + pos, msg_size - pos,
"%s:%d: ", err->file, err->line);
}
if (err->message &&
pos < msg_size) {
pos += snprintf(msg + pos, msg_size - pos,
"%s", err->message);
}
if (err->str1 &&
pos < msg_size) {
pos += snprintf(msg + pos, msg_size - pos,
": %s", err->str1);
}
if (err->str2 &&
pos < msg_size) {
pos += snprintf(msg + pos, msg_size - pos,
": %s", err->str2);
}
if (err->str3 &&
pos < msg_size) {
pos += snprintf(msg + pos, msg_size - pos,
": %s", err->str3);
}
lua_pushstring(L, msg);
lua_error(L);
}
int xml_open(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
xmlDoc *doc = xmlReadFile(filename, nullptr, 0);
if (!doc) {
lua_pushnil(L);
lua_pushstring(L, "Invalid xml file");
return 2;
}
xmlDoc **p = static_cast<xmlDoc**>(lua_newuserdata(L, sizeof(xmlDoc*)));
*p = doc;
luaL_getmetatable(L, "xml.Document");
lua_setmetatable(L, -2);
return 1;
}
int xml_new(lua_State *L) {
auto doc = xmlNewDoc(BAD_CAST "1.0");
xmlDoc **p = static_cast<xmlDoc**>(lua_newuserdata(L, sizeof(xmlDoc*)));
*p = doc;
luaL_getmetatable(L, "xml.Document");
lua_setmetatable(L, -2);
return 1;
}
#line 99 "xml.nw"
int eval_xpath(lua_State *L, xmlDocPtr doc, xmlNodePtr node, const xmlChar* query) {
xmlXPathContextPtr context = xmlXPathNewContext(doc);
if (!context) {
lua_pushnil(L);
lua_pushstring(L, "Error creating xpath context");
return 2;
}
if (node) {
xmlXPathSetContextNode(node, context);
}
xmlXPathObjectPtr result = xmlXPathEvalExpression(query, context);
xmlXPathFreeContext(context);
if (!result) {
lua_pushnil(L);
lua_pushstring(L, "Error evaluating xpath query: \"");
lua_pushstring(L, reinterpret_cast<const char*>(query));
lua_pushstring(L, "\"");
lua_concat(L, 2);
lua_concat(L, 2);
return 2;
}
#line 188 "xml.nw"
switch(result->type) {
case XPATH_NODESET:
// Haven't the slightest idea why, but libxml returns result
// of type XPATH_NODESET but nodesetval == 0 on some queries
if (!result->nodesetval) {
lua_pushnil(L);
break;
}
lua_createtable(L, result->nodesetval->nodeNr, 0);
luaL_getmetatable(L, "xml.Node");
for (int i = 0; i < result->nodesetval->nodeNr; ++i) {
auto n = result->nodesetval->nodeTab[i];
if (n->type == XML_ELEMENT_NODE) {
xmlNodePtr* p = static_cast<xmlNodePtr*>(lua_newuserdata(L, sizeof(xmlNodePtr)));
*p = n;
lua_pushnil(L);
lua_copy(L, -3, -1);
lua_setmetatable(L, -2);
} else if (n->type == XML_TEXT_NODE) {
lua_pushstring(L, reinterpret_cast<const char*>(n->content));
} else if (n->type == XML_ATTRIBUTE_NODE) {
assert(n->children);
assert(n->children->type == XML_TEXT_NODE);
lua_pushstring(L, reinterpret_cast<const char*>(n->children->content));
} else {
continue;
}
lua_rawseti(L, -3, i + 1);
}
lua_pop(L, 1);
break;
case XPATH_BOOLEAN:
lua_pushboolean(L, result->boolval);
break;
case XPATH_NUMBER:
lua_pushnumber(L, result->floatval);
break;
case XPATH_STRING:
lua_pushstring(L, reinterpret_cast<const char*>(result->stringval));
break;
default:
lua_pushnil(L);
}
#line 122 "xml.nw"
xmlXPathFreeObject(result);
return 1;
}
int doc_xpath(lua_State *L) {
xmlDoc *doc = *static_cast<xmlDoc**>(luaL_checkudata(L, 1, "xml.Document"));
const xmlChar* query = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 2));
return eval_xpath(L, doc, nullptr, query);
}
int doc_rootNode(lua_State *L) {
xmlDoc *doc = *static_cast<xmlDoc**>(luaL_checkudata(L, 1, "xml.Document"));
if (doc->children) {
auto c = doc->children;
while (c && c->type != XML_ELEMENT_NODE) { c = c->next; };
if (c)
{
xmlNodePtr* p = static_cast<xmlNodePtr*>(lua_newuserdata(L, sizeof(xmlNodePtr)));
*p = c;
luaL_getmetatable(L, "xml.Node");
lua_setmetatable(L, -2);
return 1;
}
}
lua_pushnil(L);
return 1;
}
int doc_write(lua_State *L) {
xmlDoc *doc = *static_cast<xmlDoc**>(luaL_checkudata(L, 1, "xml.Document"));
const char* filename = luaL_checkstring(L, 2);
bool format = lua_gettop(L) == 2 || lua_toboolean(L, 3);
auto ctx = xmlSaveToFilename(filename, "utf-8", (format ? XML_SAVE_FORMAT : 0));
xmlSaveDoc(ctx, doc);
xmlSaveClose(ctx);
return 0;
}
int doc_createRootNode(lua_State *L) {
xmlDoc *doc = *static_cast<xmlDoc**>(luaL_checkudata(L, 1, "xml.Document"));
const xmlChar* name = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 2));
const xmlChar* content = nullptr;
if (lua_gettop(L) > 2) {
content = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 3));
}
auto node = xmlNewDocNode(doc, nullptr, name, content);
xmlDocSetRootElement(doc, node);
xmlNodePtr* p = static_cast<xmlNodePtr*>(lua_newuserdata(L, sizeof(xmlNodePtr)));
*p = node;
luaL_getmetatable(L, "xml.Node");
lua_setmetatable(L, -2);
return 1;
}
int node_addChild(lua_State *L) {
xmlNodePtr parent = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
auto name = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 2));
auto node = xmlNewNode(nullptr, name);
xmlAddChild(parent, node);
xmlNodePtr* p = static_cast<xmlNodePtr*>(lua_newuserdata(L, sizeof(xmlNodePtr)));
*p = node;
luaL_getmetatable(L, "xml.Node");
lua_setmetatable(L, -2);
}
int xml_close(lua_State *L) {
xmlDoc *doc = *static_cast<xmlDoc**>(luaL_checkudata(L, 1, "xml.Document"));
xmlFreeDoc(doc);
return 0;
}
int node_text(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
const xmlChar* text = nullptr;
if (lua_gettop(L) > 1) {
text = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 2));
xmlNodeSetContent(node, text);
lua_pop(L, 1);
return 1;
} else {
xmlChar* text = xmlNodeGetContent(node);
if (text) {
lua_pushstring(L, reinterpret_cast<const char*>(text));
xmlFree(text);
} else {
lua_pushstring(L, "<nil>");
}
return 1;
}
}
int node_name(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
const xmlChar* text = node->name;
if (text) {
lua_pushstring(L, reinterpret_cast<const char*>(text));
} else {
lua_pushstring(L, "<nil>");
}
return 1;
}
int node_attr(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
auto attr = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 2));
if (lua_gettop(L) < 3) {
xmlChar* text = xmlGetProp(node, attr);
if (text) {
lua_pushstring(L, reinterpret_cast<const char*>(text));
xmlFree(text);
} else {
lua_pushnil(L);
}
return 1;
} else {
if (lua_isnil(L, 3)) {
xmlUnsetProp(node, attr);
} else {
const xmlChar* text = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 3));
xmlSetProp(node, attr, text);
}
lua_pop(L, 2);
return 1;
}
}
int node_xpath(lua_State *L) {
xmlNode *node = *static_cast<xmlNode**>(luaL_checkudata(L, 1, "xml.Node"));
if (!node->doc) {
return luaL_error(L, "Invalid xml.Node object: must be included in a document");
}
const xmlChar* query = reinterpret_cast<const xmlChar*>(luaL_checkstring(L, 2));
return eval_xpath(L, node->doc, node, query);
}
int node_parent(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
if (node->parent) {
xmlNodePtr* p = static_cast<xmlNodePtr*>(lua_newuserdata(L, sizeof(xmlNodePtr)));
*p = node->parent;
luaL_getmetatable(L, "xml.Node");
lua_setmetatable(L, -2);
} else {
lua_pushnil(L);
}
return 1;
}
int node_children(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
const xmlChar* filter = nullptr;
if (lua_isstring(L, 2)) {
filter = reinterpret_cast<const xmlChar*>(lua_tostring(L, 2));
}
if (node->type == XML_ELEMENT_NODE) {
lua_newtable(L);
auto n = node->children;
int i = 0;
luaL_getmetatable(L, "xml.Node");
while (n) {
if (n->type == XML_ELEMENT_NODE) {
if (!filter || xmlStrEqual(n->name, filter)) {
xmlNodePtr* p = static_cast<xmlNodePtr*>(lua_newuserdata(L, sizeof(xmlNodePtr)));
lua_pushnil(L);
lua_copy(L, -3, -1);
lua_setmetatable(L, -2);
lua_rawseti(L, -3, ++i);
*p = n;
}
}
n = n->next;
}
lua_pop(L, 1);
} else {
lua_pushnil(L);
}
return 1;
}
int node_remove(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
xmlUnlinkNode(node);
xmlFreeNode(node);
return 0;
}
int node_attributes(lua_State *L) {
xmlNodePtr node = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
if (node->type == XML_ELEMENT_NODE) {
lua_newtable(L);
auto n = node->properties;
int i = 0;
while (n) {
if (n->type == XML_ATTRIBUTE_NODE) {
lua_pushstring(L, reinterpret_cast<const char*>(n->name));
lua_pushstring(L, reinterpret_cast<const char*>(xmlGetProp(node, n->name)));
lua_rawset(L, -3);
}
n = n->next;
}
} else {
lua_pushnil(L);
}
return 1;
}
int node_eq(lua_State *L) {
xmlNodePtr a = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 1, "xml.Node"));
xmlNodePtr b = *static_cast<xmlNodePtr*>(luaL_checkudata(L, 2, "xml.Node"));
lua_pushboolean(L, a == b);
return 1;
}
}
extern "C"
int luaopen_xml(lua_State *L, int ) {
LIBXML_TEST_VERSION
xmlSetGenericErrorFunc(L, &genericErrorHandler);
xmlSetStructuredErrorFunc(L, &structuredErrorHandler);
luaL_Reg functions[] = {
{ "open", &xml_open },
{ "new", &xml_new },
{ NULL, NULL }
};
luaL_newmetatable(L, "xml.Document");
lua_newtable(L);
luaL_Reg doc_functions[] = {
{ "xpath", &doc_xpath },
{ "rootNode", &doc_rootNode },
{ "write", &doc_write },
{ "createRootNode", &doc_createRootNode },
{ NULL, NULL }
};
luaL_setfuncs(L, doc_functions, 0);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, &xml_close);
lua_setfield(L, -2, "__gc");
luaL_newmetatable(L, "xml.Node");
lua_newtable(L);
luaL_Reg node_functions[] = {
{ "text", &node_text },
{ "name", &node_name },
{ "attr", &node_attr },
{ "xpath", &node_xpath },
{ "children", &node_children },
{ "parent", &node_parent },
{ "attributes", &node_attributes },
{ "addChild", &node_addChild },
{ "remove", &node_remove },
{ NULL, NULL }
};
luaL_setfuncs(L, node_functions, 0);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, &node_eq);
lua_setfield(L, -2, "__eq");
luaL_newlib(L, functions);
return 1;
}