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/parserInternals.h>
9#include <libxml/catalog.h>
10#include <QtCore/QDate>
12#include <QtCore/QRegExp>
14#include <QtCore/QTextCodec>
19#include <config-kdoctools.h>
20#include <QtCore/QCoreApplication>
21#include <QtCore/QDebug>
22#include <QtCore/QHash>
25#if !defined( SIMPLE_XSLT )
27#define INFO( x ) if (slave) slave->infoMessage(x);
34 QString *t = (QString*)context;
35 *t += QString::fromUtf8(buffer, len);
40 QString *t = (QString*)context;
45#if defined (SIMPLE_XSLT) && defined(Q_WS_WIN)
48xmlExternalEntityLoader defaultEntityLoader = NULL;
49static xmlChar *paths[MAX_PATHS + 1];
50static int nbpaths = 0;
57static xmlParserInputPtr xsltprocExternalEntityLoader(
const char *_URL,
const char *ID,xmlParserCtxtPtr ctxt)
59 xmlParserInputPtr ret;
60 warningSAXFunc warning = NULL;
63 QString url = QLatin1String(_URL);
65 for(i = replaceURLList.constBegin(); i != replaceURLList.constEnd(); i++)
67 if (url.startsWith(i.key()))
69 url.replace(i.key(),i.value());
70 qDebug() <<
"converted" << _URL <<
"to" << url;
74 strcpy(URL,url.toLatin1().constData());
76 const char *lastsegment = URL;
77 const char *iter = URL;
82 lastsegment = iter + 1;
87 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
88 warning = ctxt->sax->warning;
89 ctxt->sax->warning = NULL;
92 if (defaultEntityLoader != NULL) {
93 ret = defaultEntityLoader(URL, ID, ctxt);
96 ctxt->sax->warning = warning;
97 qDebug() <<
"Loaded URL=\"" << URL <<
"\" ID=\"" << ID <<
"\"";
101 for (
int i = 0;i < nbpaths;i++) {
104 newURL = xmlStrdup((
const xmlChar *) paths[i]);
105 newURL = xmlStrcat(newURL, (
const xmlChar *)
"/");
106 newURL = xmlStrcat(newURL, (
const xmlChar *) lastsegment);
107 if (newURL != NULL) {
108 ret = defaultEntityLoader((
const char *)newURL, ID, ctxt);
111 ctxt->sax->warning = warning;
112 qDebug() <<
"Loaded URL=\"" << newURL <<
"\" ID=\"" << ID <<
"\"";
119 if (warning != NULL) {
120 ctxt->sax->warning = warning;
122 warning(ctxt,
"failed to load external entity \"%s\"\n", URL);
124 warning(ctxt,
"failed to load external entity \"%s\"\n", ID);
130QString
transform(
const QString &pat,
const QString& tss,
131 const QVector<const char *> ¶ms )
136#if defined (SIMPLE_XSLT) && defined(Q_WS_WIN)
139 if (!defaultEntityLoader) {
140 defaultEntityLoader = xmlGetExternalEntityLoader();
141 xmlSetExternalEntityLoader(xsltprocExternalEntityLoader);
143 replaceURLList[QLatin1String(
"http://www.oasis-open.org/docbook/xml/4.2")] = QString(
"file:///%1").arg(DOCBOOK_XML_CURRDTD);
147 xsltStylesheetPtr style_sheet =
148 xsltParseStylesheetFile((
const xmlChar *)QFile::encodeName(tss).constData());
150 if ( !style_sheet ) {
153 if (style_sheet->indent == 1)
154 xmlIndentTreeOutput = 1;
156 xmlIndentTreeOutput = 0;
160 xmlParserCtxtPtr pctxt;
162 pctxt = xmlNewParserCtxt();
163 if ( pctxt == NULL ) {
167 xmlDocPtr doc = xmlCtxtReadFile(pctxt, QFile::encodeName(pat), NULL,
168 XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_NONET);
174 if (pctxt->valid == 0) {
180 xsltTransformContextPtr ctxt;
182 ctxt = xsltNewTransformContext(style_sheet, doc);
187 QVector<const char *> p = params;
189 xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc,
const_cast<const char **
>(&p[0]));
195 xsltSaveResultTo ( outp, res, style_sheet );
196 xmlOutputBufferFlush(outp);
199 xsltFreeStylesheet(style_sheet);
201 if (parsed.isEmpty())
243 int start_index = index + 1;
244 while (parsed.at(start_index - 1) !=
'>') start_index++;
251 int endindex = parsed.indexOf(
"</FILENAME>", index);
252 int startindex = parsed.indexOf(
"<FILENAME ", index) + 1;
256 if (startindex > 0) {
257 if (startindex < endindex) {
259 index = startindex + 8;
262 index = endindex + 8;
267 index = endindex + 1;
271 filedata = parsed.mid(start_index, endindex - start_index);
277 index = filedata.indexOf(
"<FILENAME ");
280 int endindex = filedata.lastIndexOf(
"</FILENAME>");
281 while (filedata.at(endindex) !=
'>') endindex++;
283 filedata = filedata.left(index) + filedata.mid(endindex);
293 return data.toUtf8();
295 QTextCodec *
locale = QTextCodec::codecForLocale();
301 const int part_len = 5000;
305 while ( offset < data.length() )
307 part = data.mid( offset, part_len );
308 QByteArray
test =
locale->fromUnicode( part );
316 for ( uint i = 0; i < len; i++ ) {
317 QByteArray
test =
locale->fromUnicode( part.mid( i, 1 ) );
318 if (
locale->toUnicode(
test ) == part.mid( i, 1 ) ) {
319 if (buffer_len +
test.length() + 1 >
sizeof(buffer))
321 strcpy( buffer + buffer_len,
test.data() );
322 buffer_len +=
test.length();
325 res.sprintf(
"&#%d;", part.at( i ).unicode() );
327 if (buffer_len +
test.length() + 1 >
sizeof(buffer))
329 strcpy( buffer + buffer_len,
test.data() );
330 buffer_len +=
test.length();
333 result += QByteArray( buffer, buffer_len + 1);
346 if (
output.contains(
"<table-of-contents>"))
347 output.replace( QString(
"<?xml version=\"1.0\"?>" ),
348 QString(
"<?xml version=\"1.0\" encoding=\"%1\"?>").arg(
name ) );
350 name = QTextCodec::codecForLocale()->name();
351 name.replace( QString(
"ISO " ),
"iso-" );
352 output.replace( QString(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" ),
353 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)
const char * name(StandardAction id)
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)