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

kdoctools

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

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