filters

export/xsltproc.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Robert JACOLIN <rjacolin@ifrance.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 This file has been made from xsltproc.c from libxslt.
19*/
20
21/*
22 * xsltproc.c: user program for the XSL Transformation 1.0 engine
23 *
24 * See Copyright for the status of this software.
25 *
26 * daniel@veillard.com
27 */
28#include "xsltproc.h"
29
30#include <tqfile.h>
31
32#ifdef HAVE_STRING_H
33#include <string.h>
34#endif
35
36#ifdef HAVE_SYS_STAT_H
37#include <sys/stat.h>
38#endif
39#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#endif
42#ifdef HAVE_STDLIB_H
43#include <stdlib.h>
44#endif
45
46#include <libxml/debugXML.h>
47#include <libxml/xmlIO.h>
48#include <libxml/parserInternals.h>
49
50#include <libexslt/exsltconfig.h>
51
52#ifdef WIN32
53#ifdef _MSC_VER
54#include <winsock2.h>
55#pragma comment(lib, "ws2_32.lib")
56#define gettimeofday(p1,p2)
57#endif /* _MS_VER */
58#else /* WIN32 */
59#endif /* WIN32 */
60
61#ifndef HAVE_STAT
62# ifdef HAVE__STAT
63 /* MS C library seems to define stat and _stat. The definition
64 * is identical. Still, mapping them to each other causes a warning. */
65# ifndef _MSC_VER
66# define stat(x,y) _stat(x,y)
67# endif
68# define HAVE_STAT
69# endif
70#endif
71
72xmlParserInputPtr xmlNoNetExternalEntityLoader(const char *URL,
73 const char *ID,
74 xmlParserCtxtPtr ctxt);
75
76#if 0
77XSLTProc::XSLTProc(const char* fileIn, const char* fileOut, const char* xsltsheet)
78{
79 _fileIn = fileIn;
80 _fileOut = fileOut;
81 _stylesheet = xsltsheet;
82 nbparams = 0;
83 debug = 0;
84 repeat = 0;
85 novalid = 0;
86 output = NULL;
87}
88#endif
89
90XSLTProc::XSLTProc(TQString fileIn, TQString fileOut, TQString xsltsheet)
91{
92 _fileIn = TQFile::encodeName(fileIn);
93 _fileOut = TQFile::encodeName(fileOut);
94 _stylesheet = TQFile::encodeName(xsltsheet);
95 nbparams = 0;
96 debug = 0;
97 repeat = 0;
98 novalid = 0;
99 output = NULL;
100}
101
102/*static int debug = 0;
103static int repeat = 0;
104static int novalid = 0;*/
105
106/*const char *XSLTProc::params[16 + 1];
107int XSLTProc::nbparams = 0;
108static const char *output = NULL;*/
109
110void
111XSLTProc::xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename)
112{
113 xmlDocPtr res;
114
115 if (output == NULL)
116 {
117
118 if (repeat)
119 {
120 int j;
121
122 for (j = 1; j < repeat; j++)
123 {
124 res = xsltApplyStylesheet(cur, doc, params);
125 xmlFreeDoc(res);
126 xmlFreeDoc(doc);
127 doc = xmlParseFile(filename);
128 }
129 }
130 res = xsltApplyStylesheet(cur, doc, params);
131 xmlFreeDoc(doc);
132
133 if (res == NULL)
134 {
135 fprintf(stderr, "no result for %s\n", filename);
136 return;
137 }
138#ifdef LIBXML_DEBUG_ENABLED
139 if (debug)
140 xmlDebugDumpDocument(stdout, res);
141 else
142 {
143#endif
144 if (cur->methodURI == NULL)
145 {
146 xsltSaveResultToFile(stdout, res, cur);
147 }
148 else
149 {
150 if (xmlStrEqual
151 (cur->method, (const xmlChar *) "xhtml"))
152 {
153 fprintf(stderr, "non standard output xhtml\n");
154 xsltSaveResultToFile(stdout, res, cur);
155 }
156 else
157 {
158 fprintf(stderr,
159 "Unsupported non standard output %s\n",
160 cur->method);
161 }
162 }
163#ifdef LIBXML_DEBUG_ENABLED
164 }
165#endif
166 xmlFreeDoc(res);
167 }
168 else
169 {
170 xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
171 xmlFreeDoc(doc);
172 }
173}
174
175/*static void usage(const char *name) {
176 printf("Usage: %s [options] stylesheet file [file ...]\n", name);
177 printf(" Options:\n");
178 printf(" --version or -V: show the version of libxml and libxslt used\n");
179 printf(" --verbose or -v: show logs of what's happening\n");
180 printf(" --output file or -o file: save to a given file\n");
181 printf(" --timing: display the time used\n");
182 printf(" --repeat: run the transformation 20 times\n");
183 printf(" --debug: dump the tree of the result instead\n");
184 printf(" --novalid: skip the Dtd loading phase\n");
185 printf(" --noout: do not dump the result\n");
186 printf(" --maxdepth val : increase the maximum depth\n");
187#ifdef LIBXML_HTML_ENABLED
188 printf(" --html: the input document is(are) an HTML file(s)\n");
189#endif
190#ifdef LIBXML_DOCB_ENABLED
191 printf(" --docbook: the input document is SGML docbook\n");
192#endif
193 printf(" --param name value : pass a (parameter,value) pair\n");
194 printf(" string values must be quoted like \"'string'\"\n");
195 printf(" --nonet refuse to fetch DTDs or entities over network\n");
196#ifdef LIBXML_CATALOG_ENABLED
197 printf(" --catalogs : use the catalogs from $SGML_CATALOG_FILES\n");
198#endif
199#ifdef LIBXML_XINCLUDE_ENABLED
200 printf(" --xinclude : do XInclude processing on document intput\n");
201#endif
202 printf(" --profile or --norman : dump profiling informations \n");
203}*/
204
205void XSLTProc::addParam(TQString name, TQString value)
206{
207 if(nbparams < NB_PARAMETER_MAX)
208 {
209 params[nbparams] = strdup(name.latin1());
210 params[nbparams + 1] = strdup(value.latin1());
211 fprintf(stderr, "%s => ", params[0]);
212 fprintf(stderr, "%s\n", params[1]);
213 nbparams = nbparams + 2;
214 }
215}
216
217int XSLTProc::parse()
218{
219 int i = 0;
220 xsltStylesheetPtr cur = NULL;
221 xmlDocPtr doc, style;
222
223 xmlInitMemory();
224
225 LIBXML_TEST_VERSION
226
227 //xmlLineNumbersDefault(1);
228
229 if (novalid == 0)
230 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
231 else
232 xmlLoadExtDtdDefaultValue = 0;
233
234 /* Options */
235 debug = debug + 1;
236 novalid = novalid + 1;
237 output = _fileOut;
238 repeat = 20;
239 xsltMaxDepth = 5;
240 params[nbparams] = NULL;
241
242 /*
243 * Replace entities with their content.
244 */
245 xmlSubstituteEntitiesDefault(1);
246
247 /*
248 * Register the EXSLT extensions and the test module
249 */
250 //exsltRegisterAll();
251 xsltRegisterTestModule();
252
253
254 style = xmlParseFile((const char *) _stylesheet);
255 if (style == NULL)
256 {
257 fprintf(stderr, "cannot parse %s\n", _stylesheet.data());
258 cur = NULL;
259 }
260 else
261 {
262 cur = xsltLoadStylesheetPI(style);
263 if (cur != NULL)
264 {
265 /* it is an embedded stylesheet */
266 xsltProcess(style, cur, _stylesheet);
267 xsltFreeStylesheet(cur);
268 goto done;
269 }
270 cur = xsltParseStylesheetDoc(style);
271 if (cur != NULL)
272 {
273 if (cur->indent == 1)
274 xmlIndentTreeOutput = 1;
275 else
276 xmlIndentTreeOutput = 0;
277 i++;
278 }
279 else
280 {
281 xmlFreeDoc(style);
282 goto done;
283 }
284 }
285
286 /*
287 * disable CDATA from being built in the document tree
288 */
289 xmlDefaultSAXHandlerInit();
290#if LIBXML_VERSION < 21300
291 // In 2.13.0 this global variable was made constant and claimed that
292 // it "hasn't had an effect for a long time"
293 xmlDefaultSAXHandler.cdataBlock = NULL;
294#endif // LIBXML_VERSION
295
296 if ((cur != NULL) && (cur->errors == 0))
297 {
298 doc = NULL;
299 doc = xmlParseFile(_fileIn);
300 if (doc == NULL)
301 fprintf(stderr, "unable to parse %s\n", _fileIn.data());
302 else
303 xsltProcess(doc, cur, _fileIn);
304 }
305 if (cur != NULL)
306 xsltFreeStylesheet(cur);
307done:
308 xsltCleanupGlobals();
309 xmlCleanupParser();
310 xmlMemoryDump();
311 return (0);
312}
313