nokogiri-fitzsimmons 1.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. data/.autotest +26 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.ja.rdoc +666 -0
  4. data/CHANGELOG.rdoc +659 -0
  5. data/C_CODING_STYLE.rdoc +33 -0
  6. data/Manifest.txt +295 -0
  7. data/README.ja.rdoc +106 -0
  8. data/README.rdoc +178 -0
  9. data/ROADMAP.md +86 -0
  10. data/Rakefile +194 -0
  11. data/STANDARD_RESPONSES.md +47 -0
  12. data/Y_U_NO_GEMSPEC.md +155 -0
  13. data/bin/nokogiri +63 -0
  14. data/build_all +58 -0
  15. data/ext/nokogiri/depend +358 -0
  16. data/ext/nokogiri/extconf.rb +142 -0
  17. data/ext/nokogiri/html_document.c +170 -0
  18. data/ext/nokogiri/html_document.h +10 -0
  19. data/ext/nokogiri/html_element_description.c +276 -0
  20. data/ext/nokogiri/html_element_description.h +10 -0
  21. data/ext/nokogiri/html_entity_lookup.c +32 -0
  22. data/ext/nokogiri/html_entity_lookup.h +8 -0
  23. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  24. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  25. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  26. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  27. data/ext/nokogiri/nokogiri.c +133 -0
  28. data/ext/nokogiri/nokogiri.h +160 -0
  29. data/ext/nokogiri/xml_attr.c +94 -0
  30. data/ext/nokogiri/xml_attr.h +9 -0
  31. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  32. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  33. data/ext/nokogiri/xml_cdata.c +56 -0
  34. data/ext/nokogiri/xml_cdata.h +9 -0
  35. data/ext/nokogiri/xml_comment.c +54 -0
  36. data/ext/nokogiri/xml_comment.h +9 -0
  37. data/ext/nokogiri/xml_document.c +576 -0
  38. data/ext/nokogiri/xml_document.h +23 -0
  39. data/ext/nokogiri/xml_document_fragment.c +48 -0
  40. data/ext/nokogiri/xml_document_fragment.h +10 -0
  41. data/ext/nokogiri/xml_dtd.c +202 -0
  42. data/ext/nokogiri/xml_dtd.h +10 -0
  43. data/ext/nokogiri/xml_element_content.c +123 -0
  44. data/ext/nokogiri/xml_element_content.h +10 -0
  45. data/ext/nokogiri/xml_element_decl.c +69 -0
  46. data/ext/nokogiri/xml_element_decl.h +9 -0
  47. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  48. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  49. data/ext/nokogiri/xml_entity_decl.c +110 -0
  50. data/ext/nokogiri/xml_entity_decl.h +10 -0
  51. data/ext/nokogiri/xml_entity_reference.c +52 -0
  52. data/ext/nokogiri/xml_entity_reference.h +9 -0
  53. data/ext/nokogiri/xml_io.c +56 -0
  54. data/ext/nokogiri/xml_io.h +11 -0
  55. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  56. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  57. data/ext/nokogiri/xml_namespace.c +78 -0
  58. data/ext/nokogiri/xml_namespace.h +13 -0
  59. data/ext/nokogiri/xml_node.c +1480 -0
  60. data/ext/nokogiri/xml_node.h +13 -0
  61. data/ext/nokogiri/xml_node_set.c +467 -0
  62. data/ext/nokogiri/xml_node_set.h +14 -0
  63. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  64. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  65. data/ext/nokogiri/xml_reader.c +684 -0
  66. data/ext/nokogiri/xml_reader.h +10 -0
  67. data/ext/nokogiri/xml_relax_ng.c +161 -0
  68. data/ext/nokogiri/xml_relax_ng.h +9 -0
  69. data/ext/nokogiri/xml_sax_parser.c +293 -0
  70. data/ext/nokogiri/xml_sax_parser.h +39 -0
  71. data/ext/nokogiri/xml_sax_parser_context.c +222 -0
  72. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  73. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  74. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  75. data/ext/nokogiri/xml_schema.c +205 -0
  76. data/ext/nokogiri/xml_schema.h +9 -0
  77. data/ext/nokogiri/xml_syntax_error.c +58 -0
  78. data/ext/nokogiri/xml_syntax_error.h +13 -0
  79. data/ext/nokogiri/xml_text.c +52 -0
  80. data/ext/nokogiri/xml_text.h +9 -0
  81. data/ext/nokogiri/xml_xpath_context.c +319 -0
  82. data/ext/nokogiri/xml_xpath_context.h +10 -0
  83. data/ext/nokogiri/xslt_stylesheet.c +270 -0
  84. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  85. data/lib/nokogiri.rb +127 -0
  86. data/lib/nokogiri/css.rb +27 -0
  87. data/lib/nokogiri/css/node.rb +102 -0
  88. data/lib/nokogiri/css/parser.rb +720 -0
  89. data/lib/nokogiri/css/parser.y +258 -0
  90. data/lib/nokogiri/css/parser_extras.rb +91 -0
  91. data/lib/nokogiri/css/syntax_error.rb +7 -0
  92. data/lib/nokogiri/css/tokenizer.rb +152 -0
  93. data/lib/nokogiri/css/tokenizer.rex +55 -0
  94. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  95. data/lib/nokogiri/decorators/slop.rb +35 -0
  96. data/lib/nokogiri/html.rb +37 -0
  97. data/lib/nokogiri/html/builder.rb +35 -0
  98. data/lib/nokogiri/html/document.rb +254 -0
  99. data/lib/nokogiri/html/document_fragment.rb +41 -0
  100. data/lib/nokogiri/html/element_description.rb +23 -0
  101. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  102. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  103. data/lib/nokogiri/html/sax/parser.rb +52 -0
  104. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  105. data/lib/nokogiri/html/sax/push_parser.rb +16 -0
  106. data/lib/nokogiri/syntax_error.rb +4 -0
  107. data/lib/nokogiri/version.rb +88 -0
  108. data/lib/nokogiri/xml.rb +73 -0
  109. data/lib/nokogiri/xml/attr.rb +14 -0
  110. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  111. data/lib/nokogiri/xml/builder.rb +431 -0
  112. data/lib/nokogiri/xml/cdata.rb +11 -0
  113. data/lib/nokogiri/xml/character_data.rb +7 -0
  114. data/lib/nokogiri/xml/document.rb +267 -0
  115. data/lib/nokogiri/xml/document_fragment.rb +103 -0
  116. data/lib/nokogiri/xml/dtd.rb +22 -0
  117. data/lib/nokogiri/xml/element_content.rb +36 -0
  118. data/lib/nokogiri/xml/element_decl.rb +13 -0
  119. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  120. data/lib/nokogiri/xml/namespace.rb +13 -0
  121. data/lib/nokogiri/xml/node.rb +946 -0
  122. data/lib/nokogiri/xml/node/save_options.rb +61 -0
  123. data/lib/nokogiri/xml/node_set.rb +357 -0
  124. data/lib/nokogiri/xml/notation.rb +6 -0
  125. data/lib/nokogiri/xml/parse_options.rb +98 -0
  126. data/lib/nokogiri/xml/pp.rb +2 -0
  127. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  128. data/lib/nokogiri/xml/pp/node.rb +56 -0
  129. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  130. data/lib/nokogiri/xml/reader.rb +112 -0
  131. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  132. data/lib/nokogiri/xml/sax.rb +4 -0
  133. data/lib/nokogiri/xml/sax/document.rb +164 -0
  134. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  135. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  136. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  137. data/lib/nokogiri/xml/schema.rb +63 -0
  138. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  139. data/lib/nokogiri/xml/text.rb +9 -0
  140. data/lib/nokogiri/xml/xpath.rb +10 -0
  141. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  142. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  143. data/lib/nokogiri/xslt.rb +56 -0
  144. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  145. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  146. data/tasks/cross_compile.rb +153 -0
  147. data/tasks/nokogiri.org.rb +24 -0
  148. data/tasks/test.rb +95 -0
  149. data/test/css/test_nthiness.rb +159 -0
  150. data/test/css/test_parser.rb +341 -0
  151. data/test/css/test_tokenizer.rb +198 -0
  152. data/test/css/test_xpath_visitor.rb +91 -0
  153. data/test/decorators/test_slop.rb +16 -0
  154. data/test/files/2ch.html +108 -0
  155. data/test/files/address_book.rlx +12 -0
  156. data/test/files/address_book.xml +10 -0
  157. data/test/files/bar/bar.xsd +4 -0
  158. data/test/files/dont_hurt_em_why.xml +422 -0
  159. data/test/files/encoding.html +82 -0
  160. data/test/files/encoding.xhtml +84 -0
  161. data/test/files/exslt.xml +8 -0
  162. data/test/files/exslt.xslt +35 -0
  163. data/test/files/foo/foo.xsd +4 -0
  164. data/test/files/metacharset.html +10 -0
  165. data/test/files/noencoding.html +47 -0
  166. data/test/files/po.xml +32 -0
  167. data/test/files/po.xsd +66 -0
  168. data/test/files/shift_jis.html +10 -0
  169. data/test/files/shift_jis.xml +5 -0
  170. data/test/files/snuggles.xml +3 -0
  171. data/test/files/staff.dtd +10 -0
  172. data/test/files/staff.xml +59 -0
  173. data/test/files/staff.xslt +32 -0
  174. data/test/files/tlm.html +850 -0
  175. data/test/files/to_be_xincluded.xml +2 -0
  176. data/test/files/valid_bar.xml +2 -0
  177. data/test/files/xinclude.xml +4 -0
  178. data/test/helper.rb +147 -0
  179. data/test/html/sax/test_parser.rb +138 -0
  180. data/test/html/sax/test_parser_context.rb +46 -0
  181. data/test/html/test_builder.rb +164 -0
  182. data/test/html/test_document.rb +529 -0
  183. data/test/html/test_document_encoding.rb +138 -0
  184. data/test/html/test_document_fragment.rb +254 -0
  185. data/test/html/test_element_description.rb +100 -0
  186. data/test/html/test_named_characters.rb +14 -0
  187. data/test/html/test_node.rb +188 -0
  188. data/test/html/test_node_encoding.rb +27 -0
  189. data/test/test_convert_xpath.rb +135 -0
  190. data/test/test_css_cache.rb +45 -0
  191. data/test/test_encoding_handler.rb +46 -0
  192. data/test/test_memory_leak.rb +152 -0
  193. data/test/test_nokogiri.rb +132 -0
  194. data/test/test_reader.rb +488 -0
  195. data/test/test_soap4r_sax.rb +52 -0
  196. data/test/test_xslt_transforms.rb +254 -0
  197. data/test/xml/node/test_save_options.rb +28 -0
  198. data/test/xml/node/test_subclass.rb +44 -0
  199. data/test/xml/sax/test_parser.rb +338 -0
  200. data/test/xml/sax/test_parser_context.rb +106 -0
  201. data/test/xml/sax/test_push_parser.rb +157 -0
  202. data/test/xml/test_attr.rb +64 -0
  203. data/test/xml/test_attribute_decl.rb +86 -0
  204. data/test/xml/test_builder.rb +248 -0
  205. data/test/xml/test_c14n.rb +151 -0
  206. data/test/xml/test_cdata.rb +48 -0
  207. data/test/xml/test_comment.rb +29 -0
  208. data/test/xml/test_document.rb +742 -0
  209. data/test/xml/test_document_encoding.rb +28 -0
  210. data/test/xml/test_document_fragment.rb +216 -0
  211. data/test/xml/test_dtd.rb +103 -0
  212. data/test/xml/test_dtd_encoding.rb +33 -0
  213. data/test/xml/test_element_content.rb +56 -0
  214. data/test/xml/test_element_decl.rb +73 -0
  215. data/test/xml/test_entity_decl.rb +122 -0
  216. data/test/xml/test_entity_reference.rb +235 -0
  217. data/test/xml/test_namespace.rb +75 -0
  218. data/test/xml/test_node.rb +1029 -0
  219. data/test/xml/test_node_attributes.rb +53 -0
  220. data/test/xml/test_node_encoding.rb +107 -0
  221. data/test/xml/test_node_inheritance.rb +32 -0
  222. data/test/xml/test_node_reparenting.rb +374 -0
  223. data/test/xml/test_node_set.rb +755 -0
  224. data/test/xml/test_parse_options.rb +64 -0
  225. data/test/xml/test_processing_instruction.rb +30 -0
  226. data/test/xml/test_reader_encoding.rb +142 -0
  227. data/test/xml/test_relax_ng.rb +60 -0
  228. data/test/xml/test_schema.rb +94 -0
  229. data/test/xml/test_syntax_error.rb +12 -0
  230. data/test/xml/test_text.rb +45 -0
  231. data/test/xml/test_unparented_node.rb +413 -0
  232. data/test/xml/test_xinclude.rb +83 -0
  233. data/test/xml/test_xpath.rb +295 -0
  234. data/test/xslt/test_custom_functions.rb +129 -0
  235. data/test/xslt/test_exception_handling.rb +37 -0
  236. data/test_all +84 -0
  237. metadata +534 -0
@@ -0,0 +1,9 @@
1
+ #ifndef NOKOGIRI_XML_SCHEMA
2
+ #define NOKOGIRI_XML_SCHEMA
3
+
4
+ #include <nokogiri.h>
5
+
6
+ void init_xml_schema();
7
+
8
+ extern VALUE cNokogiriXmlSchema;
9
+ #endif
@@ -0,0 +1,58 @@
1
+ #include <xml_syntax_error.h>
2
+
3
+ void Nokogiri_error_array_pusher(void * ctx, xmlErrorPtr error)
4
+ {
5
+ VALUE list = (VALUE)ctx;
6
+ rb_ary_push(list, Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error));
7
+ }
8
+
9
+ void Nokogiri_error_raise(void * ctx, xmlErrorPtr error)
10
+ {
11
+ rb_exc_raise(Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error));
12
+ }
13
+
14
+ VALUE Nokogiri_wrap_xml_syntax_error(VALUE klass, xmlErrorPtr error)
15
+ {
16
+ VALUE msg, e;
17
+
18
+ if(!klass) klass = cNokogiriXmlSyntaxError;
19
+
20
+ msg = (error && error->message) ? NOKOGIRI_STR_NEW2(error->message) : Qnil;
21
+
22
+ e = rb_class_new_instance(
23
+ 1,
24
+ &msg,
25
+ klass
26
+ );
27
+
28
+ if (error)
29
+ {
30
+ rb_iv_set(e, "@domain", INT2NUM(error->domain));
31
+ rb_iv_set(e, "@code", INT2NUM(error->code));
32
+ rb_iv_set(e, "@level", INT2NUM((short)error->level));
33
+ rb_iv_set(e, "@file", RBSTR_OR_QNIL(error->file));
34
+ rb_iv_set(e, "@line", INT2NUM(error->line));
35
+ rb_iv_set(e, "@str1", RBSTR_OR_QNIL(error->str1));
36
+ rb_iv_set(e, "@str2", RBSTR_OR_QNIL(error->str2));
37
+ rb_iv_set(e, "@str3", RBSTR_OR_QNIL(error->str3));
38
+ rb_iv_set(e, "@int1", INT2NUM(error->int1));
39
+ rb_iv_set(e, "@column", INT2NUM(error->int2));
40
+ }
41
+
42
+ return e;
43
+ }
44
+
45
+ VALUE cNokogiriXmlSyntaxError;
46
+ void init_xml_syntax_error()
47
+ {
48
+ VALUE nokogiri = rb_define_module("Nokogiri");
49
+ VALUE xml = rb_define_module_under(nokogiri, "XML");
50
+
51
+ /*
52
+ * The XML::SyntaxError is raised on parse errors
53
+ */
54
+ VALUE syntax_error_mommy = rb_define_class_under(nokogiri, "SyntaxError", rb_eStandardError);
55
+ VALUE klass = rb_define_class_under(xml, "SyntaxError", syntax_error_mommy);
56
+ cNokogiriXmlSyntaxError = klass;
57
+
58
+ }
@@ -0,0 +1,13 @@
1
+ #ifndef NOKOGIRI_XML_SYNTAX_ERROR
2
+ #define NOKOGIRI_XML_SYNTAX_ERROR
3
+
4
+ #include <nokogiri.h>
5
+
6
+ void init_xml_syntax_error();
7
+ VALUE Nokogiri_wrap_xml_syntax_error(VALUE klass, xmlErrorPtr error);
8
+ void Nokogiri_error_array_pusher(void * ctx, xmlErrorPtr error);
9
+ NORETURN(void Nokogiri_error_raise(void * ctx, xmlErrorPtr error));
10
+
11
+ extern VALUE cNokogiriXmlSyntaxError;
12
+ #endif
13
+
@@ -0,0 +1,52 @@
1
+ #include <xml_text.h>
2
+
3
+ /*
4
+ * call-seq:
5
+ * new(content, document)
6
+ *
7
+ * Create a new Text element on the +document+ with +content+
8
+ */
9
+ static VALUE new(int argc, VALUE *argv, VALUE klass)
10
+ {
11
+ xmlDocPtr doc;
12
+ xmlNodePtr node;
13
+ VALUE string;
14
+ VALUE document;
15
+ VALUE rest;
16
+ VALUE rb_node;
17
+
18
+ rb_scan_args(argc, argv, "2*", &string, &document, &rest);
19
+
20
+ Data_Get_Struct(document, xmlDoc, doc);
21
+
22
+ node = xmlNewText((xmlChar *)StringValuePtr(string));
23
+ node->doc = doc->doc;
24
+
25
+ nokogiri_root_node(node);
26
+
27
+ rb_node = Nokogiri_wrap_xml_node(klass, node) ;
28
+ rb_obj_call_init(rb_node, argc, argv);
29
+
30
+ if(rb_block_given_p()) rb_yield(rb_node);
31
+
32
+ return rb_node;
33
+ }
34
+
35
+ VALUE cNokogiriXmlText ;
36
+ void init_xml_text()
37
+ {
38
+ VALUE nokogiri = rb_define_module("Nokogiri");
39
+ VALUE xml = rb_define_module_under(nokogiri, "XML");
40
+ /* */
41
+ VALUE node = rb_define_class_under(xml, "Node", rb_cObject);
42
+ VALUE char_data = rb_define_class_under(xml, "CharacterData", node);
43
+
44
+ /*
45
+ * Wraps Text nodes.
46
+ */
47
+ VALUE klass = rb_define_class_under(xml, "Text", char_data);
48
+
49
+ cNokogiriXmlText = klass;
50
+
51
+ rb_define_singleton_method(klass, "new", new, -1);
52
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef NOKOGIRI_XML_TEXT
2
+ #define NOKOGIRI_XML_TEXT
3
+
4
+ #include <nokogiri.h>
5
+
6
+ void init_xml_text();
7
+
8
+ extern VALUE cNokogiriXmlText ;
9
+ #endif
@@ -0,0 +1,319 @@
1
+ #include <xml_xpath_context.h>
2
+
3
+ int vasprintf (char **strp, const char *fmt, va_list ap);
4
+
5
+ static void deallocate(xmlXPathContextPtr ctx)
6
+ {
7
+ NOKOGIRI_DEBUG_START(ctx);
8
+ xmlXPathFreeContext(ctx);
9
+ NOKOGIRI_DEBUG_END(ctx);
10
+ }
11
+
12
+ /*
13
+ * call-seq:
14
+ * register_ns(prefix, uri)
15
+ *
16
+ * Register the namespace with +prefix+ and +uri+.
17
+ */
18
+ static VALUE register_ns(VALUE self, VALUE prefix, VALUE uri)
19
+ {
20
+ xmlXPathContextPtr ctx;
21
+ Data_Get_Struct(self, xmlXPathContext, ctx);
22
+
23
+ xmlXPathRegisterNs( ctx,
24
+ (const xmlChar *)StringValuePtr(prefix),
25
+ (const xmlChar *)StringValuePtr(uri)
26
+ );
27
+ return self;
28
+ }
29
+
30
+ /*
31
+ * call-seq:
32
+ * register_variable(name, value)
33
+ *
34
+ * Register the variable +name+ with +value+.
35
+ */
36
+ static VALUE register_variable(VALUE self, VALUE name, VALUE value)
37
+ {
38
+ xmlXPathContextPtr ctx;
39
+ xmlXPathObjectPtr xmlValue;
40
+ Data_Get_Struct(self, xmlXPathContext, ctx);
41
+
42
+ xmlValue = xmlXPathNewCString(StringValuePtr(value));
43
+
44
+ xmlXPathRegisterVariable( ctx,
45
+ (const xmlChar *)StringValuePtr(name),
46
+ xmlValue
47
+ );
48
+
49
+ return self;
50
+ }
51
+
52
+ void Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, int nargs, VALUE handler, const char* function_name)
53
+ {
54
+ int i;
55
+ VALUE result, doc;
56
+ VALUE *argv;
57
+ VALUE node_set = Qnil;
58
+ xmlNodeSetPtr xml_node_set = NULL;
59
+ xmlXPathObjectPtr obj;
60
+ nokogiriNodeSetTuple *node_set_tuple;
61
+
62
+ assert(ctx->context->doc);
63
+ assert(DOC_RUBY_OBJECT_TEST(ctx->context->doc));
64
+
65
+ argv = (VALUE *)calloc((size_t)nargs, sizeof(VALUE));
66
+ for (i = 0 ; i < nargs ; ++i) {
67
+ rb_gc_register_address(&argv[i]);
68
+ }
69
+
70
+ doc = DOC_RUBY_OBJECT(ctx->context->doc);
71
+
72
+ if (nargs > 0) {
73
+ i = nargs - 1;
74
+ do {
75
+ obj = valuePop(ctx);
76
+ switch(obj->type) {
77
+ case XPATH_STRING:
78
+ argv[i] = NOKOGIRI_STR_NEW2(obj->stringval);
79
+ break;
80
+ case XPATH_BOOLEAN:
81
+ argv[i] = obj->boolval == 1 ? Qtrue : Qfalse;
82
+ break;
83
+ case XPATH_NUMBER:
84
+ argv[i] = rb_float_new(obj->floatval);
85
+ break;
86
+ case XPATH_NODESET:
87
+ argv[i] = Nokogiri_wrap_xml_node_set(obj->nodesetval, doc);
88
+ break;
89
+ default:
90
+ argv[i] = NOKOGIRI_STR_NEW2(xmlXPathCastToString(obj));
91
+ }
92
+ xmlXPathFreeNodeSetList(obj);
93
+ } while(i-- > 0);
94
+ }
95
+
96
+ result = rb_funcall2(handler, rb_intern((const char*)function_name), nargs, argv);
97
+
98
+ for (i = 0 ; i < nargs ; ++i) {
99
+ rb_gc_unregister_address(&argv[i]);
100
+ }
101
+ free(argv);
102
+
103
+ switch(TYPE(result)) {
104
+ case T_FLOAT:
105
+ case T_BIGNUM:
106
+ case T_FIXNUM:
107
+ xmlXPathReturnNumber(ctx, NUM2DBL(result));
108
+ break;
109
+ case T_STRING:
110
+ xmlXPathReturnString(
111
+ ctx,
112
+ xmlCharStrdup(StringValuePtr(result))
113
+ );
114
+ break;
115
+ case T_TRUE:
116
+ xmlXPathReturnTrue(ctx);
117
+ break;
118
+ case T_FALSE:
119
+ xmlXPathReturnFalse(ctx);
120
+ break;
121
+ case T_NIL:
122
+ break;
123
+ case T_ARRAY:
124
+ {
125
+ VALUE args[2];
126
+ args[0] = doc;
127
+ args[1] = result;
128
+ node_set = rb_class_new_instance(2, args, cNokogiriXmlNodeSet);
129
+ Data_Get_Struct(node_set, nokogiriNodeSetTuple, node_set_tuple);
130
+ xml_node_set = node_set_tuple->node_set;
131
+ xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
132
+ }
133
+ break;
134
+ case T_DATA:
135
+ if(rb_obj_is_kind_of(result, cNokogiriXmlNodeSet)) {
136
+ Data_Get_Struct(result, nokogiriNodeSetTuple, node_set_tuple);
137
+ xml_node_set = node_set_tuple->node_set;
138
+ /* Copy the node set, otherwise it will get GC'd. */
139
+ xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
140
+ break;
141
+ }
142
+ default:
143
+ rb_raise(rb_eRuntimeError, "Invalid return type");
144
+ }
145
+ }
146
+
147
+ static void ruby_funcall(xmlXPathParserContextPtr ctx, int nargs)
148
+ {
149
+ VALUE handler = Qnil;
150
+ const char *function = NULL ;
151
+
152
+ assert(ctx);
153
+ assert(ctx->context);
154
+ assert(ctx->context->userData);
155
+ assert(ctx->context->function);
156
+
157
+ handler = (VALUE)(ctx->context->userData);
158
+ function = (const char*)(ctx->context->function);
159
+
160
+ Nokogiri_marshal_xpath_funcall_and_return_values(ctx, nargs, handler, function);
161
+ }
162
+
163
+ static xmlXPathFunction lookup( void *ctx,
164
+ const xmlChar * name,
165
+ const xmlChar* ns_uri )
166
+ {
167
+ VALUE xpath_handler = (VALUE)ctx;
168
+ if(rb_respond_to(xpath_handler, rb_intern((const char *)name)))
169
+ return ruby_funcall;
170
+
171
+ return NULL;
172
+ }
173
+
174
+ NORETURN(static void xpath_exception_handler(void * ctx, xmlErrorPtr error));
175
+ static void xpath_exception_handler(void * ctx, xmlErrorPtr error)
176
+ {
177
+ VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath"));
178
+ VALUE klass = rb_const_get(xpath, rb_intern("SyntaxError"));
179
+
180
+ rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error));
181
+ }
182
+
183
+ NORETURN(static void xpath_generic_exception_handler(void * ctx, const char *msg, ...));
184
+ static void xpath_generic_exception_handler(void * ctx, const char *msg, ...)
185
+ {
186
+ char * message;
187
+
188
+ va_list args;
189
+ va_start(args, msg);
190
+ vasprintf(&message, msg, args);
191
+ va_end(args);
192
+
193
+ rb_raise(rb_eRuntimeError, "%s", message);
194
+ }
195
+
196
+ /*
197
+ * call-seq:
198
+ * evaluate(search_path, handler = nil)
199
+ *
200
+ * Evaluate the +search_path+ returning an XML::XPath object.
201
+ */
202
+ static VALUE evaluate(int argc, VALUE *argv, VALUE self)
203
+ {
204
+ VALUE search_path, xpath_handler;
205
+ VALUE thing = Qnil;
206
+ xmlXPathContextPtr ctx;
207
+ xmlXPathObjectPtr xpath;
208
+ xmlChar *query;
209
+
210
+ Data_Get_Struct(self, xmlXPathContext, ctx);
211
+
212
+ if(rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1)
213
+ xpath_handler = Qnil;
214
+
215
+ query = (xmlChar *)StringValuePtr(search_path);
216
+
217
+ if(Qnil != xpath_handler) {
218
+ /* FIXME: not sure if this is the correct place to shove private data. */
219
+ ctx->userData = (void *)xpath_handler;
220
+ xmlXPathRegisterFuncLookup(ctx, lookup, (void *)xpath_handler);
221
+ }
222
+
223
+ xmlResetLastError();
224
+ xmlSetStructuredErrorFunc(NULL, xpath_exception_handler);
225
+
226
+ /* For some reason, xmlXPathEvalExpression will blow up with a generic error */
227
+ /* when there is a non existent function. */
228
+ xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler);
229
+
230
+ xpath = xmlXPathEvalExpression(query, ctx);
231
+ xmlSetStructuredErrorFunc(NULL, NULL);
232
+ xmlSetGenericErrorFunc(NULL, NULL);
233
+
234
+ if(xpath == NULL) {
235
+ VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath"));
236
+ VALUE klass = rb_const_get(xpath, rb_intern("SyntaxError"));
237
+
238
+ xmlErrorPtr error = xmlGetLastError();
239
+ rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error));
240
+ }
241
+
242
+ assert(ctx->doc);
243
+ assert(DOC_RUBY_OBJECT_TEST(ctx->doc));
244
+
245
+ switch(xpath->type) {
246
+ case XPATH_STRING:
247
+ thing = NOKOGIRI_STR_NEW2(xpath->stringval);
248
+ xmlFree(xpath->stringval);
249
+ break;
250
+ case XPATH_NODESET:
251
+ if(NULL == xpath->nodesetval) {
252
+ thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
253
+ DOC_RUBY_OBJECT(ctx->doc));
254
+ } else {
255
+ thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval,
256
+ DOC_RUBY_OBJECT(ctx->doc));
257
+ }
258
+ break;
259
+ case XPATH_NUMBER:
260
+ thing = rb_float_new(xpath->floatval);
261
+ break;
262
+ case XPATH_BOOLEAN:
263
+ thing = xpath->boolval == 1 ? Qtrue : Qfalse;
264
+ break;
265
+ default:
266
+ thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
267
+ DOC_RUBY_OBJECT(ctx->doc));
268
+ }
269
+
270
+ xmlXPathFreeNodeSetList(xpath);
271
+
272
+ return thing;
273
+ }
274
+
275
+ /*
276
+ * call-seq:
277
+ * new(node)
278
+ *
279
+ * Create a new XPathContext with +node+ as the reference point.
280
+ */
281
+ static VALUE new(VALUE klass, VALUE nodeobj)
282
+ {
283
+ xmlNodePtr node;
284
+ xmlXPathContextPtr ctx;
285
+ VALUE self;
286
+
287
+ xmlXPathInit();
288
+
289
+ Data_Get_Struct(nodeobj, xmlNode, node);
290
+
291
+ ctx = xmlXPathNewContext(node->doc);
292
+ ctx->node = node;
293
+ self = Data_Wrap_Struct(klass, 0, deallocate, ctx);
294
+ /*rb_iv_set(self, "@xpath_handler", Qnil); */
295
+ return self;
296
+ }
297
+
298
+ VALUE cNokogiriXmlXpathContext;
299
+ void init_xml_xpath_context(void)
300
+ {
301
+ VALUE module = rb_define_module("Nokogiri");
302
+
303
+ /*
304
+ * Nokogiri::XML
305
+ */
306
+ VALUE xml = rb_define_module_under(module, "XML");
307
+
308
+ /*
309
+ * XPathContext is the entry point for searching a Document by using XPath.
310
+ */
311
+ VALUE klass = rb_define_class_under(xml, "XPathContext", rb_cObject);
312
+
313
+ cNokogiriXmlXpathContext = klass;
314
+
315
+ rb_define_singleton_method(klass, "new", new, 1);
316
+ rb_define_method(klass, "evaluate", evaluate, -1);
317
+ rb_define_method(klass, "register_variable", register_variable, 2);
318
+ rb_define_method(klass, "register_ns", register_ns, 2);
319
+ }