• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdoctools
 

kdoctools

  • kdoctools
xslt.cpp
1 #include <libxslt/xsltconfig.h>
2 #include <libxslt/xsltInternals.h>
3 #include <libxslt/transform.h>
4 #include <libxslt/xsltutils.h>
5 #include <libxml/xmlIO.h>
6 #include <libxml/parserInternals.h>
7 #include <libxml/catalog.h>
8 #include <kdebug.h>
9 #include <kstandarddirs.h>
10 #include <tqdir.h>
11 #include <tqregexp.h>
12 #include <xslt.h>
13 #include <kinstance.h>
14 #include "tdeio_help.h"
15 #include <tdelocale.h>
16 #include <assert.h>
17 #include <kfilterbase.h>
18 #include <kfilterdev.h>
19 #include <tqtextcodec.h>
20 #include <stdlib.h>
21 #include <config.h>
22 #include <stdarg.h>
23 #include <klibloader.h>
24 #include <kcharsets.h>
25 #include <gzip/kgzipfilter.h>
26 #include <bzip2/kbzip2filter.h>
27 #include <klibloader.h>
28 #include <tqvaluevector.h>
29 
30 #if !defined( SIMPLE_XSLT )
31 extern HelpProtocol *slave;
32 #define INFO( x ) if (slave) slave->infoMessage(x);
33 #else
34 #define INFO( x )
35 #endif
36 
37 int writeToQString(void * context, const char * buffer, int len)
38 {
39  TQString *t = (TQString*)context;
40  *t += TQString::fromUtf8(buffer, len);
41  return len;
42 }
43 
44 int closeQString(void * context) {
45  TQString *t = (TQString*)context;
46  *t += '\n';
47  return 0;
48 }
49 
50 TQString transform( const TQString &pat, const TQString& tss,
51  const TQValueVector<const char *> &params )
52 {
53  TQString parsed;
54 
55  INFO(i18n("Parsing stylesheet"));
56 
57  xsltStylesheetPtr style_sheet =
58  xsltParseStylesheetFile((const xmlChar *)tss.latin1());
59 
60  if ( !style_sheet ) {
61  return parsed;
62  }
63 
64  if (style_sheet->indent == 1)
65  xmlIndentTreeOutput = 1;
66  else
67  xmlIndentTreeOutput = 0;
68 
69  INFO(i18n("Parsing document"));
70 
71  xmlDocPtr doc = xmlParseFile( pat.latin1() );
72  xsltTransformContextPtr ctxt;
73 
74  ctxt = xsltNewTransformContext(style_sheet, doc);
75  if (ctxt == NULL)
76  return parsed;
77 
78  INFO(i18n("Applying stylesheet"));
79  TQValueVector<const char *> p = params;
80  p.append( NULL );
81  xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, const_cast<const char **>(&p[0]));
82  xmlFreeDoc(doc);
83  if (res != NULL) {
84  xmlOutputBufferPtr outp = xmlOutputBufferCreateIO(writeToQString, (xmlOutputCloseCallback)closeQString, &parsed, 0);
85  outp->written = 0;
86  INFO(i18n("Writing document"));
87  xsltSaveResultTo ( outp, res, style_sheet );
88  xmlOutputBufferFlush(outp);
89  xmlFreeDoc(res);
90  }
91  xsltFreeStylesheet(style_sheet);
92 
93  if (parsed.isEmpty())
94  parsed = " "; // avoid error message
95  return parsed;
96 }
97 
98 /*
99 xmlParserInputPtr meinExternalEntityLoader(const char *URL, const char *ID,
100  xmlParserCtxtPtr ctxt) {
101  xmlParserInputPtr ret = NULL;
102 
103  // fprintf(stderr, "loading %s %s %s\n", URL, ID, ctxt->directory);
104 
105  if (URL == NULL) {
106  if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
107  ctxt->sax->warning(ctxt,
108  "failed to load external entity \"%s\"\n", ID);
109  return(NULL);
110  }
111  if (!qstrcmp(ID, "-//OASIS//DTD DocBook XML V4.1.2//EN"))
112  URL = "docbook/xml-dtd-4.1.2/docbookx.dtd";
113  if (!qstrcmp(ID, "-//OASIS//DTD XML DocBook V4.1.2//EN"))
114  URL = "docbook/xml-dtd-4.1.2/docbookx.dtd";
115 
116  TQString file;
117  if (TDEStandardDirs::exists( TQDir::currentDirPath() + "/" + URL ) )
118  file = TQDir::currentDirPath() + "/" + URL;
119  else
120  file = locate("dtd", URL);
121 
122  ret = xmlNewInputFromFile(ctxt, file.latin1());
123  if (ret == NULL) {
124  if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
125  ctxt->sax->warning(ctxt,
126 
127  "failed to load external entity \"%s\"\n", URL);
128  }
129  return(ret);
130 }
131 */
132 
133 TQString splitOut(const TQString &parsed, int index)
134 {
135  int start_index = index + 1;
136  while (parsed.at(start_index - 1) != '>') start_index++;
137 
138  int inside = 0;
139 
140  TQString filedata;
141 
142  while (true) {
143  int endindex = parsed.find("</FILENAME>", index);
144  int startindex = parsed.find("<FILENAME ", index) + 1;
145 
146 // kdDebug() << "FILENAME " << startindex << " " << endindex << " " << inside << " " << parsed.mid(startindex + 18, 15)<< " " << parsed.length() << endl;
147 
148  if (startindex > 0) {
149  if (startindex < endindex) {
150  // kdDebug() << "finding another" << endl;
151  index = startindex + 8;
152  inside++;
153  } else {
154  index = endindex + 8;
155  inside--;
156  }
157  } else {
158  inside--;
159  index = endindex + 1;
160  }
161 
162  if (inside == 0) {
163  filedata = parsed.mid(start_index, endindex - start_index);
164  break;
165  }
166 
167  }
168 
169  index = filedata.find("<FILENAME ");
170 
171  if (index > 0) {
172  int endindex = filedata.findRev("</FILENAME>");
173  while (filedata.at(endindex) != '>') endindex++;
174  endindex++;
175  filedata = filedata.left(index) + filedata.mid(endindex);
176  }
177 
178  // filedata.replace(TQRegExp(">"), "\n>");
179  return filedata;
180 }
181 
182 void fillInstance(TDEInstance &ins, const TQString &srcdir) {
183  TQString catalogs;
184 
185  if ( srcdir.isEmpty() ) {
186  catalogs += ins.dirs()->findResource("data", "ksgmltools2/customization/catalog.xml");
187  catalogs += ':';
188  catalogs += ins.dirs()->findResource("data", "ksgmltools2/docbook/xml-dtd-4.2/catalog.xml");
189  ins.dirs()->addResourceType("dtd", TDEStandardDirs::kde_default("data") + "ksgmltools2");
190  } else {
191  catalogs += srcdir +"/customization/catalog.xml:" + srcdir + "/docbook/xml-dtd-4.2/catalog.xml";
192  ins.dirs()->addResourceDir("dtd", srcdir);
193  }
194 
195  xmlLoadCatalogs(catalogs.latin1());
196 }
197 
198 extern "C" void *init_kbzip2filter();
199 
200 static TQIODevice *getBZip2device(const TQString &fileName )
201 {
202  TQFile * f = new TQFile( fileName );
203  KLibFactory * factory = static_cast<KLibFactory*>(init_kbzip2filter());
204  KFilterBase * base = static_cast<KFilterBase*>( factory->create(0, "bzip2" ) );
205 
206  if ( base )
207  {
208  base->setDevice(f, true);
209  return new KFilterDev(base, true);
210  }
211  return 0;
212 }
213 
214 bool saveToCache( const TQString &contents, const TQString &filename )
215 {
216  TQIODevice *fd = ::getBZip2device(filename);
217  if ( !fd )
218  return false;
219 
220  if (!fd->open(IO_WriteOnly))
221  {
222  delete fd;
223  return false;
224  }
225 
226  fd->writeBlock( contents.utf8() );
227  fd->close();
228  delete fd;
229  return true;
230 }
231 
232 static bool readCache( const TQString &filename,
233  const TQString &cache, TQString &output)
234 {
235  kdDebug( 7119 ) << "verifyCache " << filename << " " << cache << endl;
236  if ( !compareTimeStamps( filename, cache ) )
237  return false;
238  if ( !compareTimeStamps( locate( "dtd", "customization/tde-chunk.xsl"), cache ) )
239  return false;
240 
241  kdDebug( 7119 ) << "create filter" << endl;
242  TQIODevice *fd = ::getBZip2device(cache);
243  if ( !fd )
244  return false;
245 
246  if (!fd->open(IO_ReadOnly))
247  {
248  delete fd;
249  TQFile::remove(cache);
250  return false;
251  }
252 
253  kdDebug( 7119 ) << "reading" << endl;
254 
255  char buffer[32000];
256  int n;
257  TQCString text;
258  // Also end loop in case of error, when -1 is returned
259  while ( ( n = fd->readBlock(buffer, 31900) ) > 0)
260  {
261  buffer[n] = 0;
262  text += buffer;
263  }
264  kdDebug( 7119 ) << "read " << text.length() << endl;
265  fd->close();
266 
267  output = TQString::fromUtf8( text );
268  delete fd;
269 
270  if (n == -1)
271  return false;
272 
273  kdDebug( 7119 ) << "finished " << endl;
274 
275  return true;
276 }
277 
278 TQString lookForCache( const TQString &filename )
279 {
280  kdDebug() << "lookForCache " << filename << endl;
281  assert( filename.endsWith( ".docbook" ) );
282  assert( filename.at( 0 ) == '/' );
283 
284  TQString cache = filename.left( filename.length() - 7 );
285  TQString output;
286  if ( readCache( filename, cache + "cache.bz2", output) )
287  return output;
288  if ( readCache( filename,
289  locateLocal( "cache",
290  "tdeio_help" + cache +
291  "cache.bz2" ), output ) )
292  return output;
293 
294  return TQString::null;
295 }
296 
297 bool compareTimeStamps( const TQString &older, const TQString &newer )
298 {
299  TQFileInfo _older( older );
300  TQFileInfo _newer( newer );
301  assert( _older.exists() );
302  if ( !_newer.exists() )
303  return false;
304  return ( _newer.lastModified() > _older.lastModified() );
305 }
306 
307 TQCString fromUnicode( const TQString &data )
308 {
309  TQTextCodec *locale = TQTextCodec::codecForLocale();
310  TQCString result;
311  char buffer[30000];
312  uint buffer_len = 0;
313  uint len = 0;
314  uint offset = 0;
315  const int part_len = 5000;
316 
317  TQString part;
318 
319  while ( offset < data.length() )
320  {
321  part = data.mid( offset, part_len );
322  TQCString test = locale->fromUnicode( part );
323  if ( locale->toUnicode( test ) == part ) {
324  result += test;
325  offset += part_len;
326  continue;
327  }
328  len = part.length();
329  buffer_len = 0;
330  for ( uint i = 0; i < len; i++ ) {
331  TQCString test = locale->fromUnicode( part.mid( i, 1 ) );
332  if ( locale->toUnicode( test ) == part.mid( i, 1 ) ) {
333  if (buffer_len + test.length() + 1 > sizeof(buffer))
334  break;
335  strcpy( buffer + buffer_len, test.data() );
336  buffer_len += test.length();
337  } else {
338  TQString res;
339  res.sprintf( "&#%d;", TQChar(part.at( i )).unicode() );
340  test = locale->fromUnicode( res );
341  if (buffer_len + test.length() + 1 > sizeof(buffer))
342  break;
343  strcpy( buffer + buffer_len, test.data() );
344  buffer_len += test.length();
345  }
346  }
347  result += TQCString( buffer, buffer_len + 1);
348  offset += part_len;
349  }
350  return result;
351 }
352 
353 void replaceCharsetHeader( TQString &output )
354 {
355  TQString name = TQTextCodec::codecForLocale()->name();
356  name.replace( TQString( "ISO " ), "iso-" );
357  output.replace( TQString( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" ),
358  TQString( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\">" ).arg( name ) );
359 }

kdoctools

Skip menu "kdoctools"
  • Main Page
  • File List
  • Related Pages

kdoctools

Skip menu "kdoctools"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kdoctools by doxygen 1.9.1
This website is maintained by Timothy Pearson.