Permalink
Browse files

Merge remote-tracking branch 'ender672/custom_xslt_func'

Conflicts:
	ext/nokogiri/xslt_stylesheet.c
  • Loading branch information...
2 parents be4851d + cf0e5a3 commit bb0689d578c87884510bb68327fd8c62108aadfd @ender672 committed Nov 23, 2011
Showing with 51 additions and 9 deletions.
  1. +46 −9 ext/nokogiri/xslt_stylesheet.c
  2. +5 −0 ext/nokogiri/xslt_stylesheet.h
View
55 ext/nokogiri/xslt_stylesheet.c
@@ -10,11 +10,20 @@ VALUE xslt;
int vasprintf (char **strp, const char *fmt, va_list ap);
void vasprintf_free (void *p);
-static void dealloc(xsltStylesheetPtr doc)
+static void mark(nokogiriXsltStylesheetTuple *wrapper)
{
+ rb_gc_mark(wrapper->func_instances);
+}
+
+static void dealloc(nokogiriXsltStylesheetTuple *wrapper)
+{
+ xsltStylesheetPtr doc = wrapper->ss;
+
NOKOGIRI_DEBUG_START(doc);
xsltFreeStylesheet(doc); /* commented out for now. */
NOKOGIRI_DEBUG_END(doc);
+
+ free(wrapper);
}
static void xslt_generic_error_handler(void * ctx, const char *msg, ...)
@@ -31,6 +40,21 @@ static void xslt_generic_error_handler(void * ctx, const char *msg, ...)
vasprintf_free(message);
}
+VALUE Nokogiri_wrap_xslt_stylesheet(xsltStylesheetPtr ss)
+{
+ VALUE self;
+ nokogiriXsltStylesheetTuple *wrapper;
+
+ self = Data_Make_Struct(cNokogiriXsltStylesheet, nokogiriXsltStylesheetTuple,
+ mark, dealloc, wrapper);
+
+ ss->_private = (void *)self;
+ wrapper->ss = ss;
+ wrapper->func_instances = rb_ary_new();
+
+ return self;
+}
+
/*
* call-seq:
* parse_stylesheet_doc(document)
@@ -59,7 +83,7 @@ static VALUE parse_stylesheet_doc(VALUE klass, VALUE xmldocobj)
rb_exc_raise(exception);
}
- return Data_Wrap_Struct(klass, NULL, dealloc, ss);
+ return Nokogiri_wrap_xslt_stylesheet(ss);
}
@@ -72,14 +96,14 @@ static VALUE parse_stylesheet_doc(VALUE klass, VALUE xmldocobj)
static VALUE serialize(VALUE self, VALUE xmlobj)
{
xmlDocPtr xml ;
- xsltStylesheetPtr ss ;
+ nokogiriXsltStylesheetTuple *wrapper;
xmlChar* doc_ptr ;
int doc_len ;
VALUE rval ;
Data_Get_Struct(xmlobj, xmlDoc, xml);
- Data_Get_Struct(self, xsltStylesheet, ss);
- xsltSaveResultToString(&doc_ptr, &doc_len, xml, ss);
+ Data_Get_Struct(self, nokogiriXsltStylesheetTuple, wrapper);
+ xsltSaveResultToString(&doc_ptr, &doc_len, xml, wrapper->ss);
rval = NOKOGIRI_STR_NEW(doc_ptr, doc_len);
xmlFree(doc_ptr);
return rval ;
@@ -105,7 +129,7 @@ static VALUE transform(int argc, VALUE* argv, VALUE self)
VALUE xmldoc, paramobj ;
xmlDocPtr xml ;
xmlDocPtr result ;
- xsltStylesheetPtr ss ;
+ nokogiriXsltStylesheetTuple *wrapper;
const char** params ;
long param_len, j ;
@@ -123,7 +147,7 @@ static VALUE transform(int argc, VALUE* argv, VALUE self)
Check_Type(paramobj, T_ARRAY);
Data_Get_Struct(xmldoc, xmlDoc, xml);
- Data_Get_Struct(self, xsltStylesheet, ss);
+ Data_Get_Struct(self, nokogiriXsltStylesheetTuple, wrapper);
param_len = RARRAY_LEN(paramobj);
params = calloc((size_t)param_len+1, sizeof(char*));
@@ -134,7 +158,7 @@ static VALUE transform(int argc, VALUE* argv, VALUE self)
}
params[param_len] = 0 ;
- result = xsltApplyStylesheet(ss, xml, params);
+ result = xsltApplyStylesheet(wrapper->ss, xml, params);
free(params);
if (!result) rb_raise(rb_eRuntimeError, "could not perform xslt transform on document");
@@ -218,6 +242,8 @@ static void * initFunc(xsltTransformContextPtr ctxt, const xmlChar *uri)
VALUE obj = rb_hash_aref(modules, rb_str_new2((const char *)uri));
VALUE args = { Qfalse };
VALUE methods = rb_funcall(obj, rb_intern("instance_methods"), 1, args);
+ VALUE inst;
+ nokogiriXsltStylesheetTuple *wrapper;
int i;
for(i = 0; i < RARRAY_LEN(methods); i++) {
@@ -226,12 +252,23 @@ static void * initFunc(xsltTransformContextPtr ctxt, const xmlChar *uri)
(unsigned char *)StringValuePtr(method_name), uri, method_caller);
}
- return (void *)rb_class_new_instance(0, NULL, obj);
+ Data_Get_Struct(ctxt->style->_private, nokogiriXsltStylesheetTuple,
+ wrapper);
+ inst = rb_class_new_instance(0, NULL, obj);
+ rb_ary_push(wrapper->func_instances, inst);
+
+ return (void *)inst;
}
static void shutdownFunc(xsltTransformContextPtr ctxt,
const xmlChar *uri, void *data)
{
+ nokogiriXsltStylesheetTuple *wrapper;
+
+ Data_Get_Struct(ctxt->style->_private, nokogiriXsltStylesheetTuple,
+ wrapper);
+
+ rb_ary_clear(wrapper->func_instances);
}
/*
View
5 ext/nokogiri/xslt_stylesheet.h
@@ -6,4 +6,9 @@
void init_xslt_stylesheet();
extern VALUE cNokogiriXsltStylesheet ;
+
+typedef struct _nokogiriXsltStylesheetTuple {
+ xsltStylesheetPtr ss;
+ VALUE func_instances;
+} nokogiriXsltStylesheetTuple;
#endif

0 comments on commit bb0689d

Please sign in to comment.