forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_xpath_context.cc
More file actions
72 lines (56 loc) · 1.65 KB
/
xml_xpath_context.cc
File metadata and controls
72 lines (56 loc) · 1.65 KB
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
// Copyright 2009, Squish Tech, LLC.
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include "xml_xpath_context.h"
#include "xml_element.h"
namespace libxmljs {
XmlXpathContext::XmlXpathContext(xmlNode* node) {
ctxt = xmlXPathNewContext(node->doc);
ctxt->node = node;
}
XmlXpathContext::~XmlXpathContext() {
xmlXPathFreeContext(ctxt);
}
void
XmlXpathContext::register_ns(const xmlChar* prefix,
const xmlChar* uri) {
xmlXPathRegisterNs(ctxt, prefix, uri);
}
v8::Local<v8::Value>
XmlXpathContext::evaluate(const xmlChar* xpath) {
Nan::EscapableHandleScope scope;
xmlXPathObject* xpathobj = xmlXPathEval(xpath, ctxt);
v8::Local<v8::Value> res;
if (xpathobj) {
switch (xpathobj->type) {
case XPATH_NODESET: {
if (xmlXPathNodeSetIsEmpty(xpathobj->nodesetval)) {
res = Nan::New<v8::Array>(0);
break;
}
v8::Local<v8::Array> nodes = Nan::New<v8::Array>(xpathobj->nodesetval->nodeNr);
for (int i = 0; i != xpathobj->nodesetval->nodeNr; ++i) {
Nan::Set(nodes, i, XmlNode::New(xpathobj->nodesetval->nodeTab[i]));
}
res = nodes;
break;
}
case XPATH_BOOLEAN:
res = Nan::New<v8::Boolean>(xpathobj->boolval);
break;
case XPATH_NUMBER:
res = Nan::New<v8::Number>(xpathobj->floatval);
break;
case XPATH_STRING:
res = Nan::New<v8::String>((const char *)xpathobj->stringval,
xmlStrlen(xpathobj->stringval)).ToLocalChecked();
break;
default:
res = Nan::Null();
break;
}
}
xmlXPathFreeObject(xpathobj);
return scope.Escape(res);
}
} // namespace libxmljs