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
|
/* $Id: ruby_xml_node_set.c,v 1.3 2006/04/14 14:45:25 roscopeco Exp $ */
/* Please see the LICENSE file for copyright and distribution information */
#include "libxml.h"
#include "ruby_xml_node_set.h"
/*
* Document-class: XML::Node::Set
*
* Includes Enumerable.
*/
VALUE cXMLNodeSet;
// TODO maybe we should support [] on nodeset?
// Would also give us on xpath too...
/*
* call-seq:
* nodeset.to_a => [node, ..., node]
*
* Obtain an array of the nodes in this set.
*/
VALUE
ruby_xml_node_set_to_a(VALUE self) {
int i;
ruby_xml_node_set *rxnset;
VALUE nodeobj, set_ary;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
set_ary = rb_ary_new();
if (!((rxnset->node_set == NULL) || (rxnset->node_set->nodeNr == 0))) {
for (i = 0; i < rxnset->node_set->nodeNr; i++) {
nodeobj = ruby_xml_node_new2(cXMLNode, rxnset->xd, rxnset->node_set->nodeTab[i]);
rb_ary_push(set_ary, nodeobj);
}
}
return(set_ary);
}
/*
* call-seq:
* nodeset.each { |node| ... } => self
*
* Call the supplied block for each node in this set.
*/
VALUE
ruby_xml_node_set_each(VALUE self) {
int i;
ruby_xml_node_set *rxnset;
VALUE nodeobj;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
if (rxnset->node_set == NULL)
return(Qnil);
for (i = 0; i < rxnset->node_set->nodeNr; i++) {
switch(rxnset->node_set->nodeTab[i]->type) {
case XML_ATTRIBUTE_NODE:
nodeobj = ruby_xml_attr_new2(cXMLAttr, rxnset->xd, (xmlAttrPtr)rxnset->node_set->nodeTab[i]);
break;
default:
nodeobj = ruby_xml_node_new2(cXMLNode, rxnset->xd, rxnset->node_set->nodeTab[i]);
}
rb_yield(nodeobj);
}
return(self);
}
/*
* call-seq:
* nodeset.empty? => (true|false)
*
* Determine whether this nodeset is empty (contains no nodes).
*/
VALUE
ruby_xml_node_set_empty_q(VALUE self) {
ruby_xml_node_set *rxnset;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
return ( rxnset->node_set == NULL || rxnset->node_set->nodeNr <= 0 ) ? Qtrue : Qfalse;
}
/*
* call-seq:
* nodeset.first => node
*
* Returns the first node in this node set, or nil if none exist.
*/
VALUE
ruby_xml_node_set_first(VALUE self) {
ruby_xml_node_set *rxnset;
VALUE nodeobj;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
if (rxnset->node_set == NULL || rxnset->node_set->nodeNr < 1)
return(Qnil);
switch(rxnset->node_set->nodeTab[0]->type) {
case XML_ATTRIBUTE_NODE:
nodeobj = ruby_xml_attr_new2(cXMLAttr, rxnset->xd, (xmlAttrPtr)rxnset->node_set->nodeTab[0]);
break;
default:
nodeobj = ruby_xml_node_new2(cXMLNode, rxnset->xd, rxnset->node_set->nodeTab[0]);
}
return(nodeobj);
}
void
ruby_xml_node_set_free(ruby_xml_node_set *rxnset) {
void *data;
switch(rxnset->data_type) {
case RUBY_LIBXML_SRC_TYPE_NULL:
break;
case RUBY_LIBXML_SRC_TYPE_XPATH:
data = (void*)(rx_xpath_data *)rxnset->data;
free((rx_xpath_data *)data);
default:
rb_fatal("Unknown data type, %d", rxnset->data_type);
}
/* Don't need to free the node set because the nodeset is a child of
the XPath object that created the set.
if (rxnset->node_set != NULL)
xmlXPathFreeNodeSet(rxnset->node_set);
*/
free(rxnset);
}
/*
* call-seq:
* nodeset.length => num
*
* Obtain the length of this nodeset.
*/
VALUE
ruby_xml_node_set_length(VALUE self) {
ruby_xml_node_set *rxnset;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
if (rxnset->node_set == NULL)
return(Qnil);
else
return(INT2NUM(rxnset->node_set->nodeNr));
}
static void
ruby_xml_node_set_mark(ruby_xml_node_set *rxnset) {
if (rxnset == NULL) return;
if (!NIL_P(rxnset->xd)) rb_gc_mark(rxnset->xd);
if (!NIL_P(rxnset->xpath)) rb_gc_mark(rxnset->xpath);
}
VALUE
ruby_xml_node_set_new(VALUE class, VALUE xd, VALUE xpath,
xmlNodeSetPtr node_set) {
ruby_xml_node_set *rxnset;
rxnset = ALLOC(ruby_xml_node_set);
rxnset->node_set = node_set;
rxnset->data = NULL;
rxnset->data_type = RUBY_LIBXML_SRC_TYPE_NULL;
rxnset->xd = xd;
rxnset->xpath = xpath;
return(Data_Wrap_Struct(class, ruby_xml_node_set_mark,
ruby_xml_node_set_free, rxnset));
}
VALUE
ruby_xml_node_set_new2(VALUE xd, VALUE xpath,
xmlNodeSetPtr node_set) {
return(ruby_xml_node_set_new(cXMLNodeSet, xd, xpath, node_set));
}
/*
* call-seq:
* nodeset.xpath => xpath
*
* Obtain the xpath corresponding to this nodeset, if any.
*/
VALUE
ruby_xml_node_set_xpath_get(VALUE self) {
ruby_xml_node_set *rxnset;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
if (NIL_P(rxnset->xpath))
return(Qnil);
else
return(rxnset->xpath);
}
/*
* call-seq:
* nodeset.xpath_ctxt => context
*
* Return the xpath context corresponding to this nodeset,
* if any.
*/
VALUE
ruby_xml_node_set_xpath_data_get(VALUE self) {
ruby_xml_node_set *rxnset;
rx_xpath_data *rxxpd;
Data_Get_Struct(self, ruby_xml_node_set, rxnset);
if (rxnset->data_type != RUBY_LIBXML_SRC_TYPE_XPATH)
return(Qnil);
rxxpd = (rx_xpath_data *)rxnset->data;
return(rxxpd->ctxt);
}
// Rdoc needs to know
#ifdef RDOC_NEVER_DEFINED
mXML = rb_define_module("XML");
cXMLNode = rb_define_class_under(mXML, "Node", rb_cObject);
#endif
void
ruby_init_xml_node_set(void) {
cXMLNodeSet = rb_define_class_under(cXMLNode, "Set", rb_cObject);
rb_include_module(cXMLNodeSet, rb_const_get(rb_cObject, rb_intern("Enumerable")));
rb_define_method(cXMLNodeSet, "each", ruby_xml_node_set_each, 0);
rb_define_method(cXMLNodeSet, "empty?", ruby_xml_node_set_empty_q, 0);
rb_define_method(cXMLNodeSet, "first", ruby_xml_node_set_first, 0);
rb_define_method(cXMLNodeSet, "length", ruby_xml_node_set_length, 0);
rb_define_method(cXMLNodeSet, "to_a", ruby_xml_node_set_to_a, 0);
rb_define_method(cXMLNodeSet, "xpath", ruby_xml_node_set_xpath_get, 0);
rb_define_method(cXMLNodeSet, "xpath_ctxt",
ruby_xml_node_set_xpath_data_get, 0);
}
|