]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c
ocaml 3.09 transition
[helm.git] / helm / DEVEL / gdome_xslt / C / gdome_xslt / gdome_xslt.c
index 3c5dc42e2151670a5be31fa2211dc96d9e072b9f..232f14b12b7e4945965ae1775f50a78058efb6ca 100644 (file)
@@ -27,7 +27,9 @@
  */
 
 #include <stdio.h>
-#include <libgdome/gdome.h>
+#include <stdarg.h>
+#include <string.h>
+#include <gdome.h>
 #include <libxslt/xsltconfig.h>
 #include <libxslt/xslt.h>
 #include <libxslt/xsltutils.h>
@@ -64,6 +66,10 @@ GdomeDocument* gdome_of_libxml(xmlDocPtr n)
 
 // From now on no Gdome internal should be used directly.
 
+       /******************************/
+       /* XSLT stylesheet Processing */
+       /******************************/
+
 xsltStylesheetPtr processStylesheet(GdomeDocument* style)
 {
    xmlDocPtr style_copy;
@@ -81,7 +87,12 @@ xsltStylesheetPtr processStylesheet(GdomeDocument* style)
    return xsltParseStylesheetDoc(style_copy);
 }
 
-GdomeDocument* applyStylesheet(GdomeDocument* source, xsltStylesheetPtr style_libxslt, const char** params)
+       /*******************************/
+       /* XSLT stylesheet Application */
+       /*******************************/
+
+GdomeDocument* applyStylesheet(GdomeDocument* source, xsltStylesheetPtr
+               style_libxslt, const char** params)
 {
    xmlDocPtr source_libxml;
    xmlDocPtr output_libxml;
@@ -91,21 +102,20 @@ GdomeDocument* applyStylesheet(GdomeDocument* source, xsltStylesheetPtr style_li
 
    xsltSetGenericDebugFunc(NULL, NULL);
 
-   output_libxml = xsltApplyStylesheet(style_libxslt,
-                                       source_libxml,
-                                      params);
+   output_libxml = xsltApplyStylesheet(style_libxslt, source_libxml,
+                  params);
 
    if (output_libxml == NULL) return NULL;
 
    return gdome_of_libxml(output_libxml);
 }
 
-/***************** serialization functions *****************/
+       /******************/
+       /* Results Output */
+       /******************/
 
-int            saveResultToFilename    (const char* name,
-                                        GdomeDocument* result,
-                                        xsltStylesheetPtr style_libxslt,
-                                        int compression)
+int saveResultToFilename (const char* name, GdomeDocument* result,
+               xsltStylesheetPtr style_libxslt, int compression)
 {
        xmlDocPtr result_libxml;
 
@@ -114,15 +124,12 @@ int               saveResultToFilename    (const char* name,
 
        xsltSetGenericDebugFunc(NULL, NULL);
 
-       return xsltSaveResultToFilename(name,
-                                       result_libxml,
-                                       style_libxslt,
-                                       compression);
+       return xsltSaveResultToFilename(name, result_libxml,
+                       style_libxslt, compression);
 }
 
-int            saveResultToFile        (FILE* file,
-                                        GdomeDocument* result,
-                                        xsltStylesheetPtr style_libxslt)
+int saveResultToFile (FILE* file, GdomeDocument* result,
+               xsltStylesheetPtr style_libxslt)
 {
        xmlDocPtr result_libxml;
 
@@ -131,14 +138,11 @@ int               saveResultToFile        (FILE* file,
 
        xsltSetGenericDebugFunc(NULL, NULL);
 
-       return xsltSaveResultToFile(file,
-                                   result_libxml,
-                                   style_libxslt);
+       return xsltSaveResultToFile(file, result_libxml, style_libxslt);
 }
 
-int            saveResultToFd          (int fd,
-                                        GdomeDocument* result,
-                                        xsltStylesheetPtr style_libxslt)
+int saveResultToFd (int fd, GdomeDocument* result, xsltStylesheetPtr
+               style_libxslt)
 {
        xmlDocPtr result_libxml;
 
@@ -147,8 +151,89 @@ int                saveResultToFd          (int fd,
 
        xsltSetGenericDebugFunc(NULL, NULL);
 
-       return xsltSaveResultToFd(fd,
-                                 result_libxml,
-                                 style_libxslt);
+       return xsltSaveResultToFd(fd, result_libxml, style_libxslt);
+}
+
+       /**********************************************/
+       /* Error and Debugging Callbacks Registration */
+       /**********************************************/
+
+       /* max size of a single message passed to callbacks */
+#define MAX_MSG_SIZE   1024
+#define TRUNCATED_MSG  "... TRUNCATED ..."
+#define TRUNCATED_MSG_LEN      strlen(TRUNCATED_MSG)
+
+               /* ERROR callbacks */
+
+       /* user provided error callback, needs a string input */
+static gdomeXsltMsgCallback errorUserCallback = NULL;
+
+       /* libxslt like error callback, ignore context, builds a string
+        * input for user provided error callback and invoke it */
+void gdomeXsltErrorCallback (void *ctx, const char *msg, ...) {
+       va_list args;
+       char buf[MAX_MSG_SIZE];
+
+       if (errorUserCallback == NULL)
+               return;
+
+       va_start(args, msg);
+       if (vsnprintf(buf, MAX_MSG_SIZE, msg, args) > MAX_MSG_SIZE - 1)
+       {       /* message truncated; write TRUNCATED_MSG on it */
+               strncpy(buf+(strlen(buf) - TRUNCATED_MSG_LEN),
+                               TRUNCATED_MSG, TRUNCATED_MSG_LEN);
+       }
+       va_end(args);
+
+       (*errorUserCallback) (buf);
+
+       return;
+}
+
+       /* set user provided error callback */
+void setErrorCallback (gdomeXsltMsgCallback callback)
+{
+       errorUserCallback = callback;
+       xsltSetGenericErrorFunc(NULL,
+               (callback == NULL ? NULL : gdomeXsltErrorCallback));
+
+       return;
+}
+
+               /* DEBUG callbacks */
+
+       /* user provided debug callback, needs a string input */
+static gdomeXsltMsgCallback debugUserCallback = NULL;
+
+       /* libxslt like debug callback, ignore context, builds a string
+        * input for user provided debug callback and invoke it */
+void gdomeXsltDebugCallback (void *ctx, const char *msg, ...) {
+       va_list args;
+       char buf[MAX_MSG_SIZE];
+
+       if (debugUserCallback == NULL)
+               return;
+
+       va_start(args, msg);
+       if (vsnprintf(buf, MAX_MSG_SIZE, msg, args) > MAX_MSG_SIZE - 1)
+       {       /* message truncated; write TRUNCATED_MSG on it */
+               strncpy(buf+(strlen(buf) - TRUNCATED_MSG_LEN),
+                               TRUNCATED_MSG, TRUNCATED_MSG_LEN);
+       }
+       va_end(args);
+
+       (*debugUserCallback) (buf);
+
+       return;
+}
+
+       /* set user provided debug callback */
+void setDebugCallback (gdomeXsltMsgCallback callback)
+{
+       debugUserCallback = callback;
+       xsltSetGenericDebugFunc(NULL,
+               (callback == NULL ? NULL : gdomeXsltDebugCallback));
+
+       return;
 }