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>
11 #include <QtCore/QDir>
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);
35 *t += QString::fromUtf8(buffer, len);
45 #if defined (SIMPLE_XSLT) && defined(Q_WS_WIN)
48 xmlExternalEntityLoader defaultEntityLoader = NULL;
49 static xmlChar *paths[MAX_PATHS + 1];
50 static int nbpaths = 0;
57 static 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);
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 xmlDocPtr doc = xmlParseFile(QFile::encodeName(pat));
161 xsltTransformContextPtr ctxt;
163 ctxt = xsltNewTransformContext(style_sheet, doc);
168 QVector<const char *> p = params;
170 xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, const_cast<const char **>(&p[0]));
176 xsltSaveResultTo ( outp, res, style_sheet );
177 xmlOutputBufferFlush(outp);
180 xsltFreeStylesheet(style_sheet);
182 if (parsed.isEmpty())
224 int start_index = index + 1;
225 while (parsed.at(start_index - 1) !=
'>') start_index++;
232 int endindex = parsed.indexOf(
"</FILENAME>", index);
233 int startindex = parsed.indexOf(
"<FILENAME ", index) + 1;
237 if (startindex > 0) {
238 if (startindex < endindex) {
240 index = startindex + 8;
243 index = endindex + 8;
248 index = endindex + 1;
252 filedata = parsed.mid(start_index, endindex - start_index);
258 index = filedata.indexOf(
"<FILENAME ");
261 int endindex = filedata.lastIndexOf(
"</FILENAME>");
262 while (filedata.at(endindex) !=
'>') endindex++;
264 filedata = filedata.left(index) + filedata.mid(endindex);
274 return data.toUtf8();
276 QTextCodec *
locale = QTextCodec::codecForLocale();
282 const int part_len = 5000;
286 while ( offset < data.length() )
288 part = data.mid( offset, part_len );
289 QByteArray
test = locale->fromUnicode( part );
290 if ( locale->toUnicode( test ) == part ) {
297 for ( uint i = 0; i < len; i++ ) {
298 QByteArray test = locale->fromUnicode( part.mid( i, 1 ) );
299 if ( locale->toUnicode( test ) == part.mid( i, 1 ) ) {
300 if (buffer_len + test.length() + 1 >
sizeof(buffer))
302 strcpy( buffer + buffer_len, test.data() );
303 buffer_len += test.length();
306 res.sprintf(
"&#%d;", part.at( i ).unicode() );
307 test = locale->fromUnicode( res );
308 if (buffer_len + test.length() + 1 >
sizeof(buffer))
310 strcpy( buffer + buffer_len, test.data() );
311 buffer_len += test.length();
314 result += QByteArray( buffer, buffer_len + 1);
327 if (output.contains(
"<table-of-contents>"))
328 output.replace(
QString(
"<?xml version=\"1.0\"?>" ),
329 QString(
"<?xml version=\"1.0\" encoding=\"%1\"?>").arg( name ) );
331 name = QTextCodec::codecForLocale()->name();
332 name.replace(
QString(
"ISO " ),
"iso-" );
333 output.replace(
QString(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" ),
334 QString(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\">" ).arg( name ) );
QString transform(const QString &pat, const QString &tss, const QVector< const char * > ¶ms)
QString i18n(const char *text)
void replaceCharsetHeader(QString &output)
const char * name(StandardAction id)
QString splitOut(const QString &parsed, int index)
QByteArray fromUnicode(const QString &data)
int writeToQString(void *context, const char *buffer, int len)
int closeQString(void *context)