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
|
/* $Id: ruby_xml_xpath.c,v 1.2 2006/04/14 14:45:25 roscopeco Exp $ */
/* Please see the LICENSE file for copyright and distribution information */
#include "libxml.h"
#include "ruby_xml_xpath.h"
/*
* Document-class: XML::XPath
*
* Includes Enumerable.
*/
VALUE cXMLXPath;
VALUE eXMLXPathInvalidPath;
#ifdef LIBXML_XPATH_ENABLED
/*
* call-seq:
* xpath.debug => (true|false)
*
* Dump libxml debugging information to stdout.
* Requires Libxml be compiled with debugging enabled.
*/
VALUE
ruby_xml_xpath_debug(VALUE self) {
#ifdef LIBXML_DEBUG_ENABLED
ruby_xml_xpath *rxxp;
Data_Get_Struct(self, ruby_xml_xpath, rxxp);
if (rxxp->xpop != NULL) {
xmlXPathDebugDumpObject(stdout, rxxp->xpop, 0);
return(Qtrue);
} else {
return(Qfalse);
}
#else
rb_warn("libxml does not have debugging turned on");
return(Qfalse);
#endif
}
// TODO Maybe we should support [] or some other kind of access if poss.
/*
* call-seq:
* xpath.each { |node| ... } => self
*
* Call the supplied block for each matching node.
*/
VALUE
ruby_xml_xpath_each(VALUE self) {
ruby_xml_xpath *rxxp;
VALUE rxnset;
Data_Get_Struct(self, ruby_xml_xpath, rxxp);
if (rxxp->xpop == NULL || rxxp->xpop->type != XPATH_NODESET)
return(Qnil);
rxnset = ruby_xml_node_set_new(cXMLNodeSet, rxxp->xd, self,
rxxp->xpop->nodesetval);
ruby_xml_node_set_each(rxnset);
return(rxnset);
}
///////////////////////////////////////////////////
// TODO xpath_find is throwing TypeError:
//
// TypeError: can't convert nil into String
//
// When given a namespace when non exist.
/*
* call-seq:
* XML::XPath.find(path, namespaces = [any]) => xpath
*
* Find nodes matching the specified xpath (and optionally any of the
* supplied namespaces) and return as an XML::Node::Set.
*
* The optional namespaces argument may take one of
* two forms:
*
* * A string in the form of: "prefix:uri", or
* * An array of:
* * strings in the form like above
* * arrays in the form of ['prefix','uri']
*
* If not specified, matching nodes from any namespace
* will be included.
*/
VALUE
ruby_xml_xpath_find(int argc, VALUE *argv, VALUE class) {
#ifdef LIBXML_XPATH_ENABLED
xmlXPathCompExprPtr comp;
ruby_xml_node *node;
ruby_xml_xpath *rxxp;
ruby_xml_xpath_context *rxxpc;
ruby_xml_ns *rxns;
VALUE rnode, rprefix, ruri, xxpc, xpath, xpath_expr;
char *cp;
long i;
switch(argc) {
case 3:
/* array of namespaces we allow.
*
* Accept either:
* A string in the form of: "prefix:uri", or
* An array of:
* *) strings in the form like above
* *) arrays in the form of ['prefix','uri']
*/
/* Intentionally fall through, we deal with the last arg below
* after the XPathContext object has been setup */
case 2:
rnode = argv[0];
xpath_expr = argv[1];
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
}
Data_Get_Struct(rnode, ruby_xml_node, node);
xxpc = ruby_xml_xpath_context_new4(rnode);
if (NIL_P(xxpc))
return(Qnil);
Data_Get_Struct(xxpc, ruby_xml_xpath_context, rxxpc);
xpath = ruby_xml_xpath_new(cXMLXPath, rnode, xxpc, NULL);
Data_Get_Struct(xpath, ruby_xml_xpath, rxxp);
rxxpc->ctxt->node = node->node;
if (node->node->type == XML_DOCUMENT_NODE) {
rxxpc->ctxt->namespaces = xmlGetNsList(node->node->doc,
xmlDocGetRootElement(node->node->doc));
} else {
rxxpc->ctxt->namespaces = xmlGetNsList(node->node->doc, node->node);
}
rxxpc->ctxt->nsNr = 0;
if (rxxpc->ctxt->namespaces != NULL) {
while (rxxpc->ctxt->namespaces[rxxpc->ctxt->nsNr] != NULL)
rxxpc->ctxt->nsNr++;
}
/* Need to loop through the 2nd argument and iterate through the
* list of namespaces that we want to allow */
if (argc == 3) {
switch (TYPE(argv[2])) {
case T_STRING:
cp = strchr(StringValuePtr(argv[2]), (int)':');
if (cp == NULL) {
rprefix = argv[2];
ruri = Qnil;
} else {
rprefix = rb_str_new(StringValuePtr(argv[2]), (int)((long)cp - (long)StringValuePtr(argv[2])));
ruri = rb_str_new2(&cp[1]);
}
/* Should test the results of this */
ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
break;
case T_ARRAY:
for (i = 0; i < RARRAY_LEN(argv[2]); i++) {
switch (TYPE(RARRAY_PTR(argv[2])[i])) {
case T_STRING:
cp = strchr(StringValuePtr(RARRAY_PTR(argv[2])[i]), (int)':');
if (cp == NULL) {
rprefix = RARRAY_PTR(argv[2])[i];
ruri = Qnil;
} else {
rprefix = rb_str_new(StringValuePtr(RARRAY_PTR(argv[2])[i]), (int)((long)cp - (long)StringValuePtr(RARRAY_PTR(argv[2])[i])));
ruri = rb_str_new2(&cp[1]);
}
/* Should test the results of this */
ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
break;
case T_ARRAY:
if (RARRAY_LEN(RARRAY_PTR(argv[2])[i]) == 2) {
rprefix = RARRAY_PTR(RARRAY_PTR(argv[2])[i])[0];
ruri = RARRAY_PTR(RARRAY_PTR(argv[2])[i])[1];
ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
} else {
rb_raise(rb_eArgError, "nested array must be an array of strings, prefix and href/uri");
}
break;
default:
if (rb_obj_is_kind_of(RARRAY_PTR(argv[2])[i], cXMLNS) == Qtrue) {
Data_Get_Struct(argv[2], ruby_xml_ns, rxns);
rprefix = rb_str_new2((const char*)rxns->ns->prefix);
ruri = rb_str_new2((const char*)rxns->ns->href);
ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
} else
rb_raise(rb_eArgError, "Invalid argument type, only accept string, array of strings, or an array of arrays");
}
}
break;
default:
if (rb_obj_is_kind_of(argv[2], cXMLNS) == Qtrue) {
Data_Get_Struct(argv[2], ruby_xml_ns, rxns);
rprefix = rb_str_new2((const char*)rxns->ns->prefix);
ruri = rb_str_new2((const char*)rxns->ns->href);
ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
} else
rb_raise(rb_eArgError, "Invalid argument type, only accept string, array of strings, or an array of arrays");
}
}
comp = xmlXPathCompile((xmlChar*)StringValuePtr(xpath_expr));
if (comp == NULL) {
xmlXPathFreeCompExpr(comp);
rb_raise(eXMLXPathInvalidPath, "Invalid XPath expression");
}
rxxp->xpop = xmlXPathCompiledEval(comp, rxxpc->ctxt);
xmlXPathFreeCompExpr(comp);
if (rxxpc->ctxt->namespaces != NULL)
xmlFree(rxxpc->ctxt->namespaces);
if (rxxp->xpop == NULL)
rb_raise(eXMLXPathInvalidPath,
"Invalid XPath expression for this document");
if (rxxp->xpop->type != XPATH_NODESET)
return(Qnil);
return(ruby_xml_node_set_new2(node->xd, xpath,
rxxp->xpop->nodesetval));
#else
rb_warn("libxml was compiled without XPath support");
return(Qfalse);
#endif
}
VALUE
ruby_xml_xpath_find2(int argc, VALUE *argv) {
return(ruby_xml_xpath_find(argc, argv, cXMLXPath));
}
void
ruby_xml_xpath_free(ruby_xml_xpath *rxxp) {
if (rxxp->xpop != NULL) {
xmlXPathFreeObject(rxxp->xpop);
rxxp->xpop = NULL;
}
free(rxxp);
}
void
ruby_xml_xpath_mark(ruby_xml_xpath *rxxp) {
if (rxxp == NULL) return;
if (!NIL_P(rxxp->ctxt)) rb_gc_mark(rxxp->ctxt);
if (!NIL_P(rxxp->xd)) rb_gc_mark(rxxp->xd);
}
VALUE
ruby_xml_xpath_new(VALUE class, VALUE xd, VALUE ctxt,
xmlXPathObjectPtr xpop) {
ruby_xml_xpath *rxxp;
rxxp = ALLOC(ruby_xml_xpath);
rxxp->ctxt = ctxt;
rxxp->xd = xd;
rxxp->xpop = xpop;
return(Data_Wrap_Struct(class, ruby_xml_xpath_mark,
ruby_xml_xpath_free, rxxp));
}
/*
* call-seq:
* xpath.set => nodeset
*
* Obtain an XML::Node::Set with nodes matching this xpath.
*/
VALUE
ruby_xml_xpath_set(VALUE self) {
ruby_xml_xpath *rxxp;
Data_Get_Struct(self, ruby_xml_xpath, rxxp);
if (rxxp->xpop == NULL || rxxp->xpop->type != XPATH_NODESET)
return(Qnil);
return(ruby_xml_node_set_new(cXMLNodeSet, rxxp->xd, self,
rxxp->xpop->nodesetval));
}
/*
* call-seq:
* xpath.set_type => num
*
* Obtains the type identifier of this xpath
* set.
*/
VALUE
ruby_xml_xpath_set_type(VALUE self) {
ruby_xml_xpath *rxxp;
Data_Get_Struct(self, ruby_xml_xpath, rxxp);
return(INT2FIX(rxxp->xpop->type));
}
// TODO maybe 'string' should alias as 'to_s'?
/*
* call-seq:
* xpath.string => "xpath"
*
* Obtain a string representation of this xpath.
*/
VALUE
ruby_xml_xpath_string(VALUE self) {
ruby_xml_xpath *rxxp;
Data_Get_Struct(self, ruby_xml_xpath, rxxp);
if (rxxp->xpop->stringval == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxxp->xpop->stringval));
}
// Rdoc needs to know
#ifdef RDOC_NEVER_DEFINED
mXML = rb_define_module("XML");
#endif
void
ruby_init_xml_xpath(void) {
cXMLXPath = rb_define_class_under(mXML, "XPath", rb_cObject);
rb_include_module(cXMLNode, rb_const_get(rb_cObject, rb_intern("Enumerable")));
eXMLXPathInvalidPath = rb_define_class_under(cXMLXPath,
"InvalidPath", rb_eException);
rb_define_const(cXMLXPath, "UNDEFINED", INT2NUM(XPATH_UNDEFINED));
rb_define_const(cXMLXPath, "NODESET", INT2NUM(XPATH_NODESET));
rb_define_const(cXMLXPath, "BOOLEAN", INT2NUM(XPATH_BOOLEAN));
rb_define_const(cXMLXPath, "NUMBER", INT2NUM(XPATH_NUMBER));
rb_define_const(cXMLXPath, "STRING", INT2NUM(XPATH_STRING));
rb_define_const(cXMLXPath, "POINT", INT2NUM(XPATH_POINT));
rb_define_const(cXMLXPath, "RANGE", INT2NUM(XPATH_RANGE));
rb_define_const(cXMLXPath, "LOCATIONSET", INT2NUM(XPATH_LOCATIONSET));
rb_define_const(cXMLXPath, "USERS", INT2NUM(XPATH_USERS));
rb_define_const(cXMLXPath, "XSLT_TREE", INT2NUM(XPATH_XSLT_TREE));
rb_define_singleton_method(cXMLXPath, "find", ruby_xml_xpath_find, 2);
rb_define_method(cXMLXPath, "debug", ruby_xml_xpath_debug, 0);
rb_define_method(cXMLXPath, "each", ruby_xml_xpath_each, 0);
rb_define_method(cXMLXPath, "set", ruby_xml_xpath_set, 0);
rb_define_method(cXMLXPath, "set_type", ruby_xml_xpath_set_type, 0);
rb_define_method(cXMLXPath, "string", ruby_xml_xpath_string, 0);
}
#endif /* ifdef LIBXML_XPATH_ENABLED */
|