libxslt-ruby-r19mingw1 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +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 cLibXSLT;
9
+ VALUE cXSLT;
10
+ VALUE eXSLTError;
11
+ VALUE eXMLXSLTStylesheetRequireParsedDoc;
12
+
13
+ /*
14
+ * Document-class: LibXSLT::XSLT
15
+ *
16
+ * The libxslt gem provides Ruby language bindings for GNOME's Libxslt
17
+ * toolkit. It is free software, released under the MIT License.
18
+ *
19
+ * Using the bindings is straightforward:
20
+ *
21
+ * stylesheet_doc = XML::Document.file('stylesheet_file')
22
+ * stylesheet = XSLT::Stylesheet.new(stylesheet_doc)
23
+ *
24
+ * xml_doc = XML::Document.file('xml_file')
25
+ * result = stylesheet.apply(xml_doc)
26
+ *
27
+ *
28
+ */
29
+
30
+ #ifdef RDOC_NEVER_DEFINED
31
+ cLibXSLT = rb_define_module("XSLT");
32
+ #endif
33
+
34
+
35
+ #if defined(_WIN32)
36
+ __declspec(dllexport)
37
+ #endif
38
+
39
+ void
40
+ Init_libxslt_ruby(void) {
41
+ LIBXML_TEST_VERSION;
42
+
43
+ cLibXSLT = rb_define_module("LibXSLT");
44
+ cXSLT = rb_define_module_under(cLibXSLT, "XSLT");
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
+
66
+ /* Now load exslt. */
67
+ exsltRegisterAll();
68
+ }
@@ -0,0 +1,32 @@
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
+ #ifndef __RUBY_LIBXSLT_H__
6
+ #define __RUBY_LIBXSLT_H__
7
+
8
+ #include <ruby.h>
9
+ #include <rubyio.h>
10
+ #include <libxml/parser.h>
11
+ #include <libxml/debugXML.h>
12
+ #include <libxslt/extra.h>
13
+ #include <libxslt/xslt.h>
14
+ #include <libxslt/xsltInternals.h>
15
+ #include <libxslt/transform.h>
16
+ #include <libxslt/xsltutils.h>
17
+ #include <libexslt/exslt.h>
18
+
19
+ #include "ruby_xslt_stylesheet.h"
20
+
21
+ #include "version.h"
22
+
23
+ /*#if ((RUBY_LIBXML_VER_MAJ != RUBY_LIBXSLT_VER_MAJ) || (RUBY_LIBXML_VER_MIN != RUBY_LIBXSLT_VER_MIN))
24
+ #error "Incompatible LibXML-Ruby headers - please install same major/micro version"
25
+ #endif*/
26
+
27
+ extern VALUE cLibXSLT;
28
+ extern VALUE cXSLT;
29
+ extern VALUE eXSLTError;
30
+ extern VALUE eXMLXSLTStylesheetRequireParsedDoc;
31
+
32
+ #endif
@@ -0,0 +1,369 @@
1
+ /* $Id: ruby_xslt_stylesheet.c 42 2007-12-07 06:09:35Z transami $ */
2
+
3
+ /* See the LICENSE file for copyright and distribution information. */
4
+
5
+ #include "libxslt.h"
6
+ #include "ruby_xslt_stylesheet.h"
7
+ #include "libxml/ruby_xml_document.h"
8
+
9
+ /*
10
+ * Document-class: LibXSLT::XSLT::Stylesheet
11
+ *
12
+ * The XSLT::Stylesheet represents a XSL stylesheet that
13
+ * can be used to transform an XML document. For usage information
14
+ * refer to XSLT::Stylesheet#apply
15
+ *
16
+ */
17
+
18
+ VALUE cXSLTStylesheet;
19
+
20
+ static VALUE
21
+ ruby_xslt_stylesheet_document_klass() {
22
+ VALUE mXML = rb_const_get(rb_cObject, rb_intern("XML"));
23
+ return rb_const_get(mXML, rb_intern("Document"));
24
+ }
25
+
26
+ void
27
+ ruby_xslt_stylesheet_free(xsltStylesheetPtr xstylesheet) {
28
+ xsltFreeStylesheet(xstylesheet);
29
+ }
30
+
31
+ static VALUE
32
+ ruby_xslt_stylesheet_alloc(VALUE klass) {
33
+ return Data_Wrap_Struct(cXSLTStylesheet,
34
+ NULL, ruby_xslt_stylesheet_free,
35
+ NULL);
36
+ }
37
+
38
+
39
+ /* call-seq:
40
+ * XSLT::Stylesheet.new(document) -> XSLT::Stylesheet
41
+ *
42
+ * Creates a new XSLT stylesheet based on the specified document.
43
+ * For memory management reasons, a copy of the specified document
44
+ * will be made, so its best to create a single copy of a stylesheet
45
+ * and use it multiple times.
46
+ *
47
+ * stylesheet_doc = XML::Document.file('stylesheet_file')
48
+ * stylesheet = XSLT::Stylesheet.new(stylesheet_doc)
49
+ *
50
+ */
51
+ static VALUE
52
+ ruby_xslt_stylesheet_initialize(VALUE self, VALUE document) {
53
+ xmlDocPtr xdoc;
54
+ xmlDocPtr xcopy;
55
+ xsltStylesheetPtr xstylesheet;
56
+
57
+ if (!rb_obj_is_kind_of(document, ruby_xslt_stylesheet_document_klass()))
58
+ rb_raise(rb_eTypeError, "Must pass in an XML::Document instance.");
59
+
60
+ /* NOTE!! Since the stylesheet own the specified document, the easiest
61
+ * thing to do from a memory standpoint is too copy it and not expose
62
+ * the copy to Ruby. The other solution is expose a memory management
63
+ * API on the document object for taking ownership of the document
64
+ * and specifying when it has been freed. Then the document class
65
+ * has to be updated to always check and see if the document is
66
+ * still valid. That's all doable, but seems like a pain, so
67
+ * just copy the document for now. */
68
+ Data_Get_Struct(document, xmlDoc, xdoc);
69
+ xcopy = xmlCopyDoc(xdoc, 1);
70
+ xstylesheet = xsltParseStylesheetDoc(xcopy);
71
+ xstylesheet->_private = (void *)self;
72
+ DATA_PTR(self) = xstylesheet;
73
+
74
+ /* Save a reference to the document as an attribute accessable to ruby*/
75
+ return self;
76
+ }
77
+
78
+ /* Helper method to convert Ruby params to C params */
79
+ char **
80
+ ruby_xslt_coerce_params(VALUE params) {
81
+ char** result;
82
+ size_t length;
83
+ size_t i;
84
+
85
+ length = RARRAY_LEN(params);
86
+ result = ALLOC_N(char *, length + 2);
87
+
88
+ for (i=0; i<length; i++) {
89
+ VALUE str = rb_String(RARRAY_PTR(params)[i]);
90
+ int strLen = RSTRING_LEN(str);
91
+ result[i] = ALLOC_N(char, strLen + 1);
92
+ memset(result[i], 0, strLen + 1);
93
+ strncpy(result[i], RSTRING_PTR(str), strLen);
94
+ }
95
+
96
+ /* Null terminate the array - need to empty elements */
97
+ result[i] = NULL;
98
+ result[i+1] = NULL;
99
+
100
+ return result;
101
+ }
102
+
103
+
104
+ /* call-seq:
105
+ * stylesheet.apply(document, {params}) -> XML::Document
106
+ *
107
+ * Apply this stylesheet transformation to the provided document.
108
+ * This method may be invoked multiple times.
109
+ *
110
+ * Params:
111
+ * * document - An instance of an XML::Document
112
+ * * params - An optional hash table that specifies the values for xsl:param values embedded in the stylesheet.
113
+ *
114
+ * Example:
115
+ *
116
+ * stylesheet_doc = XML::Document.file('stylesheet_file')
117
+ * stylesheet = XSLT::Stylesheet.new(stylesheet_doc)
118
+ *
119
+ * xml_doc = XML::Document.file('xml_file')
120
+ * result = stylesheet.apply(xml_doc)
121
+ * result = stylesheet.apply(xml_doc, {:foo => 'bar'})
122
+ */
123
+ static VALUE
124
+ ruby_xslt_stylesheet_apply(int argc, VALUE *argv, VALUE self) {
125
+ xmlDocPtr xdoc;
126
+ xsltStylesheetPtr xstylesheet;
127
+ xmlDocPtr result;
128
+ VALUE document;
129
+ VALUE params;
130
+ int i;
131
+
132
+ char** pParams;
133
+
134
+ if (argc > 2 || argc < 1)
135
+ rb_raise(rb_eArgError, "wrong number of arguments (need 1 or 2)");
136
+
137
+ document = argv[0];
138
+
139
+ if (!rb_obj_is_kind_of(document, ruby_xslt_stylesheet_document_klass()))
140
+ rb_raise(rb_eTypeError, "Must pass in an XML::Document instance.");
141
+
142
+ /* Make sure params is a flat array */
143
+ params = (argc == 2 ? argv[1]: Qnil);
144
+ params = rb_Array(params);
145
+ rb_funcall(params, rb_intern("flatten!"), 0);
146
+ pParams = ruby_xslt_coerce_params(params);
147
+
148
+ Data_Get_Struct(document, xmlDoc, xdoc);
149
+ Data_Get_Struct(self, xsltStylesheet, xstylesheet);
150
+
151
+ result = xsltApplyStylesheet(xstylesheet, xdoc, (const char**)pParams);
152
+
153
+ if (!result)
154
+ rb_raise(eXSLTError, "Transformation failed");
155
+
156
+ /* Free allocated array of *chars. Note we don't have to
157
+ free the last array item since its set to NULL. */
158
+ for (i=0; i<(RARRAY_LEN(params)); i++) {
159
+ ruby_xfree(pParams[i]);
160
+ }
161
+ ruby_xfree(pParams);
162
+
163
+ return rxml_document_wrap(result);
164
+ }
165
+
166
+
167
+ /* call-seq:
168
+ * sheet.debug(to = $stdout) => (true|false)
169
+ *
170
+ * Output a debug dump of this stylesheet to the specified output
171
+ * stream (an instance of IO, defaults to $stdout). Requires
172
+ * libxml/libxslt be compiled with debugging enabled. If this
173
+ * is not the case, a warning is triggered and the method returns
174
+ * false.
175
+ */
176
+ /*VALUE
177
+ ruby_xslt_stylesheet_debug(int argc, VALUE *argv, VALUE self) {
178
+ #ifdef LIBXML_DEBUG_ENABLED
179
+ OpenFile *fptr;
180
+ VALUE io;
181
+ FILE *out;
182
+ rxml_document_t *parsed;
183
+ ruby_xslt_stylesheet *xss;
184
+
185
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
186
+ if (NIL_P(xss->parsed))
187
+ rb_raise(eXMLXSLTStylesheetRequireParsedDoc, "must have a parsed XML result");
188
+
189
+ switch (argc) {
190
+ case 0:
191
+ io = rb_stdout;
192
+ break;
193
+ case 1:
194
+ io = argv[0];
195
+ if (rb_obj_is_kind_of(io, rb_cIO) == Qfalse)
196
+ rb_raise(rb_eTypeError, "need an IO object");
197
+ break;
198
+ default:
199
+ rb_raise(rb_eArgError, "wrong number of arguments (0 or 1)");
200
+ }
201
+
202
+ Data_Get_Struct(xss->parsed, rxml_document_t, parsed);
203
+ if (parsed->doc == NULL)
204
+ return(Qnil);
205
+
206
+ GetOpenFile(io, fptr);
207
+ rb_io_check_writable(fptr);
208
+ out = GetWriteFile(fptr);
209
+ xmlDebugDumpDocument(out, parsed->doc);
210
+ return(Qtrue);
211
+ #else
212
+ rb_warn("libxml/libxslt was compiled without debugging support. Please recompile libxml/libxslt and their Ruby modules");
213
+ return(Qfalse);
214
+ #endif
215
+ }
216
+ */
217
+
218
+ // TODO should this automatically apply the sheet if not already,
219
+ // given that we're unlikely to do much else with it?
220
+
221
+ /* call-seq:
222
+ * sheet.print(to = $stdout) => number_of_bytes
223
+ *
224
+ * Output the result of the transform to the specified output
225
+ * stream (an IO instance, defaults to $stdout). You *must* call
226
+ * +apply+ before this method or an exception will be raised.
227
+ */
228
+ /*VALUE
229
+ ruby_xslt_stylesheet_print(int argc, VALUE *argv, VALUE self) {
230
+ OpenFile *fptr;
231
+ VALUE io;
232
+ FILE *out;
233
+ rxml_document_t *parsed;
234
+ ruby_xslt_stylesheet *xss;
235
+ int bytes;
236
+
237
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
238
+ if (NIL_P(xss->parsed))
239
+ rb_raise(eXMLXSLTStylesheetRequireParsedDoc, "must have a parsed XML result");
240
+
241
+ switch (argc) {
242
+ case 0:
243
+ io = rb_stdout;
244
+ break;
245
+ case 1:
246
+ io = argv[0];
247
+ if (rb_obj_is_kind_of(io, rb_cIO) == Qfalse)
248
+ rb_raise(rb_eTypeError, "need an IO object");
249
+ break;
250
+ default:
251
+ rb_raise(rb_eArgError, "wrong number of arguments (0 or 1)");
252
+ }
253
+
254
+ Data_Get_Struct(xss->parsed, rxml_document_t, parsed);
255
+ if (parsed->doc == NULL)
256
+ return(Qnil);
257
+
258
+ GetOpenFile(io, fptr);
259
+ rb_io_check_writable(fptr);
260
+ out = GetWriteFile(fptr);
261
+ bytes = xsltSaveResultToFile(out, parsed->doc, xss->xsp);
262
+
263
+ return(INT2NUM(bytes));
264
+ }*/
265
+
266
+ /* call-seq:
267
+ * stylesheet.save_result(doc, to = $stdout) => number_of_bytes
268
+ *
269
+ * Output an xml document, usually the result of an xslt transformation,
270
+ * to the specified output stream (an IO instance, defaults to $stdout).
271
+ * Output will be done according to the output specification in the xslt
272
+ * stylesheet.
273
+ *
274
+ * Returns the number of bytes written.
275
+ */
276
+ VALUE
277
+ ruby_xslt_stylesheet_save_result(int argc, VALUE *argv, VALUE self) {
278
+ xmlDocPtr xdoc;
279
+ xsltStylesheetPtr xstylesheet;
280
+ VALUE document;
281
+ OpenFile *fptr;
282
+ VALUE io;
283
+ FILE *out;
284
+ int bytes;
285
+
286
+ if (argc > 2 || argc < 1)
287
+ rb_raise(rb_eArgError, "wrong number of arguments (need 1 or 2)");
288
+
289
+ document = argv[0];
290
+
291
+ if (!rb_obj_is_kind_of(document, ruby_xslt_stylesheet_document_klass()))
292
+ rb_raise(rb_eTypeError, "Must pass in an XML::Document instance.");
293
+
294
+ switch (argc) {
295
+ case 1:
296
+ io = rb_stdout;
297
+ break;
298
+ case 2:
299
+ io = argv[1];
300
+ if (rb_obj_is_kind_of(io, rb_cIO) == Qfalse)
301
+ rb_raise(rb_eTypeError, "need an IO object");
302
+ break;
303
+ default:
304
+ rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
305
+ }
306
+
307
+ GetOpenFile(io, fptr);
308
+ rb_io_check_writable(fptr);
309
+ out = GetWriteFile(fptr);
310
+
311
+ Data_Get_Struct(document, xmlDoc, xdoc);
312
+ Data_Get_Struct(self, xsltStylesheet, xstylesheet);
313
+
314
+ bytes = xsltSaveResultToFile(out, xdoc, xstylesheet);
315
+ if ( bytes == -1 ) {
316
+ rb_raise(rb_eRuntimeError, "error saving document");
317
+ }
318
+
319
+ return(INT2NUM(bytes));
320
+ }
321
+
322
+ /* call-seq:
323
+ * stylesheet.dump_result(doc) => string
324
+ *
325
+ * Dump an xml document, usually the result of an xslt transformation,
326
+ * and return the result as a string.
327
+ * Output will be done according to the output specification in the xslt
328
+ * stylesheet. Note that this includes the encoding of the string.
329
+ */
330
+ VALUE
331
+ ruby_xslt_stylesheet_dump_result(VALUE self, VALUE document) {
332
+ // FIXME: set string encoding in ruby 1.9?
333
+ xmlDocPtr xdoc;
334
+ xsltStylesheetPtr xstylesheet;
335
+ xmlChar *result = NULL;
336
+ int len = 0, bytes = 0;
337
+ VALUE rresult;
338
+
339
+ if (!rb_obj_is_kind_of(document, ruby_xslt_stylesheet_document_klass()))
340
+ rb_raise(rb_eTypeError, "Must pass in an XML::Document instance.");
341
+
342
+ Data_Get_Struct(document, xmlDoc, xdoc);
343
+ Data_Get_Struct(self, xsltStylesheet, xstylesheet);
344
+
345
+ bytes = xsltSaveResultToString(&result, &len,
346
+ xdoc, xstylesheet);
347
+ if ( bytes == -1 ) {
348
+ rb_raise(rb_eRuntimeError, "error dumping document");
349
+ }
350
+
351
+ rresult=rb_str_new((const char*)result,len);
352
+ xmlFree(result);
353
+ return rresult;
354
+ }
355
+
356
+ #ifdef RDOC_NEVER_DEFINED
357
+ cLibXSLT = rb_define_module("LibXSLT");
358
+ cXSLT = rb_define_module_under(cLibXSLT, "XSLT");
359
+ #endif
360
+
361
+ void
362
+ ruby_init_xslt_stylesheet(void) {
363
+ cXSLTStylesheet = rb_define_class_under(cXSLT, "Stylesheet", rb_cObject);
364
+ rb_define_alloc_func(cXSLTStylesheet, ruby_xslt_stylesheet_alloc);
365
+ rb_define_method(cXSLTStylesheet, "initialize", ruby_xslt_stylesheet_initialize, 1);
366
+ rb_define_method(cXSLTStylesheet, "apply", ruby_xslt_stylesheet_apply, -1);
367
+ rb_define_method(cXSLTStylesheet, "save_result", ruby_xslt_stylesheet_save_result, -1);
368
+ rb_define_method(cXSLTStylesheet, "dump_result", ruby_xslt_stylesheet_dump_result, 1);
369
+ }