1 /*
2 * Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <libgda/libgda.h>
19 #include <libgda-xslt.h>
20 #include <libxslt/xsltutils.h>
21
22 gchar *inputfile = NULL;
23 gchar *outputfile = NULL;
24 gchar *xslfile = NULL;
25 gchar *dsn = NULL;
26
27 static GOptionEntry entries[] = {
28 { "in", 'i', 0, G_OPTION_ARG_STRING, &inputfile, "Input file", NULL},
29 { "out", 'o', 0, G_OPTION_ARG_STRING, &outputfile, "Output file", NULL},
30 { "xsl", 'x', 0, G_OPTION_ARG_STRING, &xslfile, "XSL file", NULL},
31 { "dsn", 'd', 0, G_OPTION_ARG_STRING, &dsn, "Data source name", NULL},
32 { NULL }
33 };
34
35 static int sqlxslt_process_xslt_file_ext (GdaXsltExCont *sql_ctx,
36 const char *inputFile,
37 const char *xslFileName,
38 const char *outputFileName);
39
40
41 int
main(int argc,char * argv[])42 main (int argc, char *argv[])
43 {
44 GdaConnection *cnc;
45 GError *error = NULL;
46 GOptionContext *context;
47
48 context = g_option_context_new ("LibgdaXsltProc");
49 g_option_context_add_main_entries (context, entries, NULL);
50 if (!g_option_context_parse (context, &argc, &argv, &error)) {
51 g_print ("Can't parse arguments: %s", error->message);
52 exit (EXIT_FAILURE);
53 }
54 g_option_context_free (context);
55
56 if (!inputfile) {
57 g_print ("Missing input file (use --help option)\n");
58 exit (EXIT_FAILURE);
59 }
60 if (!outputfile) {
61 g_print ("Missing output file (use --help option)\n");
62 exit (EXIT_FAILURE);
63 }
64 if (!xslfile) {
65 g_print ("Missing XSL file (use --help option)\n");
66 exit (EXIT_FAILURE);
67 }
68 if (!dsn) {
69 g_print ("Missing Data source name, using the default \"SalesTest\"\n");
70 dsn = "SalesTest";
71 }
72
73 gda_init ();
74
75 /* open connection */
76 cnc = gda_connection_open_from_dsn (dsn, NULL, GDA_CONNECTION_OPTIONS_NONE, &error);
77 if (!cnc) {
78 g_print ("Could not open connection to DSN '%s': %s\n", dsn,
79 error && error->message ? error->message : "No detail");
80 exit (EXIT_FAILURE);
81 }
82
83 /* run XSL transform */
84 GdaXsltExCont *sql_ctx;
85 int ret;
86
87 sql_ctx = gda_xslt_create_context_simple (cnc, &error);
88 if (!sql_ctx) {
89 g_print ("gda_xslt_create_context_simple error: %s\n",
90 error && error->message ? error->message : "No detail");
91 exit (EXIT_FAILURE);
92 }
93
94 ret = sqlxslt_process_xslt_file_ext (sql_ctx, inputfile, xslfile, outputfile);
95 g_print ("Checking the sql execution context\n");
96 if (sql_ctx->error)
97 g_print ("exec error: %s\n",
98 sql_ctx->error->message ? sql_ctx->error->message : "No detail");
99 else
100 g_print("No error on the sql extension\n");
101
102 ret = gda_xslt_finalize_context (sql_ctx);
103
104 /* close connection */
105 g_object_unref (G_OBJECT (cnc));
106
107 return EXIT_SUCCESS;
108 }
109
110 /*
111 * Process the input file
112 */
113 static int
sqlxslt_process_xslt_file_ext(GdaXsltExCont * sql_ctx,const char * inputFile,const char * xslFileName,const char * outputFileName)114 sqlxslt_process_xslt_file_ext (GdaXsltExCont *sql_ctx, const char *inputFile,
115 const char *xslFileName, const char *outputFileName) {
116 int ret = 0;
117 xmlDocPtr doc,res;
118 xsltStylesheetPtr xsltdoc;
119 xsltTransformContextPtr ctxt;
120
121 static int init = 0;
122
123 if( !init ) {
124 g_print ("SQL extension init.\n");
125 init = 1;
126 gda_xslt_register();
127 }
128
129 doc = xmlReadFile(inputFile,NULL,0);
130 if (!doc) {
131 g_print ("read file failed\n");
132 ret = -1;
133 goto endf;
134 }
135
136 xsltdoc = xsltParseStylesheetFile((xmlChar*) xslFileName);
137 if (!xsltdoc) {
138 g_print ("xsltParseStylesheetFile failed\n");
139 goto cleanupdoc;
140 }
141
142 ctxt = xsltNewTransformContext (xsltdoc, doc);
143 gda_xslt_set_execution_context (ctxt, sql_ctx);
144
145 res = xsltApplyStylesheetUser (xsltdoc, doc, NULL, NULL, stderr, ctxt);
146 if ((ctxt->state == XSLT_STATE_ERROR) || (ctxt->state == XSLT_STATE_STOPPED))
147 g_print ("xslt process failed (error or stop)\n");
148 if (!res) {
149 g_print ("xslt process failed, return NULL\n");
150 goto cleanupxslt;
151 }
152
153 xsltFreeTransformContext (ctxt);
154
155 ret = xsltSaveResultToFilename (outputFileName,res,xsltdoc,0);
156 if (ret < 0) {
157 g_print ("xsltSaveResultToFilename failed\n");
158 goto cleanupresult;
159 }
160
161 if (!g_file_test (outputFileName, G_FILE_TEST_EXISTS)) {
162 g_print ("No or empty XSLT output file, check your XSL!\n");
163 goto cleanupresult;
164 }
165
166 cleanupresult:
167 xmlFreeDoc (res);
168 cleanupxslt:
169 xsltFreeStylesheet (xsltdoc);
170 cleanupdoc:
171 xmlFreeDoc (doc);
172 endf:
173 return ret;
174 }
175
176