File: ruby_xml_node_set.c

package info (click to toggle)
libxml-ruby 0.5.2.0-3%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 652 kB
  • ctags: 875
  • sloc: ansic: 5,874; ruby: 1,524; xml: 144; makefile: 9
file content (170 lines) | stat: -rw-r--r-- 3,953 bytes parent folder | download
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
/* $Id: ruby_xml_node_set.c 192 2007-10-05 15:13:17Z danj $ */

/* 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) {
  ruby_xml_node_set *rxnset;
  VALUE r;

  Data_Get_Struct(self, ruby_xml_node_set, rxnset);
  r=ruby_xml_xpath_object_to_a(rxnset->rxpop);
#ifdef NODE_DEBUG
  fprintf(stderr,"node_set_to_a %s\n",rb_str2cstr(rb_ary_to_s(r),0));
#endif
  return r;
}


/*
 * call-seq:
 *    nodeset.each { |node| ... } => self
 * 
 * Call the supplied block for each node in this set.
 */
VALUE
ruby_xml_node_set_each(VALUE self) {
  ruby_xml_node_set *rxnset;

  Data_Get_Struct(self, ruby_xml_node_set, rxnset);
  return ruby_xml_xpath_object_each(rxnset->rxpop);
}


/*
 * 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 ruby_xml_xpath_object_empty_q(rxnset->rxpop);
}


/*
 * 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;

  Data_Get_Struct(self, ruby_xml_node_set, rxnset);
  return ruby_xml_xpath_object_first(rxnset->rxpop);
}

/*
 * 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);
  return ruby_xml_xpath_object_length(rxnset->rxpop);
}


static void
ruby_xml_node_set_mark(ruby_xml_node_set *rxnset) {
  if (!NIL_P(rxnset->rxpop)) rb_gc_mark(rxnset->rxpop);
}

static void
ruby_xml_node_set_free(ruby_xml_node_set *rxnset) {
  free(rxnset);
}

VALUE
ruby_xml_node_set_new(VALUE class, VALUE rxpop)
{
  ruby_xml_node_set *rxnset;
  rxnset = ALLOC(ruby_xml_node_set);
  rxnset->rxpop=rxpop;
  return Data_Wrap_Struct(class,
			  ruby_xml_node_set_mark,
			  ruby_xml_node_set_free,
			  rxnset);
}

VALUE
ruby_xml_node_set_new2(VALUE rxpop)
{
  return ruby_xml_node_set_new(cXMLNodeSet,rxpop);
}

/*
 * call-seq:
 *    nodeset.xpath => xpath
 * 
 * Obtain the xpath corresponding to this nodeset, if any.
 */
VALUE
ruby_xml_node_set_xpath_get(VALUE self) {
  rb_raise(rb_eRuntimeError,"xpath retrival is no longer supported");
}


/*
 * 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) {
  rb_raise(rb_eRuntimeError,"xpath data retrival is no longer supported");
}

// 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, "size", 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);
       
}