-
Notifications
You must be signed in to change notification settings - Fork 0
/
alto.cc
89 lines (75 loc) · 2.17 KB
/
alto.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
#include "alto.h"
#include <string>
#include <xqilla/exceptions/XQException.hpp>
#include <xqilla/xqilla-simple.hpp>
extern "C"
{
XQilla xqilla;
struct c_xqilla_result_t
{
std::string text;
int error;
};
void xq_free(c_xqilla_result xq)
{
delete xq;
}
int xq_error(c_xqilla_result xq)
{
return xq->error;
}
char const *xq_text(c_xqilla_result xq)
{
return xq->text.c_str();
}
c_xqilla_result xq_call(char const *xmlfile, char const *query, Language language, char const *suffix, int nvars,
char const **vars)
{
c_xqilla_result xqr = new c_xqilla_result_t;
xqr->text = "";
xqr->error = 0;
XQilla::Language lang;
switch (language)
{
case langXPATH:
lang = XQilla::XPATH2;
break;
case langXQUERY:
lang = XQilla::XQUERY;
break;
case langXSLT:
lang = XQilla::XSLT2;
break;
}
try
{
AutoDelete<DynamicContext> context(xqilla.createContext(lang));
AutoDelete<XQQuery> qq(xqilla.parse(X(query), context, 0, XQilla::NO_ADOPT_CONTEXT));
for (int i = 0; i < nvars; i++)
{
Item::Ptr val = context->getItemFactory()->createUntypedAtomic(X(vars[2 * i + 1]), context);
context->setExternalVariable(X(vars[2 * i]), val);
}
Sequence seq = context->resolveDocument(X(xmlfile));
if (!seq.isEmpty() && seq.first()->isNode())
{
context->setContextItem(seq.first());
context->setContextPosition(1);
context->setContextSize(1);
}
Result result = qq->execute(context);
Item::Ptr item;
while ((item = result->next(context)) != NULL)
{
xqr->text += UTF8(item->asString(context));
xqr->text += suffix;
}
}
catch (XQException &xe)
{
xe.printDebug(X("xq_call"));
xqr->error = 1;
}
return xqr;
}
}