3#include <libxslt/xsltconfig.h>
4#include <libxslt/xsltInternals.h>
5#include <libxslt/transform.h>
6#include <libxslt/xsltutils.h>
7#include <libxml/xmlIO.h>
8#include <libxml/xmlsave.h>
9#include <libxml/parserInternals.h>
10#include <libxml/catalog.h>
11#include <QtCore/QDate>
13#include <QtCore/QRegExp>
15#include <QtCore/QTextCodec>
20#include <config-kdoctools.h>
21#include <QtCore/QCoreApplication>
22#include <QtCore/QDebug>
23#include <QtCore/QHash>
26#if !defined( SIMPLE_XSLT )
28#define INFO( x ) if (slave) slave->infoMessage(x);
35 QString *t = (QString*)context;
36 *t += QString::fromUtf8(buffer, len);
41 QString *t = (QString*)context;
46#if defined (SIMPLE_XSLT) && defined(Q_WS_WIN)
49xmlExternalEntityLoader defaultEntityLoader = NULL;
50static xmlChar *paths[MAX_PATHS + 1];
51static int nbpaths = 0;
58static xmlParserInputPtr xsltprocExternalEntityLoader(
const char *_URL,
const char *ID,xmlParserCtxtPtr ctxt)
60 xmlParserInputPtr ret;
61 warningSAXFunc warning = NULL;
64 QString url = QLatin1String(_URL);
66 for(i = replaceURLList.constBegin(); i != replaceURLList.constEnd(); i++)
68 if (url.startsWith(i.key()))
70 url.replace(i.key(),i.value());
71 qDebug() <<
"converted" << _URL <<
"to" << url;
75 strcpy(URL,url.toLatin1().constData());
77 const char *lastsegment = URL;
78 const char *iter = URL;
83 lastsegment = iter + 1;
88 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
89 warning = ctxt->sax->warning;
90 ctxt->sax->warning = NULL;
93 if (defaultEntityLoader != NULL) {
94 ret = defaultEntityLoader(URL, ID, ctxt);
97 ctxt->sax->warning = warning;
98 qDebug() <<
"Loaded URL=\"" << URL <<
"\" ID=\"" << ID <<
"\"";
102 for (
int i = 0;i < nbpaths;i++) {
105 newURL = xmlStrdup((
const xmlChar *) paths[i]);
106 newURL = xmlStrcat(newURL, (
const xmlChar *)
"/");
107 newURL = xmlStrcat(newURL, (
const xmlChar *) lastsegment);
108 if (newURL != NULL) {
109 ret = defaultEntityLoader((
const char *)newURL, ID, ctxt);
112 ctxt->sax->warning = warning;
113 qDebug() <<
"Loaded URL=\"" << newURL <<
"\" ID=\"" << ID <<
"\"";
120 if (warning != NULL) {
121 ctxt->sax->warning = warning;
123 warning(ctxt,
"failed to load external entity \"%s\"\n", URL);
125 warning(ctxt,
"failed to load external entity \"%s\"\n", ID);
131QString
transform(
const QString &pat,
const QString& tss,
132 const QVector<const char *> ¶ms )
137#if defined (SIMPLE_XSLT) && defined(Q_WS_WIN)
140 if (!defaultEntityLoader) {
141 defaultEntityLoader = xmlGetExternalEntityLoader();
142 xmlSetExternalEntityLoader(xsltprocExternalEntityLoader);
144 replaceURLList[QLatin1String(
"http://www.oasis-open.org/docbook/xml/4.2")] = QString(
"file:///%1").arg(DOCBOOK_XML_CURRDTD);
148 xsltStylesheetPtr style_sheet =
149 xsltParseStylesheetFile((
const xmlChar *)QFile::encodeName(tss).constData());
151 if ( !style_sheet ) {
154 if (style_sheet->indent == 1)
155 xmlIndentTreeOutput = 1;
157 xmlIndentTreeOutput = 0;
161 xmlParserCtxtPtr pctxt;
163 pctxt = xmlNewParserCtxt();
164 if ( pctxt == NULL ) {
168 xmlDocPtr doc = xmlCtxtReadFile(pctxt, QFile::encodeName(pat), NULL,
169 XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_NONET);
175 if (pctxt->valid == 0) {
181 xsltTransformContextPtr ctxt;
183 ctxt = xsltNewTransformContext(style_sheet, doc);
188 QVector<const char *> p = params;
190 xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc,
const_cast<const char **
>(&p[0]));
196 xsltSaveResultTo ( outp, res, style_sheet );
197 xmlOutputBufferFlush(outp);
200 xsltFreeStylesheet(style_sheet);
202 if (parsed.isEmpty())
244 int start_index = index + 1;
245 while (parsed.at(start_index - 1) !=
'>') start_index++;
252 int endindex = parsed.indexOf(
"</FILENAME>", index);
253 int startindex = parsed.indexOf(
"<FILENAME ", index) + 1;
257 if (startindex > 0) {
258 if (startindex < endindex) {
260 index = startindex + 8;
263 index = endindex + 8;
268 index = endindex + 1;
272 filedata = parsed.mid(start_index, endindex - start_index);
278 index = filedata.indexOf(
"<FILENAME ");
281 int endindex = filedata.lastIndexOf(
"</FILENAME>");
282 while (filedata.at(endindex) !=
'>') endindex++;
284 filedata = filedata.left(index) + filedata.mid(endindex);
294 return data.toUtf8();
296 QTextCodec *locale = QTextCodec::codecForLocale();
302 const int part_len = 5000;
306 while ( offset < data.length() )
308 part = data.mid( offset, part_len );
309 QByteArray test = locale->fromUnicode( part );
310 if ( locale->toUnicode( test ) == part ) {
317 for ( uint i = 0; i < len; i++ ) {
318 QByteArray test = locale->fromUnicode( part.mid( i, 1 ) );
319 if ( locale->toUnicode( test ) == part.mid( i, 1 ) ) {
320 if (buffer_len + test.length() + 1 >
sizeof(buffer))
322 strcpy( buffer + buffer_len, test.data() );
323 buffer_len += test.length();
326 res.sprintf(
"&#%d;", part.at( i ).unicode() );
327 test = locale->fromUnicode( res );
328 if (buffer_len + test.length() + 1 >
sizeof(buffer))
330 strcpy( buffer + buffer_len, test.data() );
331 buffer_len += test.length();
334 result += QByteArray( buffer, buffer_len + 1);
347 if (
output.contains(
"<table-of-contents>"))
348 output.replace( QString(
"<?xml version=\"1.0\"?>" ),
349 QString(
"<?xml version=\"1.0\" encoding=\"%1\"?>").arg( name ) );
351 name = QTextCodec::codecForLocale()->name();
352 name.replace( QString(
"ISO " ),
"iso-" );
353 output.replace( QString(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" ),
354 QString(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\">" ).arg( name ) );
void output(QList< Action > actions, QHash< QString, QString > domain)
QString i18n(const char *text)
int writeToQString(void *context, const char *buffer, int len)
void replaceCharsetHeader(QString &output)
QString splitOut(const QString &parsed, int index)
int closeQString(void *context)
QByteArray fromUnicode(const QString &data)
QString transform(const QString &pat, const QString &tss, const QVector< const char * > ¶ms)