libxslt-ruby 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,69 +1,68 @@
1
- /* $Id: libxslt.c 42 2007-12-07 06:09:35Z transami $ */
2
-
3
- /* Please see the LICENSE file for copyright and distribution information */
4
-
5
- #include "libxslt.h"
6
- #include "libxml/xmlversion.h"
7
-
8
- VALUE cXMLDocument;
9
- VALUE cLibXSLT;
10
- VALUE cXSLT;
11
- VALUE eXSLTError;
12
- VALUE eXMLXSLTStylesheetRequireParsedDoc;
13
-
14
- /*
15
- * Document-class: LibXSLT::XSLT
16
- *
17
- * The libxslt gem provides Ruby language bindings for GNOME's Libxslt
18
- * toolkit. It is free software, released under the MIT License.
19
- *
20
- * Using the bindings is straightforward:
21
- *
22
- * stylesheet_doc = XML::Document.file('stylesheet_file')
23
- * stylesheet = XSLT::Stylesheet.new(stylesheet_doc)
24
- *
25
- * xml_doc = XML::Document.file('xml_file')
26
- * result = stylesheet.apply(xml_doc)
27
- *
28
- */
29
-
30
- #ifdef RDOC_NEVER_DEFINED
31
- cLibXSLT = rb_define_module("XSLT");
32
- #endif
33
-
34
-
35
- void
36
- Init_libxslt_ruby(void) {
37
- LIBXML_TEST_VERSION;
38
-
39
- cLibXSLT = rb_define_module("LibXSLT");
40
- cXSLT = rb_define_module_under(cLibXSLT, "XSLT");
41
-
42
- cXMLDocument = rb_const_get(rb_const_get(rb_const_get(rb_cObject, rb_intern("LibXML")),
43
- rb_intern("XML")),
44
- rb_intern("Document"));
45
-
46
- rb_define_const(cXSLT, "MAX_DEPTH", INT2NUM(xsltMaxDepth));
47
- rb_define_const(cXSLT, "MAX_SORT", INT2NUM(XSLT_MAX_SORT));
48
- rb_define_const(cXSLT, "ENGINE_VERSION", rb_str_new2(xsltEngineVersion));
49
- rb_define_const(cXSLT, "LIBXSLT_VERSION", INT2NUM(xsltLibxsltVersion));
50
- rb_define_const(cXSLT, "LIBXML_VERSION", INT2NUM(xsltLibxmlVersion));
51
- rb_define_const(cXSLT, "XSLT_NAMESPACE", rb_str_new2((const char*)XSLT_NAMESPACE));
52
- rb_define_const(cXSLT, "DEFAULT_VENDOR", rb_str_new2(XSLT_DEFAULT_VENDOR));
53
- rb_define_const(cXSLT, "DEFAULT_VERSION", rb_str_new2(XSLT_DEFAULT_VERSION));
54
- rb_define_const(cXSLT, "DEFAULT_URL", rb_str_new2(XSLT_DEFAULT_URL));
55
- rb_define_const(cXSLT, "NAMESPACE_LIBXSLT", rb_str_new2((const char*)XSLT_LIBXSLT_NAMESPACE));
56
- rb_define_const(cXSLT, "NAMESPACE_NORM_SAXON", rb_str_new2((const char*)XSLT_NORM_SAXON_NAMESPACE));
57
- rb_define_const(cXSLT, "NAMESPACE_SAXON", rb_str_new2((const char*)XSLT_SAXON_NAMESPACE));
58
- rb_define_const(cXSLT, "NAMESPACE_XT", rb_str_new2((const char*)XSLT_XT_NAMESPACE));
59
- rb_define_const(cXSLT, "NAMESPACE_XALAN", rb_str_new2((const char*)XSLT_XALAN_NAMESPACE));
60
-
61
- eXSLTError = rb_define_class_under(cLibXSLT, "XSLTError", rb_eRuntimeError);
62
- eXMLXSLTStylesheetRequireParsedDoc = rb_define_class_under(cLibXSLT, "ResultError", rb_eRuntimeError);
63
-
64
- ruby_init_xslt_stylesheet();
65
- ruby_init_exslt();
66
-
67
- /* Now load exslt. */
68
- exsltRegisterAll();
69
- }
1
+ /* $Id: libxslt.c 42 2007-12-07 06:09:35Z transami $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #include "libxslt.h"
6
+ #include "libxml/xmlversion.h"
7
+
8
+ VALUE cXMLDocument;
9
+ VALUE cLibXSLT;
10
+ VALUE cXSLT;
11
+ VALUE eXSLTError;
12
+ VALUE eXMLXSLTStylesheetRequireParsedDoc;
13
+
14
+ /*
15
+ * Document-class: LibXSLT::XSLT
16
+ *
17
+ * The libxslt gem provides Ruby language bindings for GNOME's Libxslt
18
+ * toolkit. It is free software, released under the MIT License.
19
+ *
20
+ * Using the bindings is straightforward:
21
+ *
22
+ * stylesheet_doc = XML::Document.file('stylesheet_file')
23
+ * stylesheet = XSLT::Stylesheet.new(stylesheet_doc)
24
+ *
25
+ * xml_doc = XML::Document.file('xml_file')
26
+ * result = stylesheet.apply(xml_doc)
27
+ *
28
+ */
29
+
30
+ #ifdef RDOC_NEVER_DEFINED
31
+ cLibXSLT = rb_define_module("XSLT");
32
+ #endif
33
+
34
+
35
+ void
36
+ Init_libxslt_ruby(void) {
37
+ LIBXML_TEST_VERSION;
38
+
39
+ cLibXSLT = rb_define_module("LibXSLT");
40
+ cXSLT = rb_define_module_under(cLibXSLT, "XSLT");
41
+
42
+ cXMLDocument = rb_const_get(rb_const_get(rb_const_get(rb_cObject, rb_intern("LibXML")),
43
+ rb_intern("XML")),
44
+ rb_intern("Document"));
45
+
46
+ rb_define_const(cXSLT, "MAX_DEPTH", INT2NUM(xsltMaxDepth));
47
+ rb_define_const(cXSLT, "MAX_SORT", INT2NUM(XSLT_MAX_SORT));
48
+ rb_define_const(cXSLT, "ENGINE_VERSION", rb_str_new2(xsltEngineVersion));
49
+ rb_define_const(cXSLT, "LIBXSLT_VERSION", INT2NUM(xsltLibxsltVersion));
50
+ rb_define_const(cXSLT, "LIBXML_VERSION", INT2NUM(xsltLibxmlVersion));
51
+ rb_define_const(cXSLT, "XSLT_NAMESPACE", rb_str_new2((const char*)XSLT_NAMESPACE));
52
+ rb_define_const(cXSLT, "DEFAULT_VENDOR", rb_str_new2(XSLT_DEFAULT_VENDOR));
53
+ rb_define_const(cXSLT, "DEFAULT_VERSION", rb_str_new2(XSLT_DEFAULT_VERSION));
54
+ rb_define_const(cXSLT, "DEFAULT_URL", rb_str_new2(XSLT_DEFAULT_URL));
55
+ rb_define_const(cXSLT, "NAMESPACE_LIBXSLT", rb_str_new2((const char*)XSLT_LIBXSLT_NAMESPACE));
56
+ rb_define_const(cXSLT, "NAMESPACE_SAXON", rb_str_new2((const char*)XSLT_SAXON_NAMESPACE));
57
+ rb_define_const(cXSLT, "NAMESPACE_XT", rb_str_new2((const char*)XSLT_XT_NAMESPACE));
58
+ rb_define_const(cXSLT, "NAMESPACE_XALAN", rb_str_new2((const char*)XSLT_XALAN_NAMESPACE));
59
+
60
+ eXSLTError = rb_define_class_under(cLibXSLT, "XSLTError", rb_eRuntimeError);
61
+ eXMLXSLTStylesheetRequireParsedDoc = rb_define_class_under(cLibXSLT, "ResultError", rb_eRuntimeError);
62
+
63
+ ruby_init_xslt_stylesheet();
64
+ ruby_init_exslt();
65
+
66
+ /* Now load exslt. */
67
+ exsltRegisterAll();
68
+ }
@@ -1,37 +1,37 @@
1
- /* $Id: libxslt.h 43 2007-12-07 12:38:59Z transami $ */
2
-
3
- /* Please see the LICENSE file for copyright and distribution information */
4
-
5
- #include <libxml/parser.h>
6
- #include <libxml/debugXML.h>
7
-
8
- #ifndef __RUBY_LIBXSLT_H__
9
- #define __RUBY_LIBXSLT_H__
10
-
11
- #include <ruby.h>
12
- #if HAVE_RUBY_IO_H
13
- #include <ruby/io.h>
14
- #else
15
- #include <rubyio.h>
16
- #endif
17
-
18
- #include <ruby_libxml.h>
19
-
20
- #include <libxslt/extra.h>
21
- #include <libxslt/xslt.h>
22
- #include <libxslt/xsltInternals.h>
23
- #include <libxslt/transform.h>
24
- #include <libxslt/xsltutils.h>
25
-
26
- #include <libexslt/exslt.h>
27
-
28
- #include "ruby_xslt_stylesheet.h"
29
- #include "ruby_exslt.h"
30
-
31
- #include "version.h"
32
-
33
- extern VALUE cXSLT;
34
- extern VALUE eXSLTError;
35
- extern VALUE eXMLXSLTStylesheetRequireParsedDoc;
36
-
37
- #endif
1
+ /* $Id: libxslt.h 43 2007-12-07 12:38:59Z transami $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #include <libxml/parser.h>
6
+ #include <libxml/debugXML.h>
7
+
8
+ #ifndef __RUBY_LIBXSLT_H__
9
+ #define __RUBY_LIBXSLT_H__
10
+
11
+ #include <ruby.h>
12
+ #if HAVE_RUBY_IO_H
13
+ #include <ruby/io.h>
14
+ #else
15
+ #include <rubyio.h>
16
+ #endif
17
+
18
+ #include <ruby_libxml.h>
19
+
20
+ #include <libxslt/extra.h>
21
+ #include <libxslt/xslt.h>
22
+ #include <libxslt/xsltInternals.h>
23
+ #include <libxslt/transform.h>
24
+ #include <libxslt/xsltutils.h>
25
+
26
+ #include <libexslt/exslt.h>
27
+
28
+ #include "ruby_xslt_stylesheet.h"
29
+ #include "ruby_exslt.h"
30
+
31
+ #include "version.h"
32
+
33
+ extern VALUE cXSLT;
34
+ extern VALUE eXSLTError;
35
+ extern VALUE eXMLXSLTStylesheetRequireParsedDoc;
36
+
37
+ #endif
@@ -1,149 +1,149 @@
1
- /* http://xmlsoft.org/XSLT/html/libxslt-extensions.html */
2
-
3
- #include "libxslt.h"
4
-
5
- /* Helper method to retrieve (and possibly initialize)
6
- the module function registry hash for +namespace+ */
7
- static VALUE
8
- ruby_xslt_module_function_hash(VALUE namespace) {
9
- VALUE ns_hash, func_hash;
10
-
11
- if ((ns_hash = rb_ivar_get(cXSLT, rb_intern("@module_function_registry"))) == Qnil) {
12
- ns_hash = rb_ivar_set(cXSLT, rb_intern("@module_function_registry"), rb_hash_new());
13
- }
14
-
15
- if ((func_hash = rb_hash_aref(ns_hash, namespace)) == Qnil) {
16
- func_hash = rb_hash_aset(ns_hash, namespace, rb_hash_new());
17
- }
18
-
19
- return func_hash;
20
- }
21
-
22
- /* Helper method for xsltRegisterExtModuleFunction callback */
23
- static void
24
- ruby_xslt_module_function_callback(xmlXPathParserContextPtr ctxt, int nargs) {
25
- VALUE callback;
26
- VALUE* args = ALLOCA_N(VALUE, nargs);
27
- const xmlChar *namespace, *name;
28
- int i;
29
-
30
- if (ctxt == NULL || ctxt->context == NULL) {
31
- return;
32
- }
33
-
34
- namespace = ctxt->context->functionURI;
35
- name = ctxt->context->function;
36
-
37
- callback = rb_hash_aref(
38
- ruby_xslt_module_function_hash(rb_str_new2((char *)namespace)),
39
- rb_str_new2((char *)name)
40
- );
41
-
42
- if (callback == Qnil) {
43
- rb_raise(rb_eArgError, "name `%s' not registered", name);
44
- }
45
-
46
- for (i = nargs - 1; i >= 0; i--) {
47
- args[i] = rxml_xpath_to_value(ctxt->context, valuePop(ctxt));
48
- }
49
-
50
- valuePush(ctxt, rxml_xpath_from_value(
51
- rb_funcall2(callback, rb_intern("call"), nargs, args)
52
- ));
53
- }
54
-
55
- /* call-seq:
56
- * XSLT.register_module_function(namespace, name) { ... } -> Proc or nil
57
- *
58
- * Registers +name+ as extension module function in +namespace+ with the
59
- * block as callback. Returns the callback if successful, or +nil+ otherwise.
60
- *
61
- * The callback will be called with whatever XPath expression you pass
62
- * into the function converted to a Ruby object. Its return value will
63
- * be converted to an XPath expression again.
64
- *
65
- * Example:
66
- *
67
- * # register your extension function
68
- * XSLT.register_module_function('http://ex.ns', 'ex-func') { |xp|
69
- * xp.to_a.join('|').upcase
70
- * }
71
- *
72
- * # then use it in your stylesheet
73
- * <xsl:stylesheet ... xmlns:ex="http://ex.ns">
74
- * ...
75
- * <xsl:value-of select="ex:ex-func(.)" />
76
- * <!-- the current node as upper case string -->
77
- * </xsl:stylesheet>
78
- */
79
- static VALUE
80
- ruby_xslt_register_module_function(VALUE class, VALUE namespace, VALUE name) {
81
- VALUE callback;
82
-
83
- if (!rb_block_given_p()) {
84
- rb_raise(rb_eArgError, "no block given");
85
- }
86
-
87
- if (xsltRegisterExtModuleFunction(
88
- BAD_CAST StringValuePtr(name),
89
- BAD_CAST StringValuePtr(namespace),
90
- ruby_xslt_module_function_callback
91
- ) != 0) {
92
- return Qnil;
93
- }
94
-
95
- callback = rb_block_proc();
96
-
97
- rb_hash_aset(ruby_xslt_module_function_hash(namespace), name, callback);
98
- return callback;
99
- }
100
-
101
- /* call-seq:
102
- * XSLT.unregister_module_function(namespace, name) -> Proc or nil
103
- *
104
- * Unregisters +name+ as extension module function in +namespace+.
105
- * Returns the previous callback if successful, or +nil+ otherwise.
106
- */
107
- static VALUE
108
- ruby_xslt_unregister_module_function(VALUE class, VALUE namespace, VALUE name) {
109
- VALUE func_hash, callback;
110
-
111
- func_hash = ruby_xslt_module_function_hash(namespace);
112
-
113
- if ((callback = rb_hash_aref(func_hash, name)) == Qnil) {
114
- return Qnil;
115
- }
116
-
117
- if (xsltUnregisterExtModuleFunction(
118
- BAD_CAST StringValuePtr(name),
119
- BAD_CAST StringValuePtr(namespace)
120
- ) != 0) {
121
- return Qnil;
122
- }
123
-
124
- rb_hash_aset(func_hash, name, Qnil);
125
- return callback;
126
- }
127
-
128
- /* call-seq:
129
- * XSLT.registered_module_function?(namespace, name) -> true or false
130
- *
131
- * Returns +true+ if +name+ is currently registered as extension module
132
- * function in +namespace+, or +false+ otherwise.
133
- */
134
- static VALUE
135
- ruby_xslt_registered_module_function_p(VALUE class, VALUE namespace, VALUE name) {
136
- return RTEST(rb_hash_aref(ruby_xslt_module_function_hash(namespace), name));
137
- }
138
-
139
- void
140
- ruby_init_exslt() {
141
- /* [HACK] Enclosing classes/modules for RDoc:
142
- * cLibXSLT = rb_define_module("LibXSLT");
143
- * cXSLT = rb_define_module_under(cLibXSLT, "XSLT");
144
- */
145
-
146
- rb_define_singleton_method(cXSLT, "register_module_function", ruby_xslt_register_module_function, 2);
147
- rb_define_singleton_method(cXSLT, "unregister_module_function", ruby_xslt_unregister_module_function, 2);
148
- rb_define_singleton_method(cXSLT, "registered_module_function?", ruby_xslt_registered_module_function_p, 2);
149
- }
1
+ /* http://xmlsoft.org/XSLT/html/libxslt-extensions.html */
2
+
3
+ #include "libxslt.h"
4
+
5
+ /* Helper method to retrieve (and possibly initialize)
6
+ the module function registry hash for +namespace+ */
7
+ static VALUE
8
+ ruby_xslt_module_function_hash(VALUE namespace) {
9
+ VALUE ns_hash, func_hash;
10
+
11
+ if ((ns_hash = rb_ivar_get(cXSLT, rb_intern("@module_function_registry"))) == Qnil) {
12
+ ns_hash = rb_ivar_set(cXSLT, rb_intern("@module_function_registry"), rb_hash_new());
13
+ }
14
+
15
+ if ((func_hash = rb_hash_aref(ns_hash, namespace)) == Qnil) {
16
+ func_hash = rb_hash_aset(ns_hash, namespace, rb_hash_new());
17
+ }
18
+
19
+ return func_hash;
20
+ }
21
+
22
+ /* Helper method for xsltRegisterExtModuleFunction callback */
23
+ static void
24
+ ruby_xslt_module_function_callback(xmlXPathParserContextPtr ctxt, int nargs) {
25
+ VALUE callback;
26
+ VALUE* args = ALLOCA_N(VALUE, nargs);
27
+ const xmlChar *namespace, *name;
28
+ int i;
29
+
30
+ if (ctxt == NULL || ctxt->context == NULL) {
31
+ return;
32
+ }
33
+
34
+ namespace = ctxt->context->functionURI;
35
+ name = ctxt->context->function;
36
+
37
+ callback = rb_hash_aref(
38
+ ruby_xslt_module_function_hash(rb_str_new2((char *)namespace)),
39
+ rb_str_new2((char *)name)
40
+ );
41
+
42
+ if (callback == Qnil) {
43
+ rb_raise(rb_eArgError, "name `%s' not registered", name);
44
+ }
45
+
46
+ for (i = nargs - 1; i >= 0; i--) {
47
+ args[i] = rxml_xpath_to_value(ctxt->context, valuePop(ctxt));
48
+ }
49
+
50
+ valuePush(ctxt, rxml_xpath_from_value(
51
+ rb_funcall2(callback, rb_intern("call"), nargs, args)
52
+ ));
53
+ }
54
+
55
+ /* call-seq:
56
+ * XSLT.register_module_function(namespace, name) { ... } -> Proc or nil
57
+ *
58
+ * Registers +name+ as extension module function in +namespace+ with the
59
+ * block as callback. Returns the callback if successful, or +nil+ otherwise.
60
+ *
61
+ * The callback will be called with whatever XPath expression you pass
62
+ * into the function converted to a Ruby object. Its return value will
63
+ * be converted to an XPath expression again.
64
+ *
65
+ * Example:
66
+ *
67
+ * # register your extension function
68
+ * XSLT.register_module_function('http://ex.ns', 'ex-func') { |xp|
69
+ * xp.to_a.join('|').upcase
70
+ * }
71
+ *
72
+ * # then use it in your stylesheet
73
+ * <xsl:stylesheet ... xmlns:ex="http://ex.ns">
74
+ * ...
75
+ * <xsl:value-of select="ex:ex-func(.)" />
76
+ * <!-- the current node as upper case string -->
77
+ * </xsl:stylesheet>
78
+ */
79
+ static VALUE
80
+ ruby_xslt_register_module_function(VALUE class, VALUE namespace, VALUE name) {
81
+ VALUE callback;
82
+
83
+ if (!rb_block_given_p()) {
84
+ rb_raise(rb_eArgError, "no block given");
85
+ }
86
+
87
+ if (xsltRegisterExtModuleFunction(
88
+ BAD_CAST StringValuePtr(name),
89
+ BAD_CAST StringValuePtr(namespace),
90
+ ruby_xslt_module_function_callback
91
+ ) != 0) {
92
+ return Qnil;
93
+ }
94
+
95
+ callback = rb_block_proc();
96
+
97
+ rb_hash_aset(ruby_xslt_module_function_hash(namespace), name, callback);
98
+ return callback;
99
+ }
100
+
101
+ /* call-seq:
102
+ * XSLT.unregister_module_function(namespace, name) -> Proc or nil
103
+ *
104
+ * Unregisters +name+ as extension module function in +namespace+.
105
+ * Returns the previous callback if successful, or +nil+ otherwise.
106
+ */
107
+ static VALUE
108
+ ruby_xslt_unregister_module_function(VALUE class, VALUE namespace, VALUE name) {
109
+ VALUE func_hash, callback;
110
+
111
+ func_hash = ruby_xslt_module_function_hash(namespace);
112
+
113
+ if ((callback = rb_hash_aref(func_hash, name)) == Qnil) {
114
+ return Qnil;
115
+ }
116
+
117
+ if (xsltUnregisterExtModuleFunction(
118
+ BAD_CAST StringValuePtr(name),
119
+ BAD_CAST StringValuePtr(namespace)
120
+ ) != 0) {
121
+ return Qnil;
122
+ }
123
+
124
+ rb_hash_aset(func_hash, name, Qnil);
125
+ return callback;
126
+ }
127
+
128
+ /* call-seq:
129
+ * XSLT.registered_module_function?(namespace, name) -> true or false
130
+ *
131
+ * Returns +true+ if +name+ is currently registered as extension module
132
+ * function in +namespace+, or +false+ otherwise.
133
+ */
134
+ static VALUE
135
+ ruby_xslt_registered_module_function_p(VALUE class, VALUE namespace, VALUE name) {
136
+ return RTEST(rb_hash_aref(ruby_xslt_module_function_hash(namespace), name));
137
+ }
138
+
139
+ void
140
+ ruby_init_exslt() {
141
+ /* [HACK] Enclosing classes/modules for RDoc:
142
+ * cLibXSLT = rb_define_module("LibXSLT");
143
+ * cXSLT = rb_define_module_under(cLibXSLT, "XSLT");
144
+ */
145
+
146
+ rb_define_singleton_method(cXSLT, "register_module_function", ruby_xslt_register_module_function, 2);
147
+ rb_define_singleton_method(cXSLT, "unregister_module_function", ruby_xslt_unregister_module_function, 2);
148
+ rb_define_singleton_method(cXSLT, "registered_module_function?", ruby_xslt_registered_module_function_p, 2);
149
+ }