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

kdoctools

  • kdoctools
meinproc.cpp
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <string.h>
6#include <sys/time.h>
7#include <unistd.h>
8#include <libxml/xmlversion.h>
9#include <libxml/xmlmemory.h>
10#include <libxml/debugXML.h>
11#include <libxml/HTMLtree.h>
12#include <libxml/xmlIO.h>
13#include <libxml/parserInternals.h>
14#include <libxslt/xsltconfig.h>
15#include <libxslt/xsltInternals.h>
16#include <libxslt/transform.h>
17#include <libxslt/xsltutils.h>
18#include <tqstring.h>
19#include <tdestandarddirs.h>
20#include <kinstance.h>
21#include <xslt.h>
22#include <tqfile.h>
23#include <tqdir.h>
24#include <tdecmdlineargs.h>
25#include <tdelocale.h>
26#include <tdeaboutdata.h>
27#include <stdlib.h>
28#include <kdebug.h>
29#include <tqtextcodec.h>
30#include <tqfileinfo.h>
31#include <tdeprocess.h>
32#include <tqvaluevector.h>
33
34extern int xmlLoadExtDtdDefaultValue;
35
36class MyPair {
37public:
38 TQString word;
39 int base;};
40
41typedef TQValueList<MyPair> PairList;
42
43void parseEntry(PairList &list, xmlNodePtr cur, int base)
44{
45 if ( !cur )
46 return;
47
48 base += atoi( ( const char* )xmlGetProp(cur, ( const xmlChar* )"header") );
49 if ( base > 10 ) // 10 is the maximum
50 base = 10;
51
52 /* We don't care what the top level element name is */
53 cur = cur->xmlChildrenNode;
54 while (cur != NULL) {
55
56 if ( cur->type == XML_TEXT_NODE ) {
57 TQString words = TQString::fromUtf8( ( char* )cur->content );
58 TQStringList wlist = TQStringList::split( ' ', words.simplifyWhiteSpace() );
59 for ( TQStringList::ConstIterator it = wlist.begin();
60 it != wlist.end(); ++it )
61 {
62 MyPair m;
63 m.word = *it;
64 m.base = base;
65 list.append( m );
66 }
67 } else if ( !xmlStrcmp( cur->name, (const xmlChar *) "entry") )
68 parseEntry( list, cur, base );
69
70 cur = cur->next;
71 }
72
73}
74
75static TDECmdLineOptions options[] =
76{
77 { "stylesheet <xsl>", I18N_NOOP( "Stylesheet to use" ), 0 },
78 { "stdout", I18N_NOOP( "Output whole document to stdout" ), 0 },
79 { "o", 0, 0 },
80 { "output <file>", I18N_NOOP("Output whole document to file" ), 0 },
81 { "htdig", I18N_NOOP( "Create a ht://dig compatible index" ), 0 },
82 { "check", I18N_NOOP( "Check the document for validity" ), 0 },
83 { "cache <file>", I18N_NOOP( "Create a cache file for the document" ), 0},
84 { "srcdir <dir>", I18N_NOOP( "Set the srcdir, for tdelibs" ), 0},
85 { "param <key>=<value>", I18N_NOOP( "Parameters to pass to the stylesheet" ), 0},
86 { "+xml", I18N_NOOP("The file to transform"), 0},
87 TDECmdLineLastOption // End of options.
88};
89
90
91
92
93int main(int argc, char **argv) {
94
95 // xsltSetGenericDebugFunc(stderr, NULL);
96
97 TDEAboutData aboutData( "meinproc", I18N_NOOP("XML-Translator" ),
98 "$Revision$",
99 I18N_NOOP("TDE Translator for XML"));
100
101 TDECmdLineArgs::init(argc, argv, &aboutData);
102 TDECmdLineArgs::addCmdLineOptions( options );
103
104 TDELocale::setMainCatalogue("tdeio_help");
105 TDEInstance ins("meinproc");
106 TDEGlobal::locale();
107
108
109 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
110 if ( args->count() != 1 ) {
111 args->usage();
112 return ( 1 );
113 }
114
115 // Need to set SRCDIR before calling fillInstance
116 TQString srcdir;
117 if ( args->isSet( "srcdir" ) )
118 srcdir = TQDir( TQFile::decodeName( args->getOption( "srcdir" ) ) ).absPath();
119 fillInstance(ins,srcdir);
120
121 LIBXML_TEST_VERSION
122
123 TQString checkFilename = TQFile::decodeName(args->arg( 0 ));
124 TQFileInfo checkFile(checkFilename);
125 if (!checkFile.exists())
126 {
127 kdError() << "File '" << checkFilename << "' does not exist." << endl;
128 return ( 2 );
129 }
130 if (!checkFile.isFile())
131 {
132 kdError() << "'" << checkFilename << "' is not a file." << endl;
133 return ( 2 );
134 }
135 if (!checkFile.isReadable())
136 {
137 kdError() << "File '" << checkFilename << "' is not readable." << endl;
138 return ( 2 );
139 }
140
141 if ( args->isSet( "check" ) ) {
142#if !defined(PATH_MAX) && defined(__GLIBC__)
143 char *pwd_buffer;
144#else
145 char pwd_buffer[PATH_MAX];
146#endif
147 TQFileInfo file( TQFile::decodeName(args->arg( 0 )) );
148#if !defined(PATH_MAX) && defined(__GLIBC__)
149 if ( !(pwd_buffer = getcwd( NULL, 0 ) ) )
150#else
151 if ( !getcwd( pwd_buffer, sizeof(pwd_buffer) - 1 ) )
152#endif
153 {
154 kdError() << "getcwd failed." << endl;
155 return 2;
156 }
157
158 TQString catalogs;
159 catalogs += locate( "dtd", "customization/catalog.xml" );
160 catalogs += " ";
161 catalogs += locate( "dtd", "docbook/xml-dtd-4.1.2/catalog.xml" );
162
163 setenv( "XML_CATALOG_FILES", TQFile::encodeName( catalogs ).data(), 1);
164 TQString exe;
165#if defined( XMLLINT )
166 exe = XMLLINT;
167#endif
168 if ( (::access( TQFile::encodeName( exe ), X_OK )!=0) ) {
169 exe = TDEStandardDirs::findExe( "xmllint" );
170 if (exe.isEmpty())
171 exe = locate( "exe", "xmllint" );
172 }
173 if ( ::access( TQFile::encodeName( exe ), X_OK )==0 ) {
174 chdir( TQFile::encodeName( file.dirPath( true ) ) );
175 TQString cmd = exe;
176 cmd += " --valid --noout ";
177 cmd += TDEProcess::quote(file.fileName());
178 cmd += " 2>&1";
179 FILE *xmllint = popen( TQFile::encodeName( cmd ), "r");
180 char buf[ 512 ];
181 bool noout = true;
182 unsigned int n;
183 while ( ( n = fread(buf, 1, sizeof( buf ), xmllint ) ) ) {
184 noout = false;
185 buf[ n ] = '\0';
186 fputs( buf, stderr );
187 }
188 pclose( xmllint );
189 chdir( pwd_buffer );
190 if ( !noout ) {
191#if !defined(PATH_MAX) && defined(__GLIBC__)
192 free( pwd_buffer );
193#endif
194 return 1;
195 }
196 } else {
197 kdWarning() << "couldn't find xmllint" << endl;
198 }
199#if !defined(PATH_MAX) && defined(__GLIBC__)
200 free( pwd_buffer );
201#endif
202 }
203
204 xmlSubstituteEntitiesDefault(1);
205 xmlLoadExtDtdDefaultValue = 1;
206
207 TQValueVector<const char *> params;
208 if (args->isSet( "output" ) ) {
209 params.append( tqstrdup( "outputFile" ) );
210 params.append( tqstrdup( TQString(TQFile::decodeName( args->getOption( "output" ) )).latin1() ) );
211 }
212 {
213 const QCStringList paramList = args->getOptionList( "param" );
214 QCStringList::ConstIterator it = paramList.begin();
215 QCStringList::ConstIterator end = paramList.end();
216 for ( ; it != end; ++it ) {
217 const TQCString tuple = *it;
218 const int ch = tuple.find( '=' );
219 if ( ch == -1 ) {
220 kdError() << "Key-Value tuple '" << tuple << "' lacks a '='!" << endl;
221 return( 2 );
222 }
223 params.append( tqstrdup( tuple.left( ch ) ) );
224 params.append( tqstrdup( tuple.mid( ch + 1 ) ) );
225 }
226 }
227 params.append( NULL );
228
229 bool index = args->isSet( "htdig" );
230 TQString tss = args->getOption( "stylesheet" );
231 if ( tss.isEmpty() )
232 tss = "customization/tde-chunk.xsl";
233 if ( index )
234 tss = "customization/htdig_index.xsl" ;
235
236 tss = locate( "dtd", tss );
237
238 if ( index ) {
239 xsltStylesheetPtr style_sheet =
240 xsltParseStylesheetFile((const xmlChar *)tss.latin1());
241
242 if (style_sheet != NULL) {
243
244 xmlDocPtr doc = xmlParseFile( TQFile::encodeName( args->arg( 0 ) ) );
245
246 xmlDocPtr res = xsltApplyStylesheet(style_sheet, doc, &params[0]);
247
248 xmlFreeDoc(doc);
249 xsltFreeStylesheet(style_sheet);
250 if (res != NULL) {
251 xmlNodePtr cur = xmlDocGetRootElement(res);
252 if (!cur || xmlStrcmp(cur->name, (const xmlChar *) "entry")) {
253 fprintf(stderr,"document of the wrong type, root node != entry");
254 xmlFreeDoc(res);
255 return(1);
256 }
257 PairList list;
258 parseEntry( list, cur, 0 );
259 int wi = 0;
260 for ( PairList::ConstIterator it = list.begin(); it != list.end();
261 ++it, ++wi )
262 fprintf( stdout, "w\t%s\t%d\t%d\n", ( *it ).word.utf8().data(),
263 1000*wi/(int)list.count(), ( *it ).base );
264
265 xmlFreeDoc(res);
266 } else {
267 kdDebug() << "couldn't parse document " << args->arg( 0 ) << endl;
268 }
269 } else {
270 kdDebug() << "couldn't parse style sheet " << tss << endl;
271 }
272
273 } else {
274 TQString output = transform(args->arg( 0 ) , tss, params);
275 if (output.isEmpty()) {
276 fprintf(stderr, "unable to parse %s\n", args->arg( 0 ));
277 return(1);
278 }
279
280 TQString cache = args->getOption( "cache" );
281 if ( !cache.isEmpty() ) {
282 if ( !saveToCache( output, cache ) ) {
283 kdError() << TQString(i18n( "Could not write to cache file %1." ).arg( cache )) << endl;
284 }
285 goto end;
286 }
287
288 if (output.find( "<FILENAME " ) == -1 || args->isSet( "stdout" ) || args->isSet("output") )
289 {
290 TQFile file;
291 if (args->isSet( "stdout" ) ) {
292 file.open( IO_WriteOnly, stdout );
293 } else {
294 if (args->isSet( "output" ) )
295 file.setName( TQFile::decodeName(args->getOption( "output" )));
296 else
297 file.setName( "index.html" );
298 file.open(IO_WriteOnly);
299 }
300 replaceCharsetHeader( output );
301
302 TQCString data = output.local8Bit();
303 file.writeBlock(data.data(), data.length());
304 file.close();
305 } else {
306 int index = 0;
307 while (true) {
308 index = output.find("<FILENAME ", index);
309 if (index == -1)
310 break;
311 int filename_index = index + strlen("<FILENAME filename=\"");
312
313 TQString filename = output.mid(filename_index,
314 output.find("\"", filename_index) -
315 filename_index);
316
317 TQString filedata = splitOut(output, index);
318 TQFile file(filename);
319 file.open(IO_WriteOnly);
320 replaceCharsetHeader( filedata );
321 TQCString data = fromUnicode( filedata );
322 file.writeBlock(data.data(), data.length());
323 file.close();
324
325 index += 8;
326 }
327 }
328 }
329 end:
330 xmlCleanupParser();
331 xmlMemoryDump();
332 return(0);
333}
334

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.4
This website is maintained by Timothy Pearson.