libxml-ruby 5.0.4 → 5.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY +17 -6
  3. data/README.rdoc +1 -1
  4. data/Rakefile +98 -98
  5. data/ext/libxml/extconf.h +1 -0
  6. data/ext/libxml/extconf.rb +5 -0
  7. data/ext/libxml/libxml.c +79 -79
  8. data/ext/libxml/ruby_xml.c +556 -556
  9. data/ext/libxml/ruby_xml_attr.c +333 -333
  10. data/ext/libxml/ruby_xml_attr.h +12 -12
  11. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  12. data/ext/libxml/ruby_xml_attributes.h +17 -17
  13. data/ext/libxml/ruby_xml_cbg.c +85 -85
  14. data/ext/libxml/ruby_xml_document.c +1129 -1129
  15. data/ext/libxml/ruby_xml_dtd.c +257 -257
  16. data/ext/libxml/ruby_xml_dtd.h +9 -9
  17. data/ext/libxml/ruby_xml_encoding.c +250 -250
  18. data/ext/libxml/ruby_xml_error.c +1003 -1003
  19. data/ext/libxml/ruby_xml_error.h +14 -14
  20. data/ext/libxml/ruby_xml_html_parser.c +91 -91
  21. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  22. data/ext/libxml/ruby_xml_html_parser_context.c +351 -351
  23. data/ext/libxml/ruby_xml_html_parser_context.h +10 -10
  24. data/ext/libxml/ruby_xml_html_parser_options.c +48 -48
  25. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  26. data/ext/libxml/ruby_xml_input_cbg.c +188 -188
  27. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  28. data/ext/libxml/ruby_xml_io.c +47 -47
  29. data/ext/libxml/ruby_xml_io.h +10 -10
  30. data/ext/libxml/ruby_xml_namespace.c +151 -151
  31. data/ext/libxml/ruby_xml_namespace.h +10 -10
  32. data/ext/libxml/ruby_xml_namespaces.c +293 -293
  33. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  34. data/ext/libxml/ruby_xml_node.h +13 -13
  35. data/ext/libxml/ruby_xml_parser.c +91 -91
  36. data/ext/libxml/ruby_xml_parser.h +10 -10
  37. data/ext/libxml/ruby_xml_parser_context.c +1009 -1009
  38. data/ext/libxml/ruby_xml_parser_context.h +10 -10
  39. data/ext/libxml/ruby_xml_parser_options.c +74 -74
  40. data/ext/libxml/ruby_xml_parser_options.h +10 -10
  41. data/ext/libxml/ruby_xml_reader.h +14 -14
  42. data/ext/libxml/ruby_xml_relaxng.h +8 -8
  43. data/ext/libxml/ruby_xml_sax2_handler.c +326 -326
  44. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  45. data/ext/libxml/ruby_xml_sax_parser.c +108 -108
  46. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  47. data/ext/libxml/ruby_xml_schema.h +25 -25
  48. data/ext/libxml/ruby_xml_schema_attribute.h +37 -37
  49. data/ext/libxml/ruby_xml_schema_element.h +11 -11
  50. data/ext/libxml/ruby_xml_schema_facet.c +50 -50
  51. data/ext/libxml/ruby_xml_schema_facet.h +9 -9
  52. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  53. data/ext/libxml/ruby_xml_version.h +9 -9
  54. data/ext/libxml/ruby_xml_writer.c +1124 -1124
  55. data/ext/libxml/ruby_xml_writer.h +6 -6
  56. data/ext/libxml/ruby_xml_xinclude.c +20 -20
  57. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  58. data/ext/libxml/ruby_xml_xpath.c +195 -195
  59. data/ext/libxml/ruby_xml_xpath.h +15 -15
  60. data/ext/libxml/ruby_xml_xpath_context.c +362 -362
  61. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  62. data/ext/libxml/ruby_xml_xpath_expression.h +10 -10
  63. data/ext/libxml/ruby_xml_xpath_object.h +17 -17
  64. data/lib/libxml/attr.rb +122 -122
  65. data/lib/libxml/attr_decl.rb +80 -80
  66. data/lib/libxml/attributes.rb +13 -13
  67. data/lib/libxml/document.rb +194 -194
  68. data/lib/libxml/error.rb +95 -95
  69. data/lib/libxml/hpricot.rb +77 -77
  70. data/lib/libxml/html_parser.rb +96 -96
  71. data/lib/libxml/namespace.rb +61 -61
  72. data/lib/libxml/namespaces.rb +37 -37
  73. data/lib/libxml/node.rb +323 -323
  74. data/lib/libxml/parser.rb +102 -102
  75. data/lib/libxml/sax_callbacks.rb +179 -179
  76. data/lib/libxml/sax_parser.rb +40 -40
  77. data/lib/libxml/tree.rb +28 -28
  78. data/lib/libxml-ruby.rb +2 -2
  79. data/lib/libxml.rb +4 -4
  80. data/lib/xml/libxml.rb +10 -10
  81. data/lib/xml.rb +13 -13
  82. data/libxml-ruby.gemspec +50 -49
  83. data/test/test_document.rb +140 -140
  84. data/test/test_document_write.rb +142 -142
  85. data/test/test_dtd.rb +126 -126
  86. data/test/test_encoding.rb +126 -126
  87. data/test/test_error.rb +197 -194
  88. data/test/test_helper.rb +21 -20
  89. data/test/test_namespace.rb +58 -58
  90. data/test/test_node.rb +235 -235
  91. data/test/test_node_write.rb +93 -93
  92. data/test/test_parser.rb +333 -333
  93. data/test/test_reader.rb +364 -364
  94. data/test/test_writer.rb +500 -468
  95. data/test/test_xml.rb +168 -168
  96. metadata +5 -5
  97. data/test/test.rb +0 -5
@@ -1,362 +1,362 @@
1
- /* Please see the LICENSE file for copyright and distribution information */
2
-
3
- #include "ruby_libxml.h"
4
- #include "ruby_xml_xpath_context.h"
5
- #include "ruby_xml_xpath_expression.h"
6
-
7
- #if RUBY_ST_H
8
- #include <ruby/st.h>
9
- #else
10
- #include <st.h>
11
- #endif
12
-
13
- #include <libxml/xpathInternals.h>
14
-
15
- /*
16
- * Document-class: LibXML::XML::XPath::Context
17
- *
18
- * The XML::XPath::Context class is used to evaluate XPath
19
- * expressions. Generally, you should not directly use this class,
20
- * but instead use the XML::Document#find and XML::Node#find methods.
21
- *
22
- * doc = XML::Document.string('<header>content</header>')
23
- * context = XPath::Context.new(doc)
24
- * context.node = doc.root
25
- * context.register_namespaces_from_node(doc.root)
26
- * nodes = context.find('/header')
27
- */
28
-
29
- VALUE cXMLXPathContext;
30
-
31
- static void rxml_xpath_context_free(xmlXPathContextPtr ctxt)
32
- {
33
- xmlXPathFreeContext(ctxt);
34
- }
35
-
36
- static void rxml_xpath_context_mark(xmlXPathContextPtr ctxt)
37
- {
38
- VALUE value = (VALUE)ctxt->doc->_private;
39
- rb_gc_mark(value);
40
- }
41
-
42
- static VALUE rxml_xpath_context_alloc(VALUE klass)
43
- {
44
- return Data_Wrap_Struct(cXMLXPathContext, rxml_xpath_context_mark, rxml_xpath_context_free, NULL);
45
- }
46
-
47
- /* call-seq:
48
- * XPath::Context.new(doc) -> XPath::Context
49
- *
50
- * Creates a new XPath context for the specified document. The
51
- * context can then be used to evaluate an XPath expression.
52
- *
53
- * doc = XML::Document.string('<header><first>hi</first></header>')
54
- * context = XPath::Context.new(doc)
55
- * nodes = XPath::Object.new('//first', context)
56
- * nodes.length == 1
57
- */
58
- static VALUE rxml_xpath_context_initialize(VALUE self, VALUE document)
59
- {
60
- xmlDocPtr xdoc;
61
-
62
- if (rb_obj_is_kind_of(document, cXMLDocument) != Qtrue)
63
- {
64
- rb_raise(rb_eTypeError, "Supplied argument must be a document or node.");
65
- }
66
-
67
- Data_Get_Struct(document, xmlDoc, xdoc);
68
- DATA_PTR(self) = xmlXPathNewContext(xdoc);
69
-
70
- return self;
71
- }
72
-
73
- /*
74
- * call-seq:
75
- * context.doc -> document
76
- *
77
- * Obtain the XML::Document this node belongs to.
78
- */
79
- static VALUE rxml_xpath_context_doc(VALUE self)
80
- {
81
- xmlDocPtr xdoc = NULL;
82
- xmlXPathContextPtr ctxt;
83
- Data_Get_Struct(self, xmlXPathContext, ctxt);
84
-
85
- xdoc = ctxt->doc;
86
- return rxml_document_wrap(xdoc);
87
- }
88
-
89
- /*
90
- * call-seq:
91
- * context.register_namespace(prefix, uri) -> (true|false)
92
- *
93
- * Register the specified namespace URI with the specified prefix
94
- * in this context.
95
-
96
- * context.register_namespace('xi', 'http://www.w3.org/2001/XInclude')
97
- */
98
- static VALUE rxml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri)
99
- {
100
- xmlXPathContextPtr ctxt;
101
- Data_Get_Struct(self, xmlXPathContext, ctxt);
102
-
103
- /* Prefix could be a symbol. */
104
- prefix = rb_obj_as_string(prefix);
105
-
106
- if (xmlXPathRegisterNs(ctxt, (xmlChar*) StringValuePtr(prefix),
107
- (xmlChar*) StringValuePtr(uri)) == 0)
108
- {
109
- return (Qtrue);
110
- }
111
- else
112
- {
113
- /* Should raise an exception, IMHO (whose?, why shouldnt it? -danj)*/
114
- rb_warning("register namespace failed");
115
- return (Qfalse);
116
- }
117
- }
118
-
119
- /* call-seq:
120
- * context.register_namespaces_from_node(node) -> self
121
- *
122
- * Helper method to read in namespaces defined on a node.
123
- *
124
- * doc = XML::Document.string('<header><first>hi</first></header>')
125
- * context = XPath::Context.new(doc)
126
- * context.register_namespaces_from_node(doc.root)
127
- */
128
- static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
129
- VALUE node)
130
- {
131
- xmlXPathContextPtr xctxt;
132
- xmlNodePtr xnode;
133
- xmlNsPtr *xnsArr;
134
-
135
- Data_Get_Struct(self, xmlXPathContext, xctxt);
136
-
137
- if (rb_obj_is_kind_of(node, cXMLDocument) == Qtrue)
138
- {
139
- xmlDocPtr xdoc;
140
- Data_Get_Struct(node, xmlDoc, xdoc);
141
- xnode = xmlDocGetRootElement(xdoc);
142
- }
143
- else if (rb_obj_is_kind_of(node, cXMLNode) == Qtrue)
144
- {
145
- Data_Get_Struct(node, xmlNode, xnode);
146
- }
147
- else
148
- {
149
- rb_raise(rb_eTypeError, "The first argument must be a document or node.");
150
- }
151
-
152
- xnsArr = xmlGetNsList(xnode->doc, xnode);
153
-
154
- if (xnsArr)
155
- {
156
- xmlNsPtr xns = *xnsArr;
157
-
158
- while (xns)
159
- {
160
- /* If there is no prefix, then this is the default namespace.
161
- Skip it for now. */
162
- if (xns->prefix)
163
- {
164
- VALUE prefix = rxml_new_cstr(xns->prefix, xctxt->doc->encoding);
165
- VALUE uri = rxml_new_cstr(xns->href, xctxt->doc->encoding);
166
- rxml_xpath_context_register_namespace(self, prefix, uri);
167
- }
168
- xns = xns->next;
169
- }
170
- xmlFree(xnsArr);
171
- }
172
-
173
- return self;
174
- }
175
-
176
- static int iterate_ns_hash(VALUE prefix, VALUE uri, VALUE self)
177
- {
178
- rxml_xpath_context_register_namespace(self, prefix, uri);
179
- return ST_CONTINUE;
180
- }
181
-
182
- /*
183
- * call-seq:
184
- * context.register_namespaces(["prefix:uri"]) -> self
185
- *
186
- * Register the specified namespaces in this context. There are
187
- * three different forms that libxml accepts. These include
188
- * a string, an array of strings, or a hash table:
189
- *
190
- * context.register_namespaces('xi:http://www.w3.org/2001/XInclude')
191
- * context.register_namespaces(['xlink:http://www.w3.org/1999/xlink',
192
- * 'xi:http://www.w3.org/2001/XInclude')
193
- * context.register_namespaces('xlink' => 'http://www.w3.org/1999/xlink',
194
- * 'xi' => 'http://www.w3.org/2001/XInclude')
195
- */
196
- static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
197
- {
198
- char *cp;
199
- long i;
200
- VALUE rprefix, ruri;
201
- xmlXPathContextPtr xctxt;
202
-
203
- Data_Get_Struct(self, xmlXPathContext, xctxt);
204
-
205
- /* Need to loop through the 2nd argument and iterate through the
206
- * list of namespaces that we want to allow */
207
- switch (TYPE(nslist))
208
- {
209
- case T_STRING:
210
- cp = strchr(StringValuePtr(nslist), (int) ':');
211
- if (cp == NULL)
212
- {
213
- rprefix = nslist;
214
- ruri = Qnil;
215
- }
216
- else
217
- {
218
- rprefix = rb_str_new(StringValuePtr(nslist), (long) ((intptr_t) cp - (intptr_t)StringValuePtr(nslist)));
219
- ruri = rxml_new_cstr((const xmlChar*)&cp[1], xctxt->doc->encoding);
220
- }
221
- /* Should test the results of this */
222
- rxml_xpath_context_register_namespace(self, rprefix, ruri);
223
- break;
224
- case T_ARRAY:
225
- for (i = 0; i < RARRAY_LEN(nslist); i++)
226
- {
227
- rxml_xpath_context_register_namespaces(self, RARRAY_PTR(nslist)[i]);
228
- }
229
- break;
230
- case T_HASH:
231
- rb_hash_foreach(nslist, iterate_ns_hash, self);
232
- break;
233
- default:
234
- rb_raise(
235
- rb_eArgError,
236
- "Invalid argument type, only accept string, array of strings, or an array of arrays");
237
- }
238
- return self;
239
- }
240
-
241
- /*
242
- * call-seq:
243
- * context.node = node
244
- *
245
- * Set the current node used by the XPath engine
246
-
247
- * doc = XML::Document.string('<header><first>hi</first></header>')
248
- * context.node = doc.root.first
249
- */
250
- static VALUE rxml_xpath_context_node_set(VALUE self, VALUE node)
251
- {
252
- xmlXPathContextPtr xctxt;
253
- xmlNodePtr xnode;
254
-
255
- Data_Get_Struct(self, xmlXPathContext, xctxt);
256
- Data_Get_Struct(node, xmlNode, xnode);
257
- xctxt->node = xnode;
258
- return node;
259
- }
260
-
261
- /*
262
- * call-seq:
263
- * context.find("xpath") -> true|false|number|string|XML::XPath::Object
264
- *
265
- * Executes the provided xpath function. The result depends on the execution
266
- * of the xpath statement. It may be true, false, a number, a string or
267
- * a node set.
268
- */
269
- static VALUE rxml_xpath_context_find(VALUE self, VALUE xpath_expr)
270
- {
271
- xmlXPathContextPtr xctxt;
272
- xmlXPathObjectPtr xobject;
273
- xmlXPathCompExprPtr xcompexpr;
274
-
275
- Data_Get_Struct(self, xmlXPathContext, xctxt);
276
-
277
- if (TYPE(xpath_expr) == T_STRING)
278
- {
279
- VALUE expression = rb_check_string_type(xpath_expr);
280
- xobject = xmlXPathEval((xmlChar*) StringValueCStr(expression), xctxt);
281
- }
282
- else if (rb_obj_is_kind_of(xpath_expr, cXMLXPathExpression))
283
- {
284
- Data_Get_Struct(xpath_expr, xmlXPathCompExpr, xcompexpr);
285
- xobject = xmlXPathCompiledEval(xcompexpr, xctxt);
286
- }
287
- else
288
- {
289
- rb_raise(rb_eTypeError,
290
- "Argument should be an instance of a String or XPath::Expression");
291
- }
292
-
293
- return rxml_xpath_to_value(xctxt, xobject);
294
- }
295
-
296
- #if LIBXML_VERSION >= 20626
297
- /*
298
- * call-seq:
299
- * context.enable_cache(size = nil)
300
- *
301
- * Enables an XPath::Context's built-in cache. If the cache is
302
- * enabled then XPath objects will be cached internally for reuse.
303
- * The size parameter controls sets the maximum number of XPath objects
304
- * that will be cached per XPath object type (node-set, string, number,
305
- * boolean, and misc objects). Set size to nil to use the default
306
- * cache size of 100.
307
- */
308
- static VALUE
309
- rxml_xpath_context_enable_cache(int argc, VALUE *argv, VALUE self)
310
- {
311
- xmlXPathContextPtr xctxt;
312
- VALUE size;
313
- int value = -1;
314
-
315
- Data_Get_Struct(self, xmlXPathContext, xctxt);
316
-
317
- if (rb_scan_args(argc, argv, "01", &size) == 1)
318
- {
319
- value = NUM2INT(size);
320
- }
321
-
322
- if (xmlXPathContextSetCache(xctxt, 1, value, 0) == -1)
323
- rxml_raise(xmlGetLastError());
324
-
325
- return self;
326
- }
327
-
328
- /*
329
- * call-seq:
330
- * context.disable_cache
331
- *
332
- * Disables an XPath::Context's built-in cache.
333
- */
334
- static VALUE
335
- rxml_xpath_context_disable_cache(VALUE self)
336
- {
337
- xmlXPathContextPtr xctxt;
338
- Data_Get_Struct(self, xmlXPathContext, xctxt);
339
-
340
- if (xmlXPathContextSetCache(xctxt, 0, 0, 0) == -1)
341
- rxml_raise(xmlGetLastError());
342
-
343
- return self;
344
- }
345
- #endif
346
-
347
- void rxml_init_xpath_context(void)
348
- {
349
- cXMLXPathContext = rb_define_class_under(mXPath, "Context", rb_cObject);
350
- rb_define_alloc_func(cXMLXPathContext, rxml_xpath_context_alloc);
351
- rb_define_method(cXMLXPathContext, "doc", rxml_xpath_context_doc, 0);
352
- rb_define_method(cXMLXPathContext, "initialize", rxml_xpath_context_initialize, 1);
353
- rb_define_method(cXMLXPathContext, "register_namespaces", rxml_xpath_context_register_namespaces, 1);
354
- rb_define_method(cXMLXPathContext, "register_namespaces_from_node", rxml_xpath_context_register_namespaces_from_node, 1);
355
- rb_define_method(cXMLXPathContext, "register_namespace", rxml_xpath_context_register_namespace, 2);
356
- rb_define_method(cXMLXPathContext, "node=", rxml_xpath_context_node_set, 1);
357
- rb_define_method(cXMLXPathContext, "find", rxml_xpath_context_find, 1);
358
- #if LIBXML_VERSION >= 20626
359
- rb_define_method(cXMLXPathContext, "enable_cache", rxml_xpath_context_enable_cache, -1);
360
- rb_define_method(cXMLXPathContext, "disable_cache", rxml_xpath_context_disable_cache, 0);
361
- #endif
362
- }
1
+ /* Please see the LICENSE file for copyright and distribution information */
2
+
3
+ #include "ruby_libxml.h"
4
+ #include "ruby_xml_xpath_context.h"
5
+ #include "ruby_xml_xpath_expression.h"
6
+
7
+ #if RUBY_ST_H
8
+ #include <ruby/st.h>
9
+ #else
10
+ #include <st.h>
11
+ #endif
12
+
13
+ #include <libxml/xpathInternals.h>
14
+
15
+ /*
16
+ * Document-class: LibXML::XML::XPath::Context
17
+ *
18
+ * The XML::XPath::Context class is used to evaluate XPath
19
+ * expressions. Generally, you should not directly use this class,
20
+ * but instead use the XML::Document#find and XML::Node#find methods.
21
+ *
22
+ * doc = XML::Document.string('<header>content</header>')
23
+ * context = XPath::Context.new(doc)
24
+ * context.node = doc.root
25
+ * context.register_namespaces_from_node(doc.root)
26
+ * nodes = context.find('/header')
27
+ */
28
+
29
+ VALUE cXMLXPathContext;
30
+
31
+ static void rxml_xpath_context_free(xmlXPathContextPtr ctxt)
32
+ {
33
+ xmlXPathFreeContext(ctxt);
34
+ }
35
+
36
+ static void rxml_xpath_context_mark(xmlXPathContextPtr ctxt)
37
+ {
38
+ VALUE value = (VALUE)ctxt->doc->_private;
39
+ rb_gc_mark(value);
40
+ }
41
+
42
+ static VALUE rxml_xpath_context_alloc(VALUE klass)
43
+ {
44
+ return Data_Wrap_Struct(cXMLXPathContext, rxml_xpath_context_mark, rxml_xpath_context_free, NULL);
45
+ }
46
+
47
+ /* call-seq:
48
+ * XPath::Context.new(doc) -> XPath::Context
49
+ *
50
+ * Creates a new XPath context for the specified document. The
51
+ * context can then be used to evaluate an XPath expression.
52
+ *
53
+ * doc = XML::Document.string('<header><first>hi</first></header>')
54
+ * context = XPath::Context.new(doc)
55
+ * nodes = XPath::Object.new('//first', context)
56
+ * nodes.length == 1
57
+ */
58
+ static VALUE rxml_xpath_context_initialize(VALUE self, VALUE document)
59
+ {
60
+ xmlDocPtr xdoc;
61
+
62
+ if (rb_obj_is_kind_of(document, cXMLDocument) != Qtrue)
63
+ {
64
+ rb_raise(rb_eTypeError, "Supplied argument must be a document or node.");
65
+ }
66
+
67
+ Data_Get_Struct(document, xmlDoc, xdoc);
68
+ DATA_PTR(self) = xmlXPathNewContext(xdoc);
69
+
70
+ return self;
71
+ }
72
+
73
+ /*
74
+ * call-seq:
75
+ * context.doc -> document
76
+ *
77
+ * Obtain the XML::Document this node belongs to.
78
+ */
79
+ static VALUE rxml_xpath_context_doc(VALUE self)
80
+ {
81
+ xmlDocPtr xdoc = NULL;
82
+ xmlXPathContextPtr ctxt;
83
+ Data_Get_Struct(self, xmlXPathContext, ctxt);
84
+
85
+ xdoc = ctxt->doc;
86
+ return rxml_document_wrap(xdoc);
87
+ }
88
+
89
+ /*
90
+ * call-seq:
91
+ * context.register_namespace(prefix, uri) -> (true|false)
92
+ *
93
+ * Register the specified namespace URI with the specified prefix
94
+ * in this context.
95
+
96
+ * context.register_namespace('xi', 'http://www.w3.org/2001/XInclude')
97
+ */
98
+ static VALUE rxml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri)
99
+ {
100
+ xmlXPathContextPtr ctxt;
101
+ Data_Get_Struct(self, xmlXPathContext, ctxt);
102
+
103
+ /* Prefix could be a symbol. */
104
+ prefix = rb_obj_as_string(prefix);
105
+
106
+ if (xmlXPathRegisterNs(ctxt, (xmlChar*) StringValuePtr(prefix),
107
+ (xmlChar*) StringValuePtr(uri)) == 0)
108
+ {
109
+ return (Qtrue);
110
+ }
111
+ else
112
+ {
113
+ /* Should raise an exception, IMHO (whose?, why shouldnt it? -danj)*/
114
+ rb_warning("register namespace failed");
115
+ return (Qfalse);
116
+ }
117
+ }
118
+
119
+ /* call-seq:
120
+ * context.register_namespaces_from_node(node) -> self
121
+ *
122
+ * Helper method to read in namespaces defined on a node.
123
+ *
124
+ * doc = XML::Document.string('<header><first>hi</first></header>')
125
+ * context = XPath::Context.new(doc)
126
+ * context.register_namespaces_from_node(doc.root)
127
+ */
128
+ static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
129
+ VALUE node)
130
+ {
131
+ xmlXPathContextPtr xctxt;
132
+ xmlNodePtr xnode;
133
+ xmlNsPtr *xnsArr;
134
+
135
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
136
+
137
+ if (rb_obj_is_kind_of(node, cXMLDocument) == Qtrue)
138
+ {
139
+ xmlDocPtr xdoc;
140
+ Data_Get_Struct(node, xmlDoc, xdoc);
141
+ xnode = xmlDocGetRootElement(xdoc);
142
+ }
143
+ else if (rb_obj_is_kind_of(node, cXMLNode) == Qtrue)
144
+ {
145
+ Data_Get_Struct(node, xmlNode, xnode);
146
+ }
147
+ else
148
+ {
149
+ rb_raise(rb_eTypeError, "The first argument must be a document or node.");
150
+ }
151
+
152
+ xnsArr = xmlGetNsList(xnode->doc, xnode);
153
+
154
+ if (xnsArr)
155
+ {
156
+ xmlNsPtr xns = *xnsArr;
157
+
158
+ while (xns)
159
+ {
160
+ /* If there is no prefix, then this is the default namespace.
161
+ Skip it for now. */
162
+ if (xns->prefix)
163
+ {
164
+ VALUE prefix = rxml_new_cstr(xns->prefix, xctxt->doc->encoding);
165
+ VALUE uri = rxml_new_cstr(xns->href, xctxt->doc->encoding);
166
+ rxml_xpath_context_register_namespace(self, prefix, uri);
167
+ }
168
+ xns = xns->next;
169
+ }
170
+ xmlFree(xnsArr);
171
+ }
172
+
173
+ return self;
174
+ }
175
+
176
+ static int iterate_ns_hash(VALUE prefix, VALUE uri, VALUE self)
177
+ {
178
+ rxml_xpath_context_register_namespace(self, prefix, uri);
179
+ return ST_CONTINUE;
180
+ }
181
+
182
+ /*
183
+ * call-seq:
184
+ * context.register_namespaces(["prefix:uri"]) -> self
185
+ *
186
+ * Register the specified namespaces in this context. There are
187
+ * three different forms that libxml accepts. These include
188
+ * a string, an array of strings, or a hash table:
189
+ *
190
+ * context.register_namespaces('xi:http://www.w3.org/2001/XInclude')
191
+ * context.register_namespaces(['xlink:http://www.w3.org/1999/xlink',
192
+ * 'xi:http://www.w3.org/2001/XInclude')
193
+ * context.register_namespaces('xlink' => 'http://www.w3.org/1999/xlink',
194
+ * 'xi' => 'http://www.w3.org/2001/XInclude')
195
+ */
196
+ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
197
+ {
198
+ char *cp;
199
+ long i;
200
+ VALUE rprefix, ruri;
201
+ xmlXPathContextPtr xctxt;
202
+
203
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
204
+
205
+ /* Need to loop through the 2nd argument and iterate through the
206
+ * list of namespaces that we want to allow */
207
+ switch (TYPE(nslist))
208
+ {
209
+ case T_STRING:
210
+ cp = strchr(StringValuePtr(nslist), (int) ':');
211
+ if (cp == NULL)
212
+ {
213
+ rprefix = nslist;
214
+ ruri = Qnil;
215
+ }
216
+ else
217
+ {
218
+ rprefix = rb_str_new(StringValuePtr(nslist), (long) ((intptr_t) cp - (intptr_t)StringValuePtr(nslist)));
219
+ ruri = rxml_new_cstr((const xmlChar*)&cp[1], xctxt->doc->encoding);
220
+ }
221
+ /* Should test the results of this */
222
+ rxml_xpath_context_register_namespace(self, rprefix, ruri);
223
+ break;
224
+ case T_ARRAY:
225
+ for (i = 0; i < RARRAY_LEN(nslist); i++)
226
+ {
227
+ rxml_xpath_context_register_namespaces(self, RARRAY_PTR(nslist)[i]);
228
+ }
229
+ break;
230
+ case T_HASH:
231
+ rb_hash_foreach(nslist, iterate_ns_hash, self);
232
+ break;
233
+ default:
234
+ rb_raise(
235
+ rb_eArgError,
236
+ "Invalid argument type, only accept string, array of strings, or an array of arrays");
237
+ }
238
+ return self;
239
+ }
240
+
241
+ /*
242
+ * call-seq:
243
+ * context.node = node
244
+ *
245
+ * Set the current node used by the XPath engine
246
+
247
+ * doc = XML::Document.string('<header><first>hi</first></header>')
248
+ * context.node = doc.root.first
249
+ */
250
+ static VALUE rxml_xpath_context_node_set(VALUE self, VALUE node)
251
+ {
252
+ xmlXPathContextPtr xctxt;
253
+ xmlNodePtr xnode;
254
+
255
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
256
+ Data_Get_Struct(node, xmlNode, xnode);
257
+ xctxt->node = xnode;
258
+ return node;
259
+ }
260
+
261
+ /*
262
+ * call-seq:
263
+ * context.find("xpath") -> true|false|number|string|XML::XPath::Object
264
+ *
265
+ * Executes the provided xpath function. The result depends on the execution
266
+ * of the xpath statement. It may be true, false, a number, a string or
267
+ * a node set.
268
+ */
269
+ static VALUE rxml_xpath_context_find(VALUE self, VALUE xpath_expr)
270
+ {
271
+ xmlXPathContextPtr xctxt;
272
+ xmlXPathObjectPtr xobject;
273
+ xmlXPathCompExprPtr xcompexpr;
274
+
275
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
276
+
277
+ if (TYPE(xpath_expr) == T_STRING)
278
+ {
279
+ VALUE expression = rb_check_string_type(xpath_expr);
280
+ xobject = xmlXPathEval((xmlChar*) StringValueCStr(expression), xctxt);
281
+ }
282
+ else if (rb_obj_is_kind_of(xpath_expr, cXMLXPathExpression))
283
+ {
284
+ Data_Get_Struct(xpath_expr, xmlXPathCompExpr, xcompexpr);
285
+ xobject = xmlXPathCompiledEval(xcompexpr, xctxt);
286
+ }
287
+ else
288
+ {
289
+ rb_raise(rb_eTypeError,
290
+ "Argument should be an instance of a String or XPath::Expression");
291
+ }
292
+
293
+ return rxml_xpath_to_value(xctxt, xobject);
294
+ }
295
+
296
+ #if LIBXML_VERSION >= 20626
297
+ /*
298
+ * call-seq:
299
+ * context.enable_cache(size = nil)
300
+ *
301
+ * Enables an XPath::Context's built-in cache. If the cache is
302
+ * enabled then XPath objects will be cached internally for reuse.
303
+ * The size parameter controls sets the maximum number of XPath objects
304
+ * that will be cached per XPath object type (node-set, string, number,
305
+ * boolean, and misc objects). Set size to nil to use the default
306
+ * cache size of 100.
307
+ */
308
+ static VALUE
309
+ rxml_xpath_context_enable_cache(int argc, VALUE *argv, VALUE self)
310
+ {
311
+ xmlXPathContextPtr xctxt;
312
+ VALUE size;
313
+ int value = -1;
314
+
315
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
316
+
317
+ if (rb_scan_args(argc, argv, "01", &size) == 1)
318
+ {
319
+ value = NUM2INT(size);
320
+ }
321
+
322
+ if (xmlXPathContextSetCache(xctxt, 1, value, 0) == -1)
323
+ rxml_raise(xmlGetLastError());
324
+
325
+ return self;
326
+ }
327
+
328
+ /*
329
+ * call-seq:
330
+ * context.disable_cache
331
+ *
332
+ * Disables an XPath::Context's built-in cache.
333
+ */
334
+ static VALUE
335
+ rxml_xpath_context_disable_cache(VALUE self)
336
+ {
337
+ xmlXPathContextPtr xctxt;
338
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
339
+
340
+ if (xmlXPathContextSetCache(xctxt, 0, 0, 0) == -1)
341
+ rxml_raise(xmlGetLastError());
342
+
343
+ return self;
344
+ }
345
+ #endif
346
+
347
+ void rxml_init_xpath_context(void)
348
+ {
349
+ cXMLXPathContext = rb_define_class_under(mXPath, "Context", rb_cObject);
350
+ rb_define_alloc_func(cXMLXPathContext, rxml_xpath_context_alloc);
351
+ rb_define_method(cXMLXPathContext, "doc", rxml_xpath_context_doc, 0);
352
+ rb_define_method(cXMLXPathContext, "initialize", rxml_xpath_context_initialize, 1);
353
+ rb_define_method(cXMLXPathContext, "register_namespaces", rxml_xpath_context_register_namespaces, 1);
354
+ rb_define_method(cXMLXPathContext, "register_namespaces_from_node", rxml_xpath_context_register_namespaces_from_node, 1);
355
+ rb_define_method(cXMLXPathContext, "register_namespace", rxml_xpath_context_register_namespace, 2);
356
+ rb_define_method(cXMLXPathContext, "node=", rxml_xpath_context_node_set, 1);
357
+ rb_define_method(cXMLXPathContext, "find", rxml_xpath_context_find, 1);
358
+ #if LIBXML_VERSION >= 20626
359
+ rb_define_method(cXMLXPathContext, "enable_cache", rxml_xpath_context_enable_cache, -1);
360
+ rb_define_method(cXMLXPathContext, "disable_cache", rxml_xpath_context_disable_cache, 0);
361
+ #endif
362
+ }