filters

xsltproc.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Robert JACOLIN <rjacolin@ifrance.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018    This file has been made from xsltproc.c from libxslt.
00019 */
00020 
00021 /*
00022  * xsltproc.c: user program for the XSL Transformation 1.0 engine
00023  *
00024  * See Copyright for the status of this software.
00025  *
00026  * daniel@veillard.com
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 /* _MS_VER */
00058 #else /* WIN32 */
00059 #endif /* WIN32 */
00060 
00061 #ifndef HAVE_STAT
00062 #  ifdef HAVE__STAT
00063      /* MS C library seems to define stat and _stat. The definition
00064       *         is identical. Still, mapping them to each other causes a warning. */
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 /*static int debug = 0;
00103 static int repeat = 0;
00104 static int novalid = 0;*/
00105 
00106 /*const char *XSLTProc::params[16 + 1];
00107 int XSLTProc::nbparams = 0;
00108 static const char *output = NULL;*/
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 /*static void usage(const char *name) {
00176     printf("Usage: %s [options] stylesheet file [file ...]\n", name);
00177     printf("   Options:\n");
00178     printf("      --version or -V: show the version of libxml and libxslt used\n");
00179     printf("      --verbose or -v: show logs of what's happening\n");
00180     printf("      --output file or -o file: save to a given file\n");
00181     printf("      --timing: display the time used\n");
00182     printf("      --repeat: run the transformation 20 times\n");
00183     printf("      --debug: dump the tree of the result instead\n");
00184     printf("      --novalid: skip the Dtd loading phase\n");
00185     printf("      --noout: do not dump the result\n");
00186     printf("      --maxdepth val : increase the maximum depth\n");
00187 #ifdef LIBXML_HTML_ENABLED
00188     printf("      --html: the input document is(are) an HTML file(s)\n");
00189 #endif
00190 #ifdef LIBXML_DOCB_ENABLED
00191     printf("      --docbook: the input document is SGML docbook\n");
00192 #endif
00193     printf("      --param name value : pass a (parameter,value) pair\n");
00194     printf("            string values must be quoted like \"'string'\"\n");
00195     printf("      --nonet refuse to fetch DTDs or entities over network\n");
00196 #ifdef LIBXML_CATALOG_ENABLED
00197     printf("      --catalogs : use the catalogs from $SGML_CATALOG_FILES\n");
00198 #endif
00199 #ifdef LIBXML_XINCLUDE_ENABLED
00200     printf("      --xinclude : do XInclude processing on document intput\n");
00201 #endif
00202     printf("      --profile or --norman : dump profiling informations \n");
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     //xmlLineNumbersDefault(1);
00228 
00229     if (novalid == 0)
00230         xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
00231     else
00232         xmlLoadExtDtdDefaultValue = 0;
00233 
00234     /* Options */
00235     debug = debug + 1;
00236     novalid = novalid + 1;
00237     output = _fileOut;
00238     repeat = 20;
00239     xsltMaxDepth = 5;
00240     params[nbparams] = NULL;
00241 
00242     /*
00243      * Replace entities with their content.
00244      */
00245     xmlSubstituteEntitiesDefault(1);
00246 
00247     /*
00248      * Register the EXSLT extensions and the test module
00249      */
00250     //exsltRegisterAll();
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             /* it is an embedded stylesheet */
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      * disable CDATA from being built in the document tree
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 
KDE Home | KDE Accessibility Home | Description of Access Keys