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
|
#include <xml_xpath_context.h>
int vasprintf (char **strp, const char *fmt, va_list ap);
static void deallocate(xmlXPathContextPtr ctx)
{
NOKOGIRI_DEBUG_START(ctx);
xmlXPathFreeContext(ctx);
NOKOGIRI_DEBUG_END(ctx);
}
/*
* call-seq:
* register_ns(prefix, uri)
*
* Register the namespace with +prefix+ and +uri+.
*/
static VALUE register_ns(VALUE self, VALUE prefix, VALUE uri)
{
xmlXPathContextPtr ctx;
Data_Get_Struct(self, xmlXPathContext, ctx);
xmlXPathRegisterNs( ctx,
(const xmlChar *)StringValueCStr(prefix),
(const xmlChar *)StringValueCStr(uri)
);
return self;
}
/*
* call-seq:
* register_variable(name, value)
*
* Register the variable +name+ with +value+.
*/
static VALUE register_variable(VALUE self, VALUE name, VALUE value)
{
xmlXPathContextPtr ctx;
xmlXPathObjectPtr xmlValue;
Data_Get_Struct(self, xmlXPathContext, ctx);
xmlValue = xmlXPathNewCString(StringValueCStr(value));
xmlXPathRegisterVariable( ctx,
(const xmlChar *)StringValueCStr(name),
xmlValue
);
return self;
}
void Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, int nargs, VALUE handler, const char* function_name)
{
int i;
VALUE result, doc;
VALUE *argv;
VALUE node_set = Qnil;
xmlNodeSetPtr xml_node_set = NULL;
xmlXPathObjectPtr obj;
assert(ctx->context->doc);
assert(DOC_RUBY_OBJECT_TEST(ctx->context->doc));
argv = (VALUE *)calloc((size_t)nargs, sizeof(VALUE));
for (i = 0 ; i < nargs ; ++i) {
rb_gc_register_address(&argv[i]);
}
doc = DOC_RUBY_OBJECT(ctx->context->doc);
if (nargs > 0) {
i = nargs - 1;
do {
obj = valuePop(ctx);
switch(obj->type) {
case XPATH_STRING:
argv[i] = NOKOGIRI_STR_NEW2(obj->stringval);
break;
case XPATH_BOOLEAN:
argv[i] = obj->boolval == 1 ? Qtrue : Qfalse;
break;
case XPATH_NUMBER:
argv[i] = rb_float_new(obj->floatval);
break;
case XPATH_NODESET:
argv[i] = Nokogiri_wrap_xml_node_set(obj->nodesetval, doc);
break;
default:
argv[i] = NOKOGIRI_STR_NEW2(xmlXPathCastToString(obj));
}
xmlXPathFreeNodeSetList(obj);
} while(i-- > 0);
}
result = rb_funcall2(handler, rb_intern((const char*)function_name), nargs, argv);
for (i = 0 ; i < nargs ; ++i) {
rb_gc_unregister_address(&argv[i]);
}
free(argv);
switch(TYPE(result)) {
case T_FLOAT:
case T_BIGNUM:
case T_FIXNUM:
xmlXPathReturnNumber(ctx, NUM2DBL(result));
break;
case T_STRING:
xmlXPathReturnString(
ctx,
xmlCharStrdup(StringValueCStr(result))
);
break;
case T_TRUE:
xmlXPathReturnTrue(ctx);
break;
case T_FALSE:
xmlXPathReturnFalse(ctx);
break;
case T_NIL:
break;
case T_ARRAY:
{
VALUE args[2];
args[0] = doc;
args[1] = result;
node_set = rb_class_new_instance(2, args, cNokogiriXmlNodeSet);
Data_Get_Struct(node_set, xmlNodeSet, xml_node_set);
xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
}
break;
case T_DATA:
if(rb_obj_is_kind_of(result, cNokogiriXmlNodeSet)) {
Data_Get_Struct(result, xmlNodeSet, xml_node_set);
/* Copy the node set, otherwise it will get GC'd. */
xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
break;
}
default:
rb_raise(rb_eRuntimeError, "Invalid return type");
}
}
static void ruby_funcall(xmlXPathParserContextPtr ctx, int nargs)
{
VALUE handler = Qnil;
const char *function = NULL ;
assert(ctx);
assert(ctx->context);
assert(ctx->context->userData);
assert(ctx->context->function);
handler = (VALUE)(ctx->context->userData);
function = (const char*)(ctx->context->function);
Nokogiri_marshal_xpath_funcall_and_return_values(ctx, nargs, handler, function);
}
static xmlXPathFunction lookup( void *ctx,
const xmlChar * name,
const xmlChar* ns_uri )
{
VALUE xpath_handler = (VALUE)ctx;
if(rb_respond_to(xpath_handler, rb_intern((const char *)name)))
return ruby_funcall;
return NULL;
}
NORETURN(static void xpath_generic_exception_handler(void * ctx, const char *msg, ...));
static void xpath_generic_exception_handler(void * ctx, const char *msg, ...)
{
char * message;
va_list args;
va_start(args, msg);
vasprintf(&message, msg, args);
va_end(args);
rb_raise(rb_eRuntimeError, "%s", message);
}
/*
* call-seq:
* evaluate(search_path, handler = nil)
*
* Evaluate the +search_path+ returning an XML::XPath object.
*/
static VALUE evaluate(int argc, VALUE *argv, VALUE self)
{
VALUE search_path, xpath_handler;
VALUE thing = Qnil;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr xpath;
xmlChar *query;
Data_Get_Struct(self, xmlXPathContext, ctx);
if(rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1)
xpath_handler = Qnil;
query = (xmlChar *)StringValueCStr(search_path);
if(Qnil != xpath_handler) {
/* FIXME: not sure if this is the correct place to shove private data. */
ctx->userData = (void *)xpath_handler;
xmlXPathRegisterFuncLookup(ctx, lookup, (void *)xpath_handler);
}
xmlResetLastError();
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);
/* For some reason, xmlXPathEvalExpression will blow up with a generic error */
/* when there is a non existent function. */
xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler);
xpath = xmlXPathEvalExpression(query, ctx);
xmlSetStructuredErrorFunc(NULL, NULL);
xmlSetGenericErrorFunc(NULL, NULL);
if(xpath == NULL) {
xmlErrorPtr error = xmlGetLastError();
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
}
assert(ctx->doc);
assert(DOC_RUBY_OBJECT_TEST(ctx->doc));
switch(xpath->type) {
case XPATH_STRING:
thing = NOKOGIRI_STR_NEW2(xpath->stringval);
xmlFree(xpath->stringval);
break;
case XPATH_NODESET:
thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval,
DOC_RUBY_OBJECT(ctx->doc));
break;
case XPATH_NUMBER:
thing = rb_float_new(xpath->floatval);
break;
case XPATH_BOOLEAN:
thing = xpath->boolval == 1 ? Qtrue : Qfalse;
break;
default:
thing = Nokogiri_wrap_xml_node_set(NULL, DOC_RUBY_OBJECT(ctx->doc));
}
xmlXPathFreeNodeSetList(xpath);
return thing;
}
/*
* call-seq:
* new(node)
*
* Create a new XPathContext with +node+ as the reference point.
*/
static VALUE new(VALUE klass, VALUE nodeobj)
{
xmlNodePtr node;
xmlXPathContextPtr ctx;
VALUE self;
xmlXPathInit();
Data_Get_Struct(nodeobj, xmlNode, node);
ctx = xmlXPathNewContext(node->doc);
ctx->node = node;
self = Data_Wrap_Struct(klass, 0, deallocate, ctx);
/*rb_iv_set(self, "@xpath_handler", Qnil); */
return self;
}
VALUE cNokogiriXmlXpathContext;
void init_xml_xpath_context(void)
{
VALUE module = rb_define_module("Nokogiri");
/*
* Nokogiri::XML
*/
VALUE xml = rb_define_module_under(module, "XML");
/*
* XPathContext is the entry point for searching a Document by using XPath.
*/
VALUE klass = rb_define_class_under(xml, "XPathContext", rb_cObject);
cNokogiriXmlXpathContext = klass;
rb_define_singleton_method(klass, "new", new, 1);
rb_define_method(klass, "evaluate", evaluate, -1);
rb_define_method(klass, "register_variable", register_variable, 2);
rb_define_method(klass, "register_ns", register_ns, 2);
}
|