1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/***************************************************************************
* Copyright (C) 2004 by Paulo Moura Guedes *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "xsl.h"
#include <libxml/globals.h>
#include <libxml/parser.h>
// Don't try to sort the libxslt includes alphabetically!
// transform.h _HAS_ to be after xsltInternals.h and xsltconfig.h _HAS_ to be
// the first libxslt include or it will break the compilation on some
// libxslt versions
#include <libxslt/xsltconfig.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
// stdlib.h is required to build on Solaris
#include <stdlib.h>
#include <tqregexp.h>
#include <tqsignal.h>
#include <tqstylesheet.h>
#include <tqthread.h>
#include <tqevent.h>
#include <tqmutex.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kstandarddirs.h>
/**
* @author Jason Keirstead <[email protected]>
*
* The thread class that actually performs the XSL processing.
* Using a thread allows async operation.
*/
class KopeteXSLThread : public TQObject, public TQThread
{
public:
/**
* Thread constructor
*
* @param xmlString The XML to be transformed
* @param xslString The XSL stylesheet we will use to transform
* @param target Target object to connect to for async operation
* @param slotCompleted Slot to fire on completion in asnc operation
*/
KopeteXSLThread( const TQString &xmlString, xsltStylesheetPtr xslDoc, TQObject *target = 0L, const char *slotCompleted = 0L );
/**
* Reimplemented from TQThread. Does the processing.
*/
virtual void run();
/**
* A user event is used to get back to the UI thread to emit the completed signal
*/
bool event( TQEvent *event );
static TQString xsltTransform( const TQString &xmlString, xsltStylesheetPtr xslDoc );
/**
* Returns the result string
*/
const TQString &result()
{ return m_resultString; };
private:
TQString m_xml;
xsltStylesheetPtr m_xsl;
TQString m_resultString;
TQObject *m_target;
const char *m_slotCompleted;
TQMutex dataMutex;
};
KopeteXSLThread::KopeteXSLThread( const TQString &xmlString, xsltStylesheetPtr xslDoc, TQObject *target, const char *slotCompleted )
{
m_xml = xmlString;
m_xsl = xslDoc;
m_target = target;
m_slotCompleted = slotCompleted;
}
void KopeteXSLThread::run()
{
dataMutex.lock();
m_resultString = xsltTransform( m_xml, m_xsl );
dataMutex.unlock();
// get back to the main thread
tqApp->postEvent( this, new TQEvent( TQEvent::User ) );
}
bool KopeteXSLThread::event( TQEvent *event )
{
if ( event->type() == TQEvent::User )
{
dataMutex.lock();
if( m_target && m_slotCompleted )
{
TQSignal completeSignal( m_target );
completeSignal.connect( m_target, m_slotCompleted );
completeSignal.setValue( m_resultString );
completeSignal.activate();
}
dataMutex.unlock();
delete this;
return true;
}
return TQObject::event( event );
}
TQString KopeteXSLThread::xsltTransform( const TQString &xmlString, xsltStylesheetPtr styleSheet )
{
// Convert TQString into a C string
TQCString xmlCString = xmlString.utf8();
TQString resultString;
TQString errorMsg;
xmlDocPtr xmlDoc = xmlParseMemory( xmlCString, xmlCString.length() );
if ( xmlDoc )
{
if ( styleSheet )
{
static TQCString appPath( TQString::tqfromLatin1("\"%1\"").tqarg( KApplication::kApplication()->dirs()->findDirs("appdata", TQString::tqfromLatin1("styles/data") ).front() ).utf8() );
static const char* params[3] = {
"appdata",
appPath,
NULL
};
xmlDocPtr resultDoc = xsltApplyStylesheet( styleSheet, xmlDoc, params );
if ( resultDoc )
{
// Save the result into the TQString
xmlChar *mem;
int size;
xmlDocDumpMemory( resultDoc, &mem, &size );
resultString = TQString::fromUtf8( TQCString( ( char * )( mem ), size + 1 ) );
xmlFree( mem );
xmlFreeDoc( resultDoc );
}
else
{
errorMsg = i18n( "Message is null." );
}
}
else
{
errorMsg = i18n( "The selected stylesheet is invalid." );
}
xmlFreeDoc( xmlDoc );
}
else
{
errorMsg = i18n( "Message could not be parsed. This is likely due to an encoding problem." );
}
if ( resultString.isEmpty() )
{
resultString = i18n( "<div><b>KLinktqStatus encountered the following error while parsing a message:</b><br />%1</div>" ).tqarg( errorMsg );
}
#ifdef RAWXSL
kdDebug(23100) << k_funcinfo << resultString << endl;
#endif
return resultString;
}
class XSLTPrivate
{
public:
xmlDocPtr xslDoc;
xsltStylesheetPtr styleSheet;
unsigned int flags;
};
XSLT::XSLT( const TQString &document, TQObject *tqparent )
: TQObject( tqparent ), d(new XSLTPrivate)
{
d->flags = 0;
d->xslDoc = 0;
d->styleSheet = 0;
// Init Stuff
xmlLoadExtDtdDefaultValue = 0;
xmlSubstituteEntitiesDefault( 1 );
setXSLT( document );
}
XSLT::~XSLT()
{
xsltFreeStylesheet( d->styleSheet );
delete d;
}
void XSLT::setXSLT( const TQString &_document )
{
// Search for '<kopete-i18n>' elements and feed them through i18n().
// After that replace the %VAR% variables with their proper XSLT counterpart.
//
// FIXME: Preprocessing the document using the TQString API is fast and simple,
// but also error-sensitive.
// In fact, there are a couple of known issues with this algorithm that
// depend on the strings in the current styles. If these strings change
// they may break the parsing here.
//
// The reason I'm doing it like this is because of issues with TQDOM and
// namespaces in earlier TQt versions. When we drop TQt 3.1.x support we
// should probably convert this to more accurate DOM code. - Martijn
//
// Actually, since we need to parse into a libxml2 document anyway, this whole
// nonsense could be replaced with some simple XPath expressions - JK
//
TQRegExp elementMatch( TQString::tqfromLatin1( "<kopete-i18n>(.*)</kopete-i18n>" ) );
elementMatch.setMinimal( true );
TQString document = _document;
int pos;
while ( ( pos = elementMatch.search( document ) ) != -1 )
{
TQString orig = elementMatch.cap( 1 );
//kdDebug( 14010 ) << k_funcinfo << "Original text: " << orig << endl;
// Split on % and go over all parts
// WARNING: If you change the translator comment, also change it in the
// styles/extracti18n Perl script, because the strings have to be
// identical!
TQStringList parts = TQStringList::split( '%', i18n(
"Translators: The %FOO% placeholders are variables that are substituted "
"in the code, please leave them untranslated", orig.utf8() ), true );
// The first part is always text, as our variables are written like %FOO%
TQStringList::Iterator it = parts.begin();
TQString trans = *it;
bool prependPercent = true;
it = parts.remove( it );
for ( it = parts.begin(); it != parts.end(); ++it )
{
prependPercent = false;
if ( *it == TQString::tqfromLatin1( "TIME" ) )
{
trans += TQString::tqfromLatin1( "<xsl:value-of select=\"@time\"/>" );
}
else if ( *it == TQString::tqfromLatin1( "TIMESTAMP" ) )
{
trans += TQString::tqfromLatin1( "<xsl:value-of select=\"@timestamp\"/>" );
}
else if ( *it == TQString::tqfromLatin1( "FORMATTEDTIMESTAMP" ) )
{
trans += TQString::tqfromLatin1( "<xsl:value-of select=\"@formattedTimestamp\"/>" );
}
else if ( *it == TQString::tqfromLatin1( "FROM_CONTACT_DISPLAYNAME" ) )
{
trans += TQString::tqfromLatin1( "<span><xsl:attribute name=\"title\">"
"<xsl:choose>"
"<xsl:when test='from/contact/@contactId=from/contact/contactDisplayName/@text'>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/metaContactDisplayName/@text\"/>"
"</xsl:when>"
"<xsl:otherwise>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/metaContactDisplayName/@text\"/> "
"(<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/@contactId\"/>)"
"</xsl:otherwise>"
"</xsl:choose></xsl:attribute>"
"<xsl:attribute name=\"dir\">"
"<xsl:value-of select=\"from/contact/contactDisplayName/@dir\"/>"
"</xsl:attribute>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/contactDisplayName/@text\"/></span>" );
}
else if ( *it == TQString::tqfromLatin1( "TO_CONTACT_DISPLAYNAME" ) )
{
trans += TQString::tqfromLatin1( "<span><xsl:attribute name=\"title\">"
"<xsl:choose>"
"<xsl:when test='to/contact/@contactId=from/contact/contactDisplayName/@text'>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/metaContactDisplayName/@text\"/>"
"</xsl:when>"
"<xsl:otherwise>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/metaContactDisplayName/@text\"/> "
"(<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/@contactId\"/>)"
"</xsl:otherwise>"
"</xsl:choose></xsl:attribute>"
"<xsl:attribute name=\"dir\">"
"<xsl:value-of select=\"to/contact/contactDisplayName/@dir\"/>"
"</xsl:attribute>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/contactDisplayName/@text\"/></span>" );
}
else if ( *it == TQString::tqfromLatin1( "FROM_METACONTACT_DISPLAYNAME" ) )
{
trans += TQString::tqfromLatin1( "<span>"
"<xsl:attribute name=\"dir\">"
"<xsl:value-of select=\"from/contact/metaContactDisplayName/@dir\"/>"
"</xsl:attribute>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/metaContactDisplayName/@text\"/></span>" );
}
else if ( *it == TQString::tqfromLatin1( "TO_METACONTACT_DISPLAYNAME" ) )
{
trans += TQString::tqfromLatin1( "<span>"
"<xsl:attribute name=\"dir\">"
"<xsl:value-of select=\"to/contact/metaContactDisplayName/@dir\"/>"
"</xsl:attribute>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/metaContactDisplayName/@text\"/></span>" );
}
else if ( *it == TQString::tqfromLatin1( "FROM_CONTACT_ID" ) )
{
trans += TQString::tqfromLatin1( "<span><xsl:attribute name=\"title\">"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/contactDisplayName/@text\"/></xsl:attribute>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"from/contact/@contactId\"/></span>" );
}
else if ( *it == TQString::tqfromLatin1( "TO_CONTACT_ID" ) )
{
trans += TQString::tqfromLatin1( "<span><xsl:attribute name=\"title\">"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/contactDisplayName/@text\"/></xsl:attribute>"
"<xsl:value-of disable-output-escaping=\"yes\" select=\"to/contact/@contactId\"/></span>" );
}
else if ( *it == TQString::tqfromLatin1( "BODY" ) )
{
trans += TQString::tqfromLatin1( "<xsl:value-of disable-output-escaping=\"yes\" select=\"body\"/>" );
}
else
{
if ( prependPercent )
trans += '%';
trans += *it;
prependPercent = true;
}
}
//kdDebug( 14010 ) << k_funcinfo << "Translated text: " << trans << endl;
// Add "<kopete-i18n>" and "</kopete-i18n>" to length, hence the '+ 27'
document.replace( uint( pos ), orig.length() + 27, trans );
}
#ifdef RAWXSL
kdDebug(14000) << k_funcinfo << document.utf8() << endl;
#endif
//Freeing the stylesheet also frees the doc pointer;
xsltFreeStylesheet( d->styleSheet );
d->styleSheet = 0;
d->xslDoc = 0;
d->flags = 0;
TQCString rawDocument = document.utf8();
d->xslDoc = xmlParseMemory( rawDocument, rawDocument.length() );
if( d->xslDoc )
{
d->styleSheet = xsltParseStylesheetDoc( d->xslDoc );
if( d->styleSheet )
{
// Check for flags
TQStringList flags;
for( xmlNodePtr child = d->xslDoc->children; child != d->xslDoc->last; child = child->next )
{
if( child->type == XML_PI_NODE )
{
//We have a flag. Enable it;
TQCString flagData( (const char*)child->content );
if( flagData.contains( "Flag:" ) )
{
flags += flagData.mid(5);
}
}
}
if( !flags.isEmpty() )
setProperty("flags", flags.join( TQString::tqfromLatin1("|") ) );
}
else
{
kdWarning(14000) << "Invalid stylesheet provided" << endl;
//We don't have a stylesheet, so free the doc pointer
xmlFreeDoc( d->xslDoc );
d->styleSheet = 0;
d->xslDoc = 0;
}
}
else
{
kdWarning(14000) << "Invalid stylesheet provided" << endl;
d->xslDoc = 0;
}
}
TQString XSLT::transform( const TQString &xmlString )
{
return KopeteXSLThread::xsltTransform( xmlString, d->styleSheet );
}
void XSLT::transformAsync( const TQString &xmlString, TQObject *target, const char *slotCompleted )
{
( new KopeteXSLThread( xmlString, d->styleSheet, target, slotCompleted ) )->start();
}
bool XSLT::isValid() const
{
return d->styleSheet != NULL;
}
void XSLT::setFlags( unsigned int flags )
{
d->flags = flags;
}
unsigned int XSLT::flags() const
{
return d->flags;
}
#include "xsl.moc"
// vim: set noet ts=4 sts=4 sw=4:
|