Verified Commit 8d7fc4f4 authored by Zander's avatar Zander 💥

Port xslt plugin to GtkComboBoxText

One less use of dep widgets
parent dda47180
/* /*
* File: plug-ins/xslt/xslt.c * File: plug-ins/xslt/xslt.c
* *
* Made by Matthieu Sozeau <mattam@netcourrier.com> * Made by Matthieu Sozeau <mattam@netcourrier.com>
* *
* Started on Thu May 16 23:21:43 2002 Matthieu Sozeau * Started on Thu May 16 23:21:43 2002 Matthieu Sozeau
* Last update Tue Jun 4 21:00:30 2002 Matthieu Sozeau * Last update Tue Jun 4 21:00:30 2002 Matthieu Sozeau
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* *
*/ */
#define G_LOG_DOMAIN "DiaXslt"
#include <config.h> #include <config.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
...@@ -43,7 +45,7 @@ ...@@ -43,7 +45,7 @@
/* Possible stylesheets */ /* Possible stylesheets */
fromxsl_t *froms = NULL; GPtrArray *froms = NULL;
/* Selected stylesheets */ /* Selected stylesheets */
toxsl_t *xsl_to; toxsl_t *xsl_to;
...@@ -54,269 +56,254 @@ static char *diafilename = NULL; ...@@ -54,269 +56,254 @@ static char *diafilename = NULL;
static char *filename = NULL; static char *filename = NULL;
static gboolean static gboolean
export_xslt(DiagramData *data, DiaContext *ctx, export_xslt (DiagramData *data,
const gchar *f, const gchar *diaf, void* user_data) DiaContext *ctx,
const gchar *f,
const gchar *diaf,
void *user_data)
{ {
if(filename != NULL) if (filename != NULL)
g_free(filename); g_free (filename);
filename = g_strdup(f);
if(diafilename != NULL)
g_free(diafilename);
diafilename = g_strdup(diaf); filename = g_strdup (f);
if (diafilename != NULL)
g_free (diafilename);
xslt_dialog_create(); diafilename = g_strdup (diaf);
/* assume the dialog does all the error reporting */ xslt_dialog_create ();
return TRUE;
}
/* assume the dialog does all the error reporting */
return TRUE;
}
void void
xslt_ok(void) xslt_ok (void)
{ {
FILE *file, *out; FILE *file, *out;
int err; int err;
gchar *stylefname; gchar *stylefname;
char *params[] = { "directory", NULL, NULL }; char *params[] = { "directory", NULL, NULL };
xsltStylesheetPtr style, codestyle; xsltStylesheetPtr style, codestyle;
xmlDocPtr doc, res; xmlDocPtr doc, res;
xmlErrorPtr error_xml = NULL; xmlErrorPtr error_xml = NULL;
gchar *directory = g_path_get_dirname(filename); gchar *directory = g_path_get_dirname (filename);
gchar *uri = g_filename_to_uri (directory, NULL, NULL); gchar *uri = g_filename_to_uri (directory, NULL, NULL);
g_free (directory); g_free (directory);
/* strange: requires an uri, but the last char is platform specifc?! */ /* strange: requires an uri, but the last char is platform specifc?! */
params[1] = g_strconcat("'", uri, G_DIR_SEPARATOR_S, "'", NULL); params[1] = g_strconcat ("'", uri, G_DIR_SEPARATOR_S, "'", NULL);
g_free (uri); g_free (uri);
file = g_fopen(diafilename, "r"); file = g_fopen (diafilename, "r");
if (file == NULL) { if (file == NULL) {
message_error(_("Couldn't open: '%s' for reading.\n"), message_error (_("Couldn't open: '%s' for reading.\n"),
dia_message_filename(diafilename)); dia_message_filename (diafilename));
return; return;
} }
out = g_fopen(filename, "w+"); out = g_fopen (filename, "w+");
if (out == NULL) { if (out == NULL) {
message_error(_("Can't open output file %s: %s\n"), message_error (_("Can't open output file %s: %s\n"),
dia_message_filename(filename), strerror(errno)); dia_message_filename (filename), strerror (errno));
return; return;
} }
xmlSubstituteEntitiesDefault(0); xmlSubstituteEntitiesDefault (0);
doc = xmlDoParseFile(diafilename, &error_xml); doc = xmlDoParseFile (diafilename, &error_xml);
if(doc == NULL) { if (doc == NULL) {
message_error(_("Error while parsing %s\n%s"), message_error (_("Error while parsing %s\n%s"),
dia_message_filename(diafilename), dia_message_filename (diafilename),
error_xml ? error_xml->message : ""); error_xml ? error_xml->message : "");
return; return;
} }
stylefname = xsl_from->xsl; stylefname = xsl_from->xsl;
style = xsltParseStylesheetFile((const xmlChar *) stylefname); style = xsltParseStylesheetFile ((const xmlChar *) stylefname);
if(style == NULL) { if(style == NULL) {
message_error(_("Error while parsing stylesheet %s\n"), message_error (_("Error while parsing stylesheet %s\n"),
dia_message_filename(stylefname)); dia_message_filename (stylefname));
return; return;
} }
res = xsltApplyStylesheet(style, doc, NULL); res = xsltApplyStylesheet (style, doc, NULL);
if(res == NULL) { if(res == NULL) {
message_error(_("Error while applying stylesheet %s\n"), message_error (_("Error while applying stylesheet %s\n"),
dia_message_filename(stylefname)); dia_message_filename (stylefname));
return; return;
} }
stylefname = xsl_to->xsl; stylefname = xsl_to->xsl;
codestyle = xsltParseStylesheetFile((const xmlChar *) stylefname); codestyle = xsltParseStylesheetFile ((const xmlChar *) stylefname);
if(codestyle == NULL) { if(codestyle == NULL) {
message_error(_("Error while parsing stylesheet: %s\n"), message_error (_("Error while parsing stylesheet: %s\n"),
dia_message_filename(stylefname)); dia_message_filename (stylefname));
return; return;
} }
xmlFreeDoc(doc); xmlFreeDoc (doc);
doc = xsltApplyStylesheet(codestyle, res, (const char **) params); doc = xsltApplyStylesheet (codestyle, res, (const char **) params);
if(doc == NULL) { if(doc == NULL) {
message_error(_("Error while applying stylesheet: %s\n"), message_error (_("Error while applying stylesheet: %s\n"),
dia_message_filename(stylefname)); dia_message_filename (stylefname));
return; return;
} }
/* Returns the number of byte written or -1 in case of failure. */ /* Returns the number of byte written or -1 in case of failure. */
err = xsltSaveResultToFile(out, doc, codestyle); err = xsltSaveResultToFile (out, doc, codestyle);
if(err < 0) { if(err < 0) {
message_error(_("Error while saving result: %s\n"), message_error (_("Error while saving result: %s\n"),
dia_message_filename(filename)); dia_message_filename (filename));
return; return;
} }
fprintf (out, "From:\t%s\n", diafilename); fprintf (out, "From:\t%s\n", diafilename);
fprintf (out, "With:\t%s\n", stylefname); fprintf (out, "With:\t%s\n", stylefname);
fprintf (out, "To:\t%s=%s\n", params[0], params[1]); fprintf (out, "To:\t%s=%s\n", params[0], params[1]);
fclose(out); fclose (out);
fclose(file); fclose (file);
xsltFreeStylesheet(codestyle); xsltFreeStylesheet (codestyle);
xsltFreeStylesheet(style); xsltFreeStylesheet (style);
xmlFreeDoc(res); xmlFreeDoc (res);
xmlFreeDoc(doc); xmlFreeDoc (doc);
xsltCleanupGlobals(); xsltCleanupGlobals ();
xslt_clear(); xslt_clear ();
} }
static toxsl_t *read_implementations(xmlNodePtr cur, gchar *path) static toxsl_t *
read_implementations (xmlNodePtr cur, gchar *path)
{ {
toxsl_t *first, *curto; toxsl_t *first, *curto;
first = curto = NULL; first = curto = NULL;
cur = cur->xmlChildrenNode; cur = cur->xmlChildrenNode;
while (cur) { while (cur) {
toxsl_t *to; toxsl_t *to;
if (xmlIsBlankNode(cur) || xmlNodeIsText(cur)) { cur = cur->next; continue; } if (xmlIsBlankNode(cur) || xmlNodeIsText(cur)) {
to = g_malloc(sizeof(toxsl_t)); cur = cur->next;
to->next = NULL; continue;
}
to->name = (gchar *)xmlGetProp(cur, (const xmlChar *) "name"); to = g_malloc (sizeof (toxsl_t));
to->xsl = (gchar *)xmlGetProp(cur, (const xmlChar *) "stylesheet"); to->next = NULL;
to->name = (gchar *) xmlGetProp (cur, (const xmlChar *) "name");
if (!(to->name && to->xsl)) { to->xsl = (gchar *) xmlGetProp (cur, (const xmlChar *) "stylesheet");
g_warning ("Name and stylesheet attributes are required for the implementation element %s in XSLT plugin configuration file", cur->name);
if (to->name) xmlFree(to->name); if (!(to->name && to->xsl)) {
if (to->xsl) xmlFree(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); if (to->name) {
to = NULL; xmlFree(to->name);
} else { }
if (first == NULL) { if (to->xsl) {
first = curto = to; xmlFree(to->xsl);
} else { }
curto->next = to; g_free(to);
curto = to; to = NULL;
} } else {
/* make filename absolute */ if (first == NULL) {
{ first = curto = to;
gchar *fname = g_strconcat(path, G_DIR_SEPARATOR_S, to->xsl, NULL); } else {
xmlFree(to->xsl); curto->next = to;
to->xsl = fname; curto = to;
} }
} /* make filename absolute */
cur = cur->next; {
gchar *fname = g_strconcat(path, G_DIR_SEPARATOR_S, to->xsl, NULL);
xmlFree(to->xsl);
to->xsl = fname;
}
} }
cur = cur->next;
return first; }
return first;
} }
static PluginInitResult read_configuration(const char *config) static PluginInitResult
read_configuration(const char *config)
{ {
xmlDocPtr doc; xmlDocPtr doc;
xmlNodePtr cur; xmlNodePtr cur;
xmlErrorPtr error_xml = NULL; xmlErrorPtr error_xml = NULL;
/* Primary xsl */ /* Primary xsl */
fromxsl_t *cur_from = NULL; gchar *path = NULL;
gchar *path = NULL;
if (!g_file_test (config, G_FILE_TEST_EXISTS))
if (!g_file_test(config, G_FILE_TEST_EXISTS)) return DIA_PLUGIN_INIT_ERROR;
return DIA_PLUGIN_INIT_ERROR;
doc = xmlDoParseFile (config, &error_xml);
doc = xmlDoParseFile(config, &error_xml);
if (doc == NULL) {
if (doc == NULL) g_critical ("Couldn't parse XSLT plugin's configuration file %s\n%s",
{ config, error_xml ? error_xml->message : "");
g_error ("Couldn't parse XSLT plugin's configuration file %s\n%s", return DIA_PLUGIN_INIT_ERROR;
config, error_xml ? error_xml->message : ""); }
return DIA_PLUGIN_INIT_ERROR;
} cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
cur = xmlDocGetRootElement(doc); g_critical ("XSLT plugin's configuration file %s is empty", config);
if (cur == NULL) return DIA_PLUGIN_INIT_ERROR;
{ }
g_error ("XSLT plugin's configuration file %s is empty", config);
return DIA_PLUGIN_INIT_ERROR; path = g_path_get_dirname(config);
}
/* We don't care about the top level element's name */
path = g_path_get_dirname(config);
cur = cur->xmlChildrenNode;
/* We don't care about the top level element's name */
while (cur) {
cur = cur->xmlChildrenNode; if (xmlIsBlankNode (cur) || xmlNodeIsText (cur)) {
cur = cur->next;
while (cur) { continue;
if (xmlIsBlankNode(cur) || xmlNodeIsText(cur)) { cur = cur->next; continue; } } else if (!xmlStrcmp (cur->name, (const xmlChar *) "language")) {
else if (!xmlStrcmp(cur->name, (const xmlChar *)"language")) { fromxsl_t *new_from = g_new (fromxsl_t, 1);
fromxsl_t *new_from = g_new(fromxsl_t,1);
new_from->next = NULL; new_from->name = (gchar *)xmlGetProp (cur, (const xmlChar *) "name");
new_from->xsl = (gchar *)xmlGetProp (cur, (const xmlChar *) "stylesheet");
new_from->name = (gchar *)xmlGetProp(cur, (const xmlChar *) "name");
new_from->xsl = (gchar *)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);
if (!(new_from->name && new_from->xsl)) { g_free (new_from);
g_warning ("'name' and 'stylesheet' attributes are required for the language element %s in XSLT plugin configuration file", cur->name); new_from = NULL;
g_free(new_from); } else {
new_from = NULL; /* make filename absolute */
} else { {
if (froms == NULL) gchar *fname = g_strconcat (path, G_DIR_SEPARATOR_S, new_from->xsl, NULL);
froms = cur_from = new_from; xmlFree (new_from->xsl);
else { new_from->xsl = fname;
if (!cur_from) }
cur_from = froms;
while (cur_from->next) new_from->xsls = read_implementations (cur, path);
cur_from = cur_from->next; if (new_from->xsls == NULL) {
cur_from->next = new_from; g_warning ("No implementation stylesheet for language %s in XSLT plugin configuration file", new_from->name);
cur_from = new_from; }
}
/* make filename absolute */ g_ptr_array_add (froms, new_from);
{ }
gchar *fname = g_strconcat(path, G_DIR_SEPARATOR_S, new_from->xsl, NULL); } else {
xmlFree(new_from->xsl); g_warning ("Wrong node name %s in XSLT plugin configuration file, 'language' expected", cur->name);
new_from->xsl = fname;
}
cur_from->xsls = read_implementations(cur, path);
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;
} }
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;
}
*/
g_free(path);
xmlFreeDoc(doc);
xmlCleanupParser();
return DIA_PLUGIN_INIT_OK; if (froms->len == 0) {
g_warning ("No stylesheets configured in %s for XSLT plugin", config);
}
g_free (path);
xmlFreeDoc (doc);
xmlCleanupParser ();
return DIA_PLUGIN_INIT_OK;
} }
...@@ -326,69 +313,61 @@ static PluginInitResult read_configuration(const char *config) ...@@ -326,69 +313,61 @@ static PluginInitResult read_configuration(const char *config)
static const gchar *extensions[] = { "code", NULL }; static const gchar *extensions[] = { "code", NULL };
static DiaExportFilter my_export_filter = { static DiaExportFilter my_export_filter = {
N_(MY_RENDERER_NAME), N_(MY_RENDERER_NAME),
extensions, extensions,
export_xslt export_xslt
}; };
DIA_PLUGIN_CHECK_INIT DIA_PLUGIN_CHECK_INIT
PluginInitResult PluginInitResult
dia_plugin_init(PluginInfo *info) dia_plugin_init (PluginInfo *info)
{ {
gchar *path; gchar *path;
PluginInitResult global_res, user_res; PluginInitResult global_res, user_res;
if (!dia_plugin_info_init(info, "XSLT", froms = g_ptr_array_new_with_free_func (g_free);
_("XSL Transformation filter"),
NULL, NULL)) if (!dia_plugin_info_init (info,
{ "XSLT",
return DIA_PLUGIN_INIT_ERROR; _("XSL Transformation filter"),
} NULL, NULL)) {
return DIA_PLUGIN_INIT_ERROR;
if (g_getenv ("DIA_XSLT_PATH") != NULL) }
path = g_build_path (G_DIR_SEPARATOR_S, g_getenv ("DIA_XSLT_PATH"), "stylesheets.xml", NULL);
else if (g_getenv ("DIA_XSLT_PATH") != NULL) {
path = dia_get_data_directory("xslt" G_DIR_SEPARATOR_S "stylesheets.xml"); path = g_build_path (G_DIR_SEPARATOR_S, g_getenv ("DIA_XSLT_PATH"), "stylesheets.xml", NULL);
} else {
global_res = read_configuration(path); path = dia_get_data_directory ("xslt" G_DIR_SEPARATOR_S "stylesheets.xml");
g_free (path); }
path = dia_config_filename("xslt" G_DIR_SEPARATOR_S global_res = read_configuration (path);
"stylesheets.xml"); g_free (path);
user_res = read_configuration(path);
g_free (path); path = dia_config_filename ("xslt" G_DIR_SEPARATOR_S "stylesheets.xml");
user_res = read_configuration(path);
if (global_res == DIA_PLUGIN_INIT_OK || user_res == DIA_PLUGIN_INIT_OK) g_free (path);
{
xsl_from = froms; if (global_res == DIA_PLUGIN_INIT_OK || user_res == DIA_PLUGIN_INIT_OK) {
xsl_to = xsl_from->xsls; xsl_from = g_ptr_array_index (froms, 0);
xsl_to = xsl_from->xsls;
filter_register_export(&my_export_filter);
return DIA_PLUGIN_INIT_OK; filter_register_export (&my_export_filter);
} else { return DIA_PLUGIN_INIT_OK;
message_error (_("No valid configuration files found for the XSLT plugin; not loading.")); } else {
return DIA_PLUGIN_INIT_ERROR; message_error (_("No valid configuration files found for the XSLT plugin; not loading."));
} return DIA_PLUGIN_INIT_ERROR;
}
} }
gboolean xslt_can_unload(PluginInfo *info) gboolean
xslt_can_unload (PluginInfo *info)
{ {
return 1; return TRUE;
} }
void xslt_unload(PluginInfo *info) void
xslt_unload(PluginInfo *info)
{ {
#if 0 g_ptr_array_unref (froms);
language_t *cur = languages, *next;
/*printf("Unloading xslt\n"); */
while(cur != NULL) {
next = cur->next;
g_free(cur);
cur = next;
}
#endif
} }
/* /*
* File: plug-ins/xslt/xslt.h * File: plug-ins/xslt/xslt.h
* *
* Made by Matthieu Sozeau <mattam@netcourrier.com> * Made by Matthieu Sozeau <mattam@netcourrier.com>
* *
* Started on Thu May 16 23:22:12 2002 Matthieu Sozeau * Started on Thu May 16 23:22:12 2002 Matthieu Sozeau
* Last update Mon Jun 3 19:36:32 2002 Matthieu Sozeau * Last update Mon Jun 3 19:36:32 2002 Matthieu Sozeau
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
...@@ -38,29 +38,27 @@ ...@@ -38,29 +38,27 @@
/* Stylesheets for a specific primary stylesheet */ /* Stylesheets for a specific primary stylesheet */
typedef struct toxsl_s { typedef struct toxsl_s {
gchar *name; gchar *name;
gchar *xsl; gchar *xsl;
GtkWidget *item; struct toxsl_s *next;
struct toxsl_s *next;
} toxsl_t; } toxsl_t;
/* Primary stylesheet for a dia object type */ /* Primary stylesheet for a dia object type */
typedef struct fromxsl_s { typedef struct fromxsl_s {
gchar *name; gchar *name;
gchar *xsl; gchar *xsl;
toxsl_t *xsls; toxsl_t *xsls;
struct fromxsl_s *next;
} fromxsl_t; } fromxsl_t;
/* Possible stylesheets */ /* Possible stylesheets */
extern fromxsl_t *froms; extern GPtrArray *froms;
/* Selected stylesheets */ /* Selected stylesheets */
extern toxsl_t *xsl_to; extern toxsl_t *xsl_to;
extern fromxsl_t *xsl_from; extern fromxsl_t *xsl_from;
void xslt_dialog_create(void); void xslt_dialog_create (void);
void xslt_ok(void); void xslt_ok (void);
void xslt_clear(void); void xslt_clear (void);
void xslt_unload(PluginInfo *info); void xslt_unload (PluginInfo *info);
gboolean xslt_can_unload(PluginInfo *info); gboolean xslt_can_unload (PluginInfo *info);
/* /*
* File: plug-ins/xslt/xsltdialog.c * File: plug-ins/xslt/xsltdialog.c
* *
* Made by Matthieu Sozeau <mattam@netcourrier.com> * Made by Matthieu Sozeau <mattam@netcourrier.com>
* *
* Started on Thu May 16 20:30:42 2002 Matthieu Sozeau * Started on Thu May 16 20:30:42 2002 Matthieu Sozeau