Re: [xml] adding transformContexts to Python



Daniel Veillard <veillard redhat com> writes:

  I agree not everything is available at the python level, usually what
got in was driven by user interest, first myself and then other people.
Patches to fill the gap gratefully accepted (but will be reviewed of
course ;-)

I've attached a simple diff of the changed bits to facilitate
transform content accessing.

This works for me... though I've yet to write a comprehensive test.


Can someone shout if they think I'm on the wrong track here.

-- 
Nic Ferrier
http://www.tapsellferrier.co.uk   for all your tapsell ferrier needs

diff -u libxslt-1.1.17/python/libxslt-python-api.xml ../libxslt-1.1.16/python/libxslt-python-api.xml
--- libxslt-1.1.17/python/libxslt-python-api.xml        2006-09-01 03:14:20.000000000 +0100
+++ ../libxslt-1.1.16/python/libxslt-python-api.xml     2006-09-01 02:25:49.000000000 +0100
@@ -11,6 +11,12 @@
       <arg name='style' type='xsltStylesheetPtr' info='a parsed XSLT stylesheet'/>
       <arg name='result' type='xmlDocPtr' info='The result document'/>
     </function>
+    <function name='xsltNewTransformContext' file='python'>
+      <info>Create a new XSLT TransformContext</info>
+      <return type='xsltTransformContextPtr' info='an xslt TransformContext'/>
+      <arg name='style' type='xsltStylesheetPtr' info='a parsed XSLT stylesheet'/>
+      <arg name='doc' type='xmlDocPtr' info='the input document'/>
+    </function>
     <function name='xsltApplyStylesheet' file='python'>
       <info>Apply the stylesheet to the document</info>
       <return type='xmlDocPtr' info="the result document or NULL in case of error"/>
@@ -18,6 +24,14 @@
       <arg name='doc' type='xmlDocPtr' info='a parsed XML document'/>
       <arg name='params' type='pythonObject' info='the parameters dictionnary'/>
     </function>
+    <function name='xsltApplyStylesheetUser' file='python'>
+      <info>Apply the stylesheet to the document</info>
+      <return type='xmlDocPtr' info="the result document or NULL in case of error"/>
+      <arg name='style' type='xsltStylesheetPtr' info='a parsed XSLT stylesheet'/>
+      <arg name='doc' type='xmlDocPtr' info='a parsed XML document'/>
+      <arg name='params' type='pythonObject' info='the parameters dictionnary'/>
+      <arg name='transformCtxt' type='xsltTransformContextPtr' info='transformation context'/>
+    </function>
     <function name='xsltRegisterErrorHandler' file='python'>
       <info>Register a Python written function to for error reporting. The function is called back as f(ctx, 
error).</info>
       <return type='int' info="1 in case of success, 0 or -1 in case of error"/>
diff -u libxslt-1.1.17/python/libxslt.c ../libxslt-1.1.16/python/libxslt.c
--- libxslt-1.1.17/python/libxslt.c     2004-07-02 14:49:07.000000000 +0100
+++ ../libxslt-1.1.16/python/libxslt.c  2006-09-01 03:09:16.000000000 +0100
@@ -502,6 +502,106 @@
     return(py_retval);
 }
 
+
+PyObject *
+libxslt_xsltNewTransformContext(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
+    PyObject *py_retval;
+    PyObject *pyobj_style;
+    PyObject *pyobj_doc;
+    xsltStylesheetPtr style;
+    xmlDocPtr doc;
+    xsltTransformContextPtr c_retval;
+
+    if (!PyArg_ParseTuple(args, (char *) "OO:xsltNewTransformContext",
+                         &pyobj_style, &pyobj_doc))
+        return(NULL);
+
+    style = (xsltStylesheetPtr) Pystylesheet_Get(pyobj_style);
+    doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
+    
+    c_retval = xsltNewTransformContext(style, doc);
+    py_retval = libxslt_xsltTransformContextPtrWrap((xsltTransformContextPtr) c_retval);
+    return (py_retval);
+}
+
+
+PyObject *
+libxslt_xsltApplyStylesheetUser(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
+    PyObject *py_retval;
+    xmlDocPtr c_retval;
+    xsltStylesheetPtr style;
+    PyObject *pyobj_style;
+    xmlDocPtr doc;
+    xsltTransformContextPtr transformCtxt;
+    PyObject *pyobj_doc;
+    PyObject *pyobj_params;
+    PyObject *pyobj_transformCtxt;
+    const char **params = NULL;
+    int len = 0, i = 0, j;
+    PyObject *name;
+    PyObject *value;
+
+    if (!PyArg_ParseTuple(args, (char *) "OOOO:xsltApplyStylesheetUser",
+                         &pyobj_style, &pyobj_doc, &pyobj_params, &pyobj_transformCtxt))
+        return(NULL);
+
+    if (pyobj_params != Py_None) {
+       if (PyDict_Check(pyobj_params)) {
+           len = PyDict_Size(pyobj_params);
+           if (len > 0) {
+               params = (const char **) xmlMalloc((len + 1) * 2 *
+                                                  sizeof(char *));
+               if (params == NULL) {
+                   printf("libxslt_xsltApplyStylesheet: out of memory\n");
+                   Py_INCREF(Py_None);
+                   return(Py_None);
+               }
+               j = 0;
+               while (PyDict_Next(pyobj_params, &i, &name, &value)) {
+                   const char *tmp;
+                   int size;
+
+                   tmp = PyString_AS_STRING(name);
+                   size = PyString_GET_SIZE(name);
+                   params[j * 2] = (char *) xmlCharStrndup(tmp, size);
+                   if (PyString_Check(value)) {
+                       tmp = PyString_AS_STRING(value);
+                       size = PyString_GET_SIZE(value);
+                       params[(j * 2) + 1] = (char *)
+                           xmlCharStrndup(tmp, size);
+                   } else {
+                       params[(j * 2) + 1] = NULL;
+                   }
+                   j = j + 1;
+               }
+               params[j * 2] = NULL;
+               params[(j * 2) + 1] = NULL;
+           }
+       } else {
+           printf("libxslt_xsltApplyStylesheet: parameters not a dict\n");
+           Py_INCREF(Py_None);
+           return(Py_None);
+       }
+    }
+    style = (xsltStylesheetPtr) Pystylesheet_Get(pyobj_style);
+    doc = (xmlDocPtr) PyxmlNode_Get(pyobj_doc);
+    transformCtxt = (xsltTransformContextPtr) PytransformCtxt_Get(pyobj_transformCtxt);
+
+    c_retval = xsltApplyStylesheetUser(style, doc, params, NULL, NULL, transformCtxt);
+    py_retval = libxml_xmlDocPtrWrap((xmlDocPtr) c_retval);
+    if (params != NULL) {
+       if (len > 0) {
+           for (i = 0;i < 2 * len;i++) {
+               if (params[i] != NULL)
+                   xmlFree((char *)params[i]);
+           }
+           xmlFree(params);
+       }
+    }
+    return(py_retval);
+}
+
+
 PyObject *
 libxslt_xsltSaveResultToString(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     PyObject *py_retval;        /* our final return value, a python string   */ 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]