Class: Nokolexbor::XPathContext

Inherits:
Object
  • Object
show all
Defined in:
lib/nokolexbor/xpath_context.rb,
ext/nokolexbor/nl_xpath_context.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(node) ⇒ Object

Create a new XPathContext with node as the reference point.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'ext/nokolexbor/nl_xpath_context.c', line 318

static VALUE
nl_xpath_context_new(VALUE klass, VALUE rb_node)
{
  xmlXPathContextPtr ctx;
  VALUE self;

  lxb_dom_node_t *node = nl_rb_node_unwrap(rb_node);

  ctx = nl_xmlXPathNewContext(node->owner_document);
  ctx->node = node;

  nl_xmlXPathRegisterNs(ctx, NOKOGIRI_PREFIX, NOKOGIRI_URI);
  nl_xmlXPathRegisterNs(ctx, NOKOGIRI_BUILTIN_PREFIX, NOKOGIRI_BUILTIN_URI);
  nl_xmlXPathRegisterFuncNS(ctx, (const xmlChar *)"css-class", NOKOGIRI_BUILTIN_URI,
                            xpath_builtin_css_class);
  nl_xmlXPathRegisterFuncNS(ctx, (const xmlChar *)"local-name-is", NOKOGIRI_BUILTIN_URI,
                            xpath_builtin_local_name_is);

  self = Data_Wrap_Struct(klass, 0, free_xml_xpath_context, ctx);
  rb_iv_set(self, "@document", nl_rb_document_get(rb_node));

  return self;
}

Instance Method Details

#evaluate(search_path, handler = nil) ⇒ Object

Evaluate the search_path returning an XML::XPath object.



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
# File 'ext/nokolexbor/nl_xpath_context.c', line 265

static VALUE
nl_xpath_context_evaluate(int argc, VALUE *argv, VALUE self)
{
  VALUE search_path, xpath_handler;
  VALUE retval = Qnil;
  xmlXPathContextPtr ctx;
  xmlXPathObjectPtr xpath;
  xmlChar *query;
  VALUE errors = rb_ary_new();

  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;
  //   nl_xmlXPathRegisterFuncLookup(ctx, handler_lookup, (void *)xpath_handler);
  // }

  nl_xmlSetStructuredErrorFunc((void *)errors, nl_xpath_error_array_pusher);
  nl_xmlSetGenericErrorFunc((void *)errors, nl_xpath_generic_exception_pusher);

  xpath = nl_xmlXPathEvalExpression(query, ctx);

  nl_xmlSetStructuredErrorFunc(NULL, NULL);
  nl_xmlSetGenericErrorFunc(NULL, NULL);

  if (xpath == NULL) {
    nl_xmlXPathFreeObject(xpath);
    rb_exc_raise(rb_ary_entry(errors, 0));
  }

  retval = xpath2ruby(xpath, ctx, nl_rb_document_get(self));
  if (retval == Qundef) {
    retval = rb_funcall(cNokolexborNodeSet, rb_intern("new"), 1, rb_ary_new());
  }

  nl_xmlXPathFreeObject(xpath);

  return retval;
}

#register_namespaces(namespaces) ⇒ Object

Register namespaces in namespaces



7
8
9
10
11
12
# File 'lib/nokolexbor/xpath_context.rb', line 7

def register_namespaces(namespaces)
  namespaces.each do |k, v|
    k = k.to_s.gsub(/.*:/, "") # strip off 'xmlns:' or 'xml:'
    register_ns(k, v)
  end
end

#register_ns(prefix, uri) ⇒ Object

Register the namespace with prefix and uri.



128
129
130
131
132
133
134
135
136
137
138
# File 'ext/nokolexbor/nl_xpath_context.c', line 128

static VALUE
nl_xpath_context_register_ns(VALUE self, VALUE prefix, VALUE uri)
{
  xmlXPathContextPtr ctx;
  Data_Get_Struct(self, xmlXPathContext, ctx);

  nl_xmlXPathRegisterNs(ctx,
                        (const xmlChar *)StringValueCStr(prefix),
                        (const xmlChar *)StringValueCStr(uri));
  return self;
}

#register_variable(name, value) ⇒ Object

Register the variable name with value.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/nokolexbor/nl_xpath_context.c', line 146

static VALUE
nl_xpath_context_register_variable(VALUE self, VALUE name, VALUE value)
{
  xmlXPathContextPtr ctx;
  xmlXPathObjectPtr xmlValue;
  Data_Get_Struct(self, xmlXPathContext, ctx);

  xmlValue = nl_xmlXPathNewCString(StringValueCStr(value));

  nl_xmlXPathRegisterVariable(ctx,
                              (const xmlChar *)StringValueCStr(name),
                              xmlValue);

  return self;
}