summaryrefslogtreecommitdiffstats
path: root/filters/xsltfilter/export/xsltproc.cc
blob: 3bd20408ae881540fce7ec12d5cc7fb233e7f82a (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* This file is part of the KDE project
   Copyright (C) 2002 Robert JACOLIN <rjacolin@ifrance.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
   This file has been made from xsltproc.c from libxslt.
*/

/*
 * xsltproc.c: user program for the XSL Transformation 1.0 engine
 *
 * See Copyright for the status of this software.
 *
 * daniel@veillard.com
 */
#include "xsltproc.h"

#include <tqfile.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <libxml/debugXML.h>
#include <libxml/xmlIO.h>
#include <libxml/parserInternals.h>

#include <libexslt/exsltconfig.h>

#ifdef WIN32
#ifdef _MSC_VER
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define gettimeofday(p1,p2)
#endif /* _MS_VER */
#else /* WIN32 */
#endif /* WIN32 */

#ifndef HAVE_STAT
#  ifdef HAVE__STAT
     /* MS C library seems to define stat and _stat. The definition
      *         is identical. Still, mapping them to each other causes a warning. */
#    ifndef _MSC_VER
#      define stat(x,y) _stat(x,y)
#    endif
#    define HAVE_STAT
#  endif
#endif

xmlParserInputPtr xmlNoNetExternalEntityLoader(const char *URL,
	                                       const char *ID,
					       xmlParserCtxtPtr ctxt);

#if 0
XSLTProc::XSLTProc(const char* fileIn, const char* fileOut, const char* xsltsheet)
{
	_fileIn = fileIn;
	_fileOut = fileOut;
	_stylesheet = xsltsheet;
	nbparams = 0;
	debug = 0;
	repeat = 0;
	novalid = 0;
	output = NULL;
}
#endif

XSLTProc::XSLTProc(TQString fileIn, TQString fileOut, TQString xsltsheet)
{
	_fileIn = TQFile::encodeName(fileIn);
	_fileOut = TQFile::encodeName(fileOut);
	_stylesheet = TQFile::encodeName(xsltsheet);
	nbparams = 0;
	debug = 0;
	repeat = 0;
	novalid = 0;
	output = NULL;
}

/*static int debug = 0;
static int repeat = 0;
static int novalid = 0;*/

/*const char *XSLTProc::params[16 + 1];
int XSLTProc::nbparams = 0;
static const char *output = NULL;*/

void
XSLTProc::xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename)
{
    xmlDocPtr res;

    if (output == NULL)
	{
		
		if (repeat)
		{
		    int j;

		    for (j = 1; j < repeat; j++)
			{
				res = xsltApplyStylesheet(cur, doc, params);
				xmlFreeDoc(res);
				xmlFreeDoc(doc);
			    doc = xmlParseFile(filename);
		    }
		}
	    res = xsltApplyStylesheet(cur, doc, params);
		xmlFreeDoc(doc);
	
		if (res == NULL)
		{
		    fprintf(stderr, "no result for %s\n", filename);
		    return;
		}
#ifdef LIBXML_DEBUG_ENABLED
		if (debug)
	    	xmlDebugDumpDocument(stdout, res);
		else
		{
#endif
	    	if (cur->methodURI == NULL)
			{
				xsltSaveResultToFile(stdout, res, cur);
		    }
			else
			{
				if (xmlStrEqual
				    (cur->method, (const xmlChar *) "xhtml"))
				{
				    fprintf(stderr, "non standard output xhtml\n");
				    xsltSaveResultToFile(stdout, res, cur);
				}
				else
				{
				    fprintf(stderr,
						    "Unsupported non standard output %s\n",
						    cur->method);
				}
	    	}
#ifdef LIBXML_DEBUG_ENABLED
		}
#endif
		xmlFreeDoc(res);
    }
	else
	{
		xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
		xmlFreeDoc(doc);
    }
}

/*static void usage(const char *name) {
    printf("Usage: %s [options] stylesheet file [file ...]\n", name);
    printf("   Options:\n");
    printf("      --version or -V: show the version of libxml and libxslt used\n");
    printf("      --verbose or -v: show logs of what's happening\n");
    printf("      --output file or -o file: save to a given file\n");
    printf("      --timing: display the time used\n");
    printf("      --repeat: run the transformation 20 times\n");
    printf("      --debug: dump the tree of the result instead\n");
    printf("      --novalid: skip the Dtd loading phase\n");
    printf("      --noout: do not dump the result\n");
    printf("      --maxdepth val : increase the maximum depth\n");
#ifdef LIBXML_HTML_ENABLED
    printf("      --html: the input document is(are) an HTML file(s)\n");
#endif
#ifdef LIBXML_DOCB_ENABLED
    printf("      --docbook: the input document is SGML docbook\n");
#endif
    printf("      --param name value : pass a (parameter,value) pair\n");
    printf("            string values must be quoted like \"'string'\"\n");
    printf("      --nonet refuse to fetch DTDs or entities over network\n");
#ifdef LIBXML_CATALOG_ENABLED
    printf("      --catalogs : use the catalogs from $SGML_CATALOG_FILES\n");
#endif
#ifdef LIBXML_XINCLUDE_ENABLED
    printf("      --xinclude : do XInclude processing on document intput\n");
#endif
    printf("      --profile or --norman : dump profiling informations \n");
}*/

void XSLTProc::addParam(TQString name, TQString value)
{
	if(nbparams < NB_PARAMETER_MAX)
	{
		params[nbparams] = strdup(name.latin1());
		params[nbparams + 1] = strdup(value.latin1());
		fprintf(stderr, "%s => ", params[0]);
		fprintf(stderr, "%s\n", params[1]);
		nbparams = nbparams + 2;
	}
}

int XSLTProc::parse()
{
    int i = 0;
    xsltStylesheetPtr cur = NULL;
    xmlDocPtr doc, style;

    xmlInitMemory();

    LIBXML_TEST_VERSION

    //xmlLineNumbersDefault(1);

    if (novalid == 0)
        xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
    else
        xmlLoadExtDtdDefaultValue = 0;

	/* Options */
	debug = debug + 1;
	novalid = novalid + 1;
	output = _fileOut;
	repeat = 20;
	xsltMaxDepth = 5;
    params[nbparams] = NULL;

    /*
     * Replace entities with their content.
     */
    xmlSubstituteEntitiesDefault(1);

    /*
     * Register the EXSLT extensions and the test module
     */
    //exsltRegisterAll();
    xsltRegisterTestModule();

    
   	style = xmlParseFile((const char *) _stylesheet);
  	if (style == NULL)
	{
		fprintf(stderr,  "cannot parse %s\n", _stylesheet.data());
		cur = NULL;
	}
	else
	{
		cur = xsltLoadStylesheetPI(style);
		if (cur != NULL)
		{
		    /* it is an embedded stylesheet */
		    xsltProcess(style, cur, _stylesheet);
		    xsltFreeStylesheet(cur);
		    goto done;
		}
		cur = xsltParseStylesheetDoc(style);
		if (cur != NULL)
		{
		    if (cur->indent == 1)
				xmlIndentTreeOutput = 1;
		    else
				xmlIndentTreeOutput = 0;
			i++;
		}
		else
		{
		    xmlFreeDoc(style);
		    goto done;
		}
	}
   
    /*
     * disable CDATA from being built in the document tree
     */
    xmlDefaultSAXHandlerInit();
    xmlDefaultSAXHandler.cdataBlock = NULL;

    if ((cur != NULL) && (cur->errors == 0))
	{
		doc = NULL;
        doc = xmlParseFile(_fileIn);
        if (doc == NULL)
			fprintf(stderr, "unable to parse %s\n", _fileIn.data());
		else
			xsltProcess(doc, cur, _fileIn);
    }
    if (cur != NULL)
		xsltFreeStylesheet(cur);
done:
    xsltCleanupGlobals();
    xmlCleanupParser();
    xmlMemoryDump();
    return (0);
}