glebm-nokogiri 1.4.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/CHANGELOG.ja.rdoc +411 -0
- data/CHANGELOG.rdoc +397 -0
- data/Manifest.txt +276 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +132 -0
- data/Rakefile +183 -0
- data/bin/nokogiri +49 -0
- data/deps.rip +5 -0
- data/ext/nokogiri/extconf.rb +97 -0
- data/ext/nokogiri/html_document.c +154 -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 +94 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +95 -0
- data/ext/nokogiri/nokogiri.h +153 -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 +464 -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 +31 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_namespace.c +84 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1347 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +418 -0
- data/ext/nokogiri/xml_node_set.h +9 -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 +665 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +168 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +286 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +159 -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 +50 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +276 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +142 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/nokogiri.rb +133 -0
- data/lib/nokogiri/css.rb +25 -0
- data/lib/nokogiri/css/generated_parser.rb +669 -0
- data/lib/nokogiri/css/generated_tokenizer.rb +145 -0
- data/lib/nokogiri/css/node.rb +99 -0
- data/lib/nokogiri/css/parser.rb +82 -0
- data/lib/nokogiri/css/parser.y +232 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +169 -0
- data/lib/nokogiri/decorators/slop.rb +33 -0
- data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
- data/lib/nokogiri/ffi/html/document.rb +28 -0
- data/lib/nokogiri/ffi/html/element_description.rb +81 -0
- data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
- data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
- data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
- data/lib/nokogiri/ffi/libxml.rb +386 -0
- data/lib/nokogiri/ffi/structs/common_node.rb +38 -0
- data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
- data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
- data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
- data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
- data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
- data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +11 -0
- data/lib/nokogiri/ffi/structs/xml_document.rb +117 -0
- data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
- data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
- data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
- data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
- data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
- data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
- data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
- data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
- data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
- data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
- data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +124 -0
- data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
- data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
- data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +38 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
- data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
- data/lib/nokogiri/ffi/weak_bucket.rb +40 -0
- data/lib/nokogiri/ffi/xml/attr.rb +41 -0
- data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
- data/lib/nokogiri/ffi/xml/comment.rb +18 -0
- data/lib/nokogiri/ffi/xml/document.rb +162 -0
- data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
- data/lib/nokogiri/ffi/xml/dtd.rb +67 -0
- data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
- data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
- data/lib/nokogiri/ffi/xml/entity_decl.rb +36 -0
- data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
- data/lib/nokogiri/ffi/xml/node.rb +556 -0
- data/lib/nokogiri/ffi/xml/node_set.rb +149 -0
- data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
- data/lib/nokogiri/ffi/xml/reader.rb +232 -0
- data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
- data/lib/nokogiri/ffi/xml/sax/parser.rb +135 -0
- data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
- data/lib/nokogiri/ffi/xml/sax/push_parser.rb +51 -0
- data/lib/nokogiri/ffi/xml/schema.rb +109 -0
- data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
- data/lib/nokogiri/ffi/xml/text.rb +18 -0
- data/lib/nokogiri/ffi/xml/xpath.rb +9 -0
- data/lib/nokogiri/ffi/xml/xpath_context.rb +148 -0
- data/lib/nokogiri/ffi/xslt/stylesheet.rb +53 -0
- data/lib/nokogiri/html.rb +35 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +90 -0
- data/lib/nokogiri/html/document_fragment.rb +36 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +48 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +37 -0
- data/lib/nokogiri/version_warning.rb +14 -0
- data/lib/nokogiri/xml.rb +67 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +418 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +194 -0
- data/lib/nokogiri/xml/document_fragment.rb +77 -0
- data/lib/nokogiri/xml/dtd.rb +11 -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 +793 -0
- data/lib/nokogiri/xml/node/save_options.rb +42 -0
- data/lib/nokogiri/xml/node_set.rb +325 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +85 -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 +74 -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 +160 -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 +57 -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 +48 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +158 -0
- data/tasks/test.rb +94 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +282 -0
- data/test/css/test_tokenizer.rb +190 -0
- data/test/css/test_xpath_visitor.rb +85 -0
- data/test/ffi/test_document.rb +35 -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/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -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/valid_bar.xml +2 -0
- data/test/helper.rb +169 -0
- data/test/html/sax/test_parser.rb +74 -0
- data/test/html/sax/test_parser_context.rb +48 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +398 -0
- data/test/html/test_document_encoding.rb +77 -0
- data/test/html/test_document_fragment.rb +182 -0
- data/test/html/test_element_description.rb +98 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +181 -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 +87 -0
- data/test/test_nokogiri.rb +138 -0
- data/test/test_reader.rb +386 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +188 -0
- data/test/xml/node/test_save_options.rb +20 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +307 -0
- data/test/xml/sax/test_parser_context.rb +63 -0
- data/test/xml/sax/test_push_parser.rb +139 -0
- data/test/xml/test_attr.rb +38 -0
- data/test/xml/test_attribute_decl.rb +82 -0
- data/test/xml/test_builder.rb +210 -0
- data/test/xml/test_cdata.rb +50 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +668 -0
- data/test/xml/test_document_encoding.rb +26 -0
- data/test/xml/test_document_fragment.rb +180 -0
- data/test/xml/test_dtd.rb +82 -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 +120 -0
- data/test/xml/test_entity_reference.rb +21 -0
- data/test/xml/test_namespace.rb +68 -0
- data/test/xml/test_node.rb +865 -0
- data/test/xml/test_node_attributes.rb +34 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_reparenting.rb +293 -0
- data/test/xml/test_node_set.rb +649 -0
- data/test/xml/test_parse_options.rb +52 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +126 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +89 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +38 -0
- data/test/xml/test_unparented_node.rb +381 -0
- data/test/xml/test_xpath.rb +138 -0
- metadata +533 -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,50 @@
|
|
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
|
+
rb_node = Nokogiri_wrap_xml_node(klass, node) ;
|
26
|
+
rb_obj_call_init(rb_node, argc, argv);
|
27
|
+
|
28
|
+
if(rb_block_given_p()) rb_yield(rb_node);
|
29
|
+
|
30
|
+
return rb_node;
|
31
|
+
}
|
32
|
+
|
33
|
+
VALUE cNokogiriXmlText ;
|
34
|
+
void init_xml_text()
|
35
|
+
{
|
36
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
37
|
+
VALUE xml = rb_define_module_under(nokogiri, "XML");
|
38
|
+
/* */
|
39
|
+
VALUE node = rb_define_class_under(xml, "Node", rb_cObject);
|
40
|
+
VALUE char_data = rb_define_class_under(xml, "CharacterData", node);
|
41
|
+
|
42
|
+
/*
|
43
|
+
* Wraps Text nodes.
|
44
|
+
*/
|
45
|
+
VALUE klass = rb_define_class_under(xml, "Text", char_data);
|
46
|
+
|
47
|
+
cNokogiriXmlText = klass;
|
48
|
+
|
49
|
+
rb_define_singleton_method(klass, "new", new, -1);
|
50
|
+
}
|
@@ -0,0 +1,276 @@
|
|
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
|
+
static void ruby_funcall(xmlXPathParserContextPtr ctx, int nargs)
|
31
|
+
{
|
32
|
+
VALUE xpath_handler = Qnil;
|
33
|
+
VALUE result;
|
34
|
+
VALUE *argv;
|
35
|
+
VALUE doc;
|
36
|
+
VALUE node_set = Qnil;
|
37
|
+
xmlNodeSetPtr xml_node_set = NULL;
|
38
|
+
xmlXPathObjectPtr obj;
|
39
|
+
int i;
|
40
|
+
|
41
|
+
assert(ctx);
|
42
|
+
assert(ctx->context);
|
43
|
+
assert(ctx->context->userData);
|
44
|
+
assert(ctx->context->doc);
|
45
|
+
assert(DOC_RUBY_OBJECT_TEST(ctx->context->doc));
|
46
|
+
|
47
|
+
xpath_handler = (VALUE)(ctx->context->userData);
|
48
|
+
|
49
|
+
argv = (VALUE *)calloc((unsigned int)nargs, sizeof(VALUE));
|
50
|
+
doc = DOC_RUBY_OBJECT(ctx->context->doc);
|
51
|
+
|
52
|
+
i = nargs - 1;
|
53
|
+
do {
|
54
|
+
obj = valuePop(ctx);
|
55
|
+
switch(obj->type) {
|
56
|
+
case XPATH_STRING:
|
57
|
+
argv[i] = NOKOGIRI_STR_NEW2(obj->stringval);
|
58
|
+
break;
|
59
|
+
case XPATH_BOOLEAN:
|
60
|
+
argv[i] = obj->boolval == 1 ? Qtrue : Qfalse;
|
61
|
+
break;
|
62
|
+
case XPATH_NUMBER:
|
63
|
+
argv[i] = rb_float_new(obj->floatval);
|
64
|
+
break;
|
65
|
+
case XPATH_NODESET:
|
66
|
+
argv[i] = Nokogiri_wrap_xml_node_set(obj->nodesetval, doc);
|
67
|
+
break;
|
68
|
+
default:
|
69
|
+
argv[i] = NOKOGIRI_STR_NEW2(xmlXPathCastToString(obj));
|
70
|
+
}
|
71
|
+
xmlXPathFreeNodeSetList(obj);
|
72
|
+
} while(i-- > 0);
|
73
|
+
|
74
|
+
result = rb_funcall2(
|
75
|
+
xpath_handler,
|
76
|
+
rb_intern((const char *)ctx->context->function),
|
77
|
+
nargs,
|
78
|
+
argv
|
79
|
+
);
|
80
|
+
free(argv);
|
81
|
+
|
82
|
+
switch(TYPE(result)) {
|
83
|
+
case T_FLOAT:
|
84
|
+
case T_BIGNUM:
|
85
|
+
case T_FIXNUM:
|
86
|
+
xmlXPathReturnNumber(ctx, NUM2DBL(result));
|
87
|
+
break;
|
88
|
+
case T_STRING:
|
89
|
+
xmlXPathReturnString(
|
90
|
+
ctx,
|
91
|
+
(xmlChar *)xmlXPathWrapCString(StringValuePtr(result))
|
92
|
+
);
|
93
|
+
break;
|
94
|
+
case T_TRUE:
|
95
|
+
xmlXPathReturnTrue(ctx);
|
96
|
+
break;
|
97
|
+
case T_FALSE:
|
98
|
+
xmlXPathReturnFalse(ctx);
|
99
|
+
break;
|
100
|
+
case T_NIL:
|
101
|
+
break;
|
102
|
+
case T_ARRAY:
|
103
|
+
{
|
104
|
+
VALUE args[2];
|
105
|
+
args[0] = doc;
|
106
|
+
args[1] = result;
|
107
|
+
node_set = rb_class_new_instance(2, args, cNokogiriXmlNodeSet);
|
108
|
+
Data_Get_Struct(node_set, xmlNodeSet, xml_node_set);
|
109
|
+
xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
|
110
|
+
}
|
111
|
+
break;
|
112
|
+
case T_DATA:
|
113
|
+
if(rb_obj_is_kind_of(result, cNokogiriXmlNodeSet)) {
|
114
|
+
Data_Get_Struct(result, xmlNodeSet, xml_node_set);
|
115
|
+
/* Copy the node set, otherwise it will get GC'd. */
|
116
|
+
xmlXPathReturnNodeSet(ctx, xmlXPathNodeSetMerge(NULL, xml_node_set));
|
117
|
+
break;
|
118
|
+
}
|
119
|
+
default:
|
120
|
+
rb_raise(rb_eRuntimeError, "Invalid return type");
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
static xmlXPathFunction lookup( void *ctx,
|
125
|
+
const xmlChar * name,
|
126
|
+
const xmlChar* ns_uri )
|
127
|
+
{
|
128
|
+
VALUE xpath_handler = (VALUE)ctx;
|
129
|
+
if(rb_respond_to(xpath_handler, rb_intern((const char *)name)))
|
130
|
+
return ruby_funcall;
|
131
|
+
|
132
|
+
return NULL;
|
133
|
+
}
|
134
|
+
|
135
|
+
NORETURN(static void xpath_exception_handler(void * ctx, xmlErrorPtr error));
|
136
|
+
static void xpath_exception_handler(void * ctx, xmlErrorPtr error)
|
137
|
+
{
|
138
|
+
VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath"));
|
139
|
+
VALUE klass = rb_const_get(xpath, rb_intern("SyntaxError"));
|
140
|
+
|
141
|
+
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error));
|
142
|
+
}
|
143
|
+
|
144
|
+
NORETURN(static void xpath_generic_exception_handler(void * ctx, const char *msg, ...));
|
145
|
+
static void xpath_generic_exception_handler(void * ctx, const char *msg, ...)
|
146
|
+
{
|
147
|
+
char * message;
|
148
|
+
|
149
|
+
va_list args;
|
150
|
+
va_start(args, msg);
|
151
|
+
vasprintf(&message, msg, args);
|
152
|
+
va_end(args);
|
153
|
+
|
154
|
+
rb_raise(rb_eRuntimeError, message);
|
155
|
+
}
|
156
|
+
|
157
|
+
/*
|
158
|
+
* call-seq:
|
159
|
+
* evaluate(search_path)
|
160
|
+
*
|
161
|
+
* Evaluate the +search_path+ returning an XML::XPath object.
|
162
|
+
*/
|
163
|
+
static VALUE evaluate(int argc, VALUE *argv, VALUE self)
|
164
|
+
{
|
165
|
+
VALUE search_path, xpath_handler;
|
166
|
+
VALUE thing = Qnil;
|
167
|
+
xmlXPathContextPtr ctx;
|
168
|
+
xmlXPathObjectPtr xpath;
|
169
|
+
xmlChar *query;
|
170
|
+
|
171
|
+
Data_Get_Struct(self, xmlXPathContext, ctx);
|
172
|
+
|
173
|
+
if(rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1)
|
174
|
+
xpath_handler = Qnil;
|
175
|
+
|
176
|
+
query = (xmlChar *)StringValuePtr(search_path);
|
177
|
+
|
178
|
+
if(Qnil != xpath_handler) {
|
179
|
+
/* FIXME: not sure if this is the correct place to shove private data. */
|
180
|
+
ctx->userData = (void *)xpath_handler;
|
181
|
+
xmlXPathRegisterFuncLookup(ctx, lookup, (void *)xpath_handler);
|
182
|
+
}
|
183
|
+
|
184
|
+
xmlResetLastError();
|
185
|
+
xmlSetStructuredErrorFunc(NULL, xpath_exception_handler);
|
186
|
+
|
187
|
+
/* For some reason, xmlXPathEvalExpression will blow up with a generic error */
|
188
|
+
/* when there is a non existent function. */
|
189
|
+
xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler);
|
190
|
+
|
191
|
+
xpath = xmlXPathEvalExpression(query, ctx);
|
192
|
+
xmlSetStructuredErrorFunc(NULL, NULL);
|
193
|
+
xmlSetGenericErrorFunc(NULL, NULL);
|
194
|
+
|
195
|
+
if(xpath == NULL) {
|
196
|
+
VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath"));
|
197
|
+
VALUE klass = rb_const_get(xpath, rb_intern("SyntaxError"));
|
198
|
+
|
199
|
+
xmlErrorPtr error = xmlGetLastError();
|
200
|
+
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error));
|
201
|
+
}
|
202
|
+
|
203
|
+
assert(ctx->doc);
|
204
|
+
assert(DOC_RUBY_OBJECT_TEST(ctx->doc));
|
205
|
+
|
206
|
+
switch(xpath->type) {
|
207
|
+
case XPATH_STRING:
|
208
|
+
thing = NOKOGIRI_STR_NEW2(xpath->stringval);
|
209
|
+
break;
|
210
|
+
case XPATH_NODESET:
|
211
|
+
if(NULL == xpath->nodesetval) {
|
212
|
+
thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
|
213
|
+
DOC_RUBY_OBJECT(ctx->doc));
|
214
|
+
} else {
|
215
|
+
thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval,
|
216
|
+
DOC_RUBY_OBJECT(ctx->doc));
|
217
|
+
}
|
218
|
+
break;
|
219
|
+
case XPATH_NUMBER:
|
220
|
+
thing = rb_float_new(xpath->floatval);
|
221
|
+
break;
|
222
|
+
case XPATH_BOOLEAN:
|
223
|
+
thing = xpath->boolval == 1 ? Qtrue : Qfalse;
|
224
|
+
break;
|
225
|
+
default:
|
226
|
+
thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
|
227
|
+
DOC_RUBY_OBJECT(ctx->doc));
|
228
|
+
}
|
229
|
+
|
230
|
+
return thing;
|
231
|
+
}
|
232
|
+
|
233
|
+
/*
|
234
|
+
* call-seq:
|
235
|
+
* new(node)
|
236
|
+
*
|
237
|
+
* Create a new XPathContext with +node+ as the reference point.
|
238
|
+
*/
|
239
|
+
static VALUE new(VALUE klass, VALUE nodeobj)
|
240
|
+
{
|
241
|
+
xmlNodePtr node;
|
242
|
+
xmlXPathContextPtr ctx;
|
243
|
+
VALUE self;
|
244
|
+
|
245
|
+
xmlXPathInit();
|
246
|
+
|
247
|
+
Data_Get_Struct(nodeobj, xmlNode, node);
|
248
|
+
|
249
|
+
ctx = xmlXPathNewContext(node->doc);
|
250
|
+
ctx->node = node;
|
251
|
+
self = Data_Wrap_Struct(klass, 0, deallocate, ctx);
|
252
|
+
/*rb_iv_set(self, "@xpath_handler", Qnil); */
|
253
|
+
return self;
|
254
|
+
}
|
255
|
+
|
256
|
+
VALUE cNokogiriXmlXpathContext;
|
257
|
+
void init_xml_xpath_context(void)
|
258
|
+
{
|
259
|
+
VALUE module = rb_define_module("Nokogiri");
|
260
|
+
|
261
|
+
/*
|
262
|
+
* Nokogiri::XML
|
263
|
+
*/
|
264
|
+
VALUE xml = rb_define_module_under(module, "XML");
|
265
|
+
|
266
|
+
/*
|
267
|
+
* XPathContext is the entry point for searching a Document by using XPath.
|
268
|
+
*/
|
269
|
+
VALUE klass = rb_define_class_under(xml, "XPathContext", rb_cObject);
|
270
|
+
|
271
|
+
cNokogiriXmlXpathContext = klass;
|
272
|
+
|
273
|
+
rb_define_singleton_method(klass, "new", new, 1);
|
274
|
+
rb_define_method(klass, "evaluate", evaluate, -1);
|
275
|
+
rb_define_method(klass, "register_ns", register_ns, 2);
|
276
|
+
}
|
@@ -0,0 +1,142 @@
|
|
1
|
+
#include <xslt_stylesheet.h>
|
2
|
+
|
3
|
+
#include <libxslt/xsltInternals.h>
|
4
|
+
#include <libxslt/xsltutils.h>
|
5
|
+
#include <libxslt/transform.h>
|
6
|
+
#include <libexslt/exslt.h>
|
7
|
+
|
8
|
+
int vasprintf (char **strp, const char *fmt, va_list ap);
|
9
|
+
|
10
|
+
static void dealloc(xsltStylesheetPtr doc)
|
11
|
+
{
|
12
|
+
NOKOGIRI_DEBUG_START(doc);
|
13
|
+
xsltFreeStylesheet(doc); /* commented out for now. */
|
14
|
+
NOKOGIRI_DEBUG_END(doc);
|
15
|
+
}
|
16
|
+
|
17
|
+
NORETURN(static void xslt_generic_error_handler(void * ctx, const char *msg, ...));
|
18
|
+
static void xslt_generic_error_handler(void * ctx, const char *msg, ...)
|
19
|
+
{
|
20
|
+
char * message;
|
21
|
+
|
22
|
+
va_list args;
|
23
|
+
va_start(args, msg);
|
24
|
+
vasprintf(&message, msg, args);
|
25
|
+
va_end(args);
|
26
|
+
|
27
|
+
rb_raise(rb_eRuntimeError, message);
|
28
|
+
}
|
29
|
+
|
30
|
+
/*
|
31
|
+
* call-seq:
|
32
|
+
* parse_stylesheet_doc(document)
|
33
|
+
*
|
34
|
+
* Parse a stylesheet from +document+.
|
35
|
+
*/
|
36
|
+
static VALUE parse_stylesheet_doc(VALUE klass, VALUE xmldocobj)
|
37
|
+
{
|
38
|
+
xmlDocPtr xml ;
|
39
|
+
xsltStylesheetPtr ss ;
|
40
|
+
Data_Get_Struct(xmldocobj, xmlDoc, xml);
|
41
|
+
exsltRegisterAll();
|
42
|
+
|
43
|
+
xsltSetGenericErrorFunc(NULL, xslt_generic_error_handler);
|
44
|
+
|
45
|
+
ss = xsltParseStylesheetDoc(xmlCopyDoc(xml, 1)); /* 1 => recursive */
|
46
|
+
|
47
|
+
xsltSetGenericErrorFunc(NULL, NULL);
|
48
|
+
|
49
|
+
return Data_Wrap_Struct(klass, NULL, dealloc, ss);
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
/*
|
54
|
+
* call-seq:
|
55
|
+
* serialize(document)
|
56
|
+
*
|
57
|
+
* Serialize +document+ to an xml string.
|
58
|
+
*/
|
59
|
+
static VALUE serialize(VALUE self, VALUE xmlobj)
|
60
|
+
{
|
61
|
+
xmlDocPtr xml ;
|
62
|
+
xsltStylesheetPtr ss ;
|
63
|
+
xmlChar* doc_ptr ;
|
64
|
+
int doc_len ;
|
65
|
+
VALUE rval ;
|
66
|
+
|
67
|
+
Data_Get_Struct(xmlobj, xmlDoc, xml);
|
68
|
+
Data_Get_Struct(self, xsltStylesheet, ss);
|
69
|
+
xsltSaveResultToString(&doc_ptr, &doc_len, xml, ss);
|
70
|
+
rval = NOKOGIRI_STR_NEW(doc_ptr, doc_len);
|
71
|
+
xmlFree(doc_ptr);
|
72
|
+
return rval ;
|
73
|
+
}
|
74
|
+
|
75
|
+
/*
|
76
|
+
* call-seq:
|
77
|
+
* transform(document, params = [])
|
78
|
+
*
|
79
|
+
* Apply an XSLT stylesheet to an XML::Document.
|
80
|
+
* +params+ is an array of strings used as XSLT parameters.
|
81
|
+
* returns Nokogiri::XML::Document
|
82
|
+
*
|
83
|
+
* Example:
|
84
|
+
*
|
85
|
+
* doc = Nokogiri::XML(File.read(ARGV[0]))
|
86
|
+
* xslt = Nokogiri::XSLT(File.read(ARGV[1]))
|
87
|
+
* puts xslt.transform(doc, ['key', 'value'])
|
88
|
+
*
|
89
|
+
*/
|
90
|
+
static VALUE transform(int argc, VALUE* argv, VALUE self)
|
91
|
+
{
|
92
|
+
VALUE xmldoc, paramobj ;
|
93
|
+
xmlDocPtr xml ;
|
94
|
+
xmlDocPtr result ;
|
95
|
+
xsltStylesheetPtr ss ;
|
96
|
+
const char** params ;
|
97
|
+
long param_len, j ;
|
98
|
+
|
99
|
+
rb_scan_args(argc, argv, "11", &xmldoc, ¶mobj);
|
100
|
+
if (NIL_P(paramobj)) { paramobj = rb_ary_new2(0) ; }
|
101
|
+
|
102
|
+
/* handle hashes as arguments. */
|
103
|
+
if(T_HASH == TYPE(paramobj)) {
|
104
|
+
paramobj = rb_funcall(paramobj, rb_intern("to_a"), 0);
|
105
|
+
paramobj = rb_funcall(paramobj, rb_intern("flatten"), 0);
|
106
|
+
}
|
107
|
+
|
108
|
+
Check_Type(paramobj, T_ARRAY);
|
109
|
+
|
110
|
+
Data_Get_Struct(xmldoc, xmlDoc, xml);
|
111
|
+
Data_Get_Struct(self, xsltStylesheet, ss);
|
112
|
+
|
113
|
+
param_len = RARRAY_LEN(paramobj);
|
114
|
+
params = calloc((size_t)param_len+1, sizeof(char*));
|
115
|
+
for (j = 0 ; j < param_len ; j++) {
|
116
|
+
VALUE entry = rb_ary_entry(paramobj, j);
|
117
|
+
const char * ptr = StringValuePtr(entry);
|
118
|
+
params[j] = ptr;
|
119
|
+
}
|
120
|
+
params[param_len] = 0 ;
|
121
|
+
|
122
|
+
result = xsltApplyStylesheet(ss, xml, params);
|
123
|
+
free(params);
|
124
|
+
|
125
|
+
if (!result) rb_raise(rb_eRuntimeError, "could not perform xslt transform on document");
|
126
|
+
|
127
|
+
return Nokogiri_wrap_xml_document(0, result) ;
|
128
|
+
}
|
129
|
+
|
130
|
+
VALUE cNokogiriXsltStylesheet ;
|
131
|
+
void init_xslt_stylesheet()
|
132
|
+
{
|
133
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
134
|
+
VALUE xslt = rb_define_module_under(nokogiri, "XSLT");
|
135
|
+
VALUE klass = rb_define_class_under(xslt, "Stylesheet", rb_cObject);
|
136
|
+
|
137
|
+
cNokogiriXsltStylesheet = klass;
|
138
|
+
|
139
|
+
rb_define_singleton_method(klass, "parse_stylesheet_doc", parse_stylesheet_doc, 1);
|
140
|
+
rb_define_method(klass, "serialize", serialize, 1);
|
141
|
+
rb_define_method(klass, "transform", transform, -1);
|
142
|
+
}
|