Hi,

        here's a patch for the xslt filter. It fixes the crashing bug
        when it couldn't be correctly configured. It can now be
        configured using a global config file (in
        DIA_PLUGIN_PATH/plugins/xslt/stylesheets.xml) or a user defined
        one in ~/.dia/plugins/xslt/stylesheets.xml. Both are parsed if they
        exist. If neither exist, it fails quietly. 
        Hope it'll make your life easier with this plugin :)

                                M@t

PS: patch -p0
? plug-ins/xslt/stylesheets.xml
? plug-ins/xslt/stylesheets.xml.in
Index: plug-ins/xslt/.cvsignore
===================================================================
RCS file: /cvs/gnome/dia/plug-ins/xslt/.cvsignore,v
retrieving revision 1.1
diff -u -r1.1 .cvsignore
--- plug-ins/xslt/.cvsignore    27 Nov 2002 13:51:42 -0000      1.1
+++ plug-ins/xslt/.cvsignore    13 Feb 2003 23:00:55 -0000
@@ -1,2 +1,4 @@
 Makefile
 Makefile.in
+*.o
+*.lo
Index: plug-ins/xslt/Makefile.am
===================================================================
RCS file: /cvs/gnome/dia/plug-ins/xslt/Makefile.am,v
retrieving revision 1.5
diff -u -r1.5 Makefile.am
--- plug-ins/xslt/Makefile.am   3 Feb 2003 20:40:51 -0000       1.5
+++ plug-ins/xslt/Makefile.am   13 Feb 2003 23:00:55 -0000
@@ -1,9 +1,9 @@
 ## Process this file with automake to produce Makefile.in
 
-XSLT_XSL = dia-uml.xsl dia-uml2c++.xsl dia-uml2java.xsl dia-uml2idl.xsl stylesheets
-XSLT_SRC = xslt.c xslt.h xsltdialog.c
+XSLT_XSL = dia-uml.xsl dia-uml2c++.xsl dia-uml2java.xsl dia-uml2idl.xsl 
+stylesheets.xml        
+XSLT_SRC = xslt.c xslt.h xsltdialog.c  
 
-xsltdir = $(pkgdatadir)/plugins/xslt
+xsltdir = $(pkgdatadir)/plugins/xslt/
 xslt_DATA = $(XSLT_XSL)
 
 if HAVE_XSLT   
@@ -11,7 +11,7 @@
 else
 plugin_sources = 
 endif
-plugin_extra_dist=$(XSLT_XSL) $(XSLT_SRC)
+plugin_extra_dist=$(XSLT_XSL)
 
 EXTRA_DIST = $(plugin_extra_dist)
 
@@ -20,5 +20,10 @@
 libxslt_filter_la_SOURCES = $(plugin_sources)
 
 libxslt_filter_la_LDFLAGS = -export-dynamic -module -avoid-version
+
+# escapes / in \/ for sed
+stylesheets.xml: stylesheets.xml.in
+       escaped=`echo ${xsltdir} | sed -e 's/\//\\\\\//g'`; \
+       sed -e s/XSLT_DATA_DIR/$$escaped/ stylesheets.xml.in > stylesheets.xml
 
 INCLUDES = -I$(top_srcdir)/intl -I$(srcdir)/../../lib $(GTK_CFLAGS) $(GNOME_CFLAGS) 
$(PANGOFT2_CFLAGS) $(XML_CFLAGS) $(UNICODE_CFLAGS) $(DEBUG_FLAGS) $(XSLT_CFLAGS)
Index: plug-ins/xslt/xslt.c
===================================================================
RCS file: /cvs/gnome/dia/plug-ins/xslt/xslt.c,v
retrieving revision 1.4
diff -u -r1.4 xslt.c
--- plug-ins/xslt/xslt.c        23 Sep 2002 19:28:00 -0000      1.4
+++ plug-ins/xslt/xslt.c        13 Feb 2003 23:00:55 -0000
@@ -32,6 +32,7 @@
 #include <libxslt/xsltInternals.h>
 #include <libxslt/transform.h>
 #include <libxslt/xsltutils.h>
+#include <libxml/tree.h>
 
 #include "xslt.h"
 
@@ -43,7 +44,7 @@
 static GModule *xslt_module;
 
 static void
-export_data(DiagramData *data, const gchar *f, 
+export_xslt(DiagramData *data, const gchar *f, 
             const gchar *diaf, void* user_data)
 {
        if(filename != NULL)
@@ -66,29 +67,21 @@
        char *params[] = { "directory", NULL, NULL };
        xsltStylesheetPtr style, codestyle;
        xmlDocPtr doc, res;
-       const gchar *pluginpath;
 
-       pluginpath = g_getenv("DIA_PLUGIN_PATH");
-       if (pluginpath == NULL) {
-         message_warning(_("DIA_PLUGIN_PATH not set.  Stylesheets not available.\n"));
-         return;
-       }
        params[1] = g_strconcat("'", g_dirname(filename), G_DIR_SEPARATOR_S, "'", 
NULL);
        
        file = fopen(diafilename, "r");
 
        if (file == NULL) {
-               message_error(_("Couldn't open: '%s' for reading.\n"), diafilename);
-               return;
+           message_error(_("Couldn't open: '%s' for reading.\n"), diafilename);
+           return;
        }
 
        out = fopen(filename, "w+");
        
-
-
        if (out == NULL) {
-               message_error(_("Couldn't open: '%s' for writing.\n"), filename);
-               return;
+           message_error(_("Couldn't open: '%s' for writing.\n"), filename);
+           return;
        }
        
        xmlSubstituteEntitiesDefault(0);
@@ -99,9 +92,7 @@
                return;
        }
        
-       stylefname = g_strconcat(pluginpath,
-                                G_DIR_SEPARATOR_S, "xslt", 
-                                G_DIR_SEPARATOR_S, xsl_from->xsl, NULL);
+       stylefname = xsl_from->xsl;
 
        style = xsltParseStylesheetFile((const xmlChar *) stylefname);
        if(style == NULL) {
@@ -111,28 +102,24 @@
        
        res = xsltApplyStylesheet(style, doc, NULL);
        if(res == NULL) {
-               message_error(_("Error while parsing stylesheet %s\n"), stylefname);
+               message_error(_("Error while applying stylesheet %s\n"), stylefname);
                return;
-       }
+       }       
        
-       g_free(stylefname);
+       stylefname = xsl_to->xsl;
        
-       stylefname = g_strconcat(pluginpath,
-                                G_DIR_SEPARATOR_S, "xslt", 
-                                G_DIR_SEPARATOR_S, xsl_to->xsl, NULL);
-
        codestyle = xsltParseStylesheetFile((const xmlChar *) stylefname);
        if(codestyle == NULL) {
-               message_error(_("Error while parsing stylesheet: %s\n"), xsl_to->xsl);
-               return;
+           message_error(_("Error while parsing stylesheet: %s\n"), xsl_to->xsl);
+           return;
        }
        
        xmlFreeDoc(doc);
        
        doc = xsltApplyStylesheet(codestyle, res, (const char **) params);
        if(doc == NULL) {
-               message_error(_("Error while applying stylesheet: %s\n"), xsl_to->xsl);
-               return;
+           message_error(_("Error while applying stylesheet: %s\n"), xsl_to->xsl);
+           return;
        }
 
 
@@ -142,7 +129,6 @@
                
        fclose(out);
        fclose(file);
-       g_free(stylefname);
 
        xsltFreeStylesheet(codestyle);
        xsltFreeStylesheet(style);
@@ -154,15 +140,135 @@
        xslt_clear();
 }
 
+static toxsl_t *read_implementations(xmlNodePtr cur) 
+{
+    toxsl_t *first, *curto;
+    first = curto = NULL;
+
+    cur = cur->xmlChildrenNode;
+
+    while (cur) {
+       if (xmlIsBlankNode(cur) || xmlNodeIsText(cur)) { cur = cur->next; continue; }
+       toxsl_t *to = g_malloc(sizeof(toxsl_t));        
+       to->next = NULL;
+       
+       to->name = xmlGetProp(cur, (const xmlChar *) "name");
+       to->xsl = xmlGetProp(cur, (const xmlChar *) "stylesheet");
+       
+       if (!(to->name && to->xsl)) {
+           g_warning ("Name and stylesheet attributes are required for the 
+implementation element %s in XSLT plugin configuration file", cur->name);
+           g_free(to);
+           to = NULL;
+       } else {
+           if (first == NULL) {
+               first = curto = to;
+           } else {
+               curto->next = to;
+               curto = to;
+           }
+       }           
+       cur = cur->next;
+    }
+    
+    return first;
+}
+
+static PluginInitResult read_configuration(const char *config) 
+{
+    xmlDocPtr doc;
+    xmlNodePtr cur;
+    /* Primary xsl */
+    fromxsl_t *cur_from = NULL;
+
+    if (!g_file_test(config, G_FILE_TEST_EXISTS))
+       return DIA_PLUGIN_INIT_ERROR;
+    
+    doc = xmlParseFile(config);
+    
+    if (doc == NULL) 
+    {
+       g_error ("Couldn't parse XSLT plugin's configuration file %s", config);
+       return DIA_PLUGIN_INIT_ERROR;
+    }
+    
+    cur = xmlDocGetRootElement(doc);
+    if (cur == NULL)
+    {
+       g_error ("XSLT plugin's configuration file %s is empty", config);
+       return DIA_PLUGIN_INIT_ERROR;
+    }
+    
+    /* We don't care about the top level element's name */
+    
+    cur = cur->xmlChildrenNode;
+
+    while (cur) {
+       if (xmlIsBlankNode(cur) || xmlNodeIsText(cur)) { cur = cur->next; continue; }
+       else if (!xmlStrcmp(cur->name, "language")) {
+           fromxsl_t *new_from = g_malloc(sizeof(fromxsl_t));
+           new_from->next = NULL;
+
+           new_from->name = xmlGetProp(cur, (const xmlChar *) "name");
+           new_from->xsl = xmlGetProp(cur, (const xmlChar *) "stylesheet");
+           
+           if (!(new_from->name && new_from->xsl)) {
+               g_warning ("'name' and 'stylesheet' attributes are required for the 
+language element %s in XSLT plugin configuration file", cur->name);
+               g_free(new_from);
+               new_from = NULL;
+           } else {
+               if (froms == NULL)
+                   froms = cur_from = new_from;
+               else {
+                   cur_from->next = new_from;
+                   cur_from = new_from;
+               }
+               
+               cur_from->xsls = read_implementations(cur);
+               if (cur_from->xsls == NULL) {
+                   g_warning ("No implementation stylesheet for language %s in XSLT 
+plugin configuration file", cur_from->name);
+               }
+           }       
+       } else {
+           g_warning ("Wrong node name %s in XSLT plugin configuration file, 
+'language' expected", cur->name);
+       }
+       cur = cur -> next;      
+    }
+   
+    if (froms == NULL) {
+       g_warning ("No stylesheets configured in %s for XSLT plugin", config);
+    }
+    
+    /*cur_from = froms;
+
+    printf("XSLT plugin configuration: \n");
+    while(cur_from != NULL)
+    {
+       printf("From: %s (%s)\n", cur_from->name, cur_from->xsl);
+       
+       cur_to = cur_from->xsls;
+       while(cur_to != NULL) {
+           printf("\tTo: %s (%s)\n", cur_to->name, cur_to->xsl);
+           cur_to = cur_to->next;
+       }
+       cur_from = cur_from->next;
+    }
+    */
+    xmlFreeDoc(doc);
+    xmlCleanupParser();
+
+    return DIA_PLUGIN_INIT_OK;
+}
+
+
 /* --- dia plug-in interface --- */
 
 #define MY_RENDERER_NAME "XSL Transformation filter"
 
 static const gchar *extensions[] = { "code", NULL };
 static DiaExportFilter my_export_filter = {
-       N_(MY_RENDERER_NAME),
-       extensions,
-       export_data
+    N_(MY_RENDERER_NAME),
+    extensions,
+    export_xslt
 };
 
 
@@ -170,118 +276,63 @@
 dia_plugin_init(PluginInfo *info)
 {
        gchar *path;
-       FILE *xsls;
-       int res;
+       PluginInitResult global_res, user_res;
 
-      /* Primary xsl */
-       fromxsl_t *cur_from;
-      /* Secondary xsl */
-       toxsl_t *cur_to, *prev_to;
-       
        if (!dia_plugin_info_init(info, "XSLT",
                                  _("XSL Transformation filter"),
-                                 xslt_can_unload, xslt_unload))
+                                 NULL, NULL))
        {
                        return DIA_PLUGIN_INIT_ERROR;
        }
        
 #ifdef G_OS_WIN32
-      /* FIXME: We can't assume \Windows is the right path, can we ? */
+       /* FIXME: We can't assume \Windows is the right path, can we ? */
        path = g_module_build_path("\Windows", "xslt");
 #else
-      /* FIXME: We should have a --with-xslt-prefix and use this */
+       /* FIXME: We should have a --with-xslt-prefix and use this */
        path = g_module_build_path("/usr/lib", "xslt");
 #endif 
        xslt_module = g_module_open(path, 0);
-       if(!xslt_module) {
+       if(xslt_module == NULL) {
                message_error(_("Could not load XSLT library (%s) : %s"), path, 
g_module_error());
                return DIA_PLUGIN_INIT_ERROR;
        }
-       filter_register_export(&my_export_filter);
-               
-       g_free(path);
-       
-       path = g_strconcat(g_getenv("DIA_PLUGIN_PATH"), 
-                          G_DIR_SEPARATOR_S, "xslt", 
-                          G_DIR_SEPARATOR_S, "stylesheets", NULL);
-       
-       if(path == NULL)
-       {
-               message_error(
-        _("Could not load XSLT plugin; DIA_PLUGIN_PATH is not set."));
-               return DIA_PLUGIN_INIT_ERROR;
-       }
-               
-      /* printf("XSLT plugin config file: %s\n", path); */
 
-       xsls = fopen(path, "r");
+       g_free(path);
 
-       if(xsls == NULL)
-       {
-               message_error("Could not find XSLT plugin configuration file : %s", 
path);
-               return DIA_PLUGIN_INIT_ERROR;
-       }
+       if (g_getenv("DIA_PLUGIN_PATH") == NULL)
+           global_res = DIA_PLUGIN_INIT_ERROR;
+       else {
+           path = g_strconcat(g_getenv("DIA_PLUGIN_PATH"), 
+                              G_DIR_SEPARATOR_S, "xslt", 
+                              G_DIR_SEPARATOR_S, "stylesheets.xml", NULL);
+           
+           /* printf("XSLT plugin config file: %s\n", path); */
+           
+           global_res = read_configuration(path);
+           
+           g_free (path);
+       }
+       
+       path = g_strconcat(dia_config_filename("plugins"),
+                          G_DIR_SEPARATOR_S, "xslt",
+                          G_DIR_SEPARATOR_S, "stylesheets.xml", NULL);
        
+       user_res = read_configuration(path);
        
-       cur_from = froms = g_malloc(sizeof(fromxsl_t));
-       cur_from->next = NULL;
-
-       while(1)
-       {
-               fscanf(xsls, "%as : %as\n", &cur_from->name, &cur_from->xsl);
-        /* printf("From: %s (%s)\n", cur_from->name, cur_from->xsl); */
-               prev_to = NULL;
-               cur_to = cur_from->xsls = g_malloc(sizeof(toxsl_t));
-               while(1)
-               {
-                       res = fscanf(xsls, "%as = %as\n", &cur_to->name, &cur_to->xsl);
-                       
-                       if(res != 2) {
-                               if(prev_to != NULL)
-                                       prev_to->next = NULL;
-                               break;
-                       }
-
-          /* printf("To: %s (%s)\n", cur_to->name, cur_to->xsl);                      
  */
-                       if(!feof(xsls)) {
-                               prev_to = cur_to;
-                               cur_to->next = g_malloc(sizeof(toxsl_t));
-                               cur_to = cur_to->next;
-                       } else {
-                               cur_to->next = NULL;
-                               fclose(xsls);
-                               break;
-                       }
-               }
-               if(!feof(xsls)) {
-                       cur_from->next = g_malloc(sizeof(fromxsl_t));
-                       cur_from = cur_from->next;
-               } else {
-                       cur_from->next = NULL;
-                       break;
-               }
-       }
-       
-       cur_from = froms;
+       g_free (path);
        
-       /*printf("XSLT plugin configuration: \n");
-       while(cur_from != NULL)
+       if (global_res == DIA_PLUGIN_INIT_OK || user_res == DIA_PLUGIN_INIT_OK)
        {
-               printf("From: %s (%s)\n", cur_from->name, cur_from->xsl);
+           xsl_from = froms;
+           xsl_to = xsl_from->xsls;
 
-               cur_to = cur_from->xsls;
-               while(cur_to != NULL) {
-                       printf("\tTo: %s (%s)\n", cur_to->name, cur_to->xsl);
-                       cur_to = cur_to->next;
-               }
-
-               cur_from = cur_from->next;
-               }*/
-
-       xsl_from = froms;
-       xsl_to = froms->xsls;
-
-       return DIA_PLUGIN_INIT_OK;
+           filter_register_export(&my_export_filter);
+           return DIA_PLUGIN_INIT_OK;
+       } else {
+           g_warning (_("No valid configuration files found for the XSLT plugin, not 
+loading."));
+           return DIA_PLUGIN_INIT_ERROR;
+       }
 }
 
 gboolean xslt_can_unload(PluginInfo *info)

Reply via email to