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
|
#include "ruby_libxml.h"
/*
* Document-class: LibXML::XML::XPath::Object
*
* A collection of nodes returned from the evaluation of an XML::XPath
* or XML::XPointer expression.
*/
VALUE cXMLXPathObject;
/* Memory management of xpath results is tricky. If a nodeset is
returned, it generally consists of pointers to nodes in the
original document. However, namespace nodes are handled differently -
libxml creates copies of them instead. Thus, when an xmlXPathObjectPtr
is freed, libxml iterates over the results to find the copied namespace
nodes to free them.
This causes problems for the bindings because the underlying document
may be freed before the xmlXPathObjectPtr instance. This might seem
counterintuitive since the xmlXPathObjectPtr marks the document.
However, once both objects go out of scope, the order of their
destruction is random.
To deal with this, the wrapper code searches for the namespace nodes
and wraps them in Ruby objects. When the Ruby objects go out of scope
then the namespace nodes are freed. */
static void rxml_xpath_object_free(rxml_xpath_object *rxpop)
{
/* We positively, absolutely cannot let libxml iterate over
the nodeTab since if the underlying document has been
freed the majority of entries are invalid, resulting in
segmentation faults.*/
if (rxpop->xpop->nodesetval && rxpop->xpop->nodesetval->nodeTab)
{
xmlFree(rxpop->xpop->nodesetval->nodeTab);
rxpop->xpop->nodesetval->nodeTab = NULL;
}
xmlXPathFreeObject(rxpop->xpop);
xfree(rxpop);
}
/* Custom free function for copied namespace nodes */
static void rxml_xpath_namespace_free(xmlNsPtr xns)
{
xmlFreeNs(xns);
}
static void rxml_xpath_object_mark(rxml_xpath_object *rxpop)
{
rb_gc_mark(rxpop->nsnodes);
if (rxpop->xdoc->_private)
rb_gc_mark((VALUE)rxpop->xdoc->_private);
}
VALUE rxml_xpath_object_wrap(xmlDocPtr xdoc, xmlXPathObjectPtr xpop)
{
int i;
rxml_xpath_object *rxpop = ALLOC(rxml_xpath_object);
rxpop->xdoc =xdoc;
rxpop->xpop = xpop;
rxpop->nsnodes = rb_ary_new();
/* Find all the extra namespace nodes and wrap them. */
if (xpop->nodesetval && xpop->nodesetval->nodeNr)
{
for (i = 0;i < xpop->nodesetval->nodeNr; i++)
{
xmlNodePtr xnode = xpop->nodesetval->nodeTab[i];
if (xnode != NULL && xnode->type == XML_NAMESPACE_DECL)
{
VALUE ns = Qnil;
xmlNsPtr xns = (xmlNsPtr)xnode;
/* Get rid of libxml's -> next hack. The issue here is
the rxml_namespace code assumes that ns->next refers
to another namespace. */
xns->next = NULL;
/* Specify a custom free function here since by default
namespace nodes will not be freed */
ns = rxml_namespace_wrap((xmlNsPtr)xnode);
RDATA(ns)->dfree = (RUBY_DATA_FUNC)rxml_xpath_namespace_free;
rb_ary_push(rxpop->nsnodes, ns);
}
}
}
return Data_Wrap_Struct(cXMLXPathObject, rxml_xpath_object_mark, rxml_xpath_object_free, rxpop);
}
static VALUE rxml_xpath_object_tabref(xmlXPathObjectPtr xpop, int index)
{
if (index < 0)
index = xpop->nodesetval->nodeNr + index;
if (index < 0 || index + 1 > xpop->nodesetval->nodeNr)
return Qnil;
switch (xpop->nodesetval->nodeTab[index]->type)
{
case XML_ATTRIBUTE_NODE:
return rxml_attr_wrap((xmlAttrPtr) xpop->nodesetval->nodeTab[index]);
break;
case XML_NAMESPACE_DECL:
return rxml_namespace_wrap((xmlNsPtr)xpop->nodesetval->nodeTab[index]);
break;
default:
return rxml_node_wrap(xpop->nodesetval->nodeTab[index]);
}
}
/*
* call-seq:
* xpath_object.to_a -> [node, ..., node]
*
* Obtain an array of the nodes in this set.
*/
static VALUE rxml_xpath_object_to_a(VALUE self)
{
VALUE set_ary, nodeobj;
rxml_xpath_object *rxpop;
xmlXPathObjectPtr xpop;
int i;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
xpop = rxpop->xpop;
set_ary = rb_ary_new();
if (!((xpop->nodesetval == NULL) || (xpop->nodesetval->nodeNr == 0)))
{
for (i = 0; i < xpop->nodesetval->nodeNr; i++)
{
nodeobj = rxml_xpath_object_tabref(xpop, i);
rb_ary_push(set_ary, nodeobj);
}
}
return (set_ary);
}
/*
* call-seq:
* xpath_object.empty? -> (true|false)
*
* Determine whether this nodeset is empty (contains no nodes).
*/
static VALUE rxml_xpath_object_empty_q(VALUE self)
{
rxml_xpath_object *rxpop;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
if (rxpop->xpop->type != XPATH_NODESET)
return Qnil;
return (rxpop->xpop->nodesetval == NULL || rxpop->xpop->nodesetval->nodeNr <= 0) ? Qtrue
: Qfalse;
}
/*
* call-seq:
* xpath_object.each { |node| ... } -> self
*
* Call the supplied block for each node in this set.
*/
static VALUE rxml_xpath_object_each(VALUE self)
{
rxml_xpath_object *rxpop;
int i;
if (rxml_xpath_object_empty_q(self) == Qtrue)
return Qnil;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
for (i = 0; i < rxpop->xpop->nodesetval->nodeNr; i++)
{
rb_yield(rxml_xpath_object_tabref(rxpop->xpop, i));
}
return (self);
}
/*
* call-seq:
* xpath_object.first -> node
*
* Returns the first node in this node set, or nil if none exist.
*/
static VALUE rxml_xpath_object_first(VALUE self)
{
rxml_xpath_object *rxpop;
if (rxml_xpath_object_empty_q(self) == Qtrue)
return Qnil;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
return rxml_xpath_object_tabref(rxpop->xpop, 0);
}
/*
* call-seq:
* xpath_object.last -> node
*
* Returns the last node in this node set, or nil if none exist.
*/
static VALUE rxml_xpath_object_last(VALUE self)
{
rxml_xpath_object *rxpop;
if (rxml_xpath_object_empty_q(self) == Qtrue)
return Qnil;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
return rxml_xpath_object_tabref(rxpop->xpop, -1);
}
/*
* call-seq:
* xpath_object[i] -> node
*
* array index into set of nodes
*/
static VALUE rxml_xpath_object_aref(VALUE self, VALUE aref)
{
rxml_xpath_object *rxpop;
if (rxml_xpath_object_empty_q(self) == Qtrue)
return Qnil;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
return rxml_xpath_object_tabref(rxpop->xpop, NUM2INT(aref));
}
/*
* call-seq:
* xpath_object.length -> num
*
* Obtain the length of the nodesetval node list.
*/
static VALUE rxml_xpath_object_length(VALUE self)
{
rxml_xpath_object *rxpop;
if (rxml_xpath_object_empty_q(self) == Qtrue)
return INT2FIX(0);
Data_Get_Struct(self, rxml_xpath_object, rxpop);
return INT2NUM(rxpop->xpop->nodesetval->nodeNr);
}
/*
* call-seq:
* xpath_object.xpath_type -> int
*
* Returns the XPath type of the result object.
* Possible values are defined as constants
* on the XML::XPath class and include:
*
* * XML::XPath::UNDEFINED
* * XML::XPath::NODESET
* * XML::XPath::BOOLEAN
* * XML::XPath::NUMBER
* * XML::XPath::STRING
* * XML::XPath::POINT
* * XML::XPath::RANGE
* * XML::XPath::LOCATIONSET
* * XML::XPath::USERS
* * XML::XPath::XSLT_TREE
*/
static VALUE rxml_xpath_object_get_type(VALUE self)
{
rxml_xpath_object *rxpop;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
return INT2FIX(rxpop->xpop->type);
}
/*
* call-seq:
* xpath_object.string -> String
*
* Returns the original XPath expression as a string.
*/
static VALUE rxml_xpath_object_string(VALUE self)
{
rxml_xpath_object *rxpop;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
if (rxpop->xpop->stringval == NULL)
return Qnil;
return rxml_new_cstr((const char*) rxpop->xpop->stringval, rxpop->xdoc->encoding);
}
/*
* call-seq:
* nodes.debug -> (true|false)
*
* Dump libxml debugging information to stdout.
* Requires Libxml be compiled with debugging enabled.
*/
static VALUE rxml_xpath_object_debug(VALUE self)
{
#ifdef LIBXML_DEBUG_ENABLED
rxml_xpath_object *rxpop;
Data_Get_Struct(self, rxml_xpath_object, rxpop);
xmlXPathDebugDumpObject(stdout, rxpop->xpop, 0);
return Qtrue;
#else
rb_warn("libxml was compiled without debugging support.")
return Qfalse;
#endif
}
void rxml_init_xpath_object(void)
{
cXMLXPathObject = rb_define_class_under(mXPath, "Object", rb_cObject);
rb_include_module(cXMLXPathObject, rb_mEnumerable);
rb_define_attr(cXMLXPathObject, "context", 1, 0);
rb_define_method(cXMLXPathObject, "each", rxml_xpath_object_each, 0);
rb_define_method(cXMLXPathObject, "xpath_type", rxml_xpath_object_get_type, 0);
rb_define_method(cXMLXPathObject, "empty?", rxml_xpath_object_empty_q, 0);
rb_define_method(cXMLXPathObject, "first", rxml_xpath_object_first, 0);
rb_define_method(cXMLXPathObject, "last", rxml_xpath_object_last, 0);
rb_define_method(cXMLXPathObject, "length", rxml_xpath_object_length, 0);
rb_define_method(cXMLXPathObject, "to_a", rxml_xpath_object_to_a, 0);
rb_define_method(cXMLXPathObject, "[]", rxml_xpath_object_aref, 1);
rb_define_method(cXMLXPathObject, "string", rxml_xpath_object_string, 0);
rb_define_method(cXMLXPathObject, "debug", rxml_xpath_object_debug, 0);
}
|