libxslt-ruby 0.6.0-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
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
+ // Includes from libxml-ruby
20
+ #include <libxml/ruby_libxml.h>
21
+ #include <libxml/ruby_xml_document.h>
22
+
23
+
24
+ #include "ruby_xslt_stylesheet.h"
25
+ #include "ruby_xslt_transform_context.h"
26
+
27
+ #include "version.h"
28
+
29
+ #define RUBY_LIBXSLT_SRC_TYPE_NULL 0
30
+ #define RUBY_LIBXSLT_SRC_TYPE_FILE 1
31
+
32
+ extern VALUE mXML;
33
+ //extern VALUE cXMLDocument;
34
+
35
+ extern VALUE cXSLT;
36
+ extern VALUE eXMLXSLTStylesheetRequireParsedDoc;
37
+
38
+ typedef struct ruby_xslt {
39
+ int data_type;
40
+ void *data;
41
+ VALUE str;
42
+ VALUE xml_doc_obj;
43
+ VALUE ctxt;
44
+ xsltStylesheetPtr xsp;
45
+ } ruby_xslt;
46
+
47
+ #if ((RUBY_LIBXML_VER_MAJ != RUBY_LIBXSLT_VER_MAJ) || (RUBY_LIBXML_VER_MIN != RUBY_LIBXSLT_VER_MIN))
48
+ #error "Incompatible LibXML-Ruby headers - please install same major/micro version"
49
+ #endif
50
+
51
+ #endif
@@ -0,0 +1,298 @@
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
+
8
+ VALUE cXSLTStylesheet;
9
+
10
+ /* call-seq:
11
+ * sheet.apply => (true|false)
12
+ *
13
+ * Apply this stylesheet transformation to the source
14
+ * document.
15
+ */
16
+ VALUE
17
+ ruby_xslt_stylesheet_apply(int argc, VALUE *argv, VALUE self) {
18
+ ruby_xslt_stylesheet *xss;
19
+ ruby_xml_document_t *rxd;
20
+ const char **params;
21
+ VALUE parameter, tmp;
22
+ int i, len;
23
+
24
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
25
+
26
+ if (NIL_P(xss->xml_doc_obj))
27
+ rb_raise(rb_eArgError, "Need a document object");
28
+
29
+ Data_Get_Struct(xss->xml_doc_obj, ruby_xml_document_t, rxd);
30
+
31
+ params = NULL;
32
+
33
+ switch(argc) {
34
+ case 0:
35
+ break;
36
+ case 1:
37
+ parameter = argv[0];
38
+ #if RUBY_VERSION_CODE >= 180
39
+ if (TYPE(parameter) == T_HASH) {
40
+ /* Convert parameter to an array */
41
+ parameter = rb_hash_to_a(parameter);
42
+ }
43
+ #endif
44
+
45
+ if (TYPE(parameter) == T_ARRAY) {
46
+ /* A hash is better than an array, but we can live with an array of arrays */
47
+ len = RARRAY(parameter)->len;
48
+ params = (void *)ALLOC_N(char *, (len * 2) + 2);
49
+ for (i=0; i < RARRAY(parameter)->len; i++) {
50
+ tmp = RARRAY(parameter)->ptr[i];
51
+
52
+ Check_Type(tmp, T_ARRAY);
53
+ Check_Type(RARRAY(tmp)->ptr[0], T_STRING);
54
+ Check_Type(RARRAY(tmp)->ptr[1], T_STRING);
55
+
56
+ params[2*i] = RSTRING(RARRAY(tmp)->ptr[0])->ptr;
57
+ params[2*i+1] = RSTRING(RARRAY(tmp)->ptr[1])->ptr;
58
+ }
59
+ params[2*i] = params[2*i+1] = 0;
60
+ } else {
61
+ /* I should test to see if the object responds to to_a and to_h before calling this, but oh well */
62
+ rb_raise(rb_eTypeError, "xslt_stylesheet_appy: expecting a hash or an array of arrays as a parameter");
63
+ }
64
+
65
+ break;
66
+ default:
67
+ rb_raise(rb_eArgError, "wrong number of arguments (0 or 1)");
68
+ }
69
+
70
+ xss->parsed = ruby_xml_document_wrap(xsltApplyStylesheet(xss->xsp,
71
+ rxd->doc, params));
72
+
73
+ if (params) {
74
+ ruby_xfree(params);
75
+ }
76
+
77
+ if (xss->parsed == Qnil)
78
+ return(Qfalse);
79
+ else
80
+ return(Qtrue);
81
+ }
82
+
83
+
84
+ /* call-seq:
85
+ * sheet.debug(to = $stdout) => (true|false)
86
+ *
87
+ * Output a debug dump of this stylesheet to the specified output
88
+ * stream (an instance of IO, defaults to $stdout). Requires
89
+ * libxml/libxslt be compiled with debugging enabled. If this
90
+ * is not the case, a warning is triggered and the method returns
91
+ * false.
92
+ */
93
+ VALUE
94
+ ruby_xslt_stylesheet_debug(int argc, VALUE *argv, VALUE self) {
95
+ #ifdef LIBXML_DEBUG_ENABLED
96
+ OpenFile *fptr;
97
+ VALUE io;
98
+ FILE *out;
99
+ ruby_xml_document_t *parsed;
100
+ ruby_xslt_stylesheet *xss;
101
+
102
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
103
+ if (NIL_P(xss->parsed))
104
+ rb_raise(eXMLXSLTStylesheetRequireParsedDoc, "must have a parsed XML result");
105
+
106
+ switch (argc) {
107
+ case 0:
108
+ io = rb_stdout;
109
+ break;
110
+ case 1:
111
+ io = argv[0];
112
+ if (rb_obj_is_kind_of(io, rb_cIO) == Qfalse)
113
+ rb_raise(rb_eTypeError, "need an IO object");
114
+ break;
115
+ default:
116
+ rb_raise(rb_eArgError, "wrong number of arguments (0 or 1)");
117
+ }
118
+
119
+ Data_Get_Struct(xss->parsed, ruby_xml_document_t, parsed);
120
+ if (parsed->doc == NULL)
121
+ return(Qnil);
122
+
123
+ GetOpenFile(io, fptr);
124
+ rb_io_check_writable(fptr);
125
+ out = GetWriteFile(fptr);
126
+ xmlDebugDumpDocument(out, parsed->doc);
127
+ return(Qtrue);
128
+ #else
129
+ rb_warn("libxml/libxslt was compiled without debugging support. Please recompile libxml/libxslt and their Ruby modules");
130
+ return(Qfalse);
131
+ #endif
132
+ }
133
+
134
+
135
+ void
136
+ ruby_xslt_stylesheet_free(ruby_xslt_stylesheet *xss) {
137
+ if (xss->xsp != NULL) {
138
+ xsltFreeStylesheet(xss->xsp);
139
+ xss->xsp = NULL;
140
+ }
141
+
142
+ ruby_xfree(xss);
143
+ }
144
+
145
+
146
+ void
147
+ ruby_xslt_stylesheet_mark(ruby_xslt_stylesheet *xss) {
148
+ if (!NIL_P(xss->parsed)) rb_gc_mark(xss->parsed);
149
+ if (!NIL_P(xss->xml_doc_obj)) rb_gc_mark(xss->xml_doc_obj);
150
+
151
+ switch (xss->data_type) {
152
+ case RUBY_LIBXSLT_SRC_TYPE_FILE:
153
+ if (xss->data != NULL)
154
+ rb_gc_mark((VALUE)xss->data);
155
+ break;
156
+ }
157
+ }
158
+
159
+
160
+ VALUE
161
+ ruby_xslt_stylesheet_new(VALUE class, xsltStylesheetPtr xsp) {
162
+ ruby_xslt_stylesheet *xss;
163
+ VALUE rval;
164
+
165
+ rval=Data_Make_Struct(cXSLTStylesheet,ruby_xslt_stylesheet,ruby_xslt_stylesheet_mark,
166
+ ruby_xslt_stylesheet_free,xss);
167
+ xss->xsp = xsp;
168
+ xss->xml_doc_obj = Qnil;
169
+ xss->parsed = Qnil;
170
+ xss->data_type = RUBY_LIBXSLT_SRC_TYPE_NULL;
171
+ xss->data = NULL;
172
+
173
+ return rval;
174
+ }
175
+
176
+ // TODO should this automatically apply the sheet if not already,
177
+ // given that we're unlikely to do much else with it?
178
+
179
+ /* call-seq:
180
+ * sheet.print(to = $stdout) => number_of_bytes
181
+ *
182
+ * Output the result of the transform to the specified output
183
+ * stream (an IO instance, defaults to $stdout). You *must* call
184
+ * +apply+ before this method or an exception will be raised.
185
+ */
186
+ VALUE
187
+ ruby_xslt_stylesheet_print(int argc, VALUE *argv, VALUE self) {
188
+ OpenFile *fptr;
189
+ VALUE io;
190
+ FILE *out;
191
+ ruby_xml_document_t *parsed;
192
+ ruby_xslt_stylesheet *xss;
193
+ int bytes;
194
+
195
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
196
+ if (NIL_P(xss->parsed))
197
+ rb_raise(eXMLXSLTStylesheetRequireParsedDoc, "must have a parsed XML result");
198
+
199
+ switch (argc) {
200
+ case 0:
201
+ io = rb_stdout;
202
+ break;
203
+ case 1:
204
+ io = argv[0];
205
+ if (rb_obj_is_kind_of(io, rb_cIO) == Qfalse)
206
+ rb_raise(rb_eTypeError, "need an IO object");
207
+ break;
208
+ default:
209
+ rb_raise(rb_eArgError, "wrong number of arguments (0 or 1)");
210
+ }
211
+
212
+ Data_Get_Struct(xss->parsed, ruby_xml_document_t, parsed);
213
+ if (parsed->doc == NULL)
214
+ return(Qnil);
215
+
216
+ GetOpenFile(io, fptr);
217
+ rb_io_check_writable(fptr);
218
+ out = GetWriteFile(fptr);
219
+ bytes = xsltSaveResultToFile(out, parsed->doc, xss->xsp);
220
+
221
+ return(INT2NUM(bytes));
222
+ }
223
+
224
+ // TODO this, too. Either way, to_s probably should have prereqs
225
+ // like this, for one thing it makes IRB use tricky...
226
+
227
+ /* call-seq:
228
+ * sheet.to_s => "result"
229
+ *
230
+ * Obtain the result of the transform as a string. You *must* call
231
+ * +apply+ before this method or an exception will be raised.
232
+ */
233
+ VALUE
234
+ ruby_xslt_stylesheet_to_s(VALUE self) {
235
+ ruby_xml_document_t *parsed;
236
+ ruby_xslt_stylesheet *xss;
237
+ xmlChar *str;
238
+ int len;
239
+
240
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
241
+ if (NIL_P(xss->parsed))
242
+ rb_raise(eXMLXSLTStylesheetRequireParsedDoc, "must have a parsed XML result");
243
+ Data_Get_Struct(xss->parsed, ruby_xml_document_t, parsed);
244
+ if (parsed->doc == NULL)
245
+ return(Qnil);
246
+
247
+ xsltSaveResultToString(&str, &len, parsed->doc, xss->xsp);
248
+ if (str == NULL)
249
+ return(Qnil);
250
+ else
251
+ return(rb_str_new((const char*)str,len));
252
+ }
253
+
254
+
255
+
256
+ /* call-seq:
257
+ * sheet.save(io) => true
258
+ *
259
+ * Save the result of the transform to the supplied open
260
+ * file (an IO instance). You *must* call +apply+ before
261
+ * this method or an exception will be raised.
262
+ */
263
+ VALUE
264
+ ruby_xslt_stylesheet_save(VALUE self, VALUE io) {
265
+ ruby_xml_document_t *parsed;
266
+ ruby_xslt_stylesheet *xss;
267
+ OpenFile *fptr;
268
+
269
+ if (rb_obj_is_kind_of(io, rb_cIO) == Qfalse)
270
+ rb_raise(rb_eArgError, "Only accept IO objects for saving");
271
+
272
+ GetOpenFile(io, fptr);
273
+
274
+ Data_Get_Struct(self, ruby_xslt_stylesheet, xss);
275
+ Data_Get_Struct(xss->parsed, ruby_xml_document_t, parsed);
276
+
277
+ xsltSaveResultToFile(fptr->f, parsed->doc, xss->xsp);
278
+
279
+ return(Qtrue);
280
+ }
281
+
282
+ #ifdef RDOC_NEVER_DEFINED
283
+ mXML = rb_define_module("XML");
284
+ cXSLT = rb_define_class_under(mXML, "XSLT", rb_cObject);
285
+ #endif
286
+
287
+ void
288
+ ruby_init_xslt_stylesheet(void) {
289
+ cXSLTStylesheet = rb_define_class_under(cXSLT, "Stylesheet", rb_cObject);
290
+ eXMLXSLTStylesheetRequireParsedDoc =
291
+ rb_define_class_under(cXSLTStylesheet, "RequireParsedDoc", rb_eException);
292
+
293
+ rb_define_method(cXSLTStylesheet, "apply", ruby_xslt_stylesheet_apply, -1);
294
+ rb_define_method(cXSLTStylesheet, "debug", ruby_xslt_stylesheet_debug, -1);
295
+ rb_define_method(cXSLTStylesheet, "print", ruby_xslt_stylesheet_print, -1);
296
+ rb_define_method(cXSLTStylesheet, "to_s", ruby_xslt_stylesheet_to_s, 0);
297
+ rb_define_method(cXSLTStylesheet, "save", ruby_xslt_stylesheet_save, 1);
298
+ }
@@ -0,0 +1,21 @@
1
+ /* $Id: ruby_xslt_stylesheet.h 42 2007-12-07 06:09:35Z transami $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information. */
4
+
5
+ #ifndef __RUBY_LIBXSLT_STYLESHEET__
6
+ #define __RUBY_LIBXSLT_STYLESHEET__
7
+
8
+ extern VALUE cXSLTStylesheet;
9
+
10
+ typedef struct ruby_xslt_stylesheet {
11
+ int data_type;
12
+ void *data;
13
+ VALUE parsed; /* XML::Document # parsed xml document after xsl apply */
14
+ VALUE xml_doc_obj; /* XML::Document */
15
+ xsltStylesheetPtr xsp;
16
+ } ruby_xslt_stylesheet;
17
+
18
+ void ruby_init_xslt_stylesheet(void);
19
+ VALUE ruby_xslt_stylesheet_new(VALUE class, xsltStylesheetPtr xsp);
20
+
21
+ #endif /* __RUBY_LIBXSLT_STYLESHEET__ */
@@ -0,0 +1,61 @@
1
+ /* $Id: ruby_xslt_transform_context.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 "ruby_xslt_transform_context.h"
7
+
8
+ VALUE cXSLTTransformContext;
9
+
10
+ void
11
+ ruby_xslt_transform_context_free(ruby_xslt_transform_context *rxtc) {
12
+ if (rxtc->ctxt != NULL) {
13
+ xsltFreeTransformContext(rxtc->ctxt);
14
+ rxtc->ctxt = NULL;
15
+ }
16
+ ruby_xfree(rxtc);
17
+ }
18
+
19
+ void
20
+ ruby_xslt_transform_context_mark(ruby_xslt_transform_context *rxtc) {
21
+ if (rxtc == NULL) return;
22
+ if (!NIL_P(rxtc->xslt)) rb_gc_mark(rxtc->xslt);
23
+ }
24
+
25
+
26
+ VALUE
27
+ ruby_xslt_transform_context_new(VALUE class, VALUE xslt,
28
+ xsltTransformContextPtr ctxt) {
29
+ ruby_xslt_transform_context *rxtc;
30
+ rxtc = ALLOC(ruby_xslt_transform_context);
31
+ rxtc->ctxt = ctxt;
32
+ rxtc->xslt = xslt;
33
+ //fprintf(stderr,"ruby_xslt_transform_context_new 2\n");
34
+ //if (class == Qfalse)
35
+ //fprintf(stderr,"ruby_xslt_transform_context_new: EEEEK!\n");
36
+ return(Data_Wrap_Struct(class, ruby_xslt_transform_context_mark,
37
+ ruby_xslt_transform_context_free, rxtc));
38
+ }
39
+
40
+
41
+ VALUE
42
+ ruby_xslt_transform_context_new2(VALUE class, VALUE xslt) {
43
+ return(ruby_xslt_transform_context_new(class, xslt, NULL));
44
+ }
45
+
46
+
47
+ VALUE
48
+ ruby_xslt_transform_context_new3(VALUE xslt) {
49
+ return(ruby_xslt_transform_context_new2(cXSLTTransformContext, xslt));
50
+ }
51
+
52
+ #ifdef RDOC_NEVER_DEFINED
53
+ mXML = rb_define_module("XML");
54
+ cXSLT = rb_define_class_under(mXML, "XSLT", rb_cObject);
55
+ #endif
56
+
57
+ void
58
+ ruby_init_xslt_transform_context(void) {
59
+ cXSLTTransformContext =
60
+ rb_define_class_under(cXSLT, "TransformContext", rb_cObject);
61
+ }
@@ -0,0 +1,22 @@
1
+ /* $Id: ruby_xslt_transform_context.h 42 2007-12-07 06:09:35Z transami $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #ifndef __RUBY_XSLT_TRANSFORM_CONTEXT__
6
+ #define __RUBY_XSLT_TRANSFORM_CONTEXT__
7
+
8
+ extern VALUE cXSLTTransformContext;
9
+
10
+ typedef struct ruby_xslt_transform_context {
11
+ xsltTransformContextPtr ctxt;
12
+ VALUE xslt;
13
+ } ruby_xslt_transform_context;
14
+
15
+ void ruby_init_xslt_transform_context(void);
16
+ void ruby_xslt_transform_context_free(ruby_xslt_transform_context *ctxt);
17
+ void ruby_xslt_transform_context_mark(ruby_xslt_transform_context *ctxt);
18
+ VALUE ruby_xslt_transform_context_new(VALUE class, VALUE xslt, xsltTransformContextPtr ctxt);
19
+ VALUE ruby_xslt_transform_context_new2(VALUE class, VALUE xslt);
20
+ VALUE ruby_xslt_transform_context_new3(VALUE xslt);
21
+
22
+ #endif