nokogiri-fitzsimmons 1.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/.gemtest +0 -0
- data/CHANGELOG.ja.rdoc +666 -0
- data/CHANGELOG.rdoc +659 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Manifest.txt +295 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +178 -0
- data/ROADMAP.md +86 -0
- data/Rakefile +194 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +63 -0
- data/build_all +58 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +142 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +133 -0
- data/ext/nokogiri/nokogiri.h +160 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +576 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +56 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +78 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1480 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +467 -0
- data/ext/nokogiri/xml_node_set.h +14 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +684 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +293 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +222 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +58 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +319 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +270 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/nokogiri.rb +127 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +102 -0
- data/lib/nokogiri/css/parser.rb +720 -0
- data/lib/nokogiri/css/parser.y +258 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +171 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +37 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +254 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/html/sax/push_parser.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +88 -0
- data/lib/nokogiri/xml.rb +73 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +431 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +267 -0
- data/lib/nokogiri/xml/document_fragment.rb +103 -0
- data/lib/nokogiri/xml/dtd.rb +22 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +946 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +357 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +98 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +164 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +56 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +153 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +341 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +91 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/to_be_xincluded.xml +2 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/files/xinclude.xml +4 -0
- data/test/helper.rb +147 -0
- data/test/html/sax/test_parser.rb +138 -0
- data/test/html/sax/test_parser_context.rb +46 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +529 -0
- data/test/html/test_document_encoding.rb +138 -0
- data/test/html/test_document_fragment.rb +254 -0
- data/test/html/test_element_description.rb +100 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +188 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +152 -0
- data/test/test_nokogiri.rb +132 -0
- data/test/test_reader.rb +488 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +254 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +338 -0
- data/test/xml/sax/test_parser_context.rb +106 -0
- data/test/xml/sax/test_push_parser.rb +157 -0
- data/test/xml/test_attr.rb +64 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +248 -0
- data/test/xml/test_c14n.rb +151 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +742 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +216 -0
- data/test/xml/test_dtd.rb +103 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +235 -0
- data/test/xml/test_namespace.rb +75 -0
- data/test/xml/test_node.rb +1029 -0
- data/test/xml/test_node_attributes.rb +53 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_inheritance.rb +32 -0
- data/test/xml/test_node_reparenting.rb +374 -0
- data/test/xml/test_node_set.rb +755 -0
- data/test/xml/test_parse_options.rb +64 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +142 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +94 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +45 -0
- data/test/xml/test_unparented_node.rb +413 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +295 -0
- data/test/xslt/test_custom_functions.rb +129 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +84 -0
- metadata +534 -0
@@ -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,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
|
+
}
|