nokogiri 1.6.2.rc1-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.autotest +26 -0
- data/.editorconfig +17 -0
- data/.gemtest +0 -0
- data/.travis.yml +25 -0
- data/CHANGELOG.ja.rdoc +857 -0
- data/CHANGELOG.rdoc +880 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Gemfile +21 -0
- data/Manifest.txt +371 -0
- data/README.ja.rdoc +112 -0
- data/README.rdoc +180 -0
- data/ROADMAP.md +89 -0
- data/Rakefile +351 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +78 -0
- data/build_all +130 -0
- data/dependencies.yml +4 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +453 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -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 +148 -0
- data/ext/nokogiri/nokogiri.h +164 -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 +577 -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 +1541 -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 +681 -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 +312 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -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 +63 -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 +307 -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 +137 -0
- data/lib/nokogiri/2.0/nokogiri.so +0 -0
- data/lib/nokogiri/2.1/nokogiri.so +0 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +52 -0
- data/lib/nokogiri/css/parser.rb +715 -0
- data/lib/nokogiri/css/parser.y +249 -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 +219 -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 +333 -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 +106 -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 +443 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +279 -0
- data/lib/nokogiri/xml/document_fragment.rb +112 -0
- data/lib/nokogiri/xml/dtd.rb +32 -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 +982 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +355 -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 +171 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -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 +102 -0
- data/suppressions/README.txt +1 -0
- data/suppressions/nokogiri_ree-1.8.7.358.supp +61 -0
- data/suppressions/nokogiri_ruby-1.8.7.370.supp +0 -0
- data/suppressions/nokogiri_ruby-1.9.2.320.supp +28 -0
- data/suppressions/nokogiri_ruby-1.9.3.327.supp +28 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +222 -0
- data/test/css/test_parser.rb +358 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +96 -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/atom.xml +344 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/bogus.xml +0 -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/saml/saml20assertion_schema.xsd +283 -0
- data/test/files/saml/saml20protocol_schema.xsd +302 -0
- data/test/files/saml/xenc_schema.xsd +146 -0
- data/test/files/saml/xmldsig_schema.xsd +318 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/shift_jis_no_charset.html +9 -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/test_document_url/bar.xml +2 -0
- data/test/files/test_document_url/document.dtd +4 -0
- data/test/files/test_document_url/document.xml +6 -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 +164 -0
- data/test/html/sax/test_parser.rb +141 -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 +619 -0
- data/test/html/test_document_encoding.rb +148 -0
- data/test/html/test_document_fragment.rb +261 -0
- data/test/html/test_element_description.rb +105 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +196 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +14 -0
- data/test/namespaces/test_namespaces_in_builder_doc.rb +75 -0
- data/test/namespaces/test_namespaces_in_cloned_doc.rb +31 -0
- data/test/namespaces/test_namespaces_in_created_doc.rb +75 -0
- data/test/namespaces/test_namespaces_in_parsed_doc.rb +66 -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 +156 -0
- data/test/test_nokogiri.rb +138 -0
- data/test/test_reader.rb +558 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +279 -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 +382 -0
- data/test/xml/sax/test_parser_context.rb +115 -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 +315 -0
- data/test/xml/test_c14n.rb +161 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +934 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +228 -0
- data/test/xml/test_dtd.rb +187 -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 +245 -0
- data/test/xml/test_namespace.rb +95 -0
- data/test/xml/test_node.rb +1155 -0
- data/test/xml/test_node_attributes.rb +113 -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 +129 -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 +422 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +376 -0
- data/test/xslt/test_custom_functions.rb +133 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +81 -0
- metadata +601 -0
@@ -0,0 +1,63 @@
|
|
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(error));
|
7
|
+
}
|
8
|
+
|
9
|
+
void Nokogiri_error_raise(void * ctx, xmlErrorPtr error)
|
10
|
+
{
|
11
|
+
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
|
12
|
+
}
|
13
|
+
|
14
|
+
VALUE Nokogiri_wrap_xml_syntax_error(xmlErrorPtr error)
|
15
|
+
{
|
16
|
+
VALUE msg, e, klass;
|
17
|
+
|
18
|
+
klass = cNokogiriXmlSyntaxError;
|
19
|
+
|
20
|
+
if (error->domain == XML_FROM_XPATH) {
|
21
|
+
VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath"));
|
22
|
+
klass = rb_const_get(xpath, rb_intern("SyntaxError"));
|
23
|
+
}
|
24
|
+
|
25
|
+
msg = (error && error->message) ? NOKOGIRI_STR_NEW2(error->message) : Qnil;
|
26
|
+
|
27
|
+
e = rb_class_new_instance(
|
28
|
+
1,
|
29
|
+
&msg,
|
30
|
+
klass
|
31
|
+
);
|
32
|
+
|
33
|
+
if (error)
|
34
|
+
{
|
35
|
+
rb_iv_set(e, "@domain", INT2NUM(error->domain));
|
36
|
+
rb_iv_set(e, "@code", INT2NUM(error->code));
|
37
|
+
rb_iv_set(e, "@level", INT2NUM((short)error->level));
|
38
|
+
rb_iv_set(e, "@file", RBSTR_OR_QNIL(error->file));
|
39
|
+
rb_iv_set(e, "@line", INT2NUM(error->line));
|
40
|
+
rb_iv_set(e, "@str1", RBSTR_OR_QNIL(error->str1));
|
41
|
+
rb_iv_set(e, "@str2", RBSTR_OR_QNIL(error->str2));
|
42
|
+
rb_iv_set(e, "@str3", RBSTR_OR_QNIL(error->str3));
|
43
|
+
rb_iv_set(e, "@int1", INT2NUM(error->int1));
|
44
|
+
rb_iv_set(e, "@column", INT2NUM(error->int2));
|
45
|
+
}
|
46
|
+
|
47
|
+
return e;
|
48
|
+
}
|
49
|
+
|
50
|
+
VALUE cNokogiriXmlSyntaxError;
|
51
|
+
void init_xml_syntax_error()
|
52
|
+
{
|
53
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
54
|
+
VALUE xml = rb_define_module_under(nokogiri, "XML");
|
55
|
+
|
56
|
+
/*
|
57
|
+
* The XML::SyntaxError is raised on parse errors
|
58
|
+
*/
|
59
|
+
VALUE syntax_error_mommy = rb_define_class_under(nokogiri, "SyntaxError", rb_eStandardError);
|
60
|
+
VALUE klass = rb_define_class_under(xml, "SyntaxError", syntax_error_mommy);
|
61
|
+
cNokogiriXmlSyntaxError = klass;
|
62
|
+
|
63
|
+
}
|
@@ -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(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,307 @@
|
|
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_generic_exception_handler(void * ctx, const char *msg, ...));
|
175
|
+
static void xpath_generic_exception_handler(void * ctx, const char *msg, ...)
|
176
|
+
{
|
177
|
+
char * message;
|
178
|
+
|
179
|
+
va_list args;
|
180
|
+
va_start(args, msg);
|
181
|
+
vasprintf(&message, msg, args);
|
182
|
+
va_end(args);
|
183
|
+
|
184
|
+
rb_raise(rb_eRuntimeError, "%s", message);
|
185
|
+
}
|
186
|
+
|
187
|
+
/*
|
188
|
+
* call-seq:
|
189
|
+
* evaluate(search_path, handler = nil)
|
190
|
+
*
|
191
|
+
* Evaluate the +search_path+ returning an XML::XPath object.
|
192
|
+
*/
|
193
|
+
static VALUE evaluate(int argc, VALUE *argv, VALUE self)
|
194
|
+
{
|
195
|
+
VALUE search_path, xpath_handler;
|
196
|
+
VALUE thing = Qnil;
|
197
|
+
xmlXPathContextPtr ctx;
|
198
|
+
xmlXPathObjectPtr xpath;
|
199
|
+
xmlChar *query;
|
200
|
+
|
201
|
+
Data_Get_Struct(self, xmlXPathContext, ctx);
|
202
|
+
|
203
|
+
if(rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1)
|
204
|
+
xpath_handler = Qnil;
|
205
|
+
|
206
|
+
query = (xmlChar *)StringValuePtr(search_path);
|
207
|
+
|
208
|
+
if(Qnil != xpath_handler) {
|
209
|
+
/* FIXME: not sure if this is the correct place to shove private data. */
|
210
|
+
ctx->userData = (void *)xpath_handler;
|
211
|
+
xmlXPathRegisterFuncLookup(ctx, lookup, (void *)xpath_handler);
|
212
|
+
}
|
213
|
+
|
214
|
+
xmlResetLastError();
|
215
|
+
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);
|
216
|
+
|
217
|
+
/* For some reason, xmlXPathEvalExpression will blow up with a generic error */
|
218
|
+
/* when there is a non existent function. */
|
219
|
+
xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler);
|
220
|
+
|
221
|
+
xpath = xmlXPathEvalExpression(query, ctx);
|
222
|
+
xmlSetStructuredErrorFunc(NULL, NULL);
|
223
|
+
xmlSetGenericErrorFunc(NULL, NULL);
|
224
|
+
|
225
|
+
if(xpath == NULL) {
|
226
|
+
xmlErrorPtr error = xmlGetLastError();
|
227
|
+
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
|
228
|
+
}
|
229
|
+
|
230
|
+
assert(ctx->doc);
|
231
|
+
assert(DOC_RUBY_OBJECT_TEST(ctx->doc));
|
232
|
+
|
233
|
+
switch(xpath->type) {
|
234
|
+
case XPATH_STRING:
|
235
|
+
thing = NOKOGIRI_STR_NEW2(xpath->stringval);
|
236
|
+
xmlFree(xpath->stringval);
|
237
|
+
break;
|
238
|
+
case XPATH_NODESET:
|
239
|
+
if(NULL == xpath->nodesetval) {
|
240
|
+
thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
|
241
|
+
DOC_RUBY_OBJECT(ctx->doc));
|
242
|
+
} else {
|
243
|
+
thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval,
|
244
|
+
DOC_RUBY_OBJECT(ctx->doc));
|
245
|
+
}
|
246
|
+
break;
|
247
|
+
case XPATH_NUMBER:
|
248
|
+
thing = rb_float_new(xpath->floatval);
|
249
|
+
break;
|
250
|
+
case XPATH_BOOLEAN:
|
251
|
+
thing = xpath->boolval == 1 ? Qtrue : Qfalse;
|
252
|
+
break;
|
253
|
+
default:
|
254
|
+
thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
|
255
|
+
DOC_RUBY_OBJECT(ctx->doc));
|
256
|
+
}
|
257
|
+
|
258
|
+
xmlXPathFreeNodeSetList(xpath);
|
259
|
+
|
260
|
+
return thing;
|
261
|
+
}
|
262
|
+
|
263
|
+
/*
|
264
|
+
* call-seq:
|
265
|
+
* new(node)
|
266
|
+
*
|
267
|
+
* Create a new XPathContext with +node+ as the reference point.
|
268
|
+
*/
|
269
|
+
static VALUE new(VALUE klass, VALUE nodeobj)
|
270
|
+
{
|
271
|
+
xmlNodePtr node;
|
272
|
+
xmlXPathContextPtr ctx;
|
273
|
+
VALUE self;
|
274
|
+
|
275
|
+
xmlXPathInit();
|
276
|
+
|
277
|
+
Data_Get_Struct(nodeobj, xmlNode, node);
|
278
|
+
|
279
|
+
ctx = xmlXPathNewContext(node->doc);
|
280
|
+
ctx->node = node;
|
281
|
+
self = Data_Wrap_Struct(klass, 0, deallocate, ctx);
|
282
|
+
/*rb_iv_set(self, "@xpath_handler", Qnil); */
|
283
|
+
return self;
|
284
|
+
}
|
285
|
+
|
286
|
+
VALUE cNokogiriXmlXpathContext;
|
287
|
+
void init_xml_xpath_context(void)
|
288
|
+
{
|
289
|
+
VALUE module = rb_define_module("Nokogiri");
|
290
|
+
|
291
|
+
/*
|
292
|
+
* Nokogiri::XML
|
293
|
+
*/
|
294
|
+
VALUE xml = rb_define_module_under(module, "XML");
|
295
|
+
|
296
|
+
/*
|
297
|
+
* XPathContext is the entry point for searching a Document by using XPath.
|
298
|
+
*/
|
299
|
+
VALUE klass = rb_define_class_under(xml, "XPathContext", rb_cObject);
|
300
|
+
|
301
|
+
cNokogiriXmlXpathContext = klass;
|
302
|
+
|
303
|
+
rb_define_singleton_method(klass, "new", new, 1);
|
304
|
+
rb_define_method(klass, "evaluate", evaluate, -1);
|
305
|
+
rb_define_method(klass, "register_variable", register_variable, 2);
|
306
|
+
rb_define_method(klass, "register_ns", register_ns, 2);
|
307
|
+
}
|