libxml-ruby 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/CHANGELOG +49 -0
  2. data/LICENSE +22 -0
  3. data/README +129 -0
  4. data/Rakefile +197 -0
  5. data/TODO +84 -0
  6. data/ext/xml/cbg.c +76 -0
  7. data/ext/xml/extconf.rb +95 -0
  8. data/ext/xml/libxml.c +86 -0
  9. data/ext/xml/libxml.h +79 -0
  10. data/ext/xml/ruby_xml_attr.c +372 -0
  11. data/ext/xml/ruby_xml_attr.h +21 -0
  12. data/ext/xml/ruby_xml_attribute.c +224 -0
  13. data/ext/xml/ruby_xml_attribute.h +21 -0
  14. data/ext/xml/ruby_xml_document.c +1159 -0
  15. data/ext/xml/ruby_xml_document.h +27 -0
  16. data/ext/xml/ruby_xml_dtd.c +168 -0
  17. data/ext/xml/ruby_xml_dtd.h +17 -0
  18. data/ext/xml/ruby_xml_input_cbg.c +167 -0
  19. data/ext/xml/ruby_xml_input_cbg.h +21 -0
  20. data/ext/xml/ruby_xml_node.c +2052 -0
  21. data/ext/xml/ruby_xml_node.h +28 -0
  22. data/ext/xml/ruby_xml_node_set.c +197 -0
  23. data/ext/xml/ruby_xml_node_set.h +26 -0
  24. data/ext/xml/ruby_xml_ns.c +153 -0
  25. data/ext/xml/ruby_xml_ns.h +21 -0
  26. data/ext/xml/ruby_xml_parser.c +1363 -0
  27. data/ext/xml/ruby_xml_parser.h +31 -0
  28. data/ext/xml/ruby_xml_parser_context.c +715 -0
  29. data/ext/xml/ruby_xml_parser_context.h +22 -0
  30. data/ext/xml/ruby_xml_sax_parser.c +181 -0
  31. data/ext/xml/ruby_xml_sax_parser.h +21 -0
  32. data/ext/xml/ruby_xml_schema.c +142 -0
  33. data/ext/xml/ruby_xml_schema.h +16 -0
  34. data/ext/xml/ruby_xml_tree.c +43 -0
  35. data/ext/xml/ruby_xml_tree.h +12 -0
  36. data/ext/xml/ruby_xml_xinclude.c +20 -0
  37. data/ext/xml/ruby_xml_xinclude.h +13 -0
  38. data/ext/xml/ruby_xml_xpath.c +357 -0
  39. data/ext/xml/ruby_xml_xpath.h +24 -0
  40. data/ext/xml/ruby_xml_xpath_context.c +124 -0
  41. data/ext/xml/ruby_xml_xpath_context.h +24 -0
  42. data/ext/xml/ruby_xml_xpointer.c +100 -0
  43. data/ext/xml/ruby_xml_xpointer.h +27 -0
  44. data/ext/xml/ruby_xml_xpointer_context.c +22 -0
  45. data/ext/xml/ruby_xml_xpointer_context.h +18 -0
  46. data/tests/copy_bug.rb +21 -0
  47. data/tests/dtd-test.rb +24 -0
  48. data/tests/model/default_validation_bug.rb +0 -0
  49. data/tests/model/rubynet.xml +78 -0
  50. data/tests/model/rubynet_project +13 -0
  51. data/tests/model/xinclude.xml +5 -0
  52. data/tests/runner.rb +13 -0
  53. data/tests/schema-test.rb +74 -0
  54. data/tests/tc_default_validation.rb +0 -0
  55. data/tests/tc_xml_document.rb +51 -0
  56. data/tests/tc_xml_document_write.rb +25 -0
  57. data/tests/tc_xml_document_write2.rb +55 -0
  58. data/tests/tc_xml_document_write3.rb +97 -0
  59. data/tests/tc_xml_node.rb +59 -0
  60. data/tests/tc_xml_node2.rb +26 -0
  61. data/tests/tc_xml_node_set.rb +25 -0
  62. data/tests/tc_xml_node_xlink.rb +28 -0
  63. data/tests/tc_xml_parser.rb +175 -0
  64. data/tests/tc_xml_parser2.rb +17 -0
  65. data/tests/tc_xml_parser3.rb +23 -0
  66. data/tests/tc_xml_parser4.rb +33 -0
  67. data/tests/tc_xml_parser5.rb +27 -0
  68. data/tests/tc_xml_parser6.rb +23 -0
  69. data/tests/tc_xml_parser7.rb +28 -0
  70. data/tests/tc_xml_parser_context.rb +89 -0
  71. data/tests/tc_xml_xinclude.rb +30 -0
  72. data/tests/tc_xml_xpath.rb +23 -0
  73. data/tests/tc_xml_xpointer.rb +78 -0
  74. metadata +144 -0
@@ -0,0 +1,13 @@
1
+ /* $Id: ruby_xml_xinclude.h,v 1.1 2006/02/21 20:40:16 roscopeco Exp $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #ifndef __RUBY_XML_XINCLUDE__
6
+ #define __RUBY_XML_XINCLUDE__
7
+
8
+ extern VALUE cXMLXInclude;
9
+ extern VALUE eXMLXIncludeError;
10
+
11
+ void ruby_init_xml_xinclude(void);
12
+
13
+ #endif
@@ -0,0 +1,357 @@
1
+ /* $Id: ruby_xml_xpath.c,v 1.1 2006/02/21 20:40:16 roscopeco Exp $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #include "libxml.h"
6
+ #include "ruby_xml_xpath.h"
7
+
8
+ VALUE cXMLXPath;
9
+ VALUE eXMLXPathInvalidPath;
10
+
11
+ #ifdef LIBXML_XPATH_ENABLED
12
+
13
+ /*
14
+ * call-seq:
15
+ * xpath.debug => (true|false)
16
+ *
17
+ * Dump libxml debugging information to stdout.
18
+ * Requires Libxml be compiled with debugging enabled.
19
+ */
20
+ VALUE
21
+ ruby_xml_xpath_debug(VALUE self) {
22
+ #ifdef LIBXML_DEBUG_ENABLED
23
+ ruby_xml_xpath *rxxp;
24
+ Data_Get_Struct(self, ruby_xml_xpath, rxxp);
25
+
26
+ if (rxxp->xpop != NULL) {
27
+ xmlXPathDebugDumpObject(stdout, rxxp->xpop, 0);
28
+ return(Qtrue);
29
+ } else {
30
+ return(Qfalse);
31
+ }
32
+ #else
33
+ rb_warn("libxml does not have debugging turned on");
34
+ return(Qfalse);
35
+ #endif
36
+ }
37
+
38
+ // TODO Maybe we should support [] or some other kind of access if poss.
39
+
40
+ /*
41
+ * call-seq:
42
+ * xpath.each { |node| ... } => self
43
+ *
44
+ * Call the supplied block for each matching node.
45
+ */
46
+ VALUE
47
+ ruby_xml_xpath_each(VALUE self) {
48
+ ruby_xml_xpath *rxxp;
49
+ VALUE rxnset;
50
+
51
+ Data_Get_Struct(self, ruby_xml_xpath, rxxp);
52
+
53
+ if (rxxp->xpop == NULL || rxxp->xpop->type != XPATH_NODESET)
54
+ return(Qnil);
55
+
56
+ rxnset = ruby_xml_node_set_new(cXMLNodeSet, rxxp->xd, self,
57
+ rxxp->xpop->nodesetval);
58
+ ruby_xml_node_set_each(rxnset);
59
+ return(rxnset);
60
+ }
61
+
62
+ ///////////////////////////////////////////////////
63
+ // TODO xpath_find is throwing TypeError:
64
+ //
65
+ // TypeError: can't convert nil into String
66
+ //
67
+ // When given a namespace when non exist.
68
+
69
+ /*
70
+ * call-seq:
71
+ * XML::XPath.find(path, namespaces = [any]) => xpath
72
+ *
73
+ * Find nodes matching the specified xpath (and optionally any of the
74
+ * supplied namespaces) and return as an XML::Node::Set.
75
+ *
76
+ * The optional namespaces argument may take one of
77
+ * two forms:
78
+ *
79
+ * * A string in the form of: "prefix:uri", or
80
+ * * An array of:
81
+ * * strings in the form like above
82
+ * * arrays in the form of ['prefix','uri']
83
+ *
84
+ * If not specified, matching nodes from any namespace
85
+ * will be included.
86
+ */
87
+ VALUE
88
+ ruby_xml_xpath_find(int argc, VALUE *argv, VALUE class) {
89
+ #ifdef LIBXML_XPATH_ENABLED
90
+ xmlXPathCompExprPtr comp;
91
+ ruby_xml_node *node;
92
+ ruby_xml_xpath *rxxp;
93
+ ruby_xml_xpath_context *rxxpc;
94
+ ruby_xml_ns *rxns;
95
+ VALUE rnode, rprefix, ruri, xxpc, xpath, xpath_expr;
96
+ char *cp;
97
+ long i;
98
+
99
+ switch(argc) {
100
+ case 3:
101
+ /* array of namespaces we allow.
102
+ *
103
+ * Accept either:
104
+ * A string in the form of: "prefix:uri", or
105
+ * An array of:
106
+ * *) strings in the form like above
107
+ * *) arrays in the form of ['prefix','uri']
108
+ */
109
+
110
+ /* Intentionally fall through, we deal with the last arg below
111
+ * after the XPathContext object has been setup */
112
+ case 2:
113
+ rnode = argv[0];
114
+ xpath_expr = argv[1];
115
+ break;
116
+ default:
117
+ rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
118
+ }
119
+
120
+ Data_Get_Struct(rnode, ruby_xml_node, node);
121
+
122
+ xxpc = ruby_xml_xpath_context_new4(rnode);
123
+ if (NIL_P(xxpc))
124
+ return(Qnil);
125
+ Data_Get_Struct(xxpc, ruby_xml_xpath_context, rxxpc);
126
+
127
+ xpath = ruby_xml_xpath_new(cXMLXPath, rnode, xxpc, NULL);
128
+ Data_Get_Struct(xpath, ruby_xml_xpath, rxxp);
129
+
130
+ rxxpc->ctxt->node = node->node;
131
+ if (node->node->type == XML_DOCUMENT_NODE) {
132
+ rxxpc->ctxt->namespaces = xmlGetNsList(node->node->doc,
133
+ xmlDocGetRootElement(node->node->doc));
134
+ } else {
135
+ rxxpc->ctxt->namespaces = xmlGetNsList(node->node->doc, node->node);
136
+ }
137
+
138
+ rxxpc->ctxt->nsNr = 0;
139
+ if (rxxpc->ctxt->namespaces != NULL) {
140
+ while (rxxpc->ctxt->namespaces[rxxpc->ctxt->nsNr] != NULL)
141
+ rxxpc->ctxt->nsNr++;
142
+ }
143
+
144
+ /* Need to loop through the 2nd argument and iterate through the
145
+ * list of namespaces that we want to allow */
146
+ if (argc == 3) {
147
+ switch (TYPE(argv[2])) {
148
+ case T_STRING:
149
+ cp = strchr(StringValuePtr(argv[2]), (int)':');
150
+ if (cp == NULL) {
151
+ rprefix = argv[2];
152
+ ruri = Qnil;
153
+ } else {
154
+ rprefix = rb_str_new(StringValuePtr(argv[2]), (int)((long)cp - (long)StringValuePtr(argv[2])));
155
+ ruri = rb_str_new2(&cp[1]);
156
+ }
157
+ /* Should test the results of this */
158
+ ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
159
+ break;
160
+ case T_ARRAY:
161
+ for (i = 0; i < RARRAY(argv[2])->len; i++) {
162
+ switch (TYPE(RARRAY(argv[2])->ptr[i])) {
163
+ case T_STRING:
164
+ cp = strchr(StringValuePtr(RARRAY(argv[2])->ptr[i]), (int)':');
165
+ if (cp == NULL) {
166
+ rprefix = RARRAY(argv[2])->ptr[i];
167
+ ruri = Qnil;
168
+ } else {
169
+ rprefix = rb_str_new(StringValuePtr(RARRAY(argv[2])->ptr[i]), (int)((long)cp - (long)StringValuePtr(RARRAY(argv[2])->ptr[i])));
170
+ ruri = rb_str_new2(&cp[1]);
171
+ }
172
+ /* Should test the results of this */
173
+ ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
174
+ break;
175
+ case T_ARRAY:
176
+ if (RARRAY(RARRAY(argv[2])->ptr[i])->len == 2) {
177
+ rprefix = RARRAY(RARRAY(argv[2])->ptr[i])->ptr[0];
178
+ ruri = RARRAY(RARRAY(argv[2])->ptr[i])->ptr[1];
179
+ ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
180
+ } else {
181
+ rb_raise(rb_eArgError, "nested array must be an array of strings, prefix and href/uri");
182
+ }
183
+ break;
184
+ default:
185
+ if (rb_obj_is_kind_of(RARRAY(argv[2])->ptr[i], cXMLNS) == Qtrue) {
186
+ Data_Get_Struct(argv[2], ruby_xml_ns, rxns);
187
+ rprefix = rb_str_new2((const char*)rxns->ns->prefix);
188
+ ruri = rb_str_new2((const char*)rxns->ns->href);
189
+ ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
190
+ } else
191
+ rb_raise(rb_eArgError, "Invalid argument type, only accept string, array of strings, or an array of arrays");
192
+ }
193
+ }
194
+ break;
195
+ default:
196
+ if (rb_obj_is_kind_of(argv[2], cXMLNS) == Qtrue) {
197
+ Data_Get_Struct(argv[2], ruby_xml_ns, rxns);
198
+ rprefix = rb_str_new2((const char*)rxns->ns->prefix);
199
+ ruri = rb_str_new2((const char*)rxns->ns->href);
200
+ ruby_xml_xpath_context_register_namespace(xxpc, rprefix, ruri);
201
+ } else
202
+ rb_raise(rb_eArgError, "Invalid argument type, only accept string, array of strings, or an array of arrays");
203
+ }
204
+ }
205
+ comp = xmlXPathCompile((xmlChar*)StringValuePtr(xpath_expr));
206
+
207
+ if (comp == NULL) {
208
+ xmlXPathFreeCompExpr(comp);
209
+ rb_raise(eXMLXPathInvalidPath, "Invalid XPath expression");
210
+ }
211
+ rxxp->xpop = xmlXPathCompiledEval(comp, rxxpc->ctxt);
212
+ xmlXPathFreeCompExpr(comp);
213
+
214
+ if (rxxpc->ctxt->namespaces != NULL)
215
+ xmlFree(rxxpc->ctxt->namespaces);
216
+
217
+ if (rxxp->xpop == NULL)
218
+ rb_raise(eXMLXPathInvalidPath,
219
+ "Invalid XPath expression for this document");
220
+
221
+ if (rxxp->xpop->type != XPATH_NODESET)
222
+ return(Qnil);
223
+
224
+ return(ruby_xml_node_set_new2(node->xd, xpath,
225
+ rxxp->xpop->nodesetval));
226
+ #else
227
+ rb_warn("libxml was compiled without XPath support");
228
+ return(Qfalse);
229
+ #endif
230
+ }
231
+
232
+
233
+ VALUE
234
+ ruby_xml_xpath_find2(int argc, VALUE *argv) {
235
+ return(ruby_xml_xpath_find(argc, argv, cXMLXPath));
236
+ }
237
+
238
+
239
+ void
240
+ ruby_xml_xpath_free(ruby_xml_xpath *rxxp) {
241
+ if (rxxp->xpop != NULL) {
242
+ xmlXPathFreeObject(rxxp->xpop);
243
+ rxxp->xpop = NULL;
244
+ }
245
+
246
+ free(rxxp);
247
+ }
248
+
249
+
250
+ void
251
+ ruby_xml_xpath_mark(ruby_xml_xpath *rxxp) {
252
+ if (rxxp == NULL) return;
253
+ if (!NIL_P(rxxp->ctxt)) rb_gc_mark(rxxp->ctxt);
254
+ if (!NIL_P(rxxp->xd)) rb_gc_mark(rxxp->xd);
255
+ }
256
+
257
+
258
+ VALUE
259
+ ruby_xml_xpath_new(VALUE class, VALUE xd, VALUE ctxt,
260
+ xmlXPathObjectPtr xpop) {
261
+ ruby_xml_xpath *rxxp;
262
+
263
+ rxxp = ALLOC(ruby_xml_xpath);
264
+ rxxp->ctxt = ctxt;
265
+ rxxp->xd = xd;
266
+ rxxp->xpop = xpop;
267
+ return(Data_Wrap_Struct(class, ruby_xml_xpath_mark,
268
+ ruby_xml_xpath_free, rxxp));
269
+ }
270
+
271
+
272
+ /*
273
+ * call-seq:
274
+ * xpath.set => nodeset
275
+ *
276
+ * Obtain an XML::Node::Set with nodes matching this xpath.
277
+ */
278
+ VALUE
279
+ ruby_xml_xpath_set(VALUE self) {
280
+ ruby_xml_xpath *rxxp;
281
+ Data_Get_Struct(self, ruby_xml_xpath, rxxp);
282
+
283
+ if (rxxp->xpop == NULL || rxxp->xpop->type != XPATH_NODESET)
284
+ return(Qnil);
285
+
286
+ return(ruby_xml_node_set_new(cXMLNodeSet, rxxp->xd, self,
287
+ rxxp->xpop->nodesetval));
288
+ }
289
+
290
+
291
+ /*
292
+ * call-seq:
293
+ * xpath.set_type => num
294
+ *
295
+ * Obtains the type identifier of this xpath
296
+ * set.
297
+ */
298
+ VALUE
299
+ ruby_xml_xpath_set_type(VALUE self) {
300
+ ruby_xml_xpath *rxxp;
301
+ Data_Get_Struct(self, ruby_xml_xpath, rxxp);
302
+
303
+ return(INT2FIX(rxxp->xpop->type));
304
+ }
305
+
306
+ // TODO maybe 'string' should alias as 'to_s'?
307
+
308
+ /*
309
+ * call-seq:
310
+ * xpath.string => "xpath"
311
+ *
312
+ * Obtain a string representation of this xpath.
313
+ */
314
+ VALUE
315
+ ruby_xml_xpath_string(VALUE self) {
316
+ ruby_xml_xpath *rxxp;
317
+ Data_Get_Struct(self, ruby_xml_xpath, rxxp);
318
+
319
+ if (rxxp->xpop->stringval == NULL)
320
+ return(Qnil);
321
+ else
322
+ return(rb_str_new2((const char*)rxxp->xpop->stringval));
323
+ }
324
+
325
+ // Rdoc needs to know
326
+ #ifdef RDOC_NEVER_DEFINED
327
+ mXML = rb_define_module("XML");
328
+ #endif
329
+
330
+ void
331
+ ruby_init_xml_xpath(void) {
332
+ cXMLXPath = rb_define_class_under(mXML, "XPath", rb_cObject);
333
+
334
+ eXMLXPathInvalidPath = rb_define_class_under(cXMLXPath,
335
+ "InvalidPath", rb_eException);
336
+
337
+ rb_define_const(cXMLXPath, "UNDEFINED", INT2NUM(XPATH_UNDEFINED));
338
+ rb_define_const(cXMLXPath, "NODESET", INT2NUM(XPATH_NODESET));
339
+ rb_define_const(cXMLXPath, "BOOLEAN", INT2NUM(XPATH_BOOLEAN));
340
+ rb_define_const(cXMLXPath, "NUMBER", INT2NUM(XPATH_NUMBER));
341
+ rb_define_const(cXMLXPath, "STRING", INT2NUM(XPATH_STRING));
342
+ rb_define_const(cXMLXPath, "POINT", INT2NUM(XPATH_POINT));
343
+ rb_define_const(cXMLXPath, "RANGE", INT2NUM(XPATH_RANGE));
344
+ rb_define_const(cXMLXPath, "LOCATIONSET", INT2NUM(XPATH_LOCATIONSET));
345
+ rb_define_const(cXMLXPath, "USERS", INT2NUM(XPATH_USERS));
346
+ rb_define_const(cXMLXPath, "XSLT_TREE", INT2NUM(XPATH_XSLT_TREE));
347
+
348
+ rb_define_singleton_method(cXMLXPath, "find", ruby_xml_xpath_find, 2);
349
+
350
+ rb_define_method(cXMLXPath, "debug", ruby_xml_xpath_debug, 0);
351
+ rb_define_method(cXMLXPath, "each", ruby_xml_xpath_each, 0);
352
+ rb_define_method(cXMLXPath, "set", ruby_xml_xpath_set, 0);
353
+ rb_define_method(cXMLXPath, "set_type", ruby_xml_xpath_set_type, 0);
354
+ rb_define_method(cXMLXPath, "string", ruby_xml_xpath_string, 0);
355
+ }
356
+
357
+ #endif /* ifdef LIBXML_XPATH_ENABLED */
@@ -0,0 +1,24 @@
1
+ /* $Id: ruby_xml_xpath.h,v 1.1 2006/02/21 20:40:16 roscopeco Exp $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #ifndef __RUBY_XML_XPATH__
6
+ #define __RUBY_XML_XPATH__
7
+
8
+ extern VALUE cXMLXPath;
9
+ extern VALUE eXMLXPathInvalidPath;
10
+
11
+ typedef struct ruby_xml_xpath {
12
+ VALUE xd;
13
+ VALUE ctxt;
14
+ xmlXPathObjectPtr xpop;
15
+ } ruby_xml_xpath;
16
+
17
+ void ruby_xml_xpath_free(ruby_xml_xpath *rxxp);
18
+ VALUE ruby_xml_xpath_find(int argc, VALUE *argv, VALUE class);
19
+ VALUE ruby_xml_xpath_find2(int argc, VALUE *argv);
20
+ VALUE ruby_xml_xpath_new(VALUE class, VALUE xd, VALUE xxpc,
21
+ xmlXPathObjectPtr xpop);
22
+ void ruby_init_xml_xpath(void);
23
+
24
+ #endif
@@ -0,0 +1,124 @@
1
+ /* $Id: ruby_xml_xpath_context.c,v 1.1 2006/02/21 20:40:16 roscopeco Exp $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #include "libxml.h"
6
+ #include "ruby_xml_xpath_context.h"
7
+
8
+
9
+ /*
10
+ * call-seq:
11
+ * context.doc => document
12
+ *
13
+ * Obtain the XML::Document associated with this XPath.
14
+ */
15
+ VALUE
16
+ ruby_xml_xpath_context_doc_get(VALUE self) {
17
+ ruby_xml_xpath_context *rxxpc;
18
+ Data_Get_Struct(self, ruby_xml_xpath_context, rxxpc);
19
+
20
+ return(rxxpc->xd);
21
+ }
22
+
23
+
24
+ void
25
+ ruby_xml_xpath_context_free(ruby_xml_xpath_context *rxxpc) {
26
+ if (rxxpc->ctxt != NULL) {
27
+ xmlXPathFreeContext(rxxpc->ctxt);
28
+ rxxpc->ctxt = NULL;
29
+ }
30
+
31
+ free(rxxpc);
32
+ }
33
+
34
+
35
+ void
36
+ ruby_xml_xpath_context_mark(ruby_xml_xpath_context *rxxpc) {
37
+ if (rxxpc == NULL) return;
38
+ if (!NIL_P(rxxpc->xd)) rb_gc_mark(rxxpc->xd);
39
+ }
40
+
41
+
42
+ VALUE
43
+ ruby_xml_xpath_context_new(VALUE class, VALUE xd,
44
+ xmlXPathContextPtr xxpc) {
45
+ ruby_xml_xpath_context *rxxpc;
46
+
47
+ rxxpc = ALLOC(ruby_xml_xpath_context);
48
+ rxxpc->ctxt = xxpc;
49
+ rxxpc->xd = xd;
50
+ return(Data_Wrap_Struct(class, ruby_xml_xpath_context_mark,
51
+ ruby_xml_xpath_context_free, rxxpc));
52
+ }
53
+
54
+
55
+ VALUE
56
+ ruby_xml_xpath_context_new2(VALUE xd, xmlXPathContextPtr xxpc) {
57
+ return(ruby_xml_xpath_context_new(cXMLXPathContext, xd, xxpc));
58
+ }
59
+
60
+
61
+ VALUE
62
+ ruby_xml_xpath_context_new3(VALUE xd) {
63
+ ruby_xml_document *rxd;
64
+ xmlXPathContextPtr ctxt;
65
+
66
+ Data_Get_Struct(xd, ruby_xml_document, rxd);
67
+ if (rxd->doc == NULL)
68
+ return(Qnil);
69
+
70
+ ctxt = xmlXPathNewContext(rxd->doc);
71
+ if (ctxt == NULL)
72
+ return(Qnil);
73
+
74
+ return(ruby_xml_xpath_context_new2(xd, ctxt));
75
+ }
76
+
77
+
78
+ VALUE
79
+ ruby_xml_xpath_context_new4(VALUE rnode) {
80
+ ruby_xml_node *node;
81
+
82
+ Data_Get_Struct(rnode, ruby_xml_node, node);
83
+ return(ruby_xml_xpath_context_new3(node->xd));
84
+ }
85
+
86
+
87
+ /*
88
+ * call-seq:
89
+ * context.register_namespace(prefix, uri) => (true|false)
90
+ *
91
+ * Register the specified namespace URI with the specified prefix
92
+ * in this context.
93
+ */
94
+ VALUE
95
+ ruby_xml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri) {
96
+ ruby_xml_xpath_context *rxxpc;
97
+
98
+ Data_Get_Struct(self, ruby_xml_xpath_context, rxxpc);
99
+ if (xmlXPathRegisterNs(rxxpc->ctxt,
100
+ (xmlChar*)StringValuePtr(prefix),
101
+ (xmlChar*)StringValuePtr(uri))
102
+ == 0) {
103
+ return(Qtrue);
104
+ } else {
105
+ /* Should raise an exception, IMHO */
106
+ return(Qfalse);
107
+ }
108
+ }
109
+
110
+ // Rdoc needs to know
111
+ #ifdef RDOC_NEVER_DEFINED
112
+ mXML = rb_define_module("XML");
113
+ cXMLXPath = rb_define_class_under(mXML, "XPath", rb_cObject);
114
+ #endif
115
+
116
+ void
117
+ ruby_init_xml_xpath_context(void) {
118
+ cXMLXPathContext = rb_define_class_under(cXMLXPath, "Context", rb_cObject);
119
+
120
+ rb_define_method(cXMLXPathContext, "register_namespace",
121
+ ruby_xml_xpath_context_register_namespace, 2);
122
+ rb_define_method(cXMLXPathContext, "doc",
123
+ ruby_xml_xpath_context_doc_get, 0);
124
+ }