File: transform.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (175 lines) | stat: -rw-r--r-- 4,988 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
 *
 * 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 the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <libgda/libgda.h>
#include <libgda-xslt.h>
#include <libxslt/xsltutils.h>

gchar *inputfile = NULL;
gchar *outputfile = NULL;
gchar *xslfile = NULL;
gchar *dsn = NULL;

static GOptionEntry entries[] = {
        { "in", 'i', 0, G_OPTION_ARG_STRING, &inputfile, "Input file", NULL},
        { "out", 'o', 0, G_OPTION_ARG_STRING, &outputfile, "Output file", NULL},
        { "xsl", 'x', 0, G_OPTION_ARG_STRING, &xslfile, "XSL file", NULL},
	{ "dsn", 'd', 0, G_OPTION_ARG_STRING, &dsn, "Data source name", NULL},
        { NULL }
};

static int sqlxslt_process_xslt_file_ext (GdaXsltExCont *sql_ctx, 
					  const char *inputFile, 
					  const char *xslFileName, 
					  const char *outputFileName);


int
main (int argc, char *argv[])
{
        GdaConnection *cnc;
	GError *error = NULL;
	GOptionContext *context;

	context = g_option_context_new ("LibgdaXsltProc");
	g_option_context_add_main_entries (context, entries, NULL);
        if (!g_option_context_parse (context, &argc, &argv, &error)) {
                g_print ("Can't parse arguments: %s", error->message);
                exit (EXIT_FAILURE);
        }
        g_option_context_free (context);

	if (!inputfile) {
		g_print ("Missing input file (use --help option)\n");
		exit (EXIT_FAILURE);
	}
	if (!outputfile) {
		g_print ("Missing output file (use --help option)\n");
		exit (EXIT_FAILURE);
	}
	if (!xslfile) {
		g_print ("Missing XSL file (use --help option)\n");
		exit (EXIT_FAILURE);
	}
	if (!dsn) {
		g_print ("Missing Data source name, using the default \"SalesTest\"\n");
		dsn = "SalesTest";
	}

        gda_init ();

	/* open connection */
	cnc = gda_connection_open_from_dsn (dsn, NULL, GDA_CONNECTION_OPTIONS_NONE, &error);
        if (!cnc) {
                g_print ("Could not open connection to DSN '%s': %s\n", dsn,
                         error && error->message ? error->message : "No detail");
                exit (EXIT_FAILURE);
        }

	/* run XSL transform */
	GdaXsltExCont *sql_ctx;
	int ret;

	sql_ctx = gda_xslt_create_context_simple (cnc, &error);
	if (!sql_ctx) {
		g_print ("gda_xslt_create_context_simple error: %s\n", 
			 error && error->message ? error->message : "No detail");
		exit (EXIT_FAILURE);
	}

	ret = sqlxslt_process_xslt_file_ext (sql_ctx, inputfile, xslfile, outputfile);
	g_print ("Checking the sql execution context\n");
	if (sql_ctx->error) 
		g_print ("exec error: %s\n",
			 sql_ctx->error->message ? sql_ctx->error->message : "No detail");
	else 
		g_print("No error on the sql extension\n");

	ret = gda_xslt_finalize_context (sql_ctx);

	/* close connection */
        g_object_unref (G_OBJECT (cnc));

        return EXIT_SUCCESS;
}

/*
 * Process the input file 
 */
static int
sqlxslt_process_xslt_file_ext (GdaXsltExCont *sql_ctx, const char *inputFile, 
			       const char *xslFileName, const char *outputFileName) {
	int ret = 0;
	xmlDocPtr doc,res;
	xsltStylesheetPtr xsltdoc;
	xsltTransformContextPtr ctxt;

	static int init = 0;

	if( !init ) {
		g_print ("SQL extension init.\n");
		init = 1;
		gda_xslt_register();
	}

	doc = xmlReadFile(inputFile,NULL,0);
	if (!doc) {
		g_print ("read file failed\n");
		ret = -1;
		goto endf;
	}

	xsltdoc = xsltParseStylesheetFile((xmlChar*) xslFileName);
	if (!xsltdoc) {
		g_print ("xsltParseStylesheetFile failed\n");
		goto cleanupdoc;
	}

	ctxt = xsltNewTransformContext (xsltdoc, doc);
	gda_xslt_set_execution_context (ctxt, sql_ctx);

	res = xsltApplyStylesheetUser (xsltdoc, doc, NULL, NULL, stderr, ctxt);
	if ((ctxt->state == XSLT_STATE_ERROR) || (ctxt->state == XSLT_STATE_STOPPED)) 
		g_print ("xslt process failed (error or stop)\n");
	if (!res) {
		g_print ("xslt process failed, return NULL\n");
		goto cleanupxslt;
	}

	xsltFreeTransformContext (ctxt);

	ret = xsltSaveResultToFilename (outputFileName,res,xsltdoc,0);
	if (ret < 0) {
		g_print ("xsltSaveResultToFilename failed\n");
		goto cleanupresult;
	}

	if (!g_file_test (outputFileName, G_FILE_TEST_EXISTS)) {
		g_print ("No or empty XSLT output file, check your XSL!\n");
		goto cleanupresult;
	}

 cleanupresult:
	xmlFreeDoc (res);
 cleanupxslt:
	xsltFreeStylesheet (xsltdoc);
 cleanupdoc:
	xmlFreeDoc (doc);
 endf:
	return ret;
}