00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "xsltproc.h"
00029
00030 #include <qfile.h>
00031
00032 #ifdef HAVE_STRING_H
00033 #include <string.h>
00034 #endif
00035
00036 #ifdef HAVE_SYS_STAT_H
00037 #include <sys/stat.h>
00038 #endif
00039 #ifdef HAVE_UNISTD_H
00040 #include <unistd.h>
00041 #endif
00042 #ifdef HAVE_STDLIB_H
00043 #include <stdlib.h>
00044 #endif
00045
00046 #include <libxml/debugXML.h>
00047 #include <libxml/xmlIO.h>
00048 #include <libxml/parserInternals.h>
00049
00050 #include <libexslt/exsltconfig.h>
00051
00052 #ifdef WIN32
00053 #ifdef _MSC_VER
00054 #include <winsock2.h>
00055 #pragma comment(lib, "ws2_32.lib")
00056 #define gettimeofday(p1,p2)
00057 #endif
00058 #else
00059 #endif
00060
00061 #ifndef HAVE_STAT
00062 # ifdef HAVE__STAT
00063
00064
00065 # ifndef _MSC_VER
00066 # define stat(x,y) _stat(x,y)
00067 # endif
00068 # define HAVE_STAT
00069 # endif
00070 #endif
00071
00072 xmlParserInputPtr xmlNoNetExternalEntityLoader(const char *URL,
00073 const char *ID,
00074 xmlParserCtxtPtr ctxt);
00075
00076 #if 0
00077 XSLTProc::XSLTProc(const char* fileIn, const char* fileOut, const char* xsltsheet)
00078 {
00079 _fileIn = fileIn;
00080 _fileOut = fileOut;
00081 _stylesheet = xsltsheet;
00082 nbparams = 0;
00083 debug = 0;
00084 repeat = 0;
00085 novalid = 0;
00086 output = NULL;
00087 }
00088 #endif
00089
00090 XSLTProc::XSLTProc(QString fileIn, QString fileOut, QString xsltsheet)
00091 {
00092 _fileIn = QFile::encodeName(fileIn);
00093 _fileOut = QFile::encodeName(fileOut);
00094 _stylesheet = QFile::encodeName(xsltsheet);
00095 nbparams = 0;
00096 debug = 0;
00097 repeat = 0;
00098 novalid = 0;
00099 output = NULL;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 void
00111 XSLTProc::xsltProcess(xmlDocPtr doc, xsltStylesheetPtr cur, const char *filename)
00112 {
00113 xmlDocPtr res;
00114
00115 if (output == NULL)
00116 {
00117
00118 if (repeat)
00119 {
00120 int j;
00121
00122 for (j = 1; j < repeat; j++)
00123 {
00124 res = xsltApplyStylesheet(cur, doc, params);
00125 xmlFreeDoc(res);
00126 xmlFreeDoc(doc);
00127 doc = xmlParseFile(filename);
00128 }
00129 }
00130 res = xsltApplyStylesheet(cur, doc, params);
00131 xmlFreeDoc(doc);
00132
00133 if (res == NULL)
00134 {
00135 fprintf(stderr, "no result for %s\n", filename);
00136 return;
00137 }
00138 #ifdef LIBXML_DEBUG_ENABLED
00139 if (debug)
00140 xmlDebugDumpDocument(stdout, res);
00141 else
00142 {
00143 #endif
00144 if (cur->methodURI == NULL)
00145 {
00146 xsltSaveResultToFile(stdout, res, cur);
00147 }
00148 else
00149 {
00150 if (xmlStrEqual
00151 (cur->method, (const xmlChar *) "xhtml"))
00152 {
00153 fprintf(stderr, "non standard output xhtml\n");
00154 xsltSaveResultToFile(stdout, res, cur);
00155 }
00156 else
00157 {
00158 fprintf(stderr,
00159 "Unsupported non standard output %s\n",
00160 cur->method);
00161 }
00162 }
00163 #ifdef LIBXML_DEBUG_ENABLED
00164 }
00165 #endif
00166 xmlFreeDoc(res);
00167 }
00168 else
00169 {
00170 xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
00171 xmlFreeDoc(doc);
00172 }
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 void XSLTProc::addParam(QString name, QString value)
00206 {
00207 if(nbparams < NB_PARAMETER_MAX)
00208 {
00209 params[nbparams] = strdup(name.latin1());
00210 params[nbparams + 1] = strdup(value.latin1());
00211 fprintf(stderr, "%s => ", params[0]);
00212 fprintf(stderr, "%s\n", params[1]);
00213 nbparams = nbparams + 2;
00214 }
00215 }
00216
00217 int XSLTProc::parse()
00218 {
00219 int i = 0;
00220 xsltStylesheetPtr cur = NULL;
00221 xmlDocPtr doc, style;
00222
00223 xmlInitMemory();
00224
00225 LIBXML_TEST_VERSION
00226
00227
00228
00229 if (novalid == 0)
00230 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
00231 else
00232 xmlLoadExtDtdDefaultValue = 0;
00233
00234
00235 debug = debug + 1;
00236 novalid = novalid + 1;
00237 output = _fileOut;
00238 repeat = 20;
00239 xsltMaxDepth = 5;
00240 params[nbparams] = NULL;
00241
00242
00243
00244
00245 xmlSubstituteEntitiesDefault(1);
00246
00247
00248
00249
00250
00251 xsltRegisterTestModule();
00252
00253
00254 style = xmlParseFile((const char *) _stylesheet);
00255 if (style == NULL)
00256 {
00257 fprintf(stderr, "cannot parse %s\n", _stylesheet.data());
00258 cur = NULL;
00259 }
00260 else
00261 {
00262 cur = xsltLoadStylesheetPI(style);
00263 if (cur != NULL)
00264 {
00265
00266 xsltProcess(style, cur, _stylesheet);
00267 xsltFreeStylesheet(cur);
00268 goto done;
00269 }
00270 cur = xsltParseStylesheetDoc(style);
00271 if (cur != NULL)
00272 {
00273 if (cur->indent == 1)
00274 xmlIndentTreeOutput = 1;
00275 else
00276 xmlIndentTreeOutput = 0;
00277 i++;
00278 }
00279 else
00280 {
00281 xmlFreeDoc(style);
00282 goto done;
00283 }
00284 }
00285
00286
00287
00288
00289 xmlDefaultSAXHandlerInit();
00290 xmlDefaultSAXHandler.cdataBlock = NULL;
00291
00292 if ((cur != NULL) && (cur->errors == 0))
00293 {
00294 doc = NULL;
00295 doc = xmlParseFile(_fileIn);
00296 if (doc == NULL)
00297 fprintf(stderr, "unable to parse %s\n", _fileIn.data());
00298 else
00299 xsltProcess(doc, cur, _fileIn);
00300 }
00301 if (cur != NULL)
00302 xsltFreeStylesheet(cur);
00303 done:
00304 xsltCleanupGlobals();
00305 xmlCleanupParser();
00306 xmlMemoryDump();
00307 return (0);
00308 }
00309